core_validation.cpp revision ea26140408c3361500b5d5b8498a4f28a3e92ef5
15b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis/* Copyright (c) 2015-2016 The Khronos Group Inc.
25b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Copyright (c) 2015-2016 Valve Corporation
35b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Copyright (c) 2015-2016 LunarG, Inc.
45b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Copyright (C) 2015-2016 Google Inc.
55b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis *
65b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Permission is hereby granted, free of charge, to any person obtaining a copy
75b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * of this software and/or associated documentation files (the "Materials"), to
85b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * deal in the Materials without restriction, including without limitation the
95b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * sell copies of the Materials, and to permit persons to whom the Materials
115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * are furnished to do so, subject to the following conditions:
125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis *
135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * The above copyright notice(s) and this permission notice shall be included
145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * in all copies or substantial portions of the Materials.
155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis *
165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis *
205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * USE OR OTHER DEALINGS IN THE MATERIALS
245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis *
255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Author: Cody Northrop <cnorthrop@google.com>
265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Author: Michael Lentine <mlentine@google.com>
275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Author: Tobin Ehlis <tobine@google.com>
285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Author: Chia-I Wu <olv@google.com>
295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Author: Chris Forbes <chrisf@ijw.co.nz>
305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Author: Mark Lobodzinski <mark@lunarg.com>
315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Author: Ian Elliott <ianelliott@google.com>
325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis */
335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Allow use of STL min and max functions in Windows
355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#define NOMINMAX
365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Turn on mem_tracker merged code
385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#define MTMERGE 1
395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <stdio.h>
415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <stdlib.h>
425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <string.h>
435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <assert.h>
445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <unordered_map>
455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <unordered_set>
465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <map>
475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <string>
485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <iostream>
495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <algorithm>
505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <list>
515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <SPIRV/spirv.hpp>
525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <set>
535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "vk_loader_platform.h"
555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "vk_dispatch_table_helper.h"
565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "vk_struct_string_helper_cpp.h"
575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if defined(__GNUC__)
585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#pragma GCC diagnostic ignored "-Wwrite-strings"
595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if defined(__GNUC__)
615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#pragma GCC diagnostic warning "-Wwrite-strings"
625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "vk_struct_size_helper.h"
645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "core_validation.h"
655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "vk_layer_config.h"
665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "vk_layer_table.h"
675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "vk_layer_data.h"
685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "vk_layer_logging.h"
695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "vk_layer_extension_utils.h"
705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include "vk_layer_utils.h"
715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if defined __ANDROID__
735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#include <android/log.h>
745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#define LOGCONSOLE(...) ((void)__android_log_print(ANDROID_LOG_INFO, "DS", __VA_ARGS__))
755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#else
765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#define LOGCONSOLE(...) printf(__VA_ARGS__)
775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisusing std::unordered_map;
805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisusing std::unordered_set;
815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// WSI Image Objects bypass usual Image Object creation methods.  A special Memory
845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Object value will be used to identify them internally.
855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic const VkDeviceMemory MEMTRACKER_SWAP_CHAIN_IMAGE_KEY = (VkDeviceMemory)(-1);
865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Track command pools and their command buffers
885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstruct CMD_POOL_INFO {
895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkCommandPoolCreateFlags createFlags;
905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t queueFamilyIndex;
915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    list<VkCommandBuffer> commandBuffers; // list container of cmd buffers allocated from this pool
925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis};
935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstruct devExts {
955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 wsi_enabled;
965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> swapchainMap;
975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkImage, VkSwapchainKHR> imageToSwapchainMap;
985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis};
995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
1005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// fwd decls
1015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstruct shader_module;
1025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstruct render_pass;
1035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
1045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstruct layer_data {
1055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    debug_report_data *report_data;
1065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::vector<VkDebugReportCallbackEXT> logging_callback;
1075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerDispatchTable *device_dispatch_table;
1085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerInstanceDispatchTable *instance_dispatch_table;
1095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
1105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// MTMERGE - stuff pulled directly from MT
1115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint64_t currentFenceId;
1125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Maps for tracking key structs related to mem_tracker state
1135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkCommandBuffer, MT_CB_INFO> cbMap;
1145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Merged w/ draw_state maps below
1155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //unordered_map<VkCommandPool, MT_CMD_POOL_INFO> commandPoolMap;
1165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //unordered_map<VkFence, MT_FENCE_INFO> fenceMap;
1175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //unordered_map<VkQueue, MT_QUEUE_INFO> queueMap;
1185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //unordered_map<VkSemaphore, MtSemaphoreState> semaphoreMap;
1195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //unordered_map<VkImageView, MT_IMAGE_VIEW_INFO> imageViewMap;
1205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //unordered_map<VkBufferView, VkBufferViewCreateInfo> bufferViewMap;
1215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkFramebuffer, MT_FB_INFO> fbMap;
1225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkRenderPass, MT_PASS_INFO> passMap;
1235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkDescriptorSet, MT_DESCRIPTOR_SET_INFO> descriptorSetMap;
1245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Images and Buffers are 2 objects that can have memory bound to them so they get special treatment
1255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<uint64_t, MT_OBJ_BINDING_INFO> imageBindingMap;
1265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<uint64_t, MT_OBJ_BINDING_INFO> bufferBindingMap;
1275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// MTMERGE - End of MT stuff
1285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
1295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    devExts device_extensions;
1305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    vector<VkQueue> queues; // all queues under given device
1315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Global set of all cmdBuffers that are inFlight on this device
1325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_set<VkCommandBuffer> globalInFlightCmdBuffers;
1335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Layer specific data
1345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkSampler, unique_ptr<SAMPLER_NODE>> sampleMap;
1355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkImageView, VkImageViewCreateInfo> imageViewMap;
1365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkImage, IMAGE_NODE> imageMap;
1375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkBufferView, VkBufferViewCreateInfo> bufferViewMap;
1385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkBuffer, BUFFER_NODE> bufferMap;
1395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkPipeline, PIPELINE_NODE *> pipelineMap;
1405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkCommandPool, CMD_POOL_INFO> commandPoolMap;
1415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> descriptorPoolMap;
1425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkDescriptorSet, SET_NODE *> setMap;
1435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkDescriptorSetLayout, LAYOUT_NODE *> descriptorSetLayoutMap;
1445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkPipelineLayout, PIPELINE_LAYOUT_NODE> pipelineLayoutMap;
14558070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> memObjMap;
1465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkFence, FENCE_NODE> fenceMap;
1475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkQueue, QUEUE_NODE> queueMap;
1485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkEvent, EVENT_NODE> eventMap;
1495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<QueryObject, bool> queryToStateMap;
1505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkQueryPool, QUERY_POOL_NODE> queryPoolMap;
1515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkSemaphore, SEMAPHORE_NODE> semaphoreMap;
1525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<void *, GLOBAL_CB_NODE *> commandBufferMap;
1535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkFramebuffer, FRAMEBUFFER_NODE> frameBufferMap;
1545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkImage, vector<ImageSubresourcePair>> imageSubresourceMap;
1555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> imageLayoutMap;
1565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkRenderPass, RENDER_PASS_NODE *> renderPassMap;
1575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<VkShaderModule, shader_module *> shaderModuleMap;
1585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Current render pass
1595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkRenderPassBeginInfo renderPassBeginInfo;
1605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t currentSubpass;
1615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
1625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Device specific data
1635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PHYS_DEV_PROPERTIES_NODE physDevProperties;
1645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// MTMERGE - added a couple of fields to constructor initializer
1655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data()
1665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        : report_data(nullptr), device_dispatch_table(nullptr), instance_dispatch_table(nullptr),
1675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
168de040b4d09f3714f94116be2a1841d448869bed2Tobin Ehlis        currentFenceId(1),
1695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
1705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        device_extensions(){};
1715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis};
1725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
1735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic const VkLayerProperties cv_global_layers[] = {{
1745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    "VK_LAYER_LUNARG_core_validation", VK_API_VERSION, 1, "LunarG Validation Layer",
1755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}};
1765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
1775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlistemplate <class TCreateInfo> void ValidateLayerOrdering(const TCreateInfo &createInfo) {
1785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool foundLayer = false;
1795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < createInfo.enabledLayerCount; ++i) {
1805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!strcmp(createInfo.ppEnabledLayerNames[i], cv_global_layers[0].layerName)) {
1815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            foundLayer = true;
1825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
1835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // This has to be logged to console as we don't have a callback at this point.
1845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!foundLayer && !strcmp(createInfo.ppEnabledLayerNames[0], "VK_LAYER_GOOGLE_unique_objects")) {
1855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            LOGCONSOLE("Cannot activate layer VK_LAYER_GOOGLE_unique_objects prior to activating %s.",
1865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       cv_global_layers[0].layerName);
1875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
1885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
1895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
1905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
1915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Code imported from shader_checker
1925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void build_def_index(shader_module *);
1935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
1945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// A forward iterator over spirv instructions. Provides easy access to len, opcode, and content words
1955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// without the caller needing to care too much about the physical SPIRV module layout.
1965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstruct spirv_inst_iter {
1975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::vector<uint32_t>::const_iterator zero;
1985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::vector<uint32_t>::const_iterator it;
1995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t len() { return *it >> 16; }
2015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t opcode() { return *it & 0x0ffffu; }
2025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t const &word(unsigned n) { return it[n]; }
2035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t offset() { return (uint32_t)(it - zero); }
2045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter() {}
2065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter(std::vector<uint32_t>::const_iterator zero, std::vector<uint32_t>::const_iterator it) : zero(zero), it(it) {}
2085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool operator==(spirv_inst_iter const &other) { return it == other.it; }
2105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool operator!=(spirv_inst_iter const &other) { return it != other.it; }
2125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter operator++(int) { /* x++ */
2145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        spirv_inst_iter ii = *this;
2155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        it += len();
2165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return ii;
2175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
2185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter operator++() { /* ++x; */
2205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        it += len();
2215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return *this;
2225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
2235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* The iterator and the value are the same thing. */
2255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter &operator*() { return *this; }
2265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter const &operator*() const { return *this; }
2275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis};
2285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstruct shader_module {
2305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* the spirv image itself */
2315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    vector<uint32_t> words;
2325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* a mapping of <id> to the first word of its def. this is useful because walking type
2335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     * trees, constant expressions, etc requires jumping all over the instruction stream.
2345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     */
2355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<unsigned, unsigned> def_index;
2365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    shader_module(VkShaderModuleCreateInfo const *pCreateInfo)
2385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        : words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
2395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis          def_index() {
2405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        build_def_index(this);
2425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
2435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* expose begin() / end() to enable range-based for */
2455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter begin() const { return spirv_inst_iter(words.begin(), words.begin() + 5); } /* first insn */
2465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter end() const { return spirv_inst_iter(words.begin(), words.end()); }         /* just past last insn */
2475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* given an offset into the module, produce an iterator there. */
2485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter at(unsigned offset) const { return spirv_inst_iter(words.begin(), words.begin() + offset); }
2495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* gets an iterator to the definition of an id */
2515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter get_def(unsigned id) const {
2525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto it = def_index.find(id);
2535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (it == def_index.end()) {
2545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return end();
2555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
2565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return at(it->second);
2575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
2585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis};
2595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// TODO : Do we need to guard access to layer_data_map w/ lock?
2615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic unordered_map<void *, layer_data *> layer_data_map;
2625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// TODO : This can be much smarter, using separate locks for separate global data
2645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic int globalLockInitialized = 0;
2655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic loader_platform_thread_mutex globalLock;
2665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#define MAX_TID 513
2675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic loader_platform_thread_id g_tidMapping[MAX_TID] = {0};
2685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic uint32_t g_maxTID = 0;
2695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
2705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// MTMERGE - start of direct pull
2715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkPhysicalDeviceMemoryProperties memProps;
2725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 clear_cmd_buf_and_mem_references(layer_data *my_data, const VkCommandBuffer cb);
2745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#define MAX_BINDING 0xFFFFFFFF
2765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic MT_OBJ_BINDING_INFO *get_object_binding_info(layer_data *my_data, uint64_t handle, VkDebugReportObjectTypeEXT type) {
2785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    MT_OBJ_BINDING_INFO *retValue = NULL;
2795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (type) {
2805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT: {
2815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto it = my_data->imageBindingMap.find(handle);
2825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (it != my_data->imageBindingMap.end())
2835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return &(*it).second;
2845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
2855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
2865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT: {
2875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto it = my_data->bufferBindingMap.find(handle);
2885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (it != my_data->bufferBindingMap.end())
2895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return &(*it).second;
2905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
2915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
2925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
2935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
2945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
2955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return retValue;
2965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
2975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// MTMERGE - end section
2985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
2995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlistemplate layer_data *get_my_data_ptr<layer_data>(void *data_key, std::unordered_map<void *, layer_data *> &data_map);
3005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
3015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
3025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void delete_queue_info_list(layer_data *my_data) {
3035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Process queue list, cleaning up each entry before deleting
3045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->queueMap.clear();
3055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
3065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
3075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Add new CBInfo for this cb to map container
3085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void add_cmd_buf_info(layer_data *my_data, VkCommandPool commandPool, const VkCommandBuffer cb) {
3095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->cbMap[cb].commandBuffer = cb;
3105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->commandPoolMap[commandPool].commandBuffers.push_front(cb);
3115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
3125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
3135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Delete CBInfo from container and clear mem references to CB
3145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 delete_cmd_buf_info(layer_data *my_data, VkCommandPool commandPool, const VkCommandBuffer cb) {
3155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 result = VK_TRUE;
3165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result = clear_cmd_buf_and_mem_references(my_data, cb);
3175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Delete the CBInfo info
3185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (result != VK_TRUE) {
3195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->commandPoolMap[commandPool].commandBuffers.remove(cb);
3205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->cbMap.erase(cb);
3215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
3225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
3235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
3245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
3255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return ptr to Info in CB map, or NULL if not found
3265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic MT_CB_INFO *get_cmd_buf_info(layer_data *my_data, const VkCommandBuffer cb) {
3275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto item = my_data->cbMap.find(cb);
3285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (item != my_data->cbMap.end()) {
3295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return &(*item).second;
3305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
3315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return NULL;
3325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
3335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
3345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
3355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void add_object_binding_info(layer_data *my_data, const uint64_t handle, const VkDebugReportObjectTypeEXT type,
3365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    const VkDeviceMemory mem) {
3375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (type) {
3385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Buffers and images are unique as their CreateInfo is in container struct
3395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT: {
3405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto pCI = &my_data->bufferBindingMap[handle];
3415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCI->mem = mem;
3425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
3435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
3445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT: {
3455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto pCI = &my_data->imageBindingMap[handle];
3465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCI->mem = mem;
3475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
3485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
3495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
3505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
3515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
3525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
3535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
3545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void add_object_create_info(layer_data *my_data, const uint64_t handle, const VkDebugReportObjectTypeEXT type,
3555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   const void *pCreateInfo) {
3565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : For any CreateInfo struct that has ptrs, need to deep copy them and appropriately clean up on Destroy
3575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (type) {
3585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Buffers and images are unique as their CreateInfo is in container struct
3595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT: {
3605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto pCI = &my_data->bufferBindingMap[handle];
3615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(pCI, 0, sizeof(MT_OBJ_BINDING_INFO));
3625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy(&pCI->create_info.buffer, pCreateInfo, sizeof(VkBufferCreateInfo));
3635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
3645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
3655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT: {
3665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto pCI = &my_data->imageBindingMap[handle];
3675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(pCI, 0, sizeof(MT_OBJ_BINDING_INFO));
3685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy(&pCI->create_info.image, pCreateInfo, sizeof(VkImageCreateInfo));
3695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
3705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
3715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Swap Chain is very unique, use my_data->imageBindingMap, but copy in
3725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // SwapChainCreatInfo's usage flags and set the mem value to a unique key. These is used by
3735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // vkCreateImageView and internal mem_tracker routines to distinguish swap chain images
3745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DEBUG_REPORT_OBJECT_TYPE_SWAPCHAIN_KHR_EXT: {
3755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto pCI = &my_data->imageBindingMap[handle];
3765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(pCI, 0, sizeof(MT_OBJ_BINDING_INFO));
3775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCI->mem = MEMTRACKER_SWAP_CHAIN_IMAGE_KEY;
3785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCI->valid = false;
3795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCI->create_info.image.usage =
3805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            const_cast<VkSwapchainCreateInfoKHR *>(static_cast<const VkSwapchainCreateInfoKHR *>(pCreateInfo))->imageUsage;
3815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
3825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
3835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
3845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
3855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
3865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
3875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
3885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Add a fence, creating one if necessary to our list of fences/fenceIds
3895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 add_fence_info(layer_data *my_data, VkFence fence, VkQueue queue, uint64_t *fenceId) {
3905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
3915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    *fenceId = my_data->currentFenceId++;
3925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
3935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // If no fence, create an internal fence to track the submissions
3945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (fence != VK_NULL_HANDLE) {
3955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->fenceMap[fence].fenceId = *fenceId;
3965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->fenceMap[fence].queue = queue;
3975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Validate that fence is in UNSIGNALED state
3985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkFenceCreateInfo *pFenceCI = &(my_data->fenceMap[fence].createInfo);
3995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pFenceCI->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
4005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
4015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               (uint64_t)fence, __LINE__, MEMTRACK_INVALID_FENCE_STATE, "MEM",
4025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               "Fence %#" PRIxLEAST64 " submitted in SIGNALED state.  Fences must be reset before being submitted",
4035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               (uint64_t)fence);
4045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
4055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
4065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Do we need to create an internal fence here for tracking purposes?
4075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
4085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Update most recently submitted fence and fenceId for Queue
4095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->queueMap[queue].lastSubmittedId = *fenceId;
4105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
4115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
4125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
4135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Remove a fenceInfo from our list of fences/fenceIds
4145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void delete_fence_info(layer_data *my_data, VkFence fence) { my_data->fenceMap.erase(fence); }
4155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
4165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Record information when a fence is known to be signalled
4175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void update_fence_tracking(layer_data *my_data, VkFence fence) {
4185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto fence_item = my_data->fenceMap.find(fence);
4195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (fence_item != my_data->fenceMap.end()) {
4205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        FENCE_NODE *pCurFenceInfo = &(*fence_item).second;
4215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkQueue queue = pCurFenceInfo->queue;
4225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto queue_item = my_data->queueMap.find(queue);
4235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (queue_item != my_data->queueMap.end()) {
4245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            QUEUE_NODE *pQueueInfo = &(*queue_item).second;
4255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pQueueInfo->lastRetiredId < pCurFenceInfo->fenceId) {
4265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pQueueInfo->lastRetiredId = pCurFenceInfo->fenceId;
4275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
4285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
4295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
4305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
4315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Update fence state in fenceCreateInfo structure
4325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto pFCI = &(my_data->fenceMap[fence].createInfo);
4335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pFCI->flags = static_cast<VkFenceCreateFlags>(pFCI->flags | VK_FENCE_CREATE_SIGNALED_BIT);
4345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
4355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
4365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Helper routine that updates the fence list for a specific queue to all-retired
4375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void retire_queue_fences(layer_data *my_data, VkQueue queue) {
4385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    QUEUE_NODE *pQueueInfo = &my_data->queueMap[queue];
4395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Set queue's lastRetired to lastSubmitted indicating all fences completed
4405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pQueueInfo->lastRetiredId = pQueueInfo->lastSubmittedId;
4415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
4425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
4435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Helper routine that updates all queues to all-retired
4445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void retire_device_fences(layer_data *my_data, VkDevice device) {
4455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Process each queue for device
4465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO: Add multiple device support
4475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto ii = my_data->queueMap.begin(); ii != my_data->queueMap.end(); ++ii) {
4485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Set queue's lastRetired to lastSubmitted indicating all fences completed
4495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        QUEUE_NODE *pQueueInfo = &(*ii).second;
4505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pQueueInfo->lastRetiredId = pQueueInfo->lastSubmittedId;
4515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
4525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
4535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
4545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Helper function to validate correct usage bits set for buffers or images
4555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  Verify that (actual & desired) flags != 0 or,
4565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//   if strict is true, verify that (actual & desired) flags == desired
4575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  In case of error, report it via dbg callbacks
4585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validate_usage_flags(layer_data *my_data, void *disp_obj, VkFlags actual, VkFlags desired, VkBool32 strict,
4595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     uint64_t obj_handle, VkDebugReportObjectTypeEXT obj_type, char const *ty_str,
4605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     char const *func_name, char const *usage_str) {
4615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 correct_usage = VK_FALSE;
4625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
4635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (strict)
4645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        correct_usage = ((actual & desired) == desired);
4655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    else
4665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        correct_usage = ((actual & desired) != 0);
4675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!correct_usage) {
4685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, obj_type, obj_handle, __LINE__,
4695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           MEMTRACK_INVALID_USAGE_FLAG, "MEM", "Invalid usage flag for %s %#" PRIxLEAST64
4705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                               " used by %s. In this case, %s should have %s set during creation.",
4715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           ty_str, obj_handle, func_name, ty_str, usage_str);
4725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
4735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
4745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
4755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
4765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Helper function to validate usage flags for images
4775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Pulls image info and then sends actual vs. desired usage off to helper above where
4785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  an error will be flagged if usage is not correct
4795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validate_image_usage_flags(layer_data *my_data, void *disp_obj, VkImage image, VkFlags desired, VkBool32 strict,
4805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           char const *func_name, char const *usage_string) {
4815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
4825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    MT_OBJ_BINDING_INFO *pBindInfo = get_object_binding_info(my_data, (uint64_t)image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT);
4835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pBindInfo) {
4845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = validate_usage_flags(my_data, disp_obj, pBindInfo->create_info.image.usage, desired, strict, (uint64_t)image,
4855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, "image", func_name, usage_string);
4865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
4875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
4885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
4895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
4905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Helper function to validate usage flags for buffers
4915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Pulls buffer info and then sends actual vs. desired usage off to helper above where
4925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  an error will be flagged if usage is not correct
4935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validate_buffer_usage_flags(layer_data *my_data, void *disp_obj, VkBuffer buffer, VkFlags desired, VkBool32 strict,
4945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            char const *func_name, char const *usage_string) {
4955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
4965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    MT_OBJ_BINDING_INFO *pBindInfo = get_object_binding_info(my_data, (uint64_t)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT);
4975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pBindInfo) {
4985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = validate_usage_flags(my_data, disp_obj, pBindInfo->create_info.buffer.usage, desired, strict, (uint64_t)buffer,
4995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, "buffer", func_name, usage_string);
5005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
5015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
5025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
5035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
5045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return ptr to info in map container containing mem, or NULL if not found
5055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  Calls to this function should be wrapped in mutex
50658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic DEVICE_MEM_INFO *get_mem_obj_info(layer_data *dev_data, const VkDeviceMemory mem) {
50758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    auto item = dev_data->memObjMap.find(mem);
50858070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    if (item != dev_data->memObjMap.end()) {
5095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return &(*item).second;
5105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
5115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return NULL;
5125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
5135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
5145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
5155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void add_mem_obj_info(layer_data *my_data, void *object, const VkDeviceMemory mem,
5165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             const VkMemoryAllocateInfo *pAllocateInfo) {
5175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(object != NULL);
5185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
5195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    memcpy(&my_data->memObjMap[mem].allocInfo, pAllocateInfo, sizeof(VkMemoryAllocateInfo));
5205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO:  Update for real hardware, actually process allocation info structures
5215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->memObjMap[mem].allocInfo.pNext = NULL;
5225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->memObjMap[mem].object = object;
5235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->memObjMap[mem].refCount = 0;
5245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->memObjMap[mem].mem = mem;
52558070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    my_data->memObjMap[mem].image = VK_NULL_HANDLE;
5265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->memObjMap[mem].memRange.offset = 0;
5275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->memObjMap[mem].memRange.size = 0;
5285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->memObjMap[mem].pData = 0;
5295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->memObjMap[mem].pDriverData = 0;
5305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->memObjMap[mem].valid = false;
5315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
5325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
53358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic VkBool32 validate_memory_is_valid(layer_data *dev_data, VkDeviceMemory mem, const char *functionName,
5345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         VkImage image = VK_NULL_HANDLE) {
5355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (mem == MEMTRACKER_SWAP_CHAIN_IMAGE_KEY) {
53658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        MT_OBJ_BINDING_INFO *pBindInfo =
53758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            get_object_binding_info(dev_data, reinterpret_cast<const uint64_t &>(image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT);
5385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pBindInfo && !pBindInfo->valid) {
53958070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
5405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           (uint64_t)(mem), __LINE__, MEMTRACK_INVALID_USAGE_FLAG, "MEM",
5415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           "%s: Cannot read invalid swapchain image %" PRIx64 ", please fill the memory before using.",
5425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           functionName, (uint64_t)(image));
5435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
5445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
54558070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        DEVICE_MEM_INFO *pMemObj = get_mem_obj_info(dev_data, mem);
5465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pMemObj && !pMemObj->valid) {
54758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
5485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           (uint64_t)(mem), __LINE__, MEMTRACK_INVALID_USAGE_FLAG, "MEM",
5495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           "%s: Cannot read invalid memory %" PRIx64 ", please fill the memory before using.", functionName,
5505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           (uint64_t)(mem));
5515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
5525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
5535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return false;
5545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
5555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
55658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic void set_memory_valid(layer_data *dev_data, VkDeviceMemory mem, bool valid, VkImage image = VK_NULL_HANDLE) {
5575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (mem == MEMTRACKER_SWAP_CHAIN_IMAGE_KEY) {
55858070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        MT_OBJ_BINDING_INFO *pBindInfo =
55958070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            get_object_binding_info(dev_data, reinterpret_cast<const uint64_t &>(image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT);
5605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pBindInfo) {
5615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pBindInfo->valid = valid;
5625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
5635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
56458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        DEVICE_MEM_INFO *pMemObj = get_mem_obj_info(dev_data, mem);
5655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pMemObj) {
5665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pMemObj->valid = valid;
5675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
5685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
5695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
5705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
5715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Find CB Info and add mem reference to list container
5725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Find Mem Obj Info and add CB reference to list container
57358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic VkBool32 update_cmd_buf_and_mem_references(layer_data *dev_data, const VkCommandBuffer cb, const VkDeviceMemory mem,
5745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  const char *apiName) {
5755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
5765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
5775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Skip validation if this image was created through WSI
5785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (mem != MEMTRACKER_SWAP_CHAIN_IMAGE_KEY) {
5795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
5805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // First update CB binding in MemObj mini CB list
58158070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        DEVICE_MEM_INFO *pMemInfo = get_mem_obj_info(dev_data, mem);
5825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pMemInfo) {
5835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Search for cmd buffer object in memory object's binding list
5845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkBool32 found = VK_FALSE;
5855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pMemInfo->pCommandBufferBindings.size() > 0) {
5865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (list<VkCommandBuffer>::iterator it = pMemInfo->pCommandBufferBindings.begin();
5875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     it != pMemInfo->pCommandBufferBindings.end(); ++it) {
5885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if ((*it) == cb) {
5895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        found = VK_TRUE;
5905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        break;
5915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
5925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
5935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
5945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // If not present, add to list
5955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (found == VK_FALSE) {
5965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pMemInfo->pCommandBufferBindings.push_front(cb);
5975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pMemInfo->refCount++;
5985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
5995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Now update CBInfo's Mem reference list
60058070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            MT_CB_INFO *pCBInfo = get_cmd_buf_info(dev_data, cb);
6015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO: keep track of all destroyed CBs so we know if this is a stale or simply invalid object
6025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pCBInfo) {
6035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Search for memory object in cmd buffer's reference list
6045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkBool32 found = VK_FALSE;
6055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pCBInfo->pMemObjList.size() > 0) {
6065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    for (auto it = pCBInfo->pMemObjList.begin(); it != pCBInfo->pMemObjList.end(); ++it) {
6075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if ((*it) == mem) {
6085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            found = VK_TRUE;
6095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            break;
6105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
6115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
6125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
6135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // If not present, add to list
6145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (found == VK_FALSE) {
6155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pCBInfo->pMemObjList.push_front(mem);
6165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
6175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
6185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
6195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
6205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
6215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
6225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
6235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Free bindings related to CB
62458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic VkBool32 clear_cmd_buf_and_mem_references(layer_data *dev_data, const VkCommandBuffer cb) {
6255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
62658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    MT_CB_INFO *pCBInfo = get_cmd_buf_info(dev_data, cb);
6275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
6285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCBInfo) {
6295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCBInfo->pMemObjList.size() > 0) {
6305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            list<VkDeviceMemory> mem_obj_list = pCBInfo->pMemObjList;
6315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (list<VkDeviceMemory>::iterator it = mem_obj_list.begin(); it != mem_obj_list.end(); ++it) {
63258070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                DEVICE_MEM_INFO *pInfo = get_mem_obj_info(dev_data, *it);
6335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pInfo) {
6345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pInfo->pCommandBufferBindings.remove(cb);
6355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pInfo->refCount--;
6365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
6375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
6385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCBInfo->pMemObjList.clear();
6395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
6405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCBInfo->activeDescriptorSets.clear();
6415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCBInfo->validate_functions.clear();
6425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
6435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
6445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
6455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
6465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Delete the entire CB list
6475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 delete_cmd_buf_info_list(layer_data *my_data) {
6485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
6495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (unordered_map<VkCommandBuffer, MT_CB_INFO>::iterator ii = my_data->cbMap.begin(); ii != my_data->cbMap.end(); ++ii) {
6505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= clear_cmd_buf_and_mem_references(my_data, (*ii).first);
6515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
6525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->cbMap.clear();
6535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
6545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
6555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
6565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For given MemObjInfo, report Obj & CB bindings
65758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic VkBool32 reportMemReferencesAndCleanUp(layer_data *dev_data, DEVICE_MEM_INFO *pMemObjInfo) {
6585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
6595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    size_t cmdBufRefCount = pMemObjInfo->pCommandBufferBindings.size();
6605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    size_t objRefCount = pMemObjInfo->pObjBindings.size();
6615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
6625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if ((pMemObjInfo->pCommandBufferBindings.size()) != 0) {
66358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
6645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           (uint64_t)pMemObjInfo->mem, __LINE__, MEMTRACK_FREED_MEM_REF, "MEM",
6655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           "Attempting to free memory object %#" PRIxLEAST64 " which still contains " PRINTF_SIZE_T_SPECIFIER
6665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           " references",
6675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           (uint64_t)pMemObjInfo->mem, (cmdBufRefCount + objRefCount));
6685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
6695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
6705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cmdBufRefCount > 0 && pMemObjInfo->pCommandBufferBindings.size() > 0) {
6715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (list<VkCommandBuffer>::const_iterator it = pMemObjInfo->pCommandBufferBindings.begin();
6725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             it != pMemObjInfo->pCommandBufferBindings.end(); ++it) {
6735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO : CommandBuffer should be source Obj here
67458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
6755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (uint64_t)(*it), __LINE__, MEMTRACK_FREED_MEM_REF, "MEM",
6765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "Command Buffer %p still has a reference to mem obj %#" PRIxLEAST64, (*it), (uint64_t)pMemObjInfo->mem);
6775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
6785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Clear the list of hanging references
6795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pMemObjInfo->pCommandBufferBindings.clear();
6805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
6815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
6825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (objRefCount > 0 && pMemObjInfo->pObjBindings.size() > 0) {
6835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto it = pMemObjInfo->pObjBindings.begin(); it != pMemObjInfo->pObjBindings.end(); ++it) {
68458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, it->type, it->handle, __LINE__,
6855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    MEMTRACK_FREED_MEM_REF, "MEM", "VK Object %#" PRIxLEAST64 " still has a reference to mem obj %#" PRIxLEAST64,
6865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    it->handle, (uint64_t)pMemObjInfo->mem);
6875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
6885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Clear the list of hanging references
6895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pMemObjInfo->pObjBindings.clear();
6905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
6915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
6925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
6935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
6945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 deleteMemObjInfo(layer_data *my_data, void *object, VkDeviceMemory mem) {
6955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
6965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto item = my_data->memObjMap.find(mem);
6975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (item != my_data->memObjMap.end()) {
6985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->memObjMap.erase(item);
6995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
7005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
7015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MEM_OBJ, "MEM",
7025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           "Request to delete memory object %#" PRIxLEAST64 " not present in memory Object Map", (uint64_t)mem);
7035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
7045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
7055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
7065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
7075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Check if fence for given CB is completed
7085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool checkCBCompleted(layer_data *my_data, const VkCommandBuffer cb, bool *complete) {
7095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    MT_CB_INFO *pCBInfo = get_cmd_buf_info(my_data, cb);
7105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = false;
7115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    *complete = true;
7125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
7135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCBInfo) {
7145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCBInfo->lastSubmittedQueue != NULL) {
7155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkQueue queue = pCBInfo->lastSubmittedQueue;
7165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            QUEUE_NODE *pQueueInfo = &my_data->queueMap[queue];
7175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pCBInfo->fenceId > pQueueInfo->lastRetiredId) {
7185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
7195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)cb, __LINE__, MEMTRACK_NONE, "MEM",
7205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   "fence %#" PRIxLEAST64 " for CB %p has not been checked for completion",
7215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   (uint64_t)pCBInfo->lastSubmittedFence, cb);
7225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                *complete = false;
7235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
7245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
7255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
7265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
7275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
7285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
72958070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic VkBool32 freeMemObjInfo(layer_data *dev_data, void *object, VkDeviceMemory mem, VkBool32 internal) {
7305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
7315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Parse global list to find info w/ mem
73258070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    DEVICE_MEM_INFO *pInfo = get_mem_obj_info(dev_data, mem);
7335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pInfo) {
7345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pInfo->allocInfo.allocationSize == 0 && !internal) {
7355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO: Verify against Valid Use section
73658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
7375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MEM_OBJ, "MEM",
7385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               "Attempting to free memory associated with a Persistent Image, %#" PRIxLEAST64 ", "
7395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               "this should not be explicitly freed\n",
7405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               (uint64_t)mem);
7415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
7425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Clear any CB bindings for completed CBs
7435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            //   TODO : Is there a better place to do this?
7445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
7455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            bool commandBufferComplete = false;
7465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            assert(pInfo->object != VK_NULL_HANDLE);
7475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            list<VkCommandBuffer>::iterator it = pInfo->pCommandBufferBindings.begin();
7485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            list<VkCommandBuffer>::iterator temp;
7495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            while (pInfo->pCommandBufferBindings.size() > 0 && it != pInfo->pCommandBufferBindings.end()) {
75058070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                skipCall |= checkCBCompleted(dev_data, *it, &commandBufferComplete);
7515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (commandBufferComplete) {
7525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    temp = it;
7535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    ++temp;
75458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                    skipCall |= clear_cmd_buf_and_mem_references(dev_data, *it);
7555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    it = temp;
7565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else {
7575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    ++it;
7585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
7595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
7605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
7615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Now verify that no references to this mem obj remain and remove bindings
7625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (0 != pInfo->refCount) {
76358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                skipCall |= reportMemReferencesAndCleanUp(dev_data, pInfo);
7645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
7655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Delete mem obj info
76658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            skipCall |= deleteMemObjInfo(dev_data, object, mem);
7675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
7685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
7695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
7705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
7715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
7725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic const char *object_type_to_string(VkDebugReportObjectTypeEXT type) {
7735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (type) {
7745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT:
7755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "image";
7765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
7775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT:
7785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "buffer";
7795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
7805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DEBUG_REPORT_OBJECT_TYPE_SWAPCHAIN_KHR_EXT:
7815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "swapchain";
7825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
7835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
7845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "unknown";
7855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
7865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
7875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
7885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Remove object binding performs 3 tasks:
7895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// 1. Remove ObjectInfo from MemObjInfo list container of obj bindings & free it
7905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// 2. Decrement refCount for MemObjInfo
7915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// 3. Clear mem binding for image/buffer by setting its handle to 0
7925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// TODO : This only applied to Buffer, Image, and Swapchain objects now, how should it be updated/customized?
79358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic VkBool32 clear_object_binding(layer_data *dev_data, void *dispObj, uint64_t handle, VkDebugReportObjectTypeEXT type) {
7945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Need to customize images/buffers/swapchains to track mem binding and clear it here appropriately
7955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
79658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    MT_OBJ_BINDING_INFO *pObjBindInfo = get_object_binding_info(dev_data, handle, type);
7975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pObjBindInfo) {
79858070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        DEVICE_MEM_INFO *pMemObjInfo = get_mem_obj_info(dev_data, pObjBindInfo->mem);
7995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Make sure this is a reasonable way to reset mem binding
8005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pObjBindInfo->mem = VK_NULL_HANDLE;
8015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pMemObjInfo) {
8025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // This obj is bound to a memory object. Remove the reference to this object in that memory object's list, decrement the
8035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // memObj's refcount
8045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // and set the objects memory binding pointer to NULL.
8055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkBool32 clearSucceeded = VK_FALSE;
8065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (auto it = pMemObjInfo->pObjBindings.begin(); it != pMemObjInfo->pObjBindings.end(); ++it) {
8075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if ((it->handle == handle) && (it->type == type)) {
8085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pMemObjInfo->refCount--;
8095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pMemObjInfo->pObjBindings.erase(it);
8105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    clearSucceeded = VK_TRUE;
8115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    break;
8125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
8135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
8145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (VK_FALSE == clearSucceeded) {
8155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
81658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_INVALID_OBJECT,
8175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "MEM", "While trying to clear mem binding for %s obj %#" PRIxLEAST64
8185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   ", unable to find that object referenced by mem obj %#" PRIxLEAST64,
8195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            object_type_to_string(type), handle, (uint64_t)pMemObjInfo->mem);
8205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
8215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
8225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
8235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
8245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
8255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
8265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For NULL mem case, output warning
8275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Make sure given object is in global object map
8285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  IF a previous binding existed, output validation error
8295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  Otherwise, add reference from objectInfo to memoryInfo
8305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  Add reference off of objInfo
8315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  device is required for error logging, need a dispatchable
8325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  object for that.
83358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic VkBool32 set_mem_binding(layer_data *dev_data, void *dispatch_object, VkDeviceMemory mem, uint64_t handle,
8345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                VkDebugReportObjectTypeEXT type, const char *apiName) {
8355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
8365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Handle NULL case separately, just clear previous binding & decrement reference
8375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (mem == VK_NULL_HANDLE) {
8385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO: Verify against Valid Use section of spec.
83958070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_INVALID_MEM_OBJ,
8405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           "MEM", "In %s, attempting to Bind Obj(%#" PRIxLEAST64 ") to NULL", apiName, handle);
8415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
84258070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        MT_OBJ_BINDING_INFO *pObjBindInfo = get_object_binding_info(dev_data, handle, type);
8435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!pObjBindInfo) {
8445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
84558070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_MISSING_MEM_BINDINGS,
8465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "MEM", "In %s, attempting to update Binding of %s Obj(%#" PRIxLEAST64 ") that's not in global list()",
8475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        object_type_to_string(type), apiName, handle);
8485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
8495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // non-null case so should have real mem obj
85058070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            DEVICE_MEM_INFO *pMemInfo = get_mem_obj_info(dev_data, mem);
8515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pMemInfo) {
8525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // TODO : Need to track mem binding for obj and report conflict here
85358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                DEVICE_MEM_INFO *pPrevBinding = get_mem_obj_info(dev_data, pObjBindInfo->mem);
8545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pPrevBinding != NULL) {
8555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |=
85658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                        log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
8575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)mem, __LINE__, MEMTRACK_REBIND_OBJECT, "MEM",
8585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "In %s, attempting to bind memory (%#" PRIxLEAST64 ") to object (%#" PRIxLEAST64
8595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                ") which has already been bound to mem object %#" PRIxLEAST64,
8605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                apiName, (uint64_t)mem, handle, (uint64_t)pPrevBinding->mem);
8615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else {
8625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    MT_OBJ_HANDLE_TYPE oht;
8635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    oht.handle = handle;
8645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    oht.type = type;
8655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pMemInfo->pObjBindings.push_front(oht);
8665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pMemInfo->refCount++;
8675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // For image objects, make sure default memory state is correctly set
8685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // TODO : What's the best/correct way to handle this?
8695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT == type) {
8705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        VkImageCreateInfo ici = pObjBindInfo->create_info.image;
8715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (ici.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
8725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            // TODO::  More memory state transition stuff.
8735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
8745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
8755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pObjBindInfo->mem = mem;
8765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
8775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
8785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
8795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
8805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
8815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
8825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
8835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For NULL mem case, clear any previous binding Else...
8845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Make sure given object is in its object map
8855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  IF a previous binding existed, update binding
8865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  Add reference from objectInfo to memoryInfo
8875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  Add reference off of object's binding info
8885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return VK_TRUE if addition is successful, VK_FALSE otherwise
88958070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic VkBool32 set_sparse_mem_binding(layer_data *dev_data, void *dispObject, VkDeviceMemory mem, uint64_t handle,
8905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                       VkDebugReportObjectTypeEXT type, const char *apiName) {
8915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
8925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Handle NULL case separately, just clear previous binding & decrement reference
8935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (mem == VK_NULL_HANDLE) {
89458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        skipCall = clear_object_binding(dev_data, dispObject, handle, type);
8955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
89658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        MT_OBJ_BINDING_INFO *pObjBindInfo = get_object_binding_info(dev_data, handle, type);
8975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!pObjBindInfo) {
8985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(
89958070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_MISSING_MEM_BINDINGS, "MEM",
9005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                "In %s, attempting to update Binding of Obj(%#" PRIxLEAST64 ") that's not in global list()", apiName, handle);
9015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
9025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // non-null case so should have real mem obj
90358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        DEVICE_MEM_INFO *pInfo = get_mem_obj_info(dev_data, mem);
9045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pInfo) {
9055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Search for object in memory object's binding list
9065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkBool32 found = VK_FALSE;
9075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pInfo->pObjBindings.size() > 0) {
9085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto it = pInfo->pObjBindings.begin(); it != pInfo->pObjBindings.end(); ++it) {
9095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (((*it).handle == handle) && ((*it).type == type)) {
9105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        found = VK_TRUE;
9115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        break;
9125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
9135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
9145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
9155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // If not present, add to list
9165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (found == VK_FALSE) {
9175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                MT_OBJ_HANDLE_TYPE oht;
9185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                oht.handle = handle;
9195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                oht.type = type;
9205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pInfo->pObjBindings.push_front(oht);
9215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pInfo->refCount++;
9225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
9235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Need to set mem binding for this object
9245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pObjBindInfo->mem = mem;
9255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
9265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
9275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
9285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
9295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
9305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlistemplate <typename T>
9315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid print_object_map_members(layer_data *my_data, void *dispObj, T const &objectName, VkDebugReportObjectTypeEXT objectType,
9325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              const char *objectStr) {
9335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto const &element : objectName) {
9345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, objectType, 0, __LINE__, MEMTRACK_NONE, "MEM",
9355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                "    %s Object list contains %s Object %#" PRIxLEAST64 " ", objectStr, objectStr, element.first);
9365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
9375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
9385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
9395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For given Object, get 'mem' obj that it's bound to or NULL if no binding
9405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 get_mem_binding_from_object(layer_data *my_data, void *dispObj, const uint64_t handle,
9415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            const VkDebugReportObjectTypeEXT type, VkDeviceMemory *mem) {
9425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
9435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    *mem = VK_NULL_HANDLE;
9445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    MT_OBJ_BINDING_INFO *pObjBindInfo = get_object_binding_info(my_data, handle, type);
9455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pObjBindInfo) {
9465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pObjBindInfo->mem) {
9475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            *mem = pObjBindInfo->mem;
9485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
9495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall =
9505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_MISSING_MEM_BINDINGS,
9515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "MEM", "Trying to get mem binding for object %#" PRIxLEAST64 " but object has no mem binding", handle);
9525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
9535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
9545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, type, handle, __LINE__, MEMTRACK_INVALID_OBJECT,
9555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           "MEM", "Trying to get mem binding for object %#" PRIxLEAST64 " but no such object in %s list", handle,
9565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           object_type_to_string(type));
9575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
9585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
9595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
9605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
9615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Print details of MemObjInfo list
96258070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlisstatic void print_mem_list(layer_data *dev_data, void *dispObj) {
96358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    DEVICE_MEM_INFO *pInfo = NULL;
9645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
9655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Early out if info is not requested
96658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    if (!(dev_data->report_data->active_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)) {
9675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
9685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
9695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
9705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Just printing each msg individually for now, may want to package these into single large print
97158070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0, __LINE__,
9725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            MEMTRACK_NONE, "MEM", "Details of Memory Object list (of size " PRINTF_SIZE_T_SPECIFIER " elements)",
97358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            dev_data->memObjMap.size());
97458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0, __LINE__,
9755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            MEMTRACK_NONE, "MEM", "=============================");
9765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
97758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    if (dev_data->memObjMap.size() <= 0)
9785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
9795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
98058070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    for (auto ii = dev_data->memObjMap.begin(); ii != dev_data->memObjMap.end(); ++ii) {
9815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pInfo = &(*ii).second;
9825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
98358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
9845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                __LINE__, MEMTRACK_NONE, "MEM", "    ===MemObjInfo at %p===", (void *)pInfo);
98558070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
9865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                __LINE__, MEMTRACK_NONE, "MEM", "    Mem object: %#" PRIxLEAST64, (uint64_t)(pInfo->mem));
98758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
9885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                __LINE__, MEMTRACK_NONE, "MEM", "    Ref Count: %u", pInfo->refCount);
9895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (0 != pInfo->allocInfo.allocationSize) {
9905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            string pAllocInfoMsg = vk_print_vkmemoryallocateinfo(&pInfo->allocInfo, "MEM(INFO):         ");
99158070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
9925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    __LINE__, MEMTRACK_NONE, "MEM", "    Mem Alloc info:\n%s", pAllocInfoMsg.c_str());
9935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
99458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
9955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    __LINE__, MEMTRACK_NONE, "MEM", "    Mem Alloc info is NULL (alloc done by vkCreateSwapchainKHR())");
9965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
9975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
99858070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
9995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                __LINE__, MEMTRACK_NONE, "MEM", "    VK OBJECT Binding list of size " PRINTF_SIZE_T_SPECIFIER " elements:",
10005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pInfo->pObjBindings.size());
10015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pInfo->pObjBindings.size() > 0) {
10025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (list<MT_OBJ_HANDLE_TYPE>::iterator it = pInfo->pObjBindings.begin(); it != pInfo->pObjBindings.end(); ++it) {
100358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
100458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                        0, __LINE__, MEMTRACK_NONE, "MEM", "       VK OBJECT %" PRIu64, it->handle);
10055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
10065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
10075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
100858070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
10095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                __LINE__, MEMTRACK_NONE, "MEM",
10105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                "    VK Command Buffer (CB) binding list of size " PRINTF_SIZE_T_SPECIFIER " elements",
10115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pInfo->pCommandBufferBindings.size());
10125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pInfo->pCommandBufferBindings.size() > 0) {
10135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (list<VkCommandBuffer>::iterator it = pInfo->pCommandBufferBindings.begin();
10145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                 it != pInfo->pCommandBufferBindings.end(); ++it) {
101558070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
101658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                        0, __LINE__, MEMTRACK_NONE, "MEM", "      VK CB %p", (*it));
10175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
10185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
10195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
10205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
10215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
10225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void printCBList(layer_data *my_data, void *dispObj) {
10235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    MT_CB_INFO *pCBInfo = NULL;
10245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
10255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Early out if info is not requested
10265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!(my_data->report_data->active_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)) {
10275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
10285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
10295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
10305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0, __LINE__,
10315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            MEMTRACK_NONE, "MEM", "Details of CB list (of size " PRINTF_SIZE_T_SPECIFIER " elements)", my_data->cbMap.size());
10325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0, __LINE__,
10335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            MEMTRACK_NONE, "MEM", "==================");
10345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
10355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->cbMap.size() <= 0)
10365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
10375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
10385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto ii = my_data->cbMap.begin(); ii != my_data->cbMap.end(); ++ii) {
10395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCBInfo = &(*ii).second;
10405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
10415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
10425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                __LINE__, MEMTRACK_NONE, "MEM", "    CB Info (%p) has CB %p, fenceId %" PRIx64 ", and fence %#" PRIxLEAST64,
10435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                (void *)pCBInfo, (void *)pCBInfo->commandBuffer, pCBInfo->fenceId, (uint64_t)pCBInfo->lastSubmittedFence);
10445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
10455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCBInfo->pMemObjList.size() <= 0)
10465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
10475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (list<VkDeviceMemory>::iterator it = pCBInfo->pMemObjList.begin(); it != pCBInfo->pMemObjList.end(); ++it) {
10485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, 0,
10495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    __LINE__, MEMTRACK_NONE, "MEM", "      Mem obj %" PRIu64, (uint64_t)(*it));
10505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
10515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
10525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
10535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
10545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
10555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
10565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Map actual TID to an index value and return that index
10575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs
10585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic uint32_t getTIDIndex() {
10595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_id tid = loader_platform_get_thread_id();
10605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < g_maxTID; i++) {
10615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (tid == g_tidMapping[i])
10625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return i;
10635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
10645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Don't yet have mapping, set it and return newly set index
10655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t retVal = (uint32_t)g_maxTID;
10665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    g_tidMapping[g_maxTID++] = tid;
10675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(g_maxTID < MAX_TID);
10685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return retVal;
10695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
10705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
10715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return a string representation of CMD_TYPE enum
10725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic string cmdTypeToString(CMD_TYPE cmd) {
10735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (cmd) {
10745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_BINDPIPELINE:
10755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_BINDPIPELINE";
10765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_BINDPIPELINEDELTA:
10775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_BINDPIPELINEDELTA";
10785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_SETVIEWPORTSTATE:
10795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_SETVIEWPORTSTATE";
10805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_SETLINEWIDTHSTATE:
10815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_SETLINEWIDTHSTATE";
10825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_SETDEPTHBIASSTATE:
10835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_SETDEPTHBIASSTATE";
10845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_SETBLENDSTATE:
10855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_SETBLENDSTATE";
10865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_SETDEPTHBOUNDSSTATE:
10875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_SETDEPTHBOUNDSSTATE";
10885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_SETSTENCILREADMASKSTATE:
10895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_SETSTENCILREADMASKSTATE";
10905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_SETSTENCILWRITEMASKSTATE:
10915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_SETSTENCILWRITEMASKSTATE";
10925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_SETSTENCILREFERENCESTATE:
10935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_SETSTENCILREFERENCESTATE";
10945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_BINDDESCRIPTORSETS:
10955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_BINDDESCRIPTORSETS";
10965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_BINDINDEXBUFFER:
10975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_BINDINDEXBUFFER";
10985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_BINDVERTEXBUFFER:
10995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_BINDVERTEXBUFFER";
11005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_DRAW:
11015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_DRAW";
11025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_DRAWINDEXED:
11035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_DRAWINDEXED";
11045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_DRAWINDIRECT:
11055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_DRAWINDIRECT";
11065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_DRAWINDEXEDINDIRECT:
11075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_DRAWINDEXEDINDIRECT";
11085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_DISPATCH:
11095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_DISPATCH";
11105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_DISPATCHINDIRECT:
11115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_DISPATCHINDIRECT";
11125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_COPYBUFFER:
11135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_COPYBUFFER";
11145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_COPYIMAGE:
11155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_COPYIMAGE";
11165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_BLITIMAGE:
11175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_BLITIMAGE";
11185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_COPYBUFFERTOIMAGE:
11195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_COPYBUFFERTOIMAGE";
11205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_COPYIMAGETOBUFFER:
11215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_COPYIMAGETOBUFFER";
11225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_CLONEIMAGEDATA:
11235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_CLONEIMAGEDATA";
11245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_UPDATEBUFFER:
11255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_UPDATEBUFFER";
11265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_FILLBUFFER:
11275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_FILLBUFFER";
11285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_CLEARCOLORIMAGE:
11295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_CLEARCOLORIMAGE";
11305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_CLEARATTACHMENTS:
11315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_CLEARCOLORATTACHMENT";
11325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_CLEARDEPTHSTENCILIMAGE:
11335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_CLEARDEPTHSTENCILIMAGE";
11345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_RESOLVEIMAGE:
11355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_RESOLVEIMAGE";
11365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_SETEVENT:
11375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_SETEVENT";
11385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_RESETEVENT:
11395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_RESETEVENT";
11405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_WAITEVENTS:
11415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_WAITEVENTS";
11425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_PIPELINEBARRIER:
11435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_PIPELINEBARRIER";
11445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_BEGINQUERY:
11455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_BEGINQUERY";
11465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_ENDQUERY:
11475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_ENDQUERY";
11485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_RESETQUERYPOOL:
11495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_RESETQUERYPOOL";
11505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_COPYQUERYPOOLRESULTS:
11515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_COPYQUERYPOOLRESULTS";
11525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_WRITETIMESTAMP:
11535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_WRITETIMESTAMP";
11545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_INITATOMICCOUNTERS:
11555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_INITATOMICCOUNTERS";
11565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_LOADATOMICCOUNTERS:
11575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_LOADATOMICCOUNTERS";
11585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_SAVEATOMICCOUNTERS:
11595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_SAVEATOMICCOUNTERS";
11605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_BEGINRENDERPASS:
11615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_BEGINRENDERPASS";
11625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case CMD_ENDRENDERPASS:
11635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "CMD_ENDRENDERPASS";
11645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
11655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "UNKNOWN";
11665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
11675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
11685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
11695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// SPIRV utility functions
11705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void build_def_index(shader_module *module) {
11715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto insn : *module) {
11725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        switch (insn.opcode()) {
11735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* Types */
11745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeVoid:
11755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeBool:
11765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeInt:
11775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeFloat:
11785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeVector:
11795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeMatrix:
11805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeImage:
11815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeSampler:
11825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeSampledImage:
11835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeArray:
11845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeRuntimeArray:
11855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeStruct:
11865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeOpaque:
11875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypePointer:
11885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeFunction:
11895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeEvent:
11905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeDeviceEvent:
11915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeReserveId:
11925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypeQueue:
11935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpTypePipe:
11945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            module->def_index[insn.word(1)] = insn.offset();
11955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
11965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
11975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* Fixed constants */
11985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpConstantTrue:
11995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpConstantFalse:
12005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpConstant:
12015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpConstantComposite:
12025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpConstantSampler:
12035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpConstantNull:
12045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            module->def_index[insn.word(2)] = insn.offset();
12055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
12065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* Specialization constants */
12085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpSpecConstantTrue:
12095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpSpecConstantFalse:
12105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpSpecConstant:
12115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpSpecConstantComposite:
12125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpSpecConstantOp:
12135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            module->def_index[insn.word(2)] = insn.offset();
12145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
12155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* Variables */
12175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpVariable:
12185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            module->def_index[insn.word(2)] = insn.offset();
12195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
12205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* Functions */
12225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpFunction:
12235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            module->def_index[insn.word(2)] = insn.offset();
12245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
12255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        default:
12275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            /* We don't care about any other defs for now. */
12285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
12295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
12305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
12315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
12325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic spirv_inst_iter find_entrypoint(shader_module *src, char const *name, VkShaderStageFlagBits stageBits) {
12345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto insn : *src) {
12355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (insn.opcode() == spv::OpEntryPoint) {
12365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto entrypointName = (char const *)&insn.word(3);
12375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto entrypointStageBits = 1u << insn.word(1);
12385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!strcmp(entrypointName, name) && (entrypointStageBits & stageBits)) {
12405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return insn;
12415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
12425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
12435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
12445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return src->end();
12465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
12475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool shader_is_spirv(VkShaderModuleCreateInfo const *pCreateInfo) {
12495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t *words = (uint32_t *)pCreateInfo->pCode;
12505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    size_t sizeInWords = pCreateInfo->codeSize / sizeof(uint32_t);
12515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* Just validate that the header makes sense. */
12535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return sizeInWords >= 5 && words[0] == spv::MagicNumber && words[1] == spv::Version;
12545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
12555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic char const *storage_class_name(unsigned sc) {
12575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (sc) {
12585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassInput:
12595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "input";
12605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassOutput:
12615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "output";
12625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassUniformConstant:
12635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "const uniform";
12645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassUniform:
12655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "uniform";
12665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassWorkgroup:
12675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "workgroup local";
12685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassCrossWorkgroup:
12695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "workgroup global";
12705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassPrivate:
12715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "private global";
12725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassFunction:
12735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "function";
12745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassGeneric:
12755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "generic";
12765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassAtomicCounter:
12775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "atomic counter";
12785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassImage:
12795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "image";
12805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::StorageClassPushConstant:
12815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "push constant";
12825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
12835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return "unknown";
12845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
12855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
12865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis/* get the value of an integral constant */
12885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisunsigned get_constant_value(shader_module const *src, unsigned id) {
12895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto value = src->get_def(id);
12905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(value != src->end());
12915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (value.opcode() != spv::OpConstant) {
12935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* TODO: Either ensure that the specialization transform is already performed on a module we're
12945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            considering here, OR -- specialize on the fly now.
12955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            */
12965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return 1;
12975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
12985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
12995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return value.word(3);
13005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
13015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
13029ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes
13039ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbesstatic void describe_type_inner(std::ostringstream &ss, shader_module const *src, unsigned type) {
13045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto insn = src->get_def(type);
13055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(insn != src->end());
13065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
13075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (insn.opcode()) {
13085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeBool:
13099ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "bool";
13109ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeInt:
13129ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
13139ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeFloat:
13159ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "float" << insn.word(2);
13169ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeVector:
13189ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "vec" << insn.word(3) << " of ";
13199ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        describe_type_inner(ss, src, insn.word(2));
13209ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeMatrix:
13229ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "mat" << insn.word(3) << " of ";
13239ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        describe_type_inner(ss, src, insn.word(2));
13249ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeArray:
13269ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "arr[" << get_constant_value(src, insn.word(3)) << "] of ";
13279ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        describe_type_inner(ss, src, insn.word(2));
13289ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypePointer:
13309ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "ptr to " << storage_class_name(insn.word(2)) << " ";
13319ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        describe_type_inner(ss, src, insn.word(3));
13329ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeStruct: {
13349ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "struct of (";
13355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (unsigned i = 2; i < insn.len(); i++) {
13369ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes            describe_type_inner(ss, src, insn.word(i));
13379ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes            if (i == insn.len() - 1) {
13389ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                ss << ")";
13399ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes            } else {
13409ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                ss << ", ";
13419ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes            }
13425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
13439ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
13455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeSampler:
13469ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "sampler";
13479ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeSampledImage:
13499ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "sampler+";
13509ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        describe_type_inner(ss, src, insn.word(2));
13519ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeImage:
13539ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
13549ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
13569ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        ss << "oddtype";
13579ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes        break;
13585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
13595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
13605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
13619ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes
13629ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbesstatic std::string describe_type(shader_module const *src, unsigned type) {
13639ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes    std::ostringstream ss;
13649ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes    describe_type_inner(ss, src, type);
13659ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes    return ss.str();
13669ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes}
13679ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes
13689ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes
13695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool types_match(shader_module const *a, shader_module const *b, unsigned a_type, unsigned b_type, bool b_arrayed) {
13705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* walk two type trees together, and complain about differences */
13715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto a_insn = a->get_def(a_type);
13725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto b_insn = b->get_def(b_type);
13735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(a_insn != a->end());
13745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(b_insn != b->end());
13755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
13765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) {
13775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
13785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return types_match(a, b, a_type, b_insn.word(2), false);
13795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
13805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
13815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (a_insn.opcode() != b_insn.opcode()) {
13825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false;
13835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
13845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
13855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (a_insn.opcode()) {
13865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* if b_arrayed and we hit a leaf type, then we can't match -- there's nowhere for the extra OpTypeArray to be! */
13875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeBool:
13885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return true && !b_arrayed;
13895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeInt:
13905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* match on width, signedness */
13915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3) && !b_arrayed;
13925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeFloat:
13935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* match on width */
13945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return a_insn.word(2) == b_insn.word(2) && !b_arrayed;
13955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeVector:
13965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeMatrix:
13975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* match on element type, count. these all have the same layout. we don't get here if
13985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * b_arrayed -- that is handled above. */
13995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return !b_arrayed && types_match(a, b, a_insn.word(2), b_insn.word(2), b_arrayed) && a_insn.word(3) == b_insn.word(3);
14005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeArray:
14015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* match on element type, count. these all have the same layout. we don't get here if
14025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * b_arrayed. This differs from vector & matrix types in that the array size is the id of a constant instruction,
14035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * not a literal within OpTypeArray */
14045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return !b_arrayed && types_match(a, b, a_insn.word(2), b_insn.word(2), b_arrayed) &&
14055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis               get_constant_value(a, a_insn.word(3)) == get_constant_value(b, b_insn.word(3));
14065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeStruct:
14075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* match on all element types */
14085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        {
14095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (b_arrayed) {
14105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                /* for the purposes of matching different levels of arrayness, structs are leaves. */
14115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return false;
14125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
14135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (a_insn.len() != b_insn.len()) {
14155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return false; /* structs cannot match if member counts differ */
14165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
14175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (unsigned i = 2; i < a_insn.len(); i++) {
14195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (!types_match(a, b, a_insn.word(i), b_insn.word(i), b_arrayed)) {
14205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    return false;
14215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
14225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
14235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return true;
14255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
14265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypePointer:
14275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* match on pointee type. storage class is expected to differ */
14285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return types_match(a, b, a_insn.word(3), b_insn.word(3), b_arrayed);
14295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
14315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* remaining types are CLisms, or may not appear in the interfaces we
14325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * are interested in. Just claim no match.
14335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         */
14345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false;
14355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
14365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
14375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic int value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def) {
14395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto it = map.find(id);
14405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (it == map.end())
14415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return def;
14425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    else
14435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return it->second;
14445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
14455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic unsigned get_locations_consumed_by_type(shader_module const *src, unsigned type, bool strip_array_level) {
14475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto insn = src->get_def(type);
14485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(insn != src->end());
14495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (insn.opcode()) {
14515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypePointer:
14525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* see through the ptr -- this is only ever at the toplevel for graphics shaders;
14535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * we're never actually passing pointers around. */
14545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return get_locations_consumed_by_type(src, insn.word(3), strip_array_level);
14555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeArray:
14565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (strip_array_level) {
14575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return get_locations_consumed_by_type(src, insn.word(2), false);
14585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
14595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return get_constant_value(src, insn.word(3)) * get_locations_consumed_by_type(src, insn.word(2), false);
14605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
14615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeMatrix:
14625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* num locations is the dimension * element size */
14635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return insn.word(3) * get_locations_consumed_by_type(src, insn.word(2), false);
14645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
14655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* everything else is just 1. */
14665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return 1;
14675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* TODO: extend to handle 64bit scalar types, whose vectors may need
14695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * multiple locations. */
14705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
14715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
14725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlistypedef std::pair<unsigned, unsigned> location_t;
14745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlistypedef std::pair<unsigned, unsigned> descriptor_slot_t;
14755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstruct interface_var {
14775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t id;
14785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t type_id;
14795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t offset;
14805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* TODO: collect the name, too? Isn't required to be present. */
14815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis};
14825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic spirv_inst_iter get_struct_type(shader_module const *src, spirv_inst_iter def, bool is_array_of_verts) {
14845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while (true) {
14855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (def.opcode() == spv::OpTypePointer) {
14875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            def = src->get_def(def.word(3));
14885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
14895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            def = src->get_def(def.word(2));
14905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            is_array_of_verts = false;
14915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (def.opcode() == spv::OpTypeStruct) {
14925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return def;
14935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
14945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return src->end();
14955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
14965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
14975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
14985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
14995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void collect_interface_block_members(layer_data *my_data, VkDevice dev, shader_module const *src,
15005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            std::map<location_t, interface_var> &out,
15015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            std::unordered_map<unsigned, unsigned> const &blocks, bool is_array_of_verts,
15025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            uint32_t id, uint32_t type_id) {
15035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* Walk down the type_id presented, trying to determine whether it's actually an interface block. */
15045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto type = get_struct_type(src, src->get_def(type_id), is_array_of_verts);
15055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (type == src->end() || blocks.find(type.word(1)) == blocks.end()) {
15065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* this isn't an interface block. */
15075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
15085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
15095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::unordered_map<unsigned, unsigned> member_components;
15115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* Walk all the OpMemberDecorate for type's result id -- first pass, collect components. */
15135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto insn : *src) {
15145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
15155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned member_index = insn.word(2);
15165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (insn.word(3) == spv::DecorationComponent) {
15185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                unsigned component = insn.word(4);
15195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                member_components[member_index] = component;
15205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
15215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
15225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
15235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* Second pass -- produce the output, from Location decorations */
15255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto insn : *src) {
15265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
15275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned member_index = insn.word(2);
15285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned member_type_id = type.word(2 + member_index);
15295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (insn.word(3) == spv::DecorationLocation) {
15315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                unsigned location = insn.word(4);
15325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                unsigned num_locations = get_locations_consumed_by_type(src, member_type_id, false);
15335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto component_it = member_components.find(member_index);
15345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                unsigned component = component_it == member_components.end() ? 0 : component_it->second;
15355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (unsigned int offset = 0; offset < num_locations; offset++) {
15375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    interface_var v;
15385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    v.id = id;
15395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    /* TODO: member index in interface_var too? */
15405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    v.type_id = member_type_id;
15415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    v.offset = offset;
15425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    out[std::make_pair(location + offset, component)] = v;
15435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
15445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
15455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
15465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
15475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
15485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void collect_interface_by_location(layer_data *my_data, VkDevice dev, shader_module const *src, spirv_inst_iter entrypoint,
15505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                          spv::StorageClass sinterface, std::map<location_t, interface_var> &out,
15515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                          bool is_array_of_verts) {
15525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::unordered_map<unsigned, unsigned> var_locations;
15535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::unordered_map<unsigned, unsigned> var_builtins;
15545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::unordered_map<unsigned, unsigned> var_components;
15555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::unordered_map<unsigned, unsigned> blocks;
15565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto insn : *src) {
15585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* We consider two interface models: SSO rendezvous-by-location, and
15605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * builtins. Complain about anything that fits neither model.
15615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         */
15625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (insn.opcode() == spv::OpDecorate) {
15635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (insn.word(2) == spv::DecorationLocation) {
15645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                var_locations[insn.word(1)] = insn.word(3);
15655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
15665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (insn.word(2) == spv::DecorationBuiltIn) {
15685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                var_builtins[insn.word(1)] = insn.word(3);
15695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
15705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (insn.word(2) == spv::DecorationComponent) {
15725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                var_components[insn.word(1)] = insn.word(3);
15735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
15745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (insn.word(2) == spv::DecorationBlock) {
15765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                blocks[insn.word(1)] = 1;
15775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
15785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
15795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
15805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* TODO: handle grouped decorations */
15825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* TODO: handle index=1 dual source outputs from FS -- two vars will
15835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     * have the same location, and we DONT want to clobber. */
15845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* find the end of the entrypoint's name string. additional zero bytes follow the actual null
15865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis       terminator, to fill out the rest of the word - so we only need to look at the last byte in
15875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis       the word to determine which word contains the terminator. */
15885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto word = 3;
15895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while (entrypoint.word(word) & 0xff000000u) {
15905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ++word;
15915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
15925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ++word;
15935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (; word < entrypoint.len(); word++) {
15955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto insn = src->get_def(entrypoint.word(word));
15965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        assert(insn != src->end());
15975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        assert(insn.opcode() == spv::OpVariable);
15985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
15995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (insn.word(3) == sinterface) {
16005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned id = insn.word(2);
16015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned type = insn.word(1);
16025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            int location = value_or_default(var_locations, id, -1);
16045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            int builtin = value_or_default(var_builtins, id, -1);
16055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned component = value_or_default(var_components, id, 0); /* unspecified is OK, is 0 */
16065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            /* All variables and interface block members in the Input or Output storage classes
16085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             * must be decorated with either a builtin or an explicit location.
16095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             *
16105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             * TODO: integrate the interface block support here. For now, don't complain --
16115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             * a valid SPIRV module will only hit this path for the interface block case, as the
16125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             * individual members of the type are decorated, rather than variable declarations.
16135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             */
16145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (location != -1) {
16165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                /* A user-defined interface variable, with a location. Where a variable
16175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                 * occupied multiple locations, emit one result for each. */
16185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                unsigned num_locations = get_locations_consumed_by_type(src, type, is_array_of_verts);
16195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (unsigned int offset = 0; offset < num_locations; offset++) {
16205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    interface_var v;
16215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    v.id = id;
16225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    v.type_id = type;
16235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    v.offset = offset;
16245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    out[std::make_pair(location + offset, component)] = v;
16255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
16265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else if (builtin == -1) {
16275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                /* An interface block instance */
16285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                collect_interface_block_members(my_data, dev, src, out, blocks, is_array_of_verts, id, type);
16295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
16305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
16315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
16325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
16335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void collect_interface_by_descriptor_slot(layer_data *my_data, VkDevice dev, shader_module const *src,
16355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 std::unordered_set<uint32_t> const &accessible_ids,
16365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 std::map<descriptor_slot_t, interface_var> &out) {
16375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::unordered_map<unsigned, unsigned> var_sets;
16395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::unordered_map<unsigned, unsigned> var_bindings;
16405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto insn : *src) {
16425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* All variables in the Uniform or UniformConstant storage classes are required to be decorated with both
16435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * DecorationDescriptorSet and DecorationBinding.
16445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         */
16455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (insn.opcode() == spv::OpDecorate) {
16465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (insn.word(2) == spv::DecorationDescriptorSet) {
16475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                var_sets[insn.word(1)] = insn.word(3);
16485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
16495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (insn.word(2) == spv::DecorationBinding) {
16515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                var_bindings[insn.word(1)] = insn.word(3);
16525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
16535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
16545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
16555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto id : accessible_ids) {
16575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto insn = src->get_def(id);
16585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        assert(insn != src->end());
16595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (insn.opcode() == spv::OpVariable &&
16615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant)) {
16625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned set = value_or_default(var_sets, insn.word(2), 0);
16635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned binding = value_or_default(var_bindings, insn.word(2), 0);
16645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto existing_it = out.find(std::make_pair(set, binding));
16665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (existing_it != out.end()) {
16675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                /* conflict within spv image */
16685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
16695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        __LINE__, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
16705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition",
16715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        insn.word(2), insn.word(1), storage_class_name(insn.word(3)), existing_it->first.first,
16725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        existing_it->first.second);
16735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
16745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            interface_var v;
16765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            v.id = insn.word(2);
16775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            v.type_id = insn.word(1);
16785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            out[std::make_pair(set, binding)] = v;
16795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
16805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
16815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
16825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool validate_interface_between_stages(layer_data *my_data, VkDevice dev, shader_module const *producer,
16845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                              spirv_inst_iter producer_entrypoint, char const *producer_name,
16855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                              shader_module const *consumer, spirv_inst_iter consumer_entrypoint,
16865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                              char const *consumer_name, bool consumer_arrayed_input) {
16875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::map<location_t, interface_var> outputs;
16885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::map<location_t, interface_var> inputs;
16895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool pass = true;
16915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    collect_interface_by_location(my_data, dev, producer, producer_entrypoint, spv::StorageClassOutput, outputs, false);
16935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    collect_interface_by_location(my_data, dev, consumer, consumer_entrypoint, spv::StorageClassInput, inputs,
16945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  consumer_arrayed_input);
16955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto a_it = outputs.begin();
16975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto b_it = inputs.begin();
16985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
16995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* maps sorted by key (location); walk them together to find mismatches */
17005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
17015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
17025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
17035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
17045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
17055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
17065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
17075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
17085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        /*dev*/ 0, __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
17095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "%s writes to output location %u.%u which is not consumed by %s", producer_name, a_first.first,
17105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        a_first.second, consumer_name)) {
17115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pass = false;
17125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
17135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            a_it++;
17145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (a_at_end || a_first > b_first) {
17155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
17165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        __LINE__, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
17175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "%s consumes input location %u.%u which is not written by %s", consumer_name, b_first.first, b_first.second,
17185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        producer_name)) {
17195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pass = false;
17205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
17215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            b_it++;
17225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
17235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id, consumer_arrayed_input)) {
17245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                /* OK! */
17255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
17265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
17275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            __LINE__, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", "Type mismatch on location %u.%u: '%s' vs '%s'",
17289ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                            a_first.first, a_first.second,
17299ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                            describe_type(producer, a_it->second.type_id).c_str(),
17309ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                            describe_type(consumer, b_it->second.type_id).c_str())) {
17315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pass = false;
17325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
17335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
17345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            a_it++;
17355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            b_it++;
17365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
17375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
17385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
17395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return pass;
17405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
17415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
17425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisenum FORMAT_TYPE {
17435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    FORMAT_TYPE_UNDEFINED,
17445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
17455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    FORMAT_TYPE_SINT,
17465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    FORMAT_TYPE_UINT,
17475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis};
17485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
17495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic unsigned get_format_type(VkFormat fmt) {
17505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (fmt) {
17515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_UNDEFINED:
17525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return FORMAT_TYPE_UNDEFINED;
17535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R8_SINT:
17545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R8G8_SINT:
17555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R8G8B8_SINT:
17565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R8G8B8A8_SINT:
17575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R16_SINT:
17585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R16G16_SINT:
17595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R16G16B16_SINT:
17605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R16G16B16A16_SINT:
17615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R32_SINT:
17625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R32G32_SINT:
17635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R32G32B32_SINT:
17645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R32G32B32A32_SINT:
17655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_B8G8R8_SINT:
17665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_B8G8R8A8_SINT:
17675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_A2B10G10R10_SINT_PACK32:
17685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_A2R10G10B10_SINT_PACK32:
17695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return FORMAT_TYPE_SINT;
17705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R8_UINT:
17715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R8G8_UINT:
17725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R8G8B8_UINT:
17735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R8G8B8A8_UINT:
17745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R16_UINT:
17755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R16G16_UINT:
17765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R16G16B16_UINT:
17775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R16G16B16A16_UINT:
17785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R32_UINT:
17795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R32G32_UINT:
17805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R32G32B32_UINT:
17815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_R32G32B32A32_UINT:
17825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_B8G8R8_UINT:
17835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_B8G8R8A8_UINT:
17845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_A2B10G10R10_UINT_PACK32:
17855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_FORMAT_A2R10G10B10_UINT_PACK32:
17865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return FORMAT_TYPE_UINT;
17875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
17885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return FORMAT_TYPE_FLOAT;
17895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
17905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
17915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
17925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis/* characterizes a SPIR-V type appearing in an interface to a FF stage,
17935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * for comparison to a VkFormat's characterization above. */
17945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic unsigned get_fundamental_type(shader_module const *src, unsigned type) {
17955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto insn = src->get_def(type);
17965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(insn != src->end());
17975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
17985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (insn.opcode()) {
17995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeInt:
18005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
18015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeFloat:
18025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return FORMAT_TYPE_FLOAT;
18035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeVector:
18045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return get_fundamental_type(src, insn.word(2));
18055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeMatrix:
18065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return get_fundamental_type(src, insn.word(2));
18075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeArray:
18085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return get_fundamental_type(src, insn.word(2));
18095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypePointer:
18105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return get_fundamental_type(src, insn.word(3));
18115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
18125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return FORMAT_TYPE_UNDEFINED;
18135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
18145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
18155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic uint32_t get_shader_stage_id(VkShaderStageFlagBits stage) {
18175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t bit_pos = u_ffs(stage);
18185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return bit_pos - 1;
18195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
18205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool validate_vi_consistency(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi) {
18225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
18235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     * each binding should be specified only once.
18245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     */
18255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
18265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool pass = true;
18275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
18295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto desc = &vi->pVertexBindingDescriptions[i];
18305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto &binding = bindings[desc->binding];
18315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (binding) {
18325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
18335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        __LINE__, SHADER_CHECKER_INCONSISTENT_VI, "SC",
18345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
18355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pass = false;
18365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
18375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
18385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            binding = desc;
18395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
18405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
18415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return pass;
18435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
18445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool validate_vi_against_vs_inputs(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi,
18465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                          shader_module const *vs, spirv_inst_iter entrypoint) {
18475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::map<location_t, interface_var> inputs;
18485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool pass = true;
18495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    collect_interface_by_location(my_data, dev, vs, entrypoint, spv::StorageClassInput, inputs, false);
18515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* Build index by location */
18535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
18545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (vi) {
18555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
18565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
18575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
18585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto it_a = attribs.begin();
18605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto it_b = inputs.begin();
18615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
18635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
18645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
18655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto a_first = a_at_end ? 0 : it_a->first;
18665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto b_first = b_at_end ? 0 : it_b->first.first;
18675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!a_at_end && (b_at_end || a_first < b_first)) {
18685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
18695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        /*dev*/ 0, __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
18705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Vertex attribute at location %d not consumed by VS", a_first)) {
18715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pass = false;
18725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
18735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            it_a++;
18745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (!b_at_end && (a_at_end || b_first < a_first)) {
18755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
18765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        __LINE__, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", "VS consumes input at location %d but not provided",
18775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        b_first)) {
18785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pass = false;
18795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
18805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            it_b++;
18815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
18825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned attrib_type = get_format_type(it_a->second->format);
18835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
18845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            /* type checking */
18865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
18875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
18885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            __LINE__, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
18895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Attribute type of `%s` at location %d does not match VS input type of `%s`",
18909ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                            string_VkFormat(it_a->second->format), a_first,
18919ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                            describe_type(vs, it_b->second.type_id).c_str())) {
18925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pass = false;
18935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
18945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
18955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
18965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            /* OK! */
18975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            it_a++;
18985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            it_b++;
18995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
19005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
19015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return pass;
19035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
19045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool validate_fs_outputs_against_render_pass(layer_data *my_data, VkDevice dev, shader_module const *fs,
19065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                    spirv_inst_iter entrypoint, RENDER_PASS_NODE const *rp, uint32_t subpass) {
19075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const std::vector<VkFormat> &color_formats = rp->subpassColorFormats[subpass];
19085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::map<location_t, interface_var> outputs;
19095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool pass = true;
19105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
19125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    collect_interface_by_location(my_data, dev, fs, entrypoint, spv::StorageClassOutput, outputs, false);
19145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto it = outputs.begin();
19165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t attachment = 0;
19175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* Walk attachment list and outputs together -- this is a little overpowered since attachments
19195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     * are currently dense, but the parallel with matching between shader stages is nice.
19205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     */
19215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
19235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (attachment == color_formats.size() || (it != outputs.end() && it->first.first < attachment)) {
19245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
19255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
19265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "FS writes to output location %d with no matching attachment", it->first.first)) {
19275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pass = false;
19285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
19295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            it++;
19305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (it == outputs.end() || it->first.first > attachment) {
19315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
19325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        __LINE__, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", "Attachment %d not written by FS", attachment)) {
19335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pass = false;
19345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
19355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            attachment++;
19365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
19375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned output_type = get_fundamental_type(fs, it->second.type_id);
19385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            unsigned att_type = get_format_type(color_formats[attachment]);
19395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            /* type checking */
19415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
19425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /*dev*/ 0,
19435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            __LINE__, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
19445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Attachment %d of type `%s` does not match FS output type of `%s`", attachment,
19459ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                            string_VkFormat(color_formats[attachment]),
19469ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                            describe_type(fs, it->second.type_id).c_str())) {
19475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pass = false;
19485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
19495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
19505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            /* OK! */
19525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            it++;
19535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            attachment++;
19545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
19555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
19565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return pass;
19585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
19595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis/* For some analyses, we need to know about all ids referenced by the static call tree of a particular
19615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * entrypoint. This is important for identifying the set of shader resources actually used by an entrypoint,
19625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * for example.
19635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * Note: we only explore parts of the image which might actually contain ids we care about for the above analyses.
19645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis *  - NOT the shader input/output interfaces.
19655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis *
19665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth
19675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis * converting parts of this to be generated from the machine-readable spec instead.
19685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis */
19695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void mark_accessible_ids(shader_module const *src, spirv_inst_iter entrypoint, std::unordered_set<uint32_t> &ids) {
19705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::unordered_set<uint32_t> worklist;
19715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    worklist.insert(entrypoint.word(2));
19725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while (!worklist.empty()) {
19745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto id_iter = worklist.begin();
19755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto id = *id_iter;
19765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        worklist.erase(id_iter);
19775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto insn = src->get_def(id);
19795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (insn == src->end()) {
19805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            /* id is something we didnt collect in build_def_index. that's OK -- we'll stumble
19815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             * across all kinds of things here that we may not care about. */
19825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
19835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
19845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* try to add to the output set */
19865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!ids.insert(id).second) {
19875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue; /* if we already saw this id, we don't want to walk it again. */
19885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
19895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
19905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        switch (insn.opcode()) {
19915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case spv::OpFunction:
19925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            /* scan whole body of the function, enlisting anything interesting */
19935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            while (++insn, insn.opcode() != spv::OpFunctionEnd) {
19945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                switch (insn.opcode()) {
19955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpLoad:
19965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicLoad:
19975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicExchange:
19985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicCompareExchange:
19995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicCompareExchangeWeak:
20005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicIIncrement:
20015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicIDecrement:
20025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicIAdd:
20035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicISub:
20045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicSMin:
20055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicUMin:
20065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicSMax:
20075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicUMax:
20085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicAnd:
20095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicOr:
20105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicXor:
20115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    worklist.insert(insn.word(3)); /* ptr */
20125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    break;
20135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpStore:
20145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAtomicStore:
20155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    worklist.insert(insn.word(1)); /* ptr */
20165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    break;
20175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpAccessChain:
20185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpInBoundsAccessChain:
20195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    worklist.insert(insn.word(3)); /* base ptr */
20205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    break;
20215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpSampledImage:
20225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSampleImplicitLod:
20235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSampleExplicitLod:
20245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSampleDrefImplicitLod:
20255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSampleDrefExplicitLod:
20265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSampleProjImplicitLod:
20275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSampleProjExplicitLod:
20285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSampleProjDrefImplicitLod:
20295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSampleProjDrefExplicitLod:
20305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageFetch:
20315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageGather:
20325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageDrefGather:
20335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageRead:
20345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImage:
20355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageQueryFormat:
20365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageQueryOrder:
20375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageQuerySizeLod:
20385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageQuerySize:
20395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageQueryLod:
20405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageQueryLevels:
20415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageQuerySamples:
20425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseSampleImplicitLod:
20435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseSampleExplicitLod:
20445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseSampleDrefImplicitLod:
20455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseSampleDrefExplicitLod:
20465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseSampleProjImplicitLod:
20475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseSampleProjExplicitLod:
20485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseSampleProjDrefImplicitLod:
20495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseSampleProjDrefExplicitLod:
20505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseFetch:
20515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseGather:
20525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageSparseDrefGather:
20535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageTexelPointer:
20545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    worklist.insert(insn.word(3)); /* image or sampled image */
20555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    break;
20565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpImageWrite:
20575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    worklist.insert(insn.word(1)); /* image -- different operand order to above */
20585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    break;
20595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpFunctionCall:
20605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    for (auto i = 3; i < insn.len(); i++) {
20615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        worklist.insert(insn.word(i)); /* fn itself, and all args */
20625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
20635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    break;
20645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
20655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case spv::OpExtInst:
20665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    for (auto i = 5; i < insn.len(); i++) {
20675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        worklist.insert(insn.word(i)); /* operands to ext inst */
20685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
20695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    break;
20705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
20715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
20725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
20735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
20745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
20755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
20765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
20775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstruct shader_stage_attributes {
20785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    char const *const name;
20795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool arrayed_input;
20805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis};
20815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
20825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic shader_stage_attributes shader_stage_attribs[] = {
20835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    {"vertex shader", false},
20845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    {"tessellation control shader", true},
20855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    {"tessellation evaluation shader", false},
20865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    {"geometry shader", true},
20875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    {"fragment shader", false},
20885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis};
20895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
20905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool validate_push_constant_block_against_pipeline(layer_data *my_data, VkDevice dev,
20915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                          std::vector<VkPushConstantRange> const *pushConstantRanges,
20925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                          shader_module const *src, spirv_inst_iter type,
20935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                          VkShaderStageFlagBits stage) {
20945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool pass = true;
20955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
20965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* strip off ptrs etc */
20975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    type = get_struct_type(src, type, false);
20985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(type != src->end());
20995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* validate directly off the offsets. this isn't quite correct for arrays
21015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     * and matrices, but is a good first step. TODO: arrays, matrices, weird
21025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     * sizes */
21035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto insn : *src) {
21045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
21055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (insn.word(3) == spv::DecorationOffset) {
21075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                unsigned offset = insn.word(4);
21085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto size = 4; /* bytes; TODO: calculate this based on the type */
21095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                bool found_range = false;
21115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto const &range : *pushConstantRanges) {
21125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (range.offset <= offset && range.offset + range.size >= offset + size) {
21135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        found_range = true;
21145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if ((range.stageFlags & stage) == 0) {
21165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
21175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        /* dev */ 0, __LINE__, SHADER_CHECKER_PUSH_CONSTANT_NOT_ACCESSIBLE_FROM_STAGE, "SC",
21185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "Push constant range covering variable starting at "
21195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "offset %u not accessible from stage %s",
21205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        offset, string_VkShaderStageFlagBits(stage))) {
21215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                pass = false;
21225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            }
21235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
21245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        break;
21265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
21275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
21285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (!found_range) {
21305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
21315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                /* dev */ 0, __LINE__, SHADER_CHECKER_PUSH_CONSTANT_OUT_OF_RANGE, "SC",
21325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Push constant range covering variable starting at "
21335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "offset %u not declared in layout",
21345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                offset)) {
21355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        pass = false;
21365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
21375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
21385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
21395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
21405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
21415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return pass;
21435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
21445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool validate_push_constant_usage(layer_data *my_data, VkDevice dev,
21465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         std::vector<VkPushConstantRange> const *pushConstantRanges, shader_module const *src,
21475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         std::unordered_set<uint32_t> accessible_ids, VkShaderStageFlagBits stage) {
21485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool pass = true;
21495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto id : accessible_ids) {
21515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto def_insn = src->get_def(id);
21525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (def_insn.opcode() == spv::OpVariable && def_insn.word(3) == spv::StorageClassPushConstant) {
21535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pass = validate_push_constant_block_against_pipeline(my_data, dev, pushConstantRanges, src,
21545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                 src->get_def(def_insn.word(1)), stage) &&
21555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   pass;
21565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
21575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
21585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return pass;
21605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
21615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For given pipelineLayout verify that the setLayout at slot.first
21635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  has the requested binding at slot.second
216437083dec1ff1ca643bfc06b5308ca5ac718d58b0Chris Forbesstatic VkDescriptorSetLayoutBinding const * get_descriptor_binding(layer_data *my_data, vector<VkDescriptorSetLayout> *pipelineLayout, descriptor_slot_t slot) {
21655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!pipelineLayout)
216737083dec1ff1ca643bfc06b5308ca5ac718d58b0Chris Forbes        return nullptr;
21685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (slot.first >= pipelineLayout->size())
217037083dec1ff1ca643bfc06b5308ca5ac718d58b0Chris Forbes        return nullptr;
21715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto const layout_node = my_data->descriptorSetLayoutMap[(*pipelineLayout)[slot.first]];
21735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto bindingIt = layout_node->bindingToIndexMap.find(slot.second);
217514fbd08bf52bcbd4b5e65538d30c643c04535e30Tobin Ehlis    if ((bindingIt == layout_node->bindingToIndexMap.end()) || (layout_node->createInfo.pBindings == NULL))
217637083dec1ff1ca643bfc06b5308ca5ac718d58b0Chris Forbes        return nullptr;
21775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
217814fbd08bf52bcbd4b5e65538d30c643c04535e30Tobin Ehlis    assert(bindingIt->second < layout_node->createInfo.bindingCount);
217937083dec1ff1ca643bfc06b5308ca5ac718d58b0Chris Forbes    return &layout_node->createInfo.pBindings[bindingIt->second];
21805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
21815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Block of code at start here for managing/tracking Pipeline state that this layer cares about
21835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic uint64_t g_drawCount[NUM_DRAW_TYPES] = {0, 0, 0, 0};
21855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// TODO : Should be tracking lastBound per commandBuffer and when draws occur, report based on that cmd buffer lastBound
21875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//   Then need to synchronize the accesses based on cmd buffer so that if I'm reading state on one cmd buffer, updates
21885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//   to that same cmd buffer by separate thread are not changing state from underneath us
21895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Track the last cmd buffer touched by this thread
21905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// prototype
21925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic GLOBAL_CB_NODE *getCBNode(layer_data *, const VkCommandBuffer);
21935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
21945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 hasDrawCmd(GLOBAL_CB_NODE *pCB) {
21955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < NUM_DRAW_TYPES; i++) {
21965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->drawCount[i])
21975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_TRUE;
21985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
21995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return VK_FALSE;
22005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
22015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
22025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Check object status for selected flag state
22035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validate_status(layer_data *my_data, GLOBAL_CB_NODE *pNode, CBStatusFlags enable_mask, CBStatusFlags status_mask,
22045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                CBStatusFlags status_flag, VkFlags msg_flags, DRAW_STATE_ERROR error_code, const char *fail_msg) {
22055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // If non-zero enable mask is present, check it against status but if enable_mask
22065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //  is 0 then no enable required so we should always just check status
22075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if ((!enable_mask) || (enable_mask & pNode->status)) {
22085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((pNode->status & status_mask) != status_flag) {
22095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO : How to pass dispatchable objects as srcObject? Here src obj should be cmd buffer
22105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return log_msg(my_data->report_data, msg_flags, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, error_code,
22115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           "DS", "CB object %#" PRIxLEAST64 ": %s", (uint64_t)(pNode->commandBuffer), fail_msg);
22125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
22135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
22145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return VK_FALSE;
22155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
22165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
22175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Retrieve pipeline node ptr for given pipeline object
22185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic PIPELINE_NODE *getPipeline(layer_data *my_data, const VkPipeline pipeline) {
22195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->pipelineMap.find(pipeline) == my_data->pipelineMap.end()) {
22205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return NULL;
22215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
22225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return my_data->pipelineMap[pipeline];
22235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
22245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
22255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return VK_TRUE if for a given PSO, the given state enum is dynamic, else return VK_FALSE
22265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 isDynamic(const PIPELINE_NODE *pPipeline, const VkDynamicState state) {
22275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pPipeline && pPipeline->graphicsPipelineCI.pDynamicState) {
22285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < pPipeline->graphicsPipelineCI.pDynamicState->dynamicStateCount; i++) {
22295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (state == pPipeline->graphicsPipelineCI.pDynamicState->pDynamicStates[i])
22305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return VK_TRUE;
22315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
22325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
22335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return VK_FALSE;
22345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
22355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
22365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Validate state stored as flags at time of draw call
22375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validate_draw_state_flags(layer_data *my_data, GLOBAL_CB_NODE *pCB, VkBool32 indexedDraw) {
22385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 result;
22395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result =
22405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        validate_status(my_data, pCB, CBSTATUS_NONE, CBSTATUS_VIEWPORT_SET, CBSTATUS_VIEWPORT_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT,
22415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_VIEWPORT_NOT_BOUND, "Dynamic viewport state not set for this command buffer");
22425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result |=
22435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        validate_status(my_data, pCB, CBSTATUS_NONE, CBSTATUS_SCISSOR_SET, CBSTATUS_SCISSOR_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT,
22445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_SCISSOR_NOT_BOUND, "Dynamic scissor state not set for this command buffer");
22455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result |= validate_status(my_data, pCB, CBSTATUS_NONE, CBSTATUS_LINE_WIDTH_SET, CBSTATUS_LINE_WIDTH_SET,
22465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_LINE_WIDTH_NOT_BOUND,
22475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              "Dynamic line width state not set for this command buffer");
22485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result |= validate_status(my_data, pCB, CBSTATUS_NONE, CBSTATUS_DEPTH_BIAS_SET, CBSTATUS_DEPTH_BIAS_SET,
22495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_DEPTH_BIAS_NOT_BOUND,
22505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              "Dynamic depth bias state not set for this command buffer");
22515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result |= validate_status(my_data, pCB, CBSTATUS_COLOR_BLEND_WRITE_ENABLE, CBSTATUS_BLEND_SET, CBSTATUS_BLEND_SET,
22525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_BLEND_NOT_BOUND,
22535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              "Dynamic blend object state not set for this command buffer");
22545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result |= validate_status(my_data, pCB, CBSTATUS_DEPTH_WRITE_ENABLE, CBSTATUS_DEPTH_BOUNDS_SET, CBSTATUS_DEPTH_BOUNDS_SET,
22555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_DEPTH_BOUNDS_NOT_BOUND,
22565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              "Dynamic depth bounds state not set for this command buffer");
22575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result |= validate_status(my_data, pCB, CBSTATUS_STENCIL_TEST_ENABLE, CBSTATUS_STENCIL_READ_MASK_SET,
22585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              CBSTATUS_STENCIL_READ_MASK_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_STENCIL_NOT_BOUND,
22595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              "Dynamic stencil read mask state not set for this command buffer");
22605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result |= validate_status(my_data, pCB, CBSTATUS_STENCIL_TEST_ENABLE, CBSTATUS_STENCIL_WRITE_MASK_SET,
22615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              CBSTATUS_STENCIL_WRITE_MASK_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_STENCIL_NOT_BOUND,
22625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              "Dynamic stencil write mask state not set for this command buffer");
22635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result |= validate_status(my_data, pCB, CBSTATUS_STENCIL_TEST_ENABLE, CBSTATUS_STENCIL_REFERENCE_SET,
22645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              CBSTATUS_STENCIL_REFERENCE_SET, VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_STENCIL_NOT_BOUND,
22655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              "Dynamic stencil reference state not set for this command buffer");
22665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (indexedDraw)
22675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result |= validate_status(my_data, pCB, CBSTATUS_NONE, CBSTATUS_INDEX_BUFFER_BOUND, CBSTATUS_INDEX_BUFFER_BOUND,
22685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  VK_DEBUG_REPORT_ERROR_BIT_EXT, DRAWSTATE_INDEX_BUFFER_NOT_BOUND,
22695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  "Index buffer object not bound to this command buffer when Indexed Draw attempted");
22705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
22715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
22725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
22735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Verify attachment reference compatibility according to spec
22745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  If one array is larger, treat missing elements of shorter array as VK_ATTACHMENT_UNUSED & other array much match this
22755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  If both AttachmentReference arrays have requested index, check their corresponding AttachementDescriptions
22765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//   to make sure that format and samples counts match.
22775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  If not, they are not compatible.
22785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool attachment_references_compatible(const uint32_t index, const VkAttachmentReference *pPrimary,
22795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                             const uint32_t primaryCount, const VkAttachmentDescription *pPrimaryAttachments,
22805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                             const VkAttachmentReference *pSecondary, const uint32_t secondaryCount,
22815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                             const VkAttachmentDescription *pSecondaryAttachments) {
22825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (index >= primaryCount) { // Check secondary as if primary is VK_ATTACHMENT_UNUSED
22835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (VK_ATTACHMENT_UNUSED != pSecondary[index].attachment)
22845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return false;
22855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else if (index >= secondaryCount) { // Check primary as if secondary is VK_ATTACHMENT_UNUSED
22865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (VK_ATTACHMENT_UNUSED != pPrimary[index].attachment)
22875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return false;
22885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else { // format and sample count must match
22895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((pPrimaryAttachments[pPrimary[index].attachment].format ==
22905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             pSecondaryAttachments[pSecondary[index].attachment].format) &&
22915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            (pPrimaryAttachments[pPrimary[index].attachment].samples ==
22925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             pSecondaryAttachments[pSecondary[index].attachment].samples))
22935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return true;
22945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
22955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Format and sample counts didn't match
22965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return false;
22975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
22985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
22995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For give primary and secondary RenderPass objects, verify that they're compatible
23005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool verify_renderpass_compatibility(layer_data *my_data, const VkRenderPass primaryRP, const VkRenderPass secondaryRP,
23015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            string &errorMsg) {
23025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    stringstream errorStr;
23035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->renderPassMap.find(primaryRP) == my_data->renderPassMap.end()) {
23045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorStr << "invalid VkRenderPass (" << primaryRP << ")";
23055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorMsg = errorStr.str();
23065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false;
23075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else if (my_data->renderPassMap.find(secondaryRP) == my_data->renderPassMap.end()) {
23085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorStr << "invalid VkRenderPass (" << secondaryRP << ")";
23095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorMsg = errorStr.str();
23105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false;
23115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
23125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Trivial pass case is exact same RP
23135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primaryRP == secondaryRP) {
23145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return true;
23155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
23165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkRenderPassCreateInfo *primaryRPCI = my_data->renderPassMap[primaryRP]->pCreateInfo;
23175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkRenderPassCreateInfo *secondaryRPCI = my_data->renderPassMap[secondaryRP]->pCreateInfo;
23185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primaryRPCI->subpassCount != secondaryRPCI->subpassCount) {
23195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorStr << "RenderPass for primary cmdBuffer has " << primaryRPCI->subpassCount
23205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                 << " subpasses but renderPass for secondary cmdBuffer has " << secondaryRPCI->subpassCount << " subpasses.";
23215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorMsg = errorStr.str();
23225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false;
23235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
23245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t spIndex = 0;
23255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (spIndex = 0; spIndex < primaryRPCI->subpassCount; ++spIndex) {
23265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // For each subpass, verify that corresponding color, input, resolve & depth/stencil attachment references are compatible
23275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t primaryColorCount = primaryRPCI->pSubpasses[spIndex].colorAttachmentCount;
23285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t secondaryColorCount = secondaryRPCI->pSubpasses[spIndex].colorAttachmentCount;
23295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t colorMax = std::max(primaryColorCount, secondaryColorCount);
23305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t cIdx = 0; cIdx < colorMax; ++cIdx) {
23315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!attachment_references_compatible(cIdx, primaryRPCI->pSubpasses[spIndex].pColorAttachments, primaryColorCount,
23325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  primaryRPCI->pAttachments, secondaryRPCI->pSubpasses[spIndex].pColorAttachments,
23335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  secondaryColorCount, secondaryRPCI->pAttachments)) {
23345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                errorStr << "color attachments at index " << cIdx << " of subpass index " << spIndex << " are not compatible.";
23355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                errorMsg = errorStr.str();
23365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return false;
23375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else if (!attachment_references_compatible(cIdx, primaryRPCI->pSubpasses[spIndex].pResolveAttachments,
23385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         primaryColorCount, primaryRPCI->pAttachments,
23395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         secondaryRPCI->pSubpasses[spIndex].pResolveAttachments,
23405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         secondaryColorCount, secondaryRPCI->pAttachments)) {
23415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                errorStr << "resolve attachments at index " << cIdx << " of subpass index " << spIndex << " are not compatible.";
23425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                errorMsg = errorStr.str();
23435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return false;
23445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else if (!attachment_references_compatible(cIdx, primaryRPCI->pSubpasses[spIndex].pDepthStencilAttachment,
23455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         primaryColorCount, primaryRPCI->pAttachments,
23465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         secondaryRPCI->pSubpasses[spIndex].pDepthStencilAttachment,
23475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         secondaryColorCount, secondaryRPCI->pAttachments)) {
23485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                errorStr << "depth/stencil attachments at index " << cIdx << " of subpass index " << spIndex
23495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                         << " are not compatible.";
23505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                errorMsg = errorStr.str();
23515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return false;
23525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
23535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
23545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t primaryInputCount = primaryRPCI->pSubpasses[spIndex].inputAttachmentCount;
23555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t secondaryInputCount = secondaryRPCI->pSubpasses[spIndex].inputAttachmentCount;
23565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t inputMax = std::max(primaryInputCount, secondaryInputCount);
23575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < inputMax; ++i) {
23585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!attachment_references_compatible(i, primaryRPCI->pSubpasses[spIndex].pInputAttachments, primaryColorCount,
23595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  primaryRPCI->pAttachments, secondaryRPCI->pSubpasses[spIndex].pInputAttachments,
23605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  secondaryColorCount, secondaryRPCI->pAttachments)) {
23615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                errorStr << "input attachments at index " << i << " of subpass index " << spIndex << " are not compatible.";
23625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                errorMsg = errorStr.str();
23635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return false;
23645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
23655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
23665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
23675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return true;
23685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
23695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
23705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For give SET_NODE, verify that its Set is compatible w/ the setLayout corresponding to pipelineLayout[layoutIndex]
23715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool verify_set_layout_compatibility(layer_data *my_data, const SET_NODE *pSet, const VkPipelineLayout layout,
23725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            const uint32_t layoutIndex, string &errorMsg) {
23735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    stringstream errorStr;
2374ea26140408c3361500b5d5b8498a4f28a3e92ef5Chris Forbes    auto pipeline_layout_it = my_data->pipelineLayoutMap.find(layout);
2375ea26140408c3361500b5d5b8498a4f28a3e92ef5Chris Forbes    if (pipeline_layout_it == my_data->pipelineLayoutMap.end()) {
23765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorStr << "invalid VkPipelineLayout (" << layout << ")";
23775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorMsg = errorStr.str();
23785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false;
23795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
2380ea26140408c3361500b5d5b8498a4f28a3e92ef5Chris Forbes    if (layoutIndex >= pipeline_layout_it->second.descriptorSetLayouts.size()) {
2381ea26140408c3361500b5d5b8498a4f28a3e92ef5Chris Forbes        errorStr << "VkPipelineLayout (" << layout << ") only contains " << pipeline_layout_it->second.descriptorSetLayouts.size()
2382ea26140408c3361500b5d5b8498a4f28a3e92ef5Chris Forbes                 << " setLayouts corresponding to sets 0-" << pipeline_layout_it->second.descriptorSetLayouts.size() - 1
23835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                 << ", but you're attempting to bind set to index " << layoutIndex;
23845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorMsg = errorStr.str();
23855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false;
23865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
23875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Get the specific setLayout from PipelineLayout that overlaps this set
2388ea26140408c3361500b5d5b8498a4f28a3e92ef5Chris Forbes    LAYOUT_NODE *pLayoutNode = my_data->descriptorSetLayoutMap[pipeline_layout_it->second.descriptorSetLayouts[layoutIndex]];
23895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pLayoutNode->layout == pSet->pLayout->layout) { // trivial pass case
23905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return true;
23915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
23925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    size_t descriptorCount = pLayoutNode->descriptorTypes.size();
23935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (descriptorCount != pSet->pLayout->descriptorTypes.size()) {
23945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorStr << "setLayout " << layoutIndex << " from pipelineLayout " << layout << " has " << descriptorCount
23955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                 << " descriptors, but corresponding set being bound has " << pSet->pLayout->descriptorTypes.size()
23965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                 << " descriptors.";
23975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        errorMsg = errorStr.str();
23985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false; // trivial fail case
23995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
24005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Now need to check set against corresponding pipelineLayout to verify compatibility
24015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (size_t i = 0; i < descriptorCount; ++i) {
24025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Need to verify that layouts are identically defined
24035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        //  TODO : Is below sufficient? Making sure that types & stageFlags match per descriptor
24045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        //    do we also need to check immutable samplers?
24055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pLayoutNode->descriptorTypes[i] != pSet->pLayout->descriptorTypes[i]) {
24065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            errorStr << "descriptor " << i << " for descriptorSet being bound is type '"
24075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     << string_VkDescriptorType(pSet->pLayout->descriptorTypes[i])
24085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     << "' but corresponding descriptor from pipelineLayout is type '"
24095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     << string_VkDescriptorType(pLayoutNode->descriptorTypes[i]) << "'";
24105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            errorMsg = errorStr.str();
24115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return false;
24125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
24135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pLayoutNode->stageFlags[i] != pSet->pLayout->stageFlags[i]) {
24145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            errorStr << "stageFlags " << i << " for descriptorSet being bound is " << pSet->pLayout->stageFlags[i]
24155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     << "' but corresponding descriptor from pipelineLayout has stageFlags " << pLayoutNode->stageFlags[i];
24165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            errorMsg = errorStr.str();
24175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return false;
24185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
24195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
24205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return true;
24215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
24225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Validate that data for each specialization entry is fully contained within the buffer.
24245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validate_specialization_offsets(layer_data *my_data, VkPipelineShaderStageCreateInfo const *info) {
24255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 pass = VK_TRUE;
24265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkSpecializationInfo const *spec = info->pSpecializationInfo;
24285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (spec) {
24305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto i = 0u; i < spec->mapEntryCount; i++) {
24315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
24325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
24335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            /*dev*/ 0, __LINE__, SHADER_CHECKER_BAD_SPECIALIZATION, "SC",
24345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Specialization entry %u (for constant id %u) references memory outside provided "
24355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "specialization data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER
24365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            " bytes provided)",
24375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
24385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize)) {
24395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pass = VK_FALSE;
24415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
24425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
24435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
24445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
24455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return pass;
24475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
24485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool descriptor_type_match(layer_data *my_data, shader_module const *module, uint32_t type_id,
24501b8c581791ac3c05d7829e04a2d8ecb964b8f2a6Chris Forbes                                  VkDescriptorType descriptor_type, unsigned &descriptor_count) {
24515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto type = module->get_def(type_id);
24525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24531b8c581791ac3c05d7829e04a2d8ecb964b8f2a6Chris Forbes    descriptor_count = 1;
24541b8c581791ac3c05d7829e04a2d8ecb964b8f2a6Chris Forbes
24557b892d2fc502b57d26dc9bff61b0f23cccc595fdChris Forbes    /* Strip off any array or ptrs. Where we remove array levels, adjust the
24567b892d2fc502b57d26dc9bff61b0f23cccc595fdChris Forbes     * descriptor count for each dimension. */
24575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer) {
24587b892d2fc502b57d26dc9bff61b0f23cccc595fdChris Forbes        if (type.opcode() == spv::OpTypeArray) {
24597b892d2fc502b57d26dc9bff61b0f23cccc595fdChris Forbes            descriptor_count *= get_constant_value(module, type.word(3));
24607b892d2fc502b57d26dc9bff61b0f23cccc595fdChris Forbes            type = module->get_def(type.word(2));
24617b892d2fc502b57d26dc9bff61b0f23cccc595fdChris Forbes        }
24627b892d2fc502b57d26dc9bff61b0f23cccc595fdChris Forbes        else {
24637b892d2fc502b57d26dc9bff61b0f23cccc595fdChris Forbes            type = module->get_def(type.word(3));
24647b892d2fc502b57d26dc9bff61b0f23cccc595fdChris Forbes        }
24655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
24665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (type.opcode()) {
24685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeStruct: {
24695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto insn : *module) {
24705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) {
24715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (insn.word(2) == spv::DecorationBlock) {
24725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    return descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
24735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
24745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else if (insn.word(2) == spv::DecorationBufferBlock) {
24755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    return descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
24765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;
24775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
24785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
24795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
24805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* Invalid */
24825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false;
24835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
24845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeSampler:
24865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return descriptor_type == VK_DESCRIPTOR_TYPE_SAMPLER;
24875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeSampledImage:
24895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return descriptor_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
24905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
24915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case spv::OpTypeImage: {
24925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* Many descriptor types backing image types-- depends on dimension
24935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * and whether the image will be used with a sampler. SPIRV for
24945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * Vulkan requires that sampled be 1 or 2 -- leaving the decision to
24955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * runtime is unacceptable.
24965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         */
24975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto dim = type.word(3);
24985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto sampled = type.word(7);
24995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
25005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (dim == spv::DimSubpassData) {
25015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
25025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (dim == spv::DimBuffer) {
25035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (sampled == 1) {
25045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
25055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
25065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
25075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
25085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (sampled == 1) {
25095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return descriptor_type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
25105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
25115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
25125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
25135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
25145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
25155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* We shouldn't really see any other junk types -- but if we do, they're
25165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     * a mismatch.
25175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     */
25185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
25195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false; /* Mismatch */
25205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
25215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
25225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2523a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbesstatic VkBool32 require_feature(layer_data *my_data, VkBool32 feature, char const *feature_name) {
2524a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes    if (!feature) {
2525a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes        if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
2526a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                    /* dev */ 0, __LINE__, SHADER_CHECKER_FEATURE_NOT_ENABLED, "SC",
2527a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                    "Shader requires VkPhysicalDeviceFeatures::%s but is not "
2528a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                    "enabled on the device",
2529a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                    feature_name)) {
2530a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            return false;
2531a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes        }
2532a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes    }
2533a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2534a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes    return true;
2535a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes}
2536a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2537a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbesstatic VkBool32 validate_shader_capabilities(layer_data *my_data, VkDevice dev, shader_module const *src)
2538a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes{
2539a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes    VkBool32 pass = VK_TRUE;
2540a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2541a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes    auto enabledFeatures = &my_data->physDevProperties.features;
2542a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2543a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes    for (auto insn : *src) {
2544a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes        if (insn.opcode() == spv::OpCapability) {
2545a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            switch (insn.word(1)) {
2546a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityMatrix:
2547a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityShader:
2548a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityInputAttachment:
2549a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilitySampled1D:
2550a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityImage1D:
2551a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilitySampledBuffer:
2552a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityImageBuffer:
2553a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityImageQuery:
2554a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityDerivativeControl:
2555a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                // Always supported by a Vulkan 1.0 implementation -- no feature bits.
2556a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2557a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2558a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityGeometry:
2559a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->geometryShader, "geometryShader");
2560a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2561a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2562a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityTessellation:
2563a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->tessellationShader, "tessellationShader");
2564a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2565a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2566a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityFloat64:
2567a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderFloat64, "shaderFloat64");
2568a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2569a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2570a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityInt64:
2571a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderInt64, "shaderInt64");
2572a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2573a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2574a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityTessellationPointSize:
2575a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityGeometryPointSize:
2576a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderTessellationAndGeometryPointSize,
2577a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                                        "shaderTessellationAndGeometryPointSize");
2578a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2579a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2580a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityImageGatherExtended:
2581a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderImageGatherExtended, "shaderImageGatherExtended");
2582a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2583a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2584a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityStorageImageMultisample:
2585a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderStorageImageMultisample, "shaderStorageImageMultisample");
2586a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2587a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2588a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityUniformBufferArrayDynamicIndexing:
2589a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderUniformBufferArrayDynamicIndexing,
2590a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                                        "shaderUniformBufferArrayDynamicIndexing");
2591a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2592a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2593a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilitySampledImageArrayDynamicIndexing:
2594a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderSampledImageArrayDynamicIndexing,
2595a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                                        "shaderSampledImageArrayDynamicIndexing");
2596a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2597a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2598a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityStorageBufferArrayDynamicIndexing:
2599a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderStorageBufferArrayDynamicIndexing,
2600a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                                        "shaderStorageBufferArrayDynamicIndexing");
2601a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2602a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2603a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityStorageImageArrayDynamicIndexing:
2604a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderStorageImageArrayDynamicIndexing,
2605a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                                        "shaderStorageImageArrayDynamicIndexing");
2606a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2607a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2608a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityClipDistance:
2609a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderClipDistance, "shaderClipDistance");
2610a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2611a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2612a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityCullDistance:
2613a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderCullDistance, "shaderCullDistance");
2614a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2615a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2616a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityImageCubeArray:
2617a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->imageCubeArray, "imageCubeArray");
2618a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2619a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2620a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilitySampleRateShading:
2621a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->sampleRateShading, "sampleRateShading");
2622a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2623a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2624a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilitySparseResidency:
2625a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderResourceResidency, "shaderResourceResidency");
2626a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2627a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2628a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityMinLod:
2629a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderResourceMinLod, "shaderResourceMinLod");
2630a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2631a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2632a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilitySampledCubeArray:
2633a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->imageCubeArray, "imageCubeArray");
2634a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2635a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2636a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityImageMSArray:
2637a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderStorageImageMultisample, "shaderStorageImageMultisample");
2638a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2639a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2640a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityStorageImageExtendedFormats:
2641a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderStorageImageExtendedFormats,
2642a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                                        "shaderStorageImageExtendedFormats");
2643a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2644a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2645a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityInterpolationFunction:
2646a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->sampleRateShading, "sampleRateShading");
2647a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2648a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2649a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityStorageImageReadWithoutFormat:
2650a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderStorageImageReadWithoutFormat,
2651a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                                        "shaderStorageImageReadWithoutFormat");
2652a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2653a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2654a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityStorageImageWriteWithoutFormat:
2655a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->shaderStorageImageWriteWithoutFormat,
2656a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                                        "shaderStorageImageWriteWithoutFormat");
2657a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2658a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2659a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            case spv::CapabilityMultiViewport:
2660a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass &= require_feature(my_data, enabledFeatures->multiViewport, "multiViewport");
2661a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2662a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2663a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            default:
2664a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, /* dev */0,
2665a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                            __LINE__, SHADER_CHECKER_BAD_CAPABILITY, "SC",
2666a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                            "Shader declares capability %u, not supported in Vulkan.",
2667a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                            insn.word(1)))
2668a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                    pass = VK_FALSE;
2669a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                break;
2670a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes            }
2671a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes        }
2672a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes    }
2673a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2674a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes    return pass;
2675a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes}
2676a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
2677a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
26785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Validate that the shaders used by the given pipeline
26795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  As a side effect this function also records the sets that are actually used by the pipeline
26805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validate_pipeline_shaders(layer_data *my_data, VkDevice dev, PIPELINE_NODE *pPipeline) {
26815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkGraphicsPipelineCreateInfo const *pCreateInfo = &pPipeline->graphicsPipelineCI;
26825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
26835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis     * before trying to do anything more: */
26845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
26855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
26865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
26875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    shader_module *shaders[5];
26885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    memset(shaders, 0, sizeof(shaders));
26895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    spirv_inst_iter entrypoints[5];
26905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    memset(entrypoints, 0, sizeof(entrypoints));
26915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    RENDER_PASS_NODE const *rp = 0;
26925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkPipelineVertexInputStateCreateInfo const *vi = 0;
26935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 pass = VK_TRUE;
26945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
26955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
26965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
26975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
26985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
26995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if ((pStage->stage & (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT | VK_SHADER_STAGE_FRAGMENT_BIT |
27005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)) == 0) {
27015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
27025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            /*dev*/ 0, __LINE__, SHADER_CHECKER_UNKNOWN_STAGE, "SC", "Unknown shader stage %d", pStage->stage)) {
27035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pass = VK_FALSE;
27045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
27055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
27065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pass = validate_specialization_offsets(my_data, pStage) && pass;
27075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
27085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto stage_id = get_shader_stage_id(pStage->stage);
27095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                shader_module *module = my_data->shaderModuleMap[pStage->module];
27105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                shaders[stage_id] = module;
27115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
27125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                /* find the entrypoint */
27135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                entrypoints[stage_id] = find_entrypoint(module, pStage->pName, pStage->stage);
27145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (entrypoints[stage_id] == module->end()) {
27155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
27165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                /*dev*/ 0, __LINE__, SHADER_CHECKER_MISSING_ENTRYPOINT, "SC",
27175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "No entrypoint found named `%s` for stage %s", pStage->pName,
27185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                string_VkShaderStageFlagBits(pStage->stage))) {
27195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        pass = VK_FALSE;
27205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
27215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
27225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
2723a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                /* validate shader capabilities against enabled device features */
2724a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes                pass = validate_shader_capabilities(my_data, dev, module) && pass;
2725a3c20a28f119d24faec013fb4513605b654c36c3Chris Forbes
27265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                /* mark accessible ids */
27275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                std::unordered_set<uint32_t> accessible_ids;
27285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                mark_accessible_ids(module, entrypoints[stage_id], accessible_ids);
27295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
27305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                /* validate descriptor set layout against what the entrypoint actually uses */
27315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                std::map<descriptor_slot_t, interface_var> descriptor_uses;
27325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                collect_interface_by_descriptor_slot(my_data, dev, module, accessible_ids, descriptor_uses);
27335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
27345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto layouts = pCreateInfo->layout != VK_NULL_HANDLE
27355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   ? &(my_data->pipelineLayoutMap[pCreateInfo->layout].descriptorSetLayouts)
27365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   : nullptr;
27375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
27385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
27395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // As a side-effect of this function, capture which sets are used by the pipeline
27405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pPipeline->active_sets.insert(it->first.first);
27415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
27425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    /* find the matching binding */
274337083dec1ff1ca643bfc06b5308ca5ac718d58b0Chris Forbes                    auto binding = get_descriptor_binding(my_data, layouts, it->first);
27441b8c581791ac3c05d7829e04a2d8ecb964b8f2a6Chris Forbes                    unsigned required_descriptor_count;
27455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
274637083dec1ff1ca643bfc06b5308ca5ac718d58b0Chris Forbes                    if (!binding) {
27475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
27485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    /*dev*/ 0, __LINE__, SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
27495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
27509ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                                    it->first.first, it->first.second, describe_type(module, it->second.type_id).c_str())) {
27515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            pass = VK_FALSE;
27525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
275337083dec1ff1ca643bfc06b5308ca5ac718d58b0Chris Forbes                    } else if (~binding->stageFlags & pStage->stage) {
27545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
27555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    /*dev*/ 0, __LINE__, SHADER_CHECKER_DESCRIPTOR_NOT_ACCESSIBLE_FROM_STAGE, "SC",
27565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "Shader uses descriptor slot %u.%u (used "
27575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "as type `%s`) but descriptor not "
27585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "accessible from stage %s",
27599ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                                    it->first.first, it->first.second,
27609ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                                    describe_type(module, it->second.type_id).c_str(),
27619ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                                    string_VkShaderStageFlagBits(pStage->stage))) {
27625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            pass = VK_FALSE;
27635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
27641b8c581791ac3c05d7829e04a2d8ecb964b8f2a6Chris Forbes                    } else if (!descriptor_type_match(my_data, module, it->second.type_id, binding->descriptorType, /*out*/ required_descriptor_count)) {
27655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
27665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    /*dev*/ 0, __LINE__, SHADER_CHECKER_DESCRIPTOR_TYPE_MISMATCH, "SC",
27675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "Type mismatch on descriptor slot "
27685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "%u.%u (used as type `%s`) but "
27695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "descriptor of type %s",
27709ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                                    it->first.first, it->first.second,
27719ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                                    describe_type(module, it->second.type_id).c_str(),
27729ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                                    string_VkDescriptorType(binding->descriptorType))) {
27735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            pass = VK_FALSE;
27745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
27751b8c581791ac3c05d7829e04a2d8ecb964b8f2a6Chris Forbes                    } else if (binding->descriptorCount < required_descriptor_count) {
27761b8c581791ac3c05d7829e04a2d8ecb964b8f2a6Chris Forbes                        if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
27771b8c581791ac3c05d7829e04a2d8ecb964b8f2a6Chris Forbes                                    /*dev*/ 0, __LINE__, SHADER_CHECKER_DESCRIPTOR_TYPE_MISMATCH, "SC",
27787b892d2fc502b57d26dc9bff61b0f23cccc595fdChris Forbes                                    "Shader expects at least %u descriptors for binding %u.%u (used as type `%s`) but only %u provided",
27799ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                                    required_descriptor_count, it->first.first, it->first.second,
27809ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                                    describe_type(module, it->second.type_id).c_str(),
27819ccb7b1342fd1a901f1ae0d202721a50d2c227f9Chris Forbes                                    binding->descriptorCount)) {
27821b8c581791ac3c05d7829e04a2d8ecb964b8f2a6Chris Forbes                            pass = VK_FALSE;
27831b8c581791ac3c05d7829e04a2d8ecb964b8f2a6Chris Forbes                        }
27845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
27855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
27865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
27875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                /* validate push constant usage */
27885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pass =
27895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    validate_push_constant_usage(my_data, dev, &my_data->pipelineLayoutMap[pCreateInfo->layout].pushConstantRanges,
27905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 module, accessible_ids, pStage->stage) &&
27915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pass;
27925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
27935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
27945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
27955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
27965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->renderPass != VK_NULL_HANDLE)
27975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        rp = my_data->renderPassMap[pCreateInfo->renderPass];
27985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
27995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    vi = pCreateInfo->pVertexInputState;
28005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
28015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (vi) {
28025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pass = validate_vi_consistency(my_data, dev, vi) && pass;
28035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
28045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
28055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (shaders[vertex_stage]) {
28065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pass = validate_vi_against_vs_inputs(my_data, dev, vi, shaders[vertex_stage], entrypoints[vertex_stage]) && pass;
28075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
28085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
28095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* TODO: enforce rules about present combinations of shaders */
28105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
28115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    int consumer = get_shader_stage_id(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
28125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
28135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while (!shaders[producer] && producer != fragment_stage) {
28145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        producer++;
28155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        consumer++;
28165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
28175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
28185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
28195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        assert(shaders[producer]);
28205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (shaders[consumer]) {
28215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pass = validate_interface_between_stages(my_data, dev, shaders[producer], entrypoints[producer],
28225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     shader_stage_attribs[producer].name, shaders[consumer], entrypoints[consumer],
28235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     shader_stage_attribs[consumer].name,
28245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     shader_stage_attribs[consumer].arrayed_input) &&
28255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   pass;
28265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
28275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            producer = consumer;
28285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
28295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
28305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
28315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (shaders[fragment_stage] && rp) {
28325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pass = validate_fs_outputs_against_render_pass(my_data, dev, shaders[fragment_stage], entrypoints[fragment_stage], rp,
28335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                       pCreateInfo->subpass) &&
28345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis               pass;
28355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
28365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
28375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return pass;
28385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
28395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
28405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return Set node ptr for specified set or else NULL
28415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic SET_NODE *getSetNode(layer_data *my_data, const VkDescriptorSet set) {
28425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->setMap.find(set) == my_data->setMap.end()) {
28435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return NULL;
28445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
28455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return my_data->setMap[set];
28465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
28475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For the given command buffer, verify that for each set set in activeSetNodes
28485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  that any dynamic descriptor in that set has a valid dynamic offset bound.
28495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  To be valid, the dynamic offset combined with the offet and range from its
28505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  descriptor update must not overflow the size of its buffer being updated
28515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validate_dynamic_offsets(layer_data *my_data, const GLOBAL_CB_NODE *pCB, const vector<SET_NODE *> activeSetNodes) {
28525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 result = VK_FALSE;
28535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
28545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkWriteDescriptorSet *pWDS = NULL;
28555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t dynOffsetIndex = 0;
28565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceSize bufferSize = 0;
28575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto set_node : activeSetNodes) {
28585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < set_node->descriptorCount; ++i) {
28595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO: Add validation for descriptors dynamically skipped in shader
28605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (set_node->ppDescriptors[i] != NULL) {
28615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                switch (set_node->ppDescriptors[i]->sType) {
28625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
28635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pWDS = (VkWriteDescriptorSet *)set_node->ppDescriptors[i];
28645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if ((pWDS->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
28655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (pWDS->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
28665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        for (uint32_t j = 0; j < pWDS->descriptorCount; ++j) {
28675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            bufferSize = my_data->bufferMap[pWDS->pBufferInfo[j].buffer].create_info->size;
28685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            if (pWDS->pBufferInfo[j].range == VK_WHOLE_SIZE) {
28695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                if ((pCB->dynamicOffsets[dynOffsetIndex] + pWDS->pBufferInfo[j].offset) > bufferSize) {
28705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    result |=
28715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
28725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)set_node->set, __LINE__,
28735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                DRAWSTATE_DYNAMIC_OFFSET_OVERFLOW, "DS",
28745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                "VkDescriptorSet (%#" PRIxLEAST64 ") bound as set #%u has range of "
28755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                "VK_WHOLE_SIZE but dynamic offset %u "
28765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                "combined with offet %#" PRIxLEAST64 " oversteps its buffer (%#" PRIxLEAST64
28775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                ") which has a size of %#" PRIxLEAST64 ".",
28785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                (uint64_t)set_node->set, i, pCB->dynamicOffsets[dynOffsetIndex],
28795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                pWDS->pBufferInfo[j].offset, (uint64_t)pWDS->pBufferInfo[j].buffer, bufferSize);
28805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                }
28815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            } else if ((pCB->dynamicOffsets[dynOffsetIndex] + pWDS->pBufferInfo[j].offset +
28825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        pWDS->pBufferInfo[j].range) > bufferSize) {
28835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
28845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)set_node->set, __LINE__,
28855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  DRAWSTATE_DYNAMIC_OFFSET_OVERFLOW, "DS",
28865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  "VkDescriptorSet (%#" PRIxLEAST64 ") bound as set #%u has dynamic offset %u. "
28875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  "Combined with offet %#" PRIxLEAST64 " and range %#" PRIxLEAST64
28885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  " from its update, this oversteps its buffer "
28895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  "(%#" PRIxLEAST64 ") which has a size of %#" PRIxLEAST64 ".",
28905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  (uint64_t)set_node->set, i, pCB->dynamicOffsets[dynOffsetIndex],
28915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  pWDS->pBufferInfo[j].offset, pWDS->pBufferInfo[j].range,
28925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  (uint64_t)pWDS->pBufferInfo[j].buffer, bufferSize);
28935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            } else if ((pCB->dynamicOffsets[dynOffsetIndex] + pWDS->pBufferInfo[j].offset +
28945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        pWDS->pBufferInfo[j].range) > bufferSize) {
28955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
28965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)set_node->set, __LINE__,
28975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  DRAWSTATE_DYNAMIC_OFFSET_OVERFLOW, "DS",
28985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  "VkDescriptorSet (%#" PRIxLEAST64 ") bound as set #%u has dynamic offset %u. "
28995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  "Combined with offet %#" PRIxLEAST64 " and range %#" PRIxLEAST64
29005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  " from its update, this oversteps its buffer "
29015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  "(%#" PRIxLEAST64 ") which has a size of %#" PRIxLEAST64 ".",
29025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  (uint64_t)set_node->set, i, pCB->dynamicOffsets[dynOffsetIndex],
29035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  pWDS->pBufferInfo[j].offset, pWDS->pBufferInfo[j].range,
29045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  (uint64_t)pWDS->pBufferInfo[j].buffer, bufferSize);
29055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            }
29065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            dynOffsetIndex++;
29075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            i += j; // Advance i to end of this set of descriptors (++i at end of for loop will move 1 index past
29085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    // last of these descriptors)
29095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
29105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
29115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    break;
29125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                default: // Currently only shadowing Write update nodes so shouldn't get here
29135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    assert(0);
29145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    continue;
29155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
29165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
29175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
29185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
29195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
29205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
29215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
29225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Validate overall state at the time of a draw call
29235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validate_draw_state(layer_data *my_data, GLOBAL_CB_NODE *pCB, VkBool32 indexedDraw) {
29245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // First check flag states
29255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 result = validate_draw_state_flags(my_data, pCB, indexedDraw);
29265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PIPELINE_NODE *pPipe = getPipeline(my_data, pCB->lastBoundPipeline);
29275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Now complete other state checks
29285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Currently only performing next check if *something* was bound (non-zero last bound)
29295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //  There is probably a better way to gate when this check happens, and to know if something *should* have been bound
29305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //  We should have that check separately and then gate this check based on that check
29315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pPipe) {
29325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->lastBoundPipelineLayout) {
29335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            string errorString;
29345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Need a vector (vs. std::set) of active Sets for dynamicOffset validation in case same set bound w/ different offsets
29355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            vector<SET_NODE *> activeSetNodes;
29365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (auto setIndex : pPipe->active_sets) {
29375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // If valid set is not bound throw an error
29385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if ((pCB->boundDescriptorSets.size() <= setIndex) || (!pCB->boundDescriptorSets[setIndex])) {
29395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
29405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      __LINE__, DRAWSTATE_DESCRIPTOR_SET_NOT_BOUND, "DS",
29415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      "VkPipeline %#" PRIxLEAST64 " uses set #%u but that set is not bound.",
29425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      (uint64_t)pPipe->pipeline, setIndex);
29435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else if (!verify_set_layout_compatibility(my_data, my_data->setMap[pCB->boundDescriptorSets[setIndex]],
29445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                            pPipe->graphicsPipelineCI.layout, setIndex, errorString)) {
29455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // Set is bound but not compatible w/ overlapping pipelineLayout from PSO
29465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    VkDescriptorSet setHandle = my_data->setMap[pCB->boundDescriptorSets[setIndex]]->set;
29475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    result |= log_msg(
29485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
29495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)setHandle, __LINE__, DRAWSTATE_PIPELINE_LAYOUTS_INCOMPATIBLE, "DS",
29505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "VkDescriptorSet (%#" PRIxLEAST64
29515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        ") bound as set #%u is not compatible with overlapping VkPipelineLayout %#" PRIxLEAST64 " due to: %s",
29525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)setHandle, setIndex, (uint64_t)pPipe->graphicsPipelineCI.layout, errorString.c_str());
29535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else { // Valid set is bound and layout compatible, validate that it's updated and verify any dynamic offsets
29545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // Pull the set node
29555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    SET_NODE *pSet = my_data->setMap[pCB->boundDescriptorSets[setIndex]];
29565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // Save vector of all active sets to verify dynamicOffsets below
29575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    activeSetNodes.push_back(pSet);
29585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // Make sure set has been updated
29595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (!pSet->pUpdateStructs) {
29605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
29615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                          VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pSet->set, __LINE__,
29625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                          DRAWSTATE_DESCRIPTOR_SET_NOT_UPDATED, "DS",
29635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                          "DS %#" PRIxLEAST64 " bound but it was never updated. It is now being used to draw so "
29645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                              "this will result in undefined behavior.",
29655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                          (uint64_t)pSet->set);
29665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
29675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
29685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
29695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // For each dynamic descriptor, make sure dynamic offset doesn't overstep buffer
29705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pCB->dynamicOffsets.empty())
29715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                result |= validate_dynamic_offsets(my_data, pCB, activeSetNodes);
29725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
29735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Verify Vtx binding
29745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pPipe->vtxBindingCount > 0) {
29755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkPipelineVertexInputStateCreateInfo *vtxInCI = &pPipe->vertexInputCI;
29765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t i = 0; i < vtxInCI->vertexBindingDescriptionCount; i++) {
29775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if ((pCB->currentDrawData.buffers.size() < (i + 1)) || (pCB->currentDrawData.buffers[i] == VK_NULL_HANDLE)) {
29785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    result |= log_msg(
29795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
29805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_VTX_INDEX_OUT_OF_BOUNDS, "DS",
29815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "The Pipeline State Object (%#" PRIxLEAST64
29825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        ") expects that this Command Buffer's vertex binding Index %d should be set via vkCmdBindVertexBuffers.",
29835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)pCB->lastBoundPipeline, i);
29845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
29855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
29865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
29875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pCB->currentDrawData.buffers.empty()) {
29885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0,
29895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  0, __LINE__, DRAWSTATE_VTX_INDEX_OUT_OF_BOUNDS, "DS",
29905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  "Vertex buffers are bound to command buffer (%#" PRIxLEAST64
29915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  ") but no vertex buffers are attached to this Pipeline State Object (%#" PRIxLEAST64 ").",
29925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  (uint64_t)pCB->commandBuffer, (uint64_t)pCB->lastBoundPipeline);
29935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
29945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
29955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // If Viewport or scissors are dynamic, verify that dynamic count matches PSO count.
29965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Skip check if rasterization is disabled or there is no viewport.
29975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((!pPipe->graphicsPipelineCI.pRasterizationState ||
29985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             !pPipe->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) &&
29995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipe->graphicsPipelineCI.pViewportState) {
30005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkBool32 dynViewport = isDynamic(pPipe, VK_DYNAMIC_STATE_VIEWPORT);
30015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkBool32 dynScissor = isDynamic(pPipe, VK_DYNAMIC_STATE_SCISSOR);
30025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dynViewport) {
30035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pCB->viewports.size() != pPipe->graphicsPipelineCI.pViewportState->viewportCount) {
30045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
30055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
30065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      "Dynamic viewportCount from vkCmdSetViewport() is " PRINTF_SIZE_T_SPECIFIER
30075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      ", but PSO viewportCount is %u. These counts must match.",
30085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      pCB->viewports.size(), pPipe->graphicsPipelineCI.pViewportState->viewportCount);
30095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
30105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
30115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dynScissor) {
30125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pCB->scissors.size() != pPipe->graphicsPipelineCI.pViewportState->scissorCount) {
30135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    result |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
30145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
30155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      "Dynamic scissorCount from vkCmdSetScissor() is " PRINTF_SIZE_T_SPECIFIER
30165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      ", but PSO scissorCount is %u. These counts must match.",
30175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      pCB->scissors.size(), pPipe->graphicsPipelineCI.pViewportState->scissorCount);
30185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
30195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
30205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
30215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
30225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
30235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
30245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
30255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Verify that create state for a pipeline is valid
30265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 verifyPipelineCreateState(layer_data *my_data, const VkDevice device, std::vector<PIPELINE_NODE *> pPipelines,
30275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                          int pipelineIndex) {
30285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
30295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
30305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PIPELINE_NODE *pPipeline = pPipelines[pipelineIndex];
30315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
30325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // If create derivative bit is set, check that we've specified a base
30335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // pipeline correctly, and that the base pipeline was created to allow
30345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // derivatives.
30355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pPipeline->graphicsPipelineCI.flags & VK_PIPELINE_CREATE_DERIVATIVE_BIT) {
30365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        PIPELINE_NODE *pBasePipeline = nullptr;
30375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!((pPipeline->graphicsPipelineCI.basePipelineHandle != VK_NULL_HANDLE) ^
30385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis              (pPipeline->graphicsPipelineCI.basePipelineIndex != -1))) {
30395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
30405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
30415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Invalid Pipeline CreateInfo: exactly one of base pipeline index and handle must be specified");
30425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (pPipeline->graphicsPipelineCI.basePipelineIndex != -1) {
30435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pPipeline->graphicsPipelineCI.basePipelineIndex >= pipelineIndex) {
30445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
30455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
30465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
30475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Invalid Pipeline CreateInfo: base pipeline must occur earlier in array than derivative pipeline.");
30485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
30495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pBasePipeline = pPipelines[pPipeline->graphicsPipelineCI.basePipelineIndex];
30505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
30515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (pPipeline->graphicsPipelineCI.basePipelineHandle != VK_NULL_HANDLE) {
30525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pBasePipeline = getPipeline(my_data, pPipeline->graphicsPipelineCI.basePipelineHandle);
30535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
30545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
30555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pBasePipeline && !(pBasePipeline->graphicsPipelineCI.flags & VK_PIPELINE_CREATE_ALLOW_DERIVATIVES_BIT)) {
30565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
30575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
30585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Invalid Pipeline CreateInfo: base pipeline does not allow derivatives.");
30595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
30605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
30615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
30625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pPipeline->graphicsPipelineCI.pColorBlendState != NULL) {
30635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!my_data->physDevProperties.features.independentBlend) {
30645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkPipelineColorBlendAttachmentState *pAttachments = pPipeline->pAttachments;
30655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t i = 1; i < pPipeline->attachmentCount; i++) {
30665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if ((pAttachments[0].blendEnable != pAttachments[i].blendEnable) ||
30675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (pAttachments[0].srcColorBlendFactor != pAttachments[i].srcColorBlendFactor) ||
30685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (pAttachments[0].dstColorBlendFactor != pAttachments[i].dstColorBlendFactor) ||
30695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (pAttachments[0].colorBlendOp != pAttachments[i].colorBlendOp) ||
30705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (pAttachments[0].srcAlphaBlendFactor != pAttachments[i].srcAlphaBlendFactor) ||
30715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (pAttachments[0].dstAlphaBlendFactor != pAttachments[i].dstAlphaBlendFactor) ||
30725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (pAttachments[0].alphaBlendOp != pAttachments[i].alphaBlendOp) ||
30735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (pAttachments[0].colorWriteMask != pAttachments[i].colorWriteMask)) {
30745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |=
30755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
30765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INDEPENDENT_BLEND, "DS", "Invalid Pipeline CreateInfo: If independent blend feature not "
30775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                   "enabled, all elements of pAttachments must be identical");
30785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
30795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
30805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
30815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!my_data->physDevProperties.features.logicOp &&
30825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            (pPipeline->graphicsPipelineCI.pColorBlendState->logicOpEnable != VK_FALSE)) {
30835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
30845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
30855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_DISABLED_LOGIC_OP, "DS",
30865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Invalid Pipeline CreateInfo: If logic operations feature not enabled, logicOpEnable must be VK_FALSE");
30875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
30885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((pPipeline->graphicsPipelineCI.pColorBlendState->logicOpEnable == VK_TRUE) &&
30895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            ((pPipeline->graphicsPipelineCI.pColorBlendState->logicOp < VK_LOGIC_OP_CLEAR) ||
30905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             (pPipeline->graphicsPipelineCI.pColorBlendState->logicOp > VK_LOGIC_OP_SET))) {
30915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
30925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
30935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_LOGIC_OP, "DS",
30945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Invalid Pipeline CreateInfo: If logicOpEnable is VK_TRUE, logicOp must be a valid VkLogicOp value");
30955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
30965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
30975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
30985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Ensure the subpass index is valid. If not, then validate_pipeline_shaders
30995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // produces nonsense errors that confuse users. Other layers should already
31005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // emit errors for renderpass being invalid.
31015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto rp_data = my_data->renderPassMap.find(pPipeline->graphicsPipelineCI.renderPass);
31025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (rp_data != my_data->renderPassMap.end() &&
31035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.subpass >= rp_data->second->pCreateInfo->subpassCount) {
31045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
31055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS", "Invalid Pipeline CreateInfo State: Subpass index %u "
31065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                           "is out of range for this renderpass (0..%u)",
31075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            pPipeline->graphicsPipelineCI.subpass, rp_data->second->pCreateInfo->subpassCount - 1);
31085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
31095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
31105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!validate_pipeline_shaders(my_data, device, pPipeline)) {
31115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = VK_TRUE;
31125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
31135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // VS is required
31145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!(pPipeline->active_shaders & VK_SHADER_STAGE_VERTEX_BIT)) {
31155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |=
31165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
31175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS", "Invalid Pipeline CreateInfo State: Vtx Shader required");
31185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
31195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Either both or neither TC/TE shaders should be defined
31205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (((pPipeline->active_shaders & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) == 0) !=
31215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ((pPipeline->active_shaders & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) == 0)) {
31225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
31235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
31245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Invalid Pipeline CreateInfo State: TE and TC shaders must be included or excluded as a pair");
31255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
31265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Compute shaders should be specified independent of Gfx shaders
31275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if ((pPipeline->active_shaders & VK_SHADER_STAGE_COMPUTE_BIT) &&
31285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        (pPipeline->active_shaders &
31295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT |
31305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis          VK_SHADER_STAGE_GEOMETRY_BIT | VK_SHADER_STAGE_FRAGMENT_BIT))) {
31315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
31325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS",
31335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Invalid Pipeline CreateInfo State: Do not specify Compute Shader for Gfx Pipeline");
31345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
31355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // VK_PRIMITIVE_TOPOLOGY_PATCH_LIST primitive topology is only valid for tessellation pipelines.
31365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Mismatching primitive topology and tessellation fails graphics pipeline creation.
31375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pPipeline->active_shaders & (VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) &&
31385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        (pPipeline->iaStateCI.topology != VK_PRIMITIVE_TOPOLOGY_PATCH_LIST)) {
31395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
31405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS", "Invalid Pipeline CreateInfo State: "
31415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                           "VK_PRIMITIVE_TOPOLOGY_PATCH_LIST must be set as IA "
31425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                           "topology for tessellation pipelines");
31435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
31445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pPipeline->iaStateCI.topology == VK_PRIMITIVE_TOPOLOGY_PATCH_LIST) {
31455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (~pPipeline->active_shaders & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) {
31465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
31475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS", "Invalid Pipeline CreateInfo State: "
31485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                               "VK_PRIMITIVE_TOPOLOGY_PATCH_LIST primitive "
31495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                               "topology is only valid for tessellation pipelines");
31505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
31515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!pPipeline->tessStateCI.patchControlPoints || (pPipeline->tessStateCI.patchControlPoints > 32)) {
31525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
31535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INVALID_PIPELINE_CREATE_STATE, "DS", "Invalid Pipeline CreateInfo State: "
31545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                               "VK_PRIMITIVE_TOPOLOGY_PATCH_LIST primitive "
31555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                               "topology used with patchControlPoints value %u."
31565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                               " patchControlPoints should be >0 and <=32.",
31575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                pPipeline->tessStateCI.patchControlPoints);
31585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
31595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
31605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Viewport state must be included if rasterization is enabled.
31615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // If the viewport state is included, the viewport and scissor counts should always match.
31625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // NOTE : Even if these are flagged as dynamic, counts need to be set correctly for shader compiler
31635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!pPipeline->graphicsPipelineCI.pRasterizationState ||
31645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        !pPipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
31655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!pPipeline->graphicsPipelineCI.pViewportState) {
31665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
31675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS", "Gfx Pipeline pViewportState is null. Even if viewport "
31685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                           "and scissors are dynamic PSO must include "
31695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                           "viewportCount and scissorCount in pViewportState.");
31705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (pPipeline->graphicsPipelineCI.pViewportState->scissorCount !=
31715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   pPipeline->graphicsPipelineCI.pViewportState->viewportCount) {
31725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
31735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
31745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Gfx Pipeline viewport count (%u) must match scissor count (%u).",
31755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                pPipeline->vpStateCI.viewportCount, pPipeline->vpStateCI.scissorCount);
31765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
31775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // If viewport or scissor are not dynamic, then verify that data is appropriate for count
31785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkBool32 dynViewport = isDynamic(pPipeline, VK_DYNAMIC_STATE_VIEWPORT);
31795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkBool32 dynScissor = isDynamic(pPipeline, VK_DYNAMIC_STATE_SCISSOR);
31805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!dynViewport) {
31815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pPipeline->graphicsPipelineCI.pViewportState->viewportCount &&
31825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    !pPipeline->graphicsPipelineCI.pViewportState->pViewports) {
31835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
31845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
31855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "Gfx Pipeline viewportCount is %u, but pViewports is NULL. For non-zero viewportCount, you "
31865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "must either include pViewports data, or include viewport in pDynamicState and set it with "
31875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "vkCmdSetViewport().",
31885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        pPipeline->graphicsPipelineCI.pViewportState->viewportCount);
31895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
31905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
31915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!dynScissor) {
31925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pPipeline->graphicsPipelineCI.pViewportState->scissorCount &&
31935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    !pPipeline->graphicsPipelineCI.pViewportState->pScissors) {
31945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
31955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        __LINE__, DRAWSTATE_VIEWPORT_SCISSOR_MISMATCH, "DS",
31965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "Gfx Pipeline scissorCount is %u, but pScissors is NULL. For non-zero scissorCount, you "
31975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "must either include pScissors data, or include scissor in pDynamicState and set it with "
31985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "vkCmdSetScissor().",
31995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        pPipeline->graphicsPipelineCI.pViewportState->scissorCount);
32005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
32015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
32025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
32035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
32045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
32055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
32065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
32075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Init the pipeline mapping info based on pipeline create info LL tree
32085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  Threading note : Calls to this function should wrapped in mutex
32095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// TODO : this should really just be in the constructor for PIPELINE_NODE
32105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic PIPELINE_NODE *initGraphicsPipeline(layer_data *dev_data, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
32115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PIPELINE_NODE *pPipeline = new PIPELINE_NODE;
32125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
32135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // First init create info
32145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    memcpy(&pPipeline->graphicsPipelineCI, pCreateInfo, sizeof(VkGraphicsPipelineCreateInfo));
32155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
32165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    size_t bufferSize = 0;
32175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkPipelineVertexInputStateCreateInfo *pVICI = NULL;
32185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkPipelineColorBlendStateCreateInfo *pCBCI = NULL;
32195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
32205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
32215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkPipelineShaderStageCreateInfo *pPSSCI = &pCreateInfo->pStages[i];
32225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
32235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        switch (pPSSCI->stage) {
32245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_SHADER_STAGE_VERTEX_BIT:
32255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy(&pPipeline->vsCI, pPSSCI, sizeof(VkPipelineShaderStageCreateInfo));
32265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeline->active_shaders |= VK_SHADER_STAGE_VERTEX_BIT;
32275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
32285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
32295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy(&pPipeline->tcsCI, pPSSCI, sizeof(VkPipelineShaderStageCreateInfo));
32305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeline->active_shaders |= VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
32315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
32325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
32335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy(&pPipeline->tesCI, pPSSCI, sizeof(VkPipelineShaderStageCreateInfo));
32345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeline->active_shaders |= VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
32355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
32365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_SHADER_STAGE_GEOMETRY_BIT:
32375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy(&pPipeline->gsCI, pPSSCI, sizeof(VkPipelineShaderStageCreateInfo));
32385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeline->active_shaders |= VK_SHADER_STAGE_GEOMETRY_BIT;
32395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
32405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_SHADER_STAGE_FRAGMENT_BIT:
32415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy(&pPipeline->fsCI, pPSSCI, sizeof(VkPipelineShaderStageCreateInfo));
32425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeline->active_shaders |= VK_SHADER_STAGE_FRAGMENT_BIT;
32435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
32445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_SHADER_STAGE_COMPUTE_BIT:
32455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO : Flag error, CS is specified through VkComputePipelineCreateInfo
32465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeline->active_shaders |= VK_SHADER_STAGE_COMPUTE_BIT;
32475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
32485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        default:
32495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO : Flag error
32505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
32515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
32525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
32535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Copy over GraphicsPipelineCreateInfo structure embedded pointers
32545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->stageCount != 0) {
32555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.pStages = new VkPipelineShaderStageCreateInfo[pCreateInfo->stageCount];
32565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        bufferSize = pCreateInfo->stageCount * sizeof(VkPipelineShaderStageCreateInfo);
32575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)pPipeline->graphicsPipelineCI.pStages, pCreateInfo->pStages, bufferSize);
32585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
32595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->pVertexInputState != NULL) {
32605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)&pPipeline->vertexInputCI, pCreateInfo->pVertexInputState, sizeof(VkPipelineVertexInputStateCreateInfo));
32615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Copy embedded ptrs
32625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pVICI = pCreateInfo->pVertexInputState;
32635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->vtxBindingCount = pVICI->vertexBindingDescriptionCount;
32645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pPipeline->vtxBindingCount) {
32655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeline->pVertexBindingDescriptions = new VkVertexInputBindingDescription[pPipeline->vtxBindingCount];
32665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            bufferSize = pPipeline->vtxBindingCount * sizeof(VkVertexInputBindingDescription);
32675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy((void *)pPipeline->pVertexBindingDescriptions, pVICI->pVertexBindingDescriptions, bufferSize);
32685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
32695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->vtxAttributeCount = pVICI->vertexAttributeDescriptionCount;
32705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pPipeline->vtxAttributeCount) {
32715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeline->pVertexAttributeDescriptions = new VkVertexInputAttributeDescription[pPipeline->vtxAttributeCount];
32725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            bufferSize = pPipeline->vtxAttributeCount * sizeof(VkVertexInputAttributeDescription);
32735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy((void *)pPipeline->pVertexAttributeDescriptions, pVICI->pVertexAttributeDescriptions, bufferSize);
32745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
32755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.pVertexInputState = &pPipeline->vertexInputCI;
32765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
32775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->pInputAssemblyState != NULL) {
32785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)&pPipeline->iaStateCI, pCreateInfo->pInputAssemblyState, sizeof(VkPipelineInputAssemblyStateCreateInfo));
32795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.pInputAssemblyState = &pPipeline->iaStateCI;
32805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
32815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->pTessellationState != NULL) {
32825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)&pPipeline->tessStateCI, pCreateInfo->pTessellationState, sizeof(VkPipelineTessellationStateCreateInfo));
32835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.pTessellationState = &pPipeline->tessStateCI;
32845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
32855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->pViewportState != NULL) {
32865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)&pPipeline->vpStateCI, pCreateInfo->pViewportState, sizeof(VkPipelineViewportStateCreateInfo));
32875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.pViewportState = &pPipeline->vpStateCI;
32885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
32895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->pRasterizationState != NULL) {
32905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)&pPipeline->rsStateCI, pCreateInfo->pRasterizationState, sizeof(VkPipelineRasterizationStateCreateInfo));
32915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.pRasterizationState = &pPipeline->rsStateCI;
32925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
32935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->pMultisampleState != NULL) {
32945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)&pPipeline->msStateCI, pCreateInfo->pMultisampleState, sizeof(VkPipelineMultisampleStateCreateInfo));
32955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.pMultisampleState = &pPipeline->msStateCI;
32965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
32975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->pDepthStencilState != NULL) {
32985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)&pPipeline->dsStateCI, pCreateInfo->pDepthStencilState, sizeof(VkPipelineDepthStencilStateCreateInfo));
32995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.pDepthStencilState = &pPipeline->dsStateCI;
33005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
33015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->pColorBlendState != NULL) {
33025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)&pPipeline->cbStateCI, pCreateInfo->pColorBlendState, sizeof(VkPipelineColorBlendStateCreateInfo));
33035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Copy embedded ptrs
33045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCBCI = pCreateInfo->pColorBlendState;
33055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->attachmentCount = pCBCI->attachmentCount;
33065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pPipeline->attachmentCount) {
33075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeline->pAttachments = new VkPipelineColorBlendAttachmentState[pPipeline->attachmentCount];
33085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            bufferSize = pPipeline->attachmentCount * sizeof(VkPipelineColorBlendAttachmentState);
33095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy((void *)pPipeline->pAttachments, pCBCI->pAttachments, bufferSize);
33105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
33115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.pColorBlendState = &pPipeline->cbStateCI;
33125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
33135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->pDynamicState != NULL) {
33145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)&pPipeline->dynStateCI, pCreateInfo->pDynamicState, sizeof(VkPipelineDynamicStateCreateInfo));
33155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pPipeline->dynStateCI.dynamicStateCount) {
33165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeline->dynStateCI.pDynamicStates = new VkDynamicState[pPipeline->dynStateCI.dynamicStateCount];
33175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            bufferSize = pPipeline->dynStateCI.dynamicStateCount * sizeof(VkDynamicState);
33185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy((void *)pPipeline->dynStateCI.pDynamicStates, pCreateInfo->pDynamicState->pDynamicStates, bufferSize);
33195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
33205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeline->graphicsPipelineCI.pDynamicState = &pPipeline->dynStateCI;
33215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
33225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pPipeline->active_sets.clear();
33235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return pPipeline;
33245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
33255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
33265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Free the Pipeline nodes
33275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void deletePipelines(layer_data *my_data) {
33285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->pipelineMap.size() <= 0)
33295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
33305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto ii = my_data->pipelineMap.begin(); ii != my_data->pipelineMap.end(); ++ii) {
33315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((*ii).second->graphicsPipelineCI.stageCount != 0) {
33325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            delete[](*ii).second->graphicsPipelineCI.pStages;
33335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
33345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete[](*ii).second->pVertexBindingDescriptions;
33355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete[](*ii).second->pVertexAttributeDescriptions;
33365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete[](*ii).second->pAttachments;
33375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((*ii).second->dynStateCI.dynamicStateCount != 0) {
33385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            delete[](*ii).second->dynStateCI.pDynamicStates;
33395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
33405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete (*ii).second;
33415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
33425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->pipelineMap.clear();
33435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
33445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
33455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For given pipeline, return number of MSAA samples, or one if MSAA disabled
33465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkSampleCountFlagBits getNumSamples(layer_data *my_data, const VkPipeline pipeline) {
33475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PIPELINE_NODE *pPipe = my_data->pipelineMap[pipeline];
33485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO == pPipe->msStateCI.sType) {
33495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return pPipe->msStateCI.rasterizationSamples;
33505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
33515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return VK_SAMPLE_COUNT_1_BIT;
33525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
33535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
33545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Validate state related to the PSO
33555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validatePipelineState(layer_data *my_data, const GLOBAL_CB_NODE *pCB, const VkPipelineBindPoint pipelineBindPoint,
33565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      const VkPipeline pipeline) {
33575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_PIPELINE_BIND_POINT_GRAPHICS == pipelineBindPoint) {
33585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Verify that any MSAA request in PSO matches sample# in bound FB
33595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Skip the check if rasterization is disabled.
33605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        PIPELINE_NODE *pPipeline = my_data->pipelineMap[pipeline];
33615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!pPipeline->graphicsPipelineCI.pRasterizationState ||
33625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            !pPipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
33635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkSampleCountFlagBits psoNumSamples = getNumSamples(my_data, pipeline);
33645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pCB->activeRenderPass) {
33655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                const VkRenderPassCreateInfo *pRPCI = my_data->renderPassMap[pCB->activeRenderPass]->pCreateInfo;
33665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                const VkSubpassDescription *pSD = &pRPCI->pSubpasses[pCB->activeSubpass];
33675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkSampleCountFlagBits subpassNumSamples = (VkSampleCountFlagBits)0;
33685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                uint32_t i;
33695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
33705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (i = 0; i < pSD->colorAttachmentCount; i++) {
33715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    VkSampleCountFlagBits samples;
33725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
33735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (pSD->pColorAttachments[i].attachment == VK_ATTACHMENT_UNUSED)
33745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        continue;
33755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
33765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    samples = pRPCI->pAttachments[pSD->pColorAttachments[i].attachment].samples;
33775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (subpassNumSamples == (VkSampleCountFlagBits)0) {
33785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        subpassNumSamples = samples;
33795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    } else if (subpassNumSamples != samples) {
33805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        subpassNumSamples = (VkSampleCountFlagBits)-1;
33815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        break;
33825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
33835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
33845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pSD->pDepthStencilAttachment && pSD->pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
33855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    const VkSampleCountFlagBits samples = pRPCI->pAttachments[pSD->pDepthStencilAttachment->attachment].samples;
33865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (subpassNumSamples == (VkSampleCountFlagBits)0)
33875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        subpassNumSamples = samples;
33885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    else if (subpassNumSamples != samples)
33895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        subpassNumSamples = (VkSampleCountFlagBits)-1;
33905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
33915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
33925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (psoNumSamples != subpassNumSamples) {
33935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
33945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   (uint64_t)pipeline, __LINE__, DRAWSTATE_NUM_SAMPLES_MISMATCH, "DS",
33955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   "Num samples mismatch! Binding PSO (%#" PRIxLEAST64
33965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   ") with %u samples while current RenderPass (%#" PRIxLEAST64 ") w/ %u samples!",
33975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   (uint64_t)pipeline, psoNumSamples, (uint64_t)pCB->activeRenderPass, subpassNumSamples);
33985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
33995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
34005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // TODO : I believe it's an error if we reach this point and don't have an activeRenderPass
34015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                //   Verify and flag error as appropriate
34025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
34035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
34045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Add more checks here
34055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
34065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Validate non-gfx pipeline updates
34075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
34085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return VK_FALSE;
34095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
34105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
34115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Block of code at start here specifically for managing/tracking DSs
34125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
34135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return Pool node ptr for specified pool or else NULL
34145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic DESCRIPTOR_POOL_NODE *getPoolNode(layer_data *my_data, const VkDescriptorPool pool) {
34155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->descriptorPoolMap.find(pool) == my_data->descriptorPoolMap.end()) {
34165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return NULL;
34175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
34185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return my_data->descriptorPoolMap[pool];
34195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
34205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
34215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic LAYOUT_NODE *getLayoutNode(layer_data *my_data, const VkDescriptorSetLayout layout) {
34225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->descriptorSetLayoutMap.find(layout) == my_data->descriptorSetLayoutMap.end()) {
34235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return NULL;
34245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
34255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return my_data->descriptorSetLayoutMap[layout];
34265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
34275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
34285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return VK_FALSE if update struct is of valid type, otherwise flag error and return code from callback
34295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validUpdateStruct(layer_data *my_data, const VkDevice device, const GENERIC_HEADER *pUpdateStruct) {
34305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (pUpdateStruct->sType) {
34315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
34325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
34335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_FALSE;
34345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
34355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
34365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       DRAWSTATE_INVALID_UPDATE_STRUCT, "DS",
34375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       "Unexpected UPDATE struct of type %s (value %u) in vkUpdateDescriptors() struct tree",
34385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       string_VkStructureType(pUpdateStruct->sType), pUpdateStruct->sType);
34395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
34405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
34415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
34425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Set count for given update struct in the last parameter
34435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return value of skipCall, which is only VK_TRUE if error occurs and callback signals execution to cease
34445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic uint32_t getUpdateCount(layer_data *my_data, const VkDevice device, const GENERIC_HEADER *pUpdateStruct) {
34455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (pUpdateStruct->sType) {
34465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
34475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return ((VkWriteDescriptorSet *)pUpdateStruct)->descriptorCount;
34485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
34495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Need to understand this case better and make sure code is correct
34505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return ((VkCopyDescriptorSet *)pUpdateStruct)->descriptorCount;
34515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
34525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return 0;
34535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
34545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return 0;
34555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
34565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
34575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For given Layout Node and binding, return index where that binding begins
34585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic uint32_t getBindingStartIndex(const LAYOUT_NODE *pLayout, const uint32_t binding) {
34595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t offsetIndex = 0;
34605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pLayout->createInfo.bindingCount; i++) {
34615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pLayout->createInfo.pBindings[i].binding == binding)
34625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
34635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        offsetIndex += pLayout->createInfo.pBindings[i].descriptorCount;
34645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
34655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return offsetIndex;
34665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
34675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
34685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For given layout node and binding, return last index that is updated
34695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic uint32_t getBindingEndIndex(const LAYOUT_NODE *pLayout, const uint32_t binding) {
34705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t offsetIndex = 0;
34715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pLayout->createInfo.bindingCount; i++) {
34725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        offsetIndex += pLayout->createInfo.pBindings[i].descriptorCount;
34735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pLayout->createInfo.pBindings[i].binding == binding)
34745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
34755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
34765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return offsetIndex - 1;
34775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
34785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
34795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For given layout and update, return the first overall index of the layout that is updated
34805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic uint32_t getUpdateStartIndex(layer_data *my_data, const VkDevice device, const LAYOUT_NODE *pLayout, const uint32_t binding,
34815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    const uint32_t arrayIndex, const GENERIC_HEADER *pUpdateStruct) {
34825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return getBindingStartIndex(pLayout, binding) + arrayIndex;
34835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
34845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
34855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For given layout and update, return the last overall index of the layout that is updated
34865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic uint32_t getUpdateEndIndex(layer_data *my_data, const VkDevice device, const LAYOUT_NODE *pLayout, const uint32_t binding,
34875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  const uint32_t arrayIndex, const GENERIC_HEADER *pUpdateStruct) {
34885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t count = getUpdateCount(my_data, device, pUpdateStruct);
34895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return getBindingStartIndex(pLayout, binding) + arrayIndex + count - 1;
34905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
34915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
34925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Verify that the descriptor type in the update struct matches what's expected by the layout
34935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validateUpdateConsistency(layer_data *my_data, const VkDevice device, const LAYOUT_NODE *pLayout,
34945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                          const GENERIC_HEADER *pUpdateStruct, uint32_t startIndex, uint32_t endIndex) {
34955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // First get actual type of update
34965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
34975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDescriptorType actualType;
34985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t i = 0;
34995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (pUpdateStruct->sType) {
35005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
35015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        actualType = ((VkWriteDescriptorSet *)pUpdateStruct)->descriptorType;
35025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
35035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
35045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* no need to validate */
35055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_FALSE;
35065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
35075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
35085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
35095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_UPDATE_STRUCT, "DS",
35105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Unexpected UPDATE struct of type %s (value %u) in vkUpdateDescriptors() struct tree",
35115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            string_VkStructureType(pUpdateStruct->sType), pUpdateStruct->sType);
35125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
35135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
35145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Set first stageFlags as reference and verify that all other updates match it
35155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkShaderStageFlags refStageFlags = pLayout->stageFlags[startIndex];
35165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = startIndex; i <= endIndex; i++) {
35175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pLayout->descriptorTypes[i] != actualType) {
35185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |= log_msg(
35195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
35205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    DRAWSTATE_DESCRIPTOR_TYPE_MISMATCH, "DS",
35215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "Write descriptor update has descriptor type %s that does not match overlapping binding descriptor type of %s!",
35225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    string_VkDescriptorType(actualType), string_VkDescriptorType(pLayout->descriptorTypes[i]));
35235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
35245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pLayout->stageFlags[i] != refStageFlags) {
35255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |= log_msg(
35265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
35275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    DRAWSTATE_DESCRIPTOR_STAGEFLAGS_MISMATCH, "DS",
35285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "Write descriptor update has stageFlags %x that do not match overlapping binding descriptor stageFlags of %x!",
35295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    refStageFlags, pLayout->stageFlags[i]);
35305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
35315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
35325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
35335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
35345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
35355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
35365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Determine the update type, allocate a new struct of that type, shadow the given pUpdate
35375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//   struct into the pNewNode param. Return VK_TRUE if error condition encountered and callback signals early exit.
35385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// NOTE : Calls to this function should be wrapped in mutex
35395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 shadowUpdateNode(layer_data *my_data, const VkDevice device, GENERIC_HEADER *pUpdate, GENERIC_HEADER **pNewNode) {
35405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
35415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkWriteDescriptorSet *pWDS = NULL;
35425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkCopyDescriptorSet *pCDS = NULL;
35435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (pUpdate->sType) {
35445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
35455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pWDS = new VkWriteDescriptorSet;
35465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        *pNewNode = (GENERIC_HEADER *)pWDS;
35475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy(pWDS, pUpdate, sizeof(VkWriteDescriptorSet));
35485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
35495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        switch (pWDS->descriptorType) {
35505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_DESCRIPTOR_TYPE_SAMPLER:
35515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
35525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
35535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
35545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkDescriptorImageInfo *info = new VkDescriptorImageInfo[pWDS->descriptorCount];
35555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy(info, pWDS->pImageInfo, pWDS->descriptorCount * sizeof(VkDescriptorImageInfo));
35565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pWDS->pImageInfo = info;
35575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } break;
35585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
35595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
35605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkBufferView *info = new VkBufferView[pWDS->descriptorCount];
35615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy(info, pWDS->pTexelBufferView, pWDS->descriptorCount * sizeof(VkBufferView));
35625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pWDS->pTexelBufferView = info;
35635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } break;
35645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
35655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
35665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
35675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
35685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkDescriptorBufferInfo *info = new VkDescriptorBufferInfo[pWDS->descriptorCount];
35695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy(info, pWDS->pBufferInfo, pWDS->descriptorCount * sizeof(VkDescriptorBufferInfo));
35705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pWDS->pBufferInfo = info;
35715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } break;
35725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        default:
35735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_ERROR_VALIDATION_FAILED_EXT;
35745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
35755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
35765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
35775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
35785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCDS = new VkCopyDescriptorSet;
35795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        *pNewNode = (GENERIC_HEADER *)pCDS;
35805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy(pCDS, pUpdate, sizeof(VkCopyDescriptorSet));
35815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
35825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
35835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
35845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    DRAWSTATE_INVALID_UPDATE_STRUCT, "DS",
35855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "Unexpected UPDATE struct of type %s (value %u) in vkUpdateDescriptors() struct tree",
35865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    string_VkStructureType(pUpdate->sType), pUpdate->sType))
35875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_TRUE;
35885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
35895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Make sure that pNext for the end of shadow copy is NULL
35905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    (*pNewNode)->pNext = NULL;
35915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
35925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
35935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
35945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Verify that given sampler is valid
35955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validateSampler(const layer_data *my_data, const VkSampler *pSampler, const VkBool32 immutable) {
35965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
35975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto sampIt = my_data->sampleMap.find(*pSampler);
35985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (sampIt == my_data->sampleMap.end()) {
35995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!immutable) {
36005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT,
36015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)*pSampler, __LINE__, DRAWSTATE_SAMPLER_DESCRIPTOR_ERROR, "DS",
36025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkUpdateDescriptorSets: Attempt to update descriptor with invalid sampler %#" PRIxLEAST64,
36035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)*pSampler);
36045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else { // immutable
36055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT,
36065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)*pSampler, __LINE__, DRAWSTATE_SAMPLER_DESCRIPTOR_ERROR, "DS",
36075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkUpdateDescriptorSets: Attempt to update descriptor whose binding has an invalid immutable "
36085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "sampler %#" PRIxLEAST64,
36095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)*pSampler);
36105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
36115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
36125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Any further checks we want to do on the sampler?
36135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
36145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
36155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
36165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
36175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// find layout(s) on the cmd buf level
36185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool FindLayout(const GLOBAL_CB_NODE *pCB, VkImage image, VkImageSubresource range, IMAGE_CMD_BUF_LAYOUT_NODE &node) {
36195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ImageSubresourcePair imgpair = {image, true, range};
36205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto imgsubIt = pCB->imageLayoutMap.find(imgpair);
36215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (imgsubIt == pCB->imageLayoutMap.end()) {
36225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        imgpair = {image, false, VkImageSubresource()};
36235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        imgsubIt = pCB->imageLayoutMap.find(imgpair);
36245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (imgsubIt == pCB->imageLayoutMap.end())
36255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return false;
36265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
36275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    node = imgsubIt->second;
36285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return true;
36295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
36305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
36315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// find layout(s) on the global level
36325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool FindLayout(const layer_data *my_data, ImageSubresourcePair imgpair, VkImageLayout &layout) {
36335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto imgsubIt = my_data->imageLayoutMap.find(imgpair);
36345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (imgsubIt == my_data->imageLayoutMap.end()) {
36355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        imgpair = {imgpair.image, false, VkImageSubresource()};
36365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        imgsubIt = my_data->imageLayoutMap.find(imgpair);
36375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (imgsubIt == my_data->imageLayoutMap.end())
36385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return false;
36395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
36405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layout = imgsubIt->second.layout;
36415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return true;
36425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
36435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
36445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool FindLayout(const layer_data *my_data, VkImage image, VkImageSubresource range, VkImageLayout &layout) {
36455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ImageSubresourcePair imgpair = {image, true, range};
36465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return FindLayout(my_data, imgpair, layout);
36475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
36485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
36495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool FindLayouts(const layer_data *my_data, VkImage image, std::vector<VkImageLayout> &layouts) {
36505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto sub_data = my_data->imageSubresourceMap.find(image);
36515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (sub_data == my_data->imageSubresourceMap.end())
36525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false;
36535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto imgIt = my_data->imageMap.find(image);
36545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (imgIt == my_data->imageMap.end())
36555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return false;
36565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool ignoreGlobal = false;
36575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO: Make this robust for >1 aspect mask. Now it will just say ignore
36585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // potential errors in this case.
36595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (sub_data->second.size() >= (imgIt->second.createInfo.arrayLayers * imgIt->second.createInfo.mipLevels + 1)) {
36605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ignoreGlobal = true;
36615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
36625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto imgsubpair : sub_data->second) {
36635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (ignoreGlobal && !imgsubpair.hasSubresource)
36645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
36655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto img_data = my_data->imageLayoutMap.find(imgsubpair);
36665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (img_data != my_data->imageLayoutMap.end()) {
36675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            layouts.push_back(img_data->second.layout);
36685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
36695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
36705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return true;
36715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
36725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
36735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Set the layout on the global level
36745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid SetLayout(layer_data *my_data, ImageSubresourcePair imgpair, const VkImageLayout &layout) {
36755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkImage &image = imgpair.image;
36765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO (mlentine): Maybe set format if new? Not used atm.
36775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->imageLayoutMap[imgpair].layout = layout;
36785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO (mlentine): Maybe make vector a set?
36795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto subresource = std::find(my_data->imageSubresourceMap[image].begin(), my_data->imageSubresourceMap[image].end(), imgpair);
36805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (subresource == my_data->imageSubresourceMap[image].end()) {
36815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->imageSubresourceMap[image].push_back(imgpair);
36825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
36835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
36845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
36855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid SetLayout(layer_data *my_data, VkImage image, const VkImageLayout &layout) {
36865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ImageSubresourcePair imgpair = {image, false, VkImageSubresource()};
36875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    SetLayout(my_data, imgpair, layout);
36885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
36895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
36905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid SetLayout(layer_data *my_data, VkImage image, VkImageSubresource range, const VkImageLayout &layout) {
36915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ImageSubresourcePair imgpair = {image, true, range};
36925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    SetLayout(my_data, imgpair, layout);
36935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
36945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
36955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Set the layout on the cmdbuf level
36965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid SetLayout(GLOBAL_CB_NODE *pCB, VkImage image, ImageSubresourcePair imgpair, const IMAGE_CMD_BUF_LAYOUT_NODE &node) {
36975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pCB->imageLayoutMap[imgpair] = node;
36985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO (mlentine): Maybe make vector a set?
36995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto subresource = std::find(pCB->imageSubresourceMap[image].begin(), pCB->imageSubresourceMap[image].end(), imgpair);
37005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (subresource == pCB->imageSubresourceMap[image].end()) {
37015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->imageSubresourceMap[image].push_back(imgpair);
37025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
37035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
37045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
37055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid SetLayout(GLOBAL_CB_NODE *pCB, VkImage image, ImageSubresourcePair imgpair, const VkImageLayout &layout) {
37065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO (mlentine): Maybe make vector a set?
37075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (std::find(pCB->imageSubresourceMap[image].begin(), pCB->imageSubresourceMap[image].end(), imgpair) !=
37085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->imageSubresourceMap[image].end()) {
37095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->imageLayoutMap[imgpair].layout = layout;
37105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
37115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO (mlentine): Could be expensive and might need to be removed.
37125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        assert(imgpair.hasSubresource);
37135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        IMAGE_CMD_BUF_LAYOUT_NODE node;
37145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        FindLayout(pCB, image, imgpair.subresource, node);
37155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SetLayout(pCB, image, imgpair, {node.initialLayout, layout});
37165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
37175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
37185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
37195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid SetLayout(GLOBAL_CB_NODE *pCB, VkImage image, const IMAGE_CMD_BUF_LAYOUT_NODE &node) {
37205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ImageSubresourcePair imgpair = {image, false, VkImageSubresource()};
37215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    SetLayout(pCB, image, imgpair, node);
37225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
37235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
37245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid SetLayout(GLOBAL_CB_NODE *pCB, VkImage image, VkImageSubresource range, const IMAGE_CMD_BUF_LAYOUT_NODE &node) {
37255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ImageSubresourcePair imgpair = {image, true, range};
37265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    SetLayout(pCB, image, imgpair, node);
37275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
37285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
37295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid SetLayout(GLOBAL_CB_NODE *pCB, VkImage image, const VkImageLayout &layout) {
37305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ImageSubresourcePair imgpair = {image, false, VkImageSubresource()};
37315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    SetLayout(pCB, image, imgpair, layout);
37325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
37335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
37345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid SetLayout(GLOBAL_CB_NODE *pCB, VkImage image, VkImageSubresource range, const VkImageLayout &layout) {
37355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ImageSubresourcePair imgpair = {image, true, range};
37365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    SetLayout(pCB, image, imgpair, layout);
37375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
37385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
37395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid SetLayout(const layer_data *dev_data, GLOBAL_CB_NODE *pCB, VkImageView imageView, const VkImageLayout &layout) {
37405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto image_view_data = dev_data->imageViewMap.find(imageView);
37415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(image_view_data != dev_data->imageViewMap.end());
37425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkImage &image = image_view_data->second.image;
37435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkImageSubresourceRange &subRange = image_view_data->second.subresourceRange;
37445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO: Do not iterate over every possibility - consolidate where possible
37455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t j = 0; j < subRange.levelCount; j++) {
37465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t level = subRange.baseMipLevel + j;
37475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t k = 0; k < subRange.layerCount; k++) {
37485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t layer = subRange.baseArrayLayer + k;
37495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkImageSubresource sub = {subRange.aspectMask, level, layer};
37505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            SetLayout(pCB, image, sub, layout);
37515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
37525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
37535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
37545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
37555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Verify that given imageView is valid
37565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validateImageView(const layer_data *my_data, const VkImageView *pImageView, const VkImageLayout imageLayout) {
37575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
37585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto ivIt = my_data->imageViewMap.find(*pImageView);
37595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (ivIt == my_data->imageViewMap.end()) {
37605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
37615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)*pImageView, __LINE__, DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS",
37625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "vkUpdateDescriptorSets: Attempt to update descriptor with invalid imageView %#" PRIxLEAST64,
37635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)*pImageView);
37645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
37655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Validate that imageLayout is compatible with aspectMask and image format
37665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkImageAspectFlags aspectMask = ivIt->second.subresourceRange.aspectMask;
37675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkImage image = ivIt->second.image;
37685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Check here in case we have a bad image
37695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkFormat format = VK_FORMAT_MAX_ENUM;
37705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto imgIt = my_data->imageMap.find(image);
37715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (imgIt != my_data->imageMap.end()) {
37725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            format = (*imgIt).second.createInfo.format;
37735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
37745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Also need to check the swapchains.
37755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto swapchainIt = my_data->device_extensions.imageToSwapchainMap.find(image);
37765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (swapchainIt != my_data->device_extensions.imageToSwapchainMap.end()) {
37775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkSwapchainKHR swapchain = swapchainIt->second;
37785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto swapchain_nodeIt = my_data->device_extensions.swapchainMap.find(swapchain);
37795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (swapchain_nodeIt != my_data->device_extensions.swapchainMap.end()) {
37805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    SWAPCHAIN_NODE *pswapchain_node = swapchain_nodeIt->second;
37815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    format = pswapchain_node->createInfo.imageFormat;
37825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
37835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
37845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
37855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (format == VK_FORMAT_MAX_ENUM) {
37865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
37875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)image, __LINE__, DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS",
37885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkUpdateDescriptorSets: Attempt to update descriptor with invalid image %#" PRIxLEAST64
37895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                " in imageView %#" PRIxLEAST64,
37905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)image, (uint64_t)*pImageView);
37915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
37925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkBool32 ds = vk_format_is_depth_or_stencil(format);
37935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            switch (imageLayout) {
37945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
37955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Only Color bit must be set
37965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if ((aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
37975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |=
37985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
37995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)*pImageView, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "DS",
38005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkUpdateDescriptorSets: Updating descriptor with layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL "
38015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "and imageView %#" PRIxLEAST64 ""
38025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                " that does not have VK_IMAGE_ASPECT_COLOR_BIT set.",
38035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)*pImageView);
38045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
38055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // format must NOT be DS
38065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (ds) {
38075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |=
38085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
38095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)*pImageView, __LINE__, DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS",
38105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkUpdateDescriptorSets: Updating descriptor with layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL "
38115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "and imageView %#" PRIxLEAST64 ""
38125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                " but the image format is %s which is not a color format.",
38135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)*pImageView, string_VkFormat(format));
38145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
38155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
38165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
38175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
38185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Depth or stencil bit must be set, but both must NOT be set
38195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
38205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
38215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        // both  must NOT be set
38225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        skipCall |=
38235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
38245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    (uint64_t)*pImageView, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "DS",
38255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "vkUpdateDescriptorSets: Updating descriptor with imageView %#" PRIxLEAST64 ""
38265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    " that has both STENCIL and DEPTH aspects set",
38275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    (uint64_t)*pImageView);
38285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
38295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else if (!(aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
38305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // Neither were set
38315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |=
38325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
38335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)*pImageView, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "DS",
38345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkUpdateDescriptorSets: Updating descriptor with layout %s and imageView %#" PRIxLEAST64 ""
38355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                " that does not have STENCIL or DEPTH aspect set.",
38365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                string_VkImageLayout(imageLayout), (uint64_t)*pImageView);
38375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
38385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // format must be DS
38395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (!ds) {
38405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |=
38415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT,
38425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)*pImageView, __LINE__, DRAWSTATE_IMAGEVIEW_DESCRIPTOR_ERROR, "DS",
38435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkUpdateDescriptorSets: Updating descriptor with layout %s and imageView %#" PRIxLEAST64 ""
38445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                " but the image format is %s which is not a depth/stencil format.",
38455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                string_VkImageLayout(imageLayout), (uint64_t)*pImageView, string_VkFormat(format));
38465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
38475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
38485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            default:
38495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // anything to check for other layouts?
38505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
38515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
38525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
38535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
38545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
38555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
38565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
38575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Verify that given bufferView is valid
38585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validateBufferView(const layer_data *my_data, const VkBufferView *pBufferView) {
38595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
38605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto sampIt = my_data->bufferViewMap.find(*pBufferView);
38615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (sampIt == my_data->bufferViewMap.end()) {
38625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT,
38635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)*pBufferView, __LINE__, DRAWSTATE_BUFFERVIEW_DESCRIPTOR_ERROR, "DS",
38645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "vkUpdateDescriptorSets: Attempt to update descriptor with invalid bufferView %#" PRIxLEAST64,
38655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)*pBufferView);
38665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
38675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Any further checks we want to do on the bufferView?
38685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
38695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
38705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
38715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
38725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Verify that given bufferInfo is valid
38735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validateBufferInfo(const layer_data *my_data, const VkDescriptorBufferInfo *pBufferInfo) {
38745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
38755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto sampIt = my_data->bufferMap.find(pBufferInfo->buffer);
38765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (sampIt == my_data->bufferMap.end()) {
38775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |=
38785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
38795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (uint64_t)pBufferInfo->buffer, __LINE__, DRAWSTATE_BUFFERINFO_DESCRIPTOR_ERROR, "DS",
38805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "vkUpdateDescriptorSets: Attempt to update descriptor where bufferInfo has invalid buffer %#" PRIxLEAST64,
38815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (uint64_t)pBufferInfo->buffer);
38825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
38835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Any further checks we want to do on the bufferView?
38845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
38855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
38865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
38875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
38885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validateUpdateContents(const layer_data *my_data, const VkWriteDescriptorSet *pWDS,
38895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                       const VkDescriptorSetLayoutBinding *pLayoutBinding) {
38905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
38915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // First verify that for the given Descriptor type, the correct DescriptorInfo data is supplied
38925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkSampler *pSampler = NULL;
38935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 immutable = VK_FALSE;
38945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t i = 0;
38955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // For given update type, verify that update contents are correct
38965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (pWDS->descriptorType) {
38975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_SAMPLER:
38985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < pWDS->descriptorCount; ++i) {
38995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validateSampler(my_data, &(pWDS->pImageInfo[i].sampler), immutable);
39005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
39015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
39025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
39035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < pWDS->descriptorCount; ++i) {
39045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (NULL == pLayoutBinding->pImmutableSamplers) {
39055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pSampler = &(pWDS->pImageInfo[i].sampler);
39065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (immutable) {
39075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(
39085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT,
39095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)*pSampler, __LINE__, DRAWSTATE_INCONSISTENT_IMMUTABLE_SAMPLER_UPDATE, "DS",
39105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkUpdateDescriptorSets: Update #%u is not an immutable sampler %#" PRIxLEAST64
39115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        ", but previous update(s) from this "
39125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "VkWriteDescriptorSet struct used an immutable sampler. All updates from a single struct must either "
39135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "use immutable or non-immutable samplers.",
39145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        i, (uint64_t)*pSampler);
39155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
39165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
39175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (i > 0 && !immutable) {
39185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(
39195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT,
39205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)*pSampler, __LINE__, DRAWSTATE_INCONSISTENT_IMMUTABLE_SAMPLER_UPDATE, "DS",
39215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkUpdateDescriptorSets: Update #%u is an immutable sampler, but previous update(s) from this "
39225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "VkWriteDescriptorSet struct used a non-immutable sampler. All updates from a single struct must either "
39235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "use immutable or non-immutable samplers.",
39245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        i);
39255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
39265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                immutable = VK_TRUE;
39275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pSampler = &(pLayoutBinding->pImmutableSamplers[i]);
39285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
39295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validateSampler(my_data, pSampler, immutable);
39305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
39315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Intentionally fall through here to also validate image stuff
39325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
39335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
39345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
39355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < pWDS->descriptorCount; ++i) {
39365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validateImageView(my_data, &(pWDS->pImageInfo[i].imageView), pWDS->pImageInfo[i].imageLayout);
39375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
39385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
39395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
39405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
39415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < pWDS->descriptorCount; ++i) {
39425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validateBufferView(my_data, &(pWDS->pTexelBufferView[i]));
39435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
39445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
39455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
39465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
39475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
39485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
39495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < pWDS->descriptorCount; ++i) {
39505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validateBufferInfo(my_data, &(pWDS->pBufferInfo[i]));
39515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
39525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
39535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default:
39545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
39555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
39565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
39575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
39585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Validate that given set is valid and that it's not being used by an in-flight CmdBuffer
39595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// func_str is the name of the calling function
39605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return VK_FALSE if no errors occur
39615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return VK_TRUE if validation error occurs and callback returns VK_TRUE (to skip upcoming API call down the chain)
39625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 validateIdleDescriptorSet(const layer_data *my_data, VkDescriptorSet set, std::string func_str) {
39635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
39645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto set_node = my_data->setMap.find(set);
39655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (set_node == my_data->setMap.end()) {
39665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
39675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             (uint64_t)(set), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
39685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             "Cannot call %s() on descriptor set %" PRIxLEAST64 " that has not been allocated.", func_str.c_str(),
39695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             (uint64_t)(set));
39705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
39715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (set_node->second->in_use.load()) {
39725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
39735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)(set), __LINE__, DRAWSTATE_OBJECT_INUSE,
39745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 "DS", "Cannot call %s() on descriptor set %" PRIxLEAST64 " that is in use by a command buffer.",
39755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 func_str.c_str(), (uint64_t)(set));
39765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
39775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
39785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
39795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
39805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void invalidateBoundCmdBuffers(layer_data *dev_data, const SET_NODE *pSet) {
39815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Flag any CBs this set is bound to as INVALID
39825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto cb : pSet->boundCmdBuffers) {
39835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto cb_node = dev_data->commandBufferMap.find(cb);
39845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (cb_node != dev_data->commandBufferMap.end()) {
39855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            cb_node->second->state = CB_INVALID;
39865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
39875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
39885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
39895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// update DS mappings based on write and copy update arrays
39905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 dsUpdate(layer_data *my_data, VkDevice device, uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pWDS,
39915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                         uint32_t descriptorCopyCount, const VkCopyDescriptorSet *pCDS) {
39925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
39935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
39945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    LAYOUT_NODE *pLayout = NULL;
39955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDescriptorSetLayoutCreateInfo *pLayoutCI = NULL;
39965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate Write updates
39975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t i = 0;
39985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (i = 0; i < descriptorWriteCount; i++) {
39995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkDescriptorSet ds = pWDS[i].dstSet;
40005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SET_NODE *pSet = my_data->setMap[ds];
40015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Set being updated cannot be in-flight
40025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((skipCall = validateIdleDescriptorSet(my_data, ds, "VkUpdateDescriptorSets")) == VK_TRUE)
40035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return skipCall;
40045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // If set is bound to any cmdBuffers, mark them invalid
40055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        invalidateBoundCmdBuffers(my_data, pSet);
40065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        GENERIC_HEADER *pUpdate = (GENERIC_HEADER *)&pWDS[i];
40075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pLayout = pSet->pLayout;
40085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // First verify valid update struct
40095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((skipCall = validUpdateStruct(my_data, device, pUpdate)) == VK_TRUE) {
40105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
40115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
40125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t binding = 0, endIndex = 0;
40135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        binding = pWDS[i].dstBinding;
40145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto bindingToIndex = pLayout->bindingToIndexMap.find(binding);
40155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Make sure that layout being updated has the binding being updated
40165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (bindingToIndex == pLayout->bindingToIndexMap.end()) {
40175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
40185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)(ds), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
40195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Descriptor Set %" PRIu64 " does not have binding to match "
40205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "update binding %u for update type "
40215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "%s!",
40225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)(ds), binding, string_VkStructureType(pUpdate->sType));
40235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
40245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Next verify that update falls within size of given binding
40255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            endIndex = getUpdateEndIndex(my_data, device, pLayout, binding, pWDS[i].dstArrayElement, pUpdate);
40265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (getBindingEndIndex(pLayout, binding) < endIndex) {
40275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pLayoutCI = &pLayout->createInfo;
40285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                string DSstr = vk_print_vkdescriptorsetlayoutcreateinfo(pLayoutCI, "{DS}    ");
40295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
40305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
40315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(ds), __LINE__, DRAWSTATE_DESCRIPTOR_UPDATE_OUT_OF_BOUNDS, "DS",
40325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Descriptor update type of %s is out of bounds for matching binding %u in Layout w/ CI:\n%s!",
40335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            string_VkStructureType(pUpdate->sType), binding, DSstr.c_str());
40345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else { // TODO : should we skip update on a type mismatch or force it?
40355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                uint32_t startIndex;
40365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                startIndex = getUpdateStartIndex(my_data, device, pLayout, binding, pWDS[i].dstArrayElement, pUpdate);
40375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Layout bindings match w/ update, now verify that update type
40385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // & stageFlags are the same for entire update
40395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if ((skipCall = validateUpdateConsistency(my_data, device, pLayout, pUpdate, startIndex, endIndex)) == VK_FALSE) {
40405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // The update is within bounds and consistent, but need to
40415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // make sure contents make sense as well
40425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if ((skipCall = validateUpdateContents(my_data, &pWDS[i],
40435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                           &pLayout->createInfo.pBindings[bindingToIndex->second])) == VK_FALSE) {
40445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        // Update is good. Save the update info
40455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        // Create new update struct for this set's shadow copy
40465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        GENERIC_HEADER *pNewNode = NULL;
40475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        skipCall |= shadowUpdateNode(my_data, device, pUpdate, &pNewNode);
40485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (NULL == pNewNode) {
40495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            skipCall |= log_msg(
40505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
40515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)(ds), __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
40525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Out of memory while attempting to allocate UPDATE struct in vkUpdateDescriptors()");
40535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        } else {
40545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            // Insert shadow node into LL of updates for this set
40555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            pNewNode->pNext = pSet->pUpdateStructs;
40565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            pSet->pUpdateStructs = pNewNode;
40575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            // Now update appropriate descriptor(s) to point to new Update node
40585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            for (uint32_t j = startIndex; j <= endIndex; j++) {
40595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                assert(j < pSet->descriptorCount);
40605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                pSet->ppDescriptors[j] = pNewNode;
40615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            }
40625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
40635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
40645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
40655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
40665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
40675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
40685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Now validate copy updates
40695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (i = 0; i < descriptorCopyCount; ++i) {
40705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SET_NODE *pSrcSet = NULL, *pDstSet = NULL;
40715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        LAYOUT_NODE *pSrcLayout = NULL, *pDstLayout = NULL;
40725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t srcStartIndex = 0, srcEndIndex = 0, dstStartIndex = 0, dstEndIndex = 0;
40735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // For each copy make sure that update falls within given layout and that types match
40745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pSrcSet = my_data->setMap[pCDS[i].srcSet];
40755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pDstSet = my_data->setMap[pCDS[i].dstSet];
40765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Set being updated cannot be in-flight
40775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((skipCall = validateIdleDescriptorSet(my_data, pDstSet->set, "VkUpdateDescriptorSets")) == VK_TRUE)
40785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return skipCall;
40795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        invalidateBoundCmdBuffers(my_data, pDstSet);
40805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pSrcLayout = pSrcSet->pLayout;
40815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pDstLayout = pDstSet->pLayout;
40825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Validate that src binding is valid for src set layout
40835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pSrcLayout->bindingToIndexMap.find(pCDS[i].srcBinding) == pSrcLayout->bindingToIndexMap.end()) {
40845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
40855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)pSrcSet->set, __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
40865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Copy descriptor update %u has srcBinding %u "
40875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "which is out of bounds for underlying SetLayout "
40885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "%#" PRIxLEAST64 " which only has bindings 0-%u.",
40895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                i, pCDS[i].srcBinding, (uint64_t)pSrcLayout->layout, pSrcLayout->createInfo.bindingCount - 1);
40905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (pDstLayout->bindingToIndexMap.find(pCDS[i].dstBinding) == pDstLayout->bindingToIndexMap.end()) {
40915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
40925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)pDstSet->set, __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
40935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Copy descriptor update %u has dstBinding %u "
40945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "which is out of bounds for underlying SetLayout "
40955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "%#" PRIxLEAST64 " which only has bindings 0-%u.",
40965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                i, pCDS[i].dstBinding, (uint64_t)pDstLayout->layout, pDstLayout->createInfo.bindingCount - 1);
40975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
40985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Proceed with validation. Bindings are ok, but make sure update is within bounds of given layout
40995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            srcEndIndex = getUpdateEndIndex(my_data, device, pSrcLayout, pCDS[i].srcBinding, pCDS[i].srcArrayElement,
41005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            (const GENERIC_HEADER *)&(pCDS[i]));
41015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dstEndIndex = getUpdateEndIndex(my_data, device, pDstLayout, pCDS[i].dstBinding, pCDS[i].dstArrayElement,
41025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            (const GENERIC_HEADER *)&(pCDS[i]));
41035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (getBindingEndIndex(pSrcLayout, pCDS[i].srcBinding) < srcEndIndex) {
41045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pLayoutCI = &pSrcLayout->createInfo;
41055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                string DSstr = vk_print_vkdescriptorsetlayoutcreateinfo(pLayoutCI, "{DS}    ");
41065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
41075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
41085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)pSrcSet->set, __LINE__, DRAWSTATE_DESCRIPTOR_UPDATE_OUT_OF_BOUNDS, "DS",
41095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Copy descriptor src update is out of bounds for matching binding %u in Layout w/ CI:\n%s!",
41105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            pCDS[i].srcBinding, DSstr.c_str());
41115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else if (getBindingEndIndex(pDstLayout, pCDS[i].dstBinding) < dstEndIndex) {
41125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pLayoutCI = &pDstLayout->createInfo;
41135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                string DSstr = vk_print_vkdescriptorsetlayoutcreateinfo(pLayoutCI, "{DS}    ");
41145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
41155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
41165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)pDstSet->set, __LINE__, DRAWSTATE_DESCRIPTOR_UPDATE_OUT_OF_BOUNDS, "DS",
41175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Copy descriptor dest update is out of bounds for matching binding %u in Layout w/ CI:\n%s!",
41185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            pCDS[i].dstBinding, DSstr.c_str());
41195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
41205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                srcStartIndex = getUpdateStartIndex(my_data, device, pSrcLayout, pCDS[i].srcBinding, pCDS[i].srcArrayElement,
41215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                    (const GENERIC_HEADER *)&(pCDS[i]));
41225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dstStartIndex = getUpdateStartIndex(my_data, device, pDstLayout, pCDS[i].dstBinding, pCDS[i].dstArrayElement,
41235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                    (const GENERIC_HEADER *)&(pCDS[i]));
41245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (uint32_t j = 0; j < pCDS[i].descriptorCount; ++j) {
41255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // For copy just make sure that the types match and then perform the update
41265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (pSrcLayout->descriptorTypes[srcStartIndex + j] != pDstLayout->descriptorTypes[dstStartIndex + j]) {
41275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
41285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            __LINE__, DRAWSTATE_DESCRIPTOR_TYPE_MISMATCH, "DS",
41295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "Copy descriptor update index %u, update count #%u, has src update descriptor type %s "
41305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "that does not match overlapping dest descriptor type of %s!",
41315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            i, j + 1, string_VkDescriptorType(pSrcLayout->descriptorTypes[srcStartIndex + j]),
41325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            string_VkDescriptorType(pDstLayout->descriptorTypes[dstStartIndex + j]));
41335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    } else {
41345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        // point dst descriptor at corresponding src descriptor
41355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        // TODO : This may be a hole. I believe copy should be its own copy,
41365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        //  otherwise a subsequent write update to src will incorrectly affect the copy
41375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        pDstSet->ppDescriptors[j + dstStartIndex] = pSrcSet->ppDescriptors[j + srcStartIndex];
41385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        pDstSet->pUpdateStructs = pSrcSet->pUpdateStructs;
41395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
41405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
41415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
41425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
41435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
41445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
41455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
41465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
41475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Verify that given pool has descriptors that are being requested for allocation
41485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validate_descriptor_availability_in_pool(layer_data *dev_data, DESCRIPTOR_POOL_NODE *pPoolNode, uint32_t count,
41495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         const VkDescriptorSetLayout *pSetLayouts) {
41505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
41515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t i = 0, j = 0;
41525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (i = 0; i < count; ++i) {
41535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        LAYOUT_NODE *pLayout = getLayoutNode(dev_data, pSetLayouts[i]);
41545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (NULL == pLayout) {
41555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
41565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
41575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)pSetLayouts[i], __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
41585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Unable to find set layout node for layout %#" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
41595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)pSetLayouts[i]);
41605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
41615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t typeIndex = 0, poolSizeCount = 0;
41625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (j = 0; j < pLayout->createInfo.bindingCount; ++j) {
41635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                typeIndex = static_cast<uint32_t>(pLayout->createInfo.pBindings[j].descriptorType);
41645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                poolSizeCount = pLayout->createInfo.pBindings[j].descriptorCount;
41655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (poolSizeCount > pPoolNode->availableDescriptorTypeCount[typeIndex]) {
41665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
41675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t)pLayout->layout, __LINE__,
41685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        DRAWSTATE_DESCRIPTOR_POOL_EMPTY, "DS",
41695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "Unable to allocate %u descriptors of type %s from pool %#" PRIxLEAST64
41705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        ". This pool only has %u descriptors of this type remaining.",
41715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        poolSizeCount, string_VkDescriptorType(pLayout->createInfo.pBindings[j].descriptorType),
41725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        (uint64_t)pPoolNode->pool, pPoolNode->availableDescriptorTypeCount[typeIndex]);
41735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else { // Decrement available descriptors of this type
41745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pPoolNode->availableDescriptorTypeCount[typeIndex] -= poolSizeCount;
41755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
41765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
41775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
41785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
41795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
41805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
41815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
41825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Free the shadowed update node for this Set
41835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// NOTE : Calls to this function should be wrapped in mutex
41845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void freeShadowUpdateTree(SET_NODE *pSet) {
41855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GENERIC_HEADER *pShadowUpdate = pSet->pUpdateStructs;
41865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pSet->pUpdateStructs = NULL;
41875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GENERIC_HEADER *pFreeUpdate = pShadowUpdate;
41885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Clear the descriptor mappings as they will now be invalid
41895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    memset(pSet->ppDescriptors, 0, pSet->descriptorCount * sizeof(GENERIC_HEADER *));
41905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while (pShadowUpdate) {
41915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pFreeUpdate = pShadowUpdate;
41925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pShadowUpdate = (GENERIC_HEADER *)pShadowUpdate->pNext;
41935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkWriteDescriptorSet *pWDS = NULL;
41945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        switch (pFreeUpdate->sType) {
41955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET:
41965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pWDS = (VkWriteDescriptorSet *)pFreeUpdate;
41975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            switch (pWDS->descriptorType) {
41985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DESCRIPTOR_TYPE_SAMPLER:
41995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
42005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
42015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
42025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                delete[] pWDS->pImageInfo;
42035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } break;
42045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
42055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
42065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                delete[] pWDS->pTexelBufferView;
42075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } break;
42085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
42095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
42105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
42115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
42125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                delete[] pWDS->pBufferInfo;
42135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } break;
42145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            default:
42155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
42165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
42175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
42185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET:
42195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
42205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        default:
42215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            assert(0);
42225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
42235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
42245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete pFreeUpdate;
42255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
42265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
42275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
42285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Free all DS Pools including their Sets & related sub-structs
42295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// NOTE : Calls to this function should be wrapped in mutex
42305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void deletePools(layer_data *my_data) {
42315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->descriptorPoolMap.size() <= 0)
42325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
42335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto ii = my_data->descriptorPoolMap.begin(); ii != my_data->descriptorPoolMap.end(); ++ii) {
42345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SET_NODE *pSet = (*ii).second->pSets;
42355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SET_NODE *pFreeSet = pSet;
42365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        while (pSet) {
42375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pFreeSet = pSet;
42385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pSet = pSet->pNext;
42395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Freeing layouts handled in deleteLayouts() function
42405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Free Update shadow struct tree
42415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            freeShadowUpdateTree(pFreeSet);
42425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            delete[] pFreeSet->ppDescriptors;
42435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            delete pFreeSet;
42445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
42455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete (*ii).second;
42465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
42475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->descriptorPoolMap.clear();
42485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
42495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
42505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// WARN : Once deleteLayouts() called, any layout ptrs in Pool/Set data structure will be invalid
42515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// NOTE : Calls to this function should be wrapped in mutex
42525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void deleteLayouts(layer_data *my_data) {
42535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->descriptorSetLayoutMap.size() <= 0)
42545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
42555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto ii = my_data->descriptorSetLayoutMap.begin(); ii != my_data->descriptorSetLayoutMap.end(); ++ii) {
42565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        LAYOUT_NODE *pLayout = (*ii).second;
42575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pLayout->createInfo.pBindings) {
42585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t i = 0; i < pLayout->createInfo.bindingCount; i++) {
42595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                delete[] pLayout->createInfo.pBindings[i].pImmutableSamplers;
42605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
42615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            delete[] pLayout->createInfo.pBindings;
42625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
42635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete pLayout;
42645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
42655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->descriptorSetLayoutMap.clear();
42665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
42675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
42685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Currently clearing a set is removing all previous updates to that set
42695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  TODO : Validate if this is correct clearing behavior
42705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void clearDescriptorSet(layer_data *my_data, VkDescriptorSet set) {
42715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    SET_NODE *pSet = getSetNode(my_data, set);
42725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!pSet) {
42735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Return error
42745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
42755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        freeShadowUpdateTree(pSet);
42765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
42775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
42785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
42795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void clearDescriptorPool(layer_data *my_data, const VkDevice device, const VkDescriptorPool pool,
42805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                VkDescriptorPoolResetFlags flags) {
42815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    DESCRIPTOR_POOL_NODE *pPool = getPoolNode(my_data, pool);
42825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!pPool) {
42835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
42845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                (uint64_t)pool, __LINE__, DRAWSTATE_INVALID_POOL, "DS",
42855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                "Unable to find pool node for pool %#" PRIxLEAST64 " specified in vkResetDescriptorPool() call", (uint64_t)pool);
42865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
42875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO: validate flags
42885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // For every set off of this pool, clear it
42895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SET_NODE *pSet = pPool->pSets;
42905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        while (pSet) {
42915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            clearDescriptorSet(my_data, pSet->set);
42925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pSet = pSet->pNext;
42935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
42945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Reset available count to max count for this pool
42955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < pPool->availableDescriptorTypeCount.size(); ++i) {
42965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPool->availableDescriptorTypeCount[i] = pPool->maxDescriptorTypeCount[i];
42975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
42985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
42995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
43005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
43015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// For given CB object, fetch associated CB Node from map
43025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic GLOBAL_CB_NODE *getCBNode(layer_data *my_data, const VkCommandBuffer cb) {
43035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->commandBufferMap.count(cb) == 0) {
43045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : How to pass cb as srcObj here?
43055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__,
43065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                DRAWSTATE_INVALID_COMMAND_BUFFER, "DS", "Attempt to use CommandBuffer %#" PRIxLEAST64 " that doesn't exist!",
43075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                (uint64_t)(cb));
43085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return NULL;
43095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
43105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return my_data->commandBufferMap[cb];
43115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
43125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
43135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Free all CB Nodes
43145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// NOTE : Calls to this function should be wrapped in mutex
43155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void deleteCommandBuffers(layer_data *my_data) {
43165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->commandBufferMap.size() <= 0) {
43175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
43185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
43195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto ii = my_data->commandBufferMap.begin(); ii != my_data->commandBufferMap.end(); ++ii) {
43205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete (*ii).second;
43215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
43225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->commandBufferMap.clear();
43235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
43245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
43255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 report_error_no_cb_begin(const layer_data *dev_data, const VkCommandBuffer cb, const char *caller_name) {
43265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
43275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   (uint64_t)cb, __LINE__, DRAWSTATE_NO_BEGIN_COMMAND_BUFFER, "DS",
43285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   "You must call vkBeginCommandBuffer() before this call to %s", caller_name);
43295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
43305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
43315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 validateCmdsInCmdBuffer(const layer_data *dev_data, const GLOBAL_CB_NODE *pCB, const CMD_TYPE cmd_type) {
43325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!pCB->activeRenderPass)
43335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_FALSE;
43345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
43355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB->activeSubpassContents == VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS && cmd_type != CMD_EXECUTECOMMANDS) {
43365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
43375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
43385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             "Commands cannot be called in a subpass using secondary command buffers.");
43395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else if (pCB->activeSubpassContents == VK_SUBPASS_CONTENTS_INLINE && cmd_type == CMD_EXECUTECOMMANDS) {
43405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
43415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
43425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             "vkCmdExecuteCommands() cannot be called in a subpass using inline commands.");
43435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
43445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
43455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
43465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
43475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool checkGraphicsBit(const layer_data *my_data, VkQueueFlags flags, const char *name) {
43485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!(flags & VK_QUEUE_GRAPHICS_BIT))
43495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
43505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
43515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       "Cannot call %s on a command buffer allocated from a pool without graphics capabilities.", name);
43525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return false;
43535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
43545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
43555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool checkComputeBit(const layer_data *my_data, VkQueueFlags flags, const char *name) {
43565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!(flags & VK_QUEUE_COMPUTE_BIT))
43575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
43585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
43595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       "Cannot call %s on a command buffer allocated from a pool without compute capabilities.", name);
43605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return false;
43615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
43625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
43635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool checkGraphicsOrComputeBit(const layer_data *my_data, VkQueueFlags flags, const char *name) {
43645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!((flags & VK_QUEUE_GRAPHICS_BIT) || (flags & VK_QUEUE_COMPUTE_BIT)))
43655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
43665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
43675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       "Cannot call %s on a command buffer allocated from a pool without graphics capabilities.", name);
43685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return false;
43695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
43705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
43715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Add specified CMD to the CmdBuffer in given pCB, flagging errors if CB is not
43725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  in the recording state or if there's an issue with the Cmd ordering
43735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 addCmd(const layer_data *my_data, GLOBAL_CB_NODE *pCB, const CMD_TYPE cmd, const char *caller_name) {
43745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
43755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto pool_data = my_data->commandPoolMap.find(pCB->createInfo.commandPool);
43765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pool_data != my_data->commandPoolMap.end()) {
43775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkQueueFlags flags = my_data->physDevProperties.queue_family_properties[pool_data->second.queueFamilyIndex].queueFlags;
43785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        switch (cmd) {
43795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_BINDPIPELINE:
43805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_BINDPIPELINEDELTA:
43815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_BINDDESCRIPTORSETS:
43825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_FILLBUFFER:
43835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_CLEARCOLORIMAGE:
43845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_SETEVENT:
43855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_RESETEVENT:
43865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_WAITEVENTS:
43875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_BEGINQUERY:
43885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_ENDQUERY:
43895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_RESETQUERYPOOL:
43905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_COPYQUERYPOOLRESULTS:
43915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_WRITETIMESTAMP:
43925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= checkGraphicsOrComputeBit(my_data, flags, cmdTypeToString(cmd).c_str());
43935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
43945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_SETVIEWPORTSTATE:
43955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_SETSCISSORSTATE:
43965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_SETLINEWIDTHSTATE:
43975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_SETDEPTHBIASSTATE:
43985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_SETBLENDSTATE:
43995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_SETDEPTHBOUNDSSTATE:
44005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_SETSTENCILREADMASKSTATE:
44015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_SETSTENCILWRITEMASKSTATE:
44025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_SETSTENCILREFERENCESTATE:
44035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_BINDINDEXBUFFER:
44045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_BINDVERTEXBUFFER:
44055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_DRAW:
44065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_DRAWINDEXED:
44075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_DRAWINDIRECT:
44085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_DRAWINDEXEDINDIRECT:
44095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_BLITIMAGE:
44105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_CLEARATTACHMENTS:
44115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_CLEARDEPTHSTENCILIMAGE:
44125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_RESOLVEIMAGE:
44135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_BEGINRENDERPASS:
44145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_NEXTSUBPASS:
44155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_ENDRENDERPASS:
44165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= checkGraphicsBit(my_data, flags, cmdTypeToString(cmd).c_str());
44175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
44185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_DISPATCH:
44195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_DISPATCHINDIRECT:
44205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= checkComputeBit(my_data, flags, cmdTypeToString(cmd).c_str());
44215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
44225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_COPYBUFFER:
44235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_COPYIMAGE:
44245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_COPYBUFFERTOIMAGE:
44255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_COPYIMAGETOBUFFER:
44265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_CLONEIMAGEDATA:
44275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_UPDATEBUFFER:
44285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_PIPELINEBARRIER:
44295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case CMD_EXECUTECOMMANDS:
44305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
44315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        default:
44325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
44335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
44345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
44355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB->state != CB_RECORDING) {
44365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= report_error_no_cb_begin(my_data, pCB->commandBuffer, caller_name);
44375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validateCmdsInCmdBuffer(my_data, pCB, cmd);
44385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        CMD_NODE cmdNode = {};
44395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // init cmd node and append to end of cmd LL
44405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cmdNode.cmdNumber = ++pCB->numCmds;
44415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cmdNode.type = cmd;
44425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->cmds.push_back(cmdNode);
44435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
44445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
44455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
44465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Reset the command buffer state
44475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  Maintain the createInfo and set state to CB_NEW, but clear all other state
44485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void resetCB(layer_data *my_data, const VkCommandBuffer cb) {
44495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = my_data->commandBufferMap[cb];
44505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
44515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->cmds.clear();
44525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Reset CB state (note that createInfo is not cleared)
44535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->commandBuffer = cb;
44545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(&pCB->beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
44555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(&pCB->inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo));
44565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->fence = 0;
44575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->numCmds = 0;
44585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(pCB->drawCount, 0, NUM_DRAW_TYPES * sizeof(uint64_t));
44595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->state = CB_NEW;
44605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->submitCount = 0;
44615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status = 0;
44625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->lastBoundPipeline = 0;
44635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->lastVtxBinding = 0;
44645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->boundVtxBuffers.clear();
44655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->viewports.clear();
44665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->scissors.clear();
44675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->lineWidth = 0;
44685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->depthBiasConstantFactor = 0;
44695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->depthBiasClamp = 0;
44705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->depthBiasSlopeFactor = 0;
44715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(pCB->blendConstants, 0, 4 * sizeof(float));
44725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->minDepthBounds = 0;
44735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->maxDepthBounds = 0;
44745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(&pCB->front, 0, sizeof(stencil_data));
44755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(&pCB->back, 0, sizeof(stencil_data));
44765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->lastBoundDescriptorSet = 0;
44775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->lastBoundPipelineLayout = 0;
44785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(&pCB->activeRenderPassBeginInfo, 0, sizeof(pCB->activeRenderPassBeginInfo));
44795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->activeRenderPass = 0;
44805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->activeSubpassContents = VK_SUBPASS_CONTENTS_INLINE;
44815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->activeSubpass = 0;
44825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->framebuffer = 0;
44835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Before clearing uniqueBoundSets, remove this CB off of its boundCBs
44845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto set : pCB->uniqueBoundSets) {
44855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto set_node = my_data->setMap.find(set);
44865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (set_node != my_data->setMap.end()) {
44875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                set_node->second->boundCmdBuffers.erase(pCB->commandBuffer);
44885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
44895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
44905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->uniqueBoundSets.clear();
44915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->destroyedSets.clear();
44925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->updatedSets.clear();
44935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->destroyedFramebuffers.clear();
44945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->boundDescriptorSets.clear();
44955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->waitedEvents.clear();
44965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->semaphores.clear();
44975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->events.clear();
44985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->waitedEventsBeforeQueryReset.clear();
44995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->queryToStateMap.clear();
45005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->activeQueries.clear();
45015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->startedQueries.clear();
45025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->imageLayoutMap.clear();
45035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->eventToStageMap.clear();
45045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->drawData.clear();
45055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->currentDrawData.buffers.clear();
45065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->primaryCommandBuffer = VK_NULL_HANDLE;
45075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->secondaryCommandBuffers.clear();
45085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->dynamicOffsets.clear();
45095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
45105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
45115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
45125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Set PSO-related status bits for CB, including dynamic state set via PSO
45135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void set_cb_pso_status(GLOBAL_CB_NODE *pCB, const PIPELINE_NODE *pPipe) {
45145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pPipe->cbStateCI.attachmentCount; i++) {
45155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (0 != pPipe->pAttachments[i].colorWriteMask) {
45165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->status |= CBSTATUS_COLOR_BLEND_WRITE_ENABLE;
45175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
45185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
45195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pPipe->dsStateCI.depthWriteEnable) {
45205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_DEPTH_WRITE_ENABLE;
45215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
45225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pPipe->dsStateCI.stencilTestEnable) {
45235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_STENCIL_TEST_ENABLE;
45245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
45255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Account for any dynamic state not set via this PSO
45265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!pPipe->dynStateCI.dynamicStateCount) { // All state is static
45275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status = CBSTATUS_ALL;
45285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
45295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // First consider all state on
45305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Then unset any state that's noted as dynamic in PSO
45315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Finally OR that into CB statemask
45325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        CBStatusFlags psoDynStateMask = CBSTATUS_ALL;
45335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < pPipe->dynStateCI.dynamicStateCount; i++) {
45345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            switch (pPipe->dynStateCI.pDynamicStates[i]) {
45355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DYNAMIC_STATE_VIEWPORT:
45365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                psoDynStateMask &= ~CBSTATUS_VIEWPORT_SET;
45375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
45385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DYNAMIC_STATE_SCISSOR:
45395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                psoDynStateMask &= ~CBSTATUS_SCISSOR_SET;
45405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
45415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DYNAMIC_STATE_LINE_WIDTH:
45425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                psoDynStateMask &= ~CBSTATUS_LINE_WIDTH_SET;
45435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
45445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DYNAMIC_STATE_DEPTH_BIAS:
45455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                psoDynStateMask &= ~CBSTATUS_DEPTH_BIAS_SET;
45465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
45475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
45485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                psoDynStateMask &= ~CBSTATUS_BLEND_SET;
45495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
45505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
45515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                psoDynStateMask &= ~CBSTATUS_DEPTH_BOUNDS_SET;
45525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
45535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
45545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                psoDynStateMask &= ~CBSTATUS_STENCIL_READ_MASK_SET;
45555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
45565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
45575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                psoDynStateMask &= ~CBSTATUS_STENCIL_WRITE_MASK_SET;
45585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
45595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
45605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                psoDynStateMask &= ~CBSTATUS_STENCIL_REFERENCE_SET;
45615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
45625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            default:
45635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // TODO : Flag error here
45645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
45655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
45665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
45675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= psoDynStateMask;
45685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
45695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
45705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
45715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Print the last bound Gfx Pipeline
45725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 printPipeline(layer_data *my_data, const VkCommandBuffer cb) {
45735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
45745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(my_data, cb);
45755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
45765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        PIPELINE_NODE *pPipeTrav = getPipeline(my_data, pCB->lastBoundPipeline);
45775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!pPipeTrav) {
45785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // nothing to print
45795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
45805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
45815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                __LINE__, DRAWSTATE_NONE, "DS", "%s",
45825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                vk_print_vkgraphicspipelinecreateinfo(&pPipeTrav->graphicsPipelineCI, "{DS}").c_str());
45835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
45845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
45855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
45865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
45875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
45885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Print details of DS config to stdout
45895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 printDSConfig(layer_data *my_data, const VkCommandBuffer cb) {
45905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
45915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(my_data, cb);
45925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB && pCB->lastBoundDescriptorSet) {
45935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SET_NODE *pSet = getSetNode(my_data, pCB->lastBoundDescriptorSet);
45945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        DESCRIPTOR_POOL_NODE *pPool = getPoolNode(my_data, pSet->pool);
45955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Print out pool details
45965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
45975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_NONE, "DS", "Details for pool %#" PRIxLEAST64 ".", (uint64_t)pPool->pool);
45985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        string poolStr = vk_print_vkdescriptorpoolcreateinfo(&pPool->createInfo, " ");
45995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
46005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_NONE, "DS", "%s", poolStr.c_str());
46015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Print out set details
46025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        char prefix[10];
46035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t index = 0;
46045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
46055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_NONE, "DS", "Details for descriptor set %#" PRIxLEAST64 ".", (uint64_t)pSet->set);
46065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        LAYOUT_NODE *pLayout = pSet->pLayout;
46075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Print layout details
46085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
46095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_NONE, "DS", "Layout #%u, (object %#" PRIxLEAST64 ") for DS %#" PRIxLEAST64 ".", index + 1,
46105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(pLayout->layout), (uint64_t)(pSet->set));
46115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        sprintf(prefix, "  [L%u] ", index);
46125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        string DSLstr = vk_print_vkdescriptorsetlayoutcreateinfo(&pLayout->createInfo, prefix).c_str();
46135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
46145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_NONE, "DS", "%s", DSLstr.c_str());
46155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        index++;
46165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        GENERIC_HEADER *pUpdate = pSet->pUpdateStructs;
46175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pUpdate) {
46185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
46195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
46205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_NONE, "DS", "Update Chain [UC] for descriptor set %#" PRIxLEAST64 ":", (uint64_t)pSet->set);
46215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            sprintf(prefix, "  [UC] ");
46225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
46235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                __LINE__, DRAWSTATE_NONE, "DS", "%s", dynamic_display(pUpdate, prefix).c_str());
46245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO : If there is a "view" associated with this update, print CI for that view
46255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
46265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (0 != pSet->descriptorCount) {
46275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
46285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
46295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_NONE, "DS", "No Update Chain for descriptor set %#" PRIxLEAST64
46305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  " which has %u descriptors (vkUpdateDescriptors has not been called)",
46315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)pSet->set, pSet->descriptorCount);
46325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
46335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
46345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
46355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_NONE, "DS", "FYI: No descriptors in descriptor set %#" PRIxLEAST64 ".", (uint64_t)pSet->set);
46365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
46375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
46385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
46395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
46405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
46415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
46425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void printCB(layer_data *my_data, const VkCommandBuffer cb) {
46435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(my_data, cb);
46445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB && pCB->cmds.size() > 0) {
46455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
46465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                DRAWSTATE_NONE, "DS", "Cmds in CB %p", (void *)cb);
46475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        vector<CMD_NODE> cmds = pCB->cmds;
46485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto ii = cmds.begin(); ii != cmds.end(); ++ii) {
46495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO : Need to pass cb as srcObj here
46505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
46515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    __LINE__, DRAWSTATE_NONE, "DS", "  CMD#%" PRIu64 ": %s", (*ii).cmdNumber, cmdTypeToString((*ii).type).c_str());
46525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
46535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
46545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Nothing to print
46555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
46565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
46575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
46585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 synchAndPrintDSConfig(layer_data *my_data, const VkCommandBuffer cb) {
46595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
46605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!(my_data->report_data->active_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)) {
46615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return skipCall;
46625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
46635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= printDSConfig(my_data, cb);
46645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= printPipeline(my_data, cb);
46655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
46665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
46675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
46685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Flags validation error if the associated call is made inside a render pass. The apiName
46695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// routine should ONLY be called outside a render pass.
46705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 insideRenderPass(const layer_data *my_data, GLOBAL_CB_NODE *pCB, const char *apiName) {
46715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 inside = VK_FALSE;
46725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB->activeRenderPass) {
46735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        inside = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
46745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                         (uint64_t)pCB->commandBuffer, __LINE__, DRAWSTATE_INVALID_RENDERPASS_CMD, "DS",
46755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                         "%s: It is invalid to issue this call inside an active render pass (%#" PRIxLEAST64 ")", apiName,
46765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                         (uint64_t)pCB->activeRenderPass);
46775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
46785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return inside;
46795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
46805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
46815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Flags validation error if the associated call is made outside a render pass. The apiName
46825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// routine should ONLY be called inside a render pass.
46835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 outsideRenderPass(const layer_data *my_data, GLOBAL_CB_NODE *pCB, const char *apiName) {
46845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 outside = VK_FALSE;
46855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (((pCB->createInfo.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) && (!pCB->activeRenderPass)) ||
46865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ((pCB->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) && (!pCB->activeRenderPass) &&
46875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         !(pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT))) {
46885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        outside = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
46895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                          (uint64_t)pCB->commandBuffer, __LINE__, DRAWSTATE_NO_ACTIVE_RENDERPASS, "DS",
46905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                          "%s: This call must be issued inside an active render pass.", apiName);
46915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
46925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return outside;
46935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
46945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
46955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void init_core_validation(layer_data *my_data, const VkAllocationCallbacks *pAllocator) {
4696d11c4ee2bd2a5672093d76630d895e824ee1a571Mark Lobodzinski
4697d11c4ee2bd2a5672093d76630d895e824ee1a571Mark Lobodzinski    layer_debug_actions(my_data->report_data, my_data->logging_callback, pAllocator, "lunarg_core_validation");
46985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
46995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!globalLockInitialized) {
47005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_create_mutex(&globalLock);
47015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        globalLockInitialized = 1;
47025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
47035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
47045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Zero out memory property data
47055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    memset(&memProps, 0, sizeof(VkPhysicalDeviceMemoryProperties));
47065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
47075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
47085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
47105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
47115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerInstanceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
47125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(chain_info->u.pLayerInfo);
47145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
47155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance");
47165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (fpCreateInstance == NULL)
47175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_INITIALIZATION_FAILED;
47185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Advance the link info for the next element on the chain
47205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
47215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
47235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (result != VK_SUCCESS)
47245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return result;
47255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
47275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
47285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_init_instance_dispatch_table(*pInstance, my_data->instance_dispatch_table, fpGetInstanceProcAddr);
47295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->report_data = debug_report_create_instance(my_data->instance_dispatch_table, *pInstance,
47315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                        pCreateInfo->enabledExtensionCount, pCreateInfo->ppEnabledExtensionNames);
47325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    init_core_validation(my_data, pAllocator);
47345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ValidateLayerOrdering(*pCreateInfo);
47365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
47385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
47395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis/* hook DestroyInstance to remove tableInstanceMap entry */
47415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) {
47425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODOSC : Shouldn't need any customization here
47435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dispatch_key key = get_dispatch_key(instance);
47445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TBD: Need any locking this early, in case this function is called at the
47455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // same time by more than one thread?
47465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(key, layer_data_map);
47475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
47485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pTable->DestroyInstance(instance, pAllocator);
47495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
47515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Clean up logging callback, if any
47525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while (my_data->logging_callback.size() > 0) {
47535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkDebugReportCallbackEXT callback = my_data->logging_callback.back();
47545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        layer_destroy_msg_callback(my_data->report_data, callback, pAllocator);
47555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->logging_callback.pop_back();
47565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
47575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_debug_report_destroy_instance(my_data->report_data);
47595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    delete my_data->instance_dispatch_table;
47605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data_map.erase(key);
47615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
47625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (layer_data_map.empty()) {
47635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Release mutex when destroying last instance.
47645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_delete_mutex(&globalLock);
47655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        globalLockInitialized = 0;
47665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
47675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
47685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void createDeviceRegisterExtensions(const VkDeviceCreateInfo *pCreateInfo, VkDevice device) {
47705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t i;
47715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TBD: Need any locking, in case this function is called at the same time
47725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // by more than one thread?
47735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
47745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->device_extensions.wsi_enabled = false;
47755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerDispatchTable *pDisp = dev_data->device_dispatch_table;
47775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr;
47785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR)gpa(device, "vkCreateSwapchainKHR");
47795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR)gpa(device, "vkDestroySwapchainKHR");
47805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR)gpa(device, "vkGetSwapchainImagesKHR");
47815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR)gpa(device, "vkAcquireNextImageKHR");
47825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR)gpa(device, "vkQueuePresentKHR");
47835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
47855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0)
47865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->device_extensions.wsi_enabled = true;
47875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
47885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
47895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
47915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                              const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
47925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerDeviceCreateInfo *chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
47935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
47945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    assert(chain_info->u.pLayerInfo);
47955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
47965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
47975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(NULL, "vkCreateDevice");
47985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (fpCreateDevice == NULL) {
47995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_INITIALIZATION_FAILED;
48005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
48015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
48025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Advance the link info for the next element on the chain
48035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
48045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
48055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = fpCreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
48065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (result != VK_SUCCESS) {
48075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return result;
48085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
48095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
48105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
48115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
48125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
48135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
48145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Setup device dispatch table
48155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_device_data->device_dispatch_table = new VkLayerDispatchTable;
48165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_init_device_dispatch_table(*pDevice, my_device_data->device_dispatch_table, fpGetDeviceProcAddr);
48175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
48185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
48195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    createDeviceRegisterExtensions(pCreateInfo, *pDevice);
48205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Get physical device limits for this device
48215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_instance_data->instance_dispatch_table->GetPhysicalDeviceProperties(gpu, &(my_device_data->physDevProperties.properties));
48225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t count;
48235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_instance_data->instance_dispatch_table->GetPhysicalDeviceQueueFamilyProperties(gpu, &count, nullptr);
48245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_device_data->physDevProperties.queue_family_properties.resize(count);
48255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_instance_data->instance_dispatch_table->GetPhysicalDeviceQueueFamilyProperties(
48265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        gpu, &count, &my_device_data->physDevProperties.queue_family_properties[0]);
48275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO: device limits should make sure these are compatible
48285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCreateInfo->pEnabledFeatures) {
48295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_device_data->physDevProperties.features = *pCreateInfo->pEnabledFeatures;
48305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
48315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(&my_device_data->physDevProperties.features, 0, sizeof(VkPhysicalDeviceFeatures));
48325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
48335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
48345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
48355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    ValidateLayerOrdering(*pCreateInfo);
48365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
48375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
48385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
48395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
48405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// prototype
48415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void deleteRenderPasses(layer_data *);
48425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
48435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODOSC : Shouldn't need any customization here
48445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dispatch_key key = get_dispatch_key(device);
48455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(key, layer_data_map);
48465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Free all the memory
48475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
48485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    deletePipelines(dev_data);
48495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    deleteRenderPasses(dev_data);
48505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    deleteCommandBuffers(dev_data);
48515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    deletePools(dev_data);
48525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    deleteLayouts(dev_data);
48535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->imageViewMap.clear();
48545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->imageMap.clear();
48555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->imageSubresourceMap.clear();
48565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->imageLayoutMap.clear();
48575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->bufferViewMap.clear();
48585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->bufferMap.clear();
48595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
48605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
48615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
48625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
48635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
48645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            (uint64_t)device, __LINE__, MEMTRACK_NONE, "MEM", "Printing List details prior to vkDestroyDevice()");
48655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
48665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            (uint64_t)device, __LINE__, MEMTRACK_NONE, "MEM", "================================================");
48675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    print_mem_list(dev_data, device);
48685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    printCBList(dev_data, device);
48695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = delete_cmd_buf_info_list(dev_data);
48705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Report any memory leaks
487158070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    DEVICE_MEM_INFO *pInfo = NULL;
48725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (dev_data->memObjMap.size() > 0) {
48735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto ii = dev_data->memObjMap.begin(); ii != dev_data->memObjMap.end(); ++ii) {
48745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pInfo = &(*ii).second;
48755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pInfo->allocInfo.allocationSize != 0) {
48765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Valid Usage: All child objects created on device must have been destroyed prior to destroying device
48775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
48785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
48795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)pInfo->mem, __LINE__, MEMTRACK_MEMORY_LEAK,
48805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "MEM", "Mem Object %" PRIu64 " has not been freed. You should clean up this memory by calling "
48815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   "vkFreeMemory(%" PRIu64 ") prior to vkDestroyDevice().",
48825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(pInfo->mem), (uint64_t)(pInfo->mem));
48835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
48845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
48855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
48865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Queues persist until device is destroyed
48875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    delete_queue_info_list(dev_data);
48885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_debug_report_destroy_device(device);
48895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
48905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
48915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if DISPATCH_MAP_DEBUG
48925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    fprintf(stderr, "Device: %p, key: %p\n", device, key);
48935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
48945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerDispatchTable *pDisp = dev_data->device_dispatch_table;
48955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
48965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pDisp->DestroyDevice(device, pAllocator);
48975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
48985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#else
48995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->device_dispatch_table->DestroyDevice(device, pAllocator);
49005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
49015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    delete dev_data->device_dispatch_table;
49025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data_map.erase(key);
49035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
49045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
49055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
49065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
49075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceMemoryProperties *pMemoryProperties) {
49085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);
49095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerInstanceDispatchTable *pInstanceTable = my_data->instance_dispatch_table;
49105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pInstanceTable->GetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties);
49115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    memcpy(&memProps, pMemoryProperties, sizeof(VkPhysicalDeviceMemoryProperties));
49125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
49135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
49145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
49155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic const VkExtensionProperties instance_extensions[] = {{VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION}};
49165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
49175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
49185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties *pProperties) {
49195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);
49205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
49215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
49225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
49235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties *pProperties) {
49245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return util_GetLayerProperties(ARRAY_SIZE(cv_global_layers), cv_global_layers, pCount, pProperties);
49255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
49265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
49275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// TODO: Why does this exist - can we just use global?
49285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic const VkLayerProperties cv_device_layers[] = {{
49295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    "VK_LAYER_LUNARG_core_validation", VK_API_VERSION, 1, "LunarG Validation Layer",
49305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}};
49315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
49325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
49335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                                    const char *pLayerName, uint32_t *pCount,
49345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                                    VkExtensionProperties *pProperties) {
49355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pLayerName == NULL) {
49365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dispatch_key key = get_dispatch_key(physicalDevice);
49375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        layer_data *my_data = get_my_data_ptr(key, layer_data_map);
49385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return my_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);
49395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
49405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return util_GetExtensionProperties(0, NULL, pCount, pProperties);
49415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
49425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
49435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
49445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
49455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount, VkLayerProperties *pProperties) {
49465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* draw_state physical device layers are the same as global */
49475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return util_GetLayerProperties(ARRAY_SIZE(cv_device_layers), cv_device_layers, pCount, pProperties);
49485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
49495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
49505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// This validates that the initial layout specified in the command buffer for
49515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// the IMAGE is the same
49525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// as the global IMAGE layout
49535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 ValidateCmdBufImageLayouts(VkCommandBuffer cmdBuffer) {
49545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
49555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
49565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
49575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto cb_image_data : pCB->imageLayoutMap) {
49585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkImageLayout imageLayout;
49595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!FindLayout(dev_data, cb_image_data.first, imageLayout)) {
49605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
49615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
49625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Cannot submit cmd buffer using deleted image %" PRIu64 ".",
49635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        reinterpret_cast<const uint64_t &>(cb_image_data.first));
49645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
49655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (cb_image_data.second.initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
49665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // TODO: Set memory invalid which is in mem_tracker currently
49675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else if (imageLayout != cb_image_data.second.initialLayout) {
49685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
49695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT,
49705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     "DS", "Cannot submit cmd buffer using image with layout %s when "
49715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           "first use is %s.",
49725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     string_VkImageLayout(imageLayout), string_VkImageLayout(cb_image_data.second.initialLayout));
49735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
49745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            SetLayout(dev_data, cb_image_data.first, cb_image_data.second.layout);
49755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
49765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
49775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
49785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
49795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Track which resources are in-flight by atomically incrementing their "in_use" count
49805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 validateAndIncrementResources(layer_data *my_data, GLOBAL_CB_NODE *pCB) {
49815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
49825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto drawDataElement : pCB->drawData) {
49835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto buffer : drawDataElement.buffers) {
49845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto buffer_data = my_data->bufferMap.find(buffer);
49855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (buffer_data == my_data->bufferMap.end()) {
49865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
49875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     (uint64_t)(buffer), __LINE__, DRAWSTATE_INVALID_BUFFER, "DS",
49885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     "Cannot submit cmd buffer using deleted buffer %" PRIu64 ".", (uint64_t)(buffer));
49895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
49905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                buffer_data->second.in_use.fetch_add(1);
49915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
49925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
49935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
49945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto set : pCB->uniqueBoundSets) {
49955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto setNode = my_data->setMap.find(set);
49965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (setNode == my_data->setMap.end()) {
49975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
49985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
49995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)(set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
50005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Cannot submit cmd buffer using deleted descriptor set %" PRIu64 ".", (uint64_t)(set));
50015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
50025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            setNode->second->in_use.fetch_add(1);
50035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
50045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
50055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto semaphore : pCB->semaphores) {
50065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto semaphoreNode = my_data->semaphoreMap.find(semaphore);
50075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (semaphoreNode == my_data->semaphoreMap.end()) {
50085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
50095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
50105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        reinterpret_cast<uint64_t &>(semaphore), __LINE__, DRAWSTATE_INVALID_SEMAPHORE, "DS",
50115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Cannot submit cmd buffer using deleted semaphore %" PRIu64 ".", reinterpret_cast<uint64_t &>(semaphore));
50125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
50135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            semaphoreNode->second.in_use.fetch_add(1);
50145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
50155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
50165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto event : pCB->events) {
50175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto eventNode = my_data->eventMap.find(event);
50185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (eventNode == my_data->eventMap.end()) {
50195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
50205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
50215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        reinterpret_cast<uint64_t &>(event), __LINE__, DRAWSTATE_INVALID_EVENT, "DS",
50225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Cannot submit cmd buffer using deleted event %" PRIu64 ".", reinterpret_cast<uint64_t &>(event));
50235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
50245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            eventNode->second.in_use.fetch_add(1);
50255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
50265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
50275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
50285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
50295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
50305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid decrementResources(layer_data *my_data, VkCommandBuffer cmdBuffer) {
50315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(my_data, cmdBuffer);
50325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto drawDataElement : pCB->drawData) {
50335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto buffer : drawDataElement.buffers) {
50345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto buffer_data = my_data->bufferMap.find(buffer);
50355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (buffer_data != my_data->bufferMap.end()) {
50365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                buffer_data->second.in_use.fetch_sub(1);
50375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
50385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
50395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
50405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto set : pCB->uniqueBoundSets) {
50415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto setNode = my_data->setMap.find(set);
50425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (setNode != my_data->setMap.end()) {
50435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            setNode->second->in_use.fetch_sub(1);
50445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
50455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
50465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto semaphore : pCB->semaphores) {
50475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto semaphoreNode = my_data->semaphoreMap.find(semaphore);
50485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (semaphoreNode != my_data->semaphoreMap.end()) {
50495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            semaphoreNode->second.in_use.fetch_sub(1);
50505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
50515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
50525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto event : pCB->events) {
50535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto eventNode = my_data->eventMap.find(event);
50545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (eventNode != my_data->eventMap.end()) {
50555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            eventNode->second.in_use.fetch_sub(1);
50565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
50575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
50585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto queryStatePair : pCB->queryToStateMap) {
50595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->queryToStateMap[queryStatePair.first] = queryStatePair.second;
50605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
50615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto eventStagePair : pCB->eventToStageMap) {
50625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->eventMap[eventStagePair.first].stageMask = eventStagePair.second;
50635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
50645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
50655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
50665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid decrementResources(layer_data *my_data, uint32_t fenceCount, const VkFence *pFences) {
50675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < fenceCount; ++i) {
50685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto fence_data = my_data->fenceMap.find(pFences[i]);
50695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (fence_data == my_data->fenceMap.end() || !fence_data->second.needsSignaled)
50705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return;
50715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        fence_data->second.needsSignaled = false;
50725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        fence_data->second.in_use.fetch_sub(1);
50730a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        decrementResources(my_data, fence_data->second.priorFences.size(), &fence_data->second.priorFences[0]);
50745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto cmdBuffer : fence_data->second.cmdBuffers) {
50755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            decrementResources(my_data, cmdBuffer);
50765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
50775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
50785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
50795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
50805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid decrementResources(layer_data *my_data, VkQueue queue) {
50815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto queue_data = my_data->queueMap.find(queue);
50825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (queue_data != my_data->queueMap.end()) {
50835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto cmdBuffer : queue_data->second.untrackedCmdBuffers) {
50845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            decrementResources(my_data, cmdBuffer);
50855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
50865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        queue_data->second.untrackedCmdBuffers.clear();
50870a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        decrementResources(my_data, queue_data->second.lastFences.size(), &queue_data->second.lastFences[0]);
50880a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    }
50890a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine}
50900a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine
50910a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentinevoid updateTrackedCommandBuffers(layer_data *dev_data, VkQueue queue, VkQueue other_queue, VkFence fence) {
50920a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    if (queue == other_queue) {
50930a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        return;
50940a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    }
50950a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    auto queue_data = dev_data->queueMap.find(queue);
50960a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    auto other_queue_data = dev_data->queueMap.find(other_queue);
50970a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    if (queue_data == dev_data->queueMap.end() || other_queue_data == dev_data->queueMap.end()) {
50980a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        return;
50990a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    }
51000a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    for (auto fence : other_queue_data->second.lastFences) {
51010a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        queue_data->second.lastFences.push_back(fence);
51020a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    }
51030a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    if (fence != VK_NULL_HANDLE) {
51040a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        auto fence_data = dev_data->fenceMap.find(fence);
51050a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        if (fence_data == dev_data->fenceMap.end()) {
51060a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            return;
51070a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        }
51080a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        for (auto cmdbuffer : other_queue_data->second.untrackedCmdBuffers) {
51090a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            fence_data->second.cmdBuffers.push_back(cmdbuffer);
51100a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        }
51110a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        other_queue_data->second.untrackedCmdBuffers.clear();
51120a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    } else {
51130a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        for (auto cmdbuffer : other_queue_data->second.untrackedCmdBuffers) {
51140a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            queue_data->second.untrackedCmdBuffers.push_back(cmdbuffer);
51150a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        }
51160a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        other_queue_data->second.untrackedCmdBuffers.clear();
51175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
51185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
51195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
51205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid trackCommandBuffers(layer_data *my_data, VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence) {
51215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto queue_data = my_data->queueMap.find(queue);
51225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (fence != VK_NULL_HANDLE) {
51230a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        vector<VkFence> prior_fences;
51245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto fence_data = my_data->fenceMap.find(fence);
51255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (fence_data == my_data->fenceMap.end()) {
51265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return;
51275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
51285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (queue_data != my_data->queueMap.end()) {
51290a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            prior_fences = queue_data->second.lastFences;
51300a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            queue_data->second.lastFences.clear();
51310a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            queue_data->second.lastFences.push_back(fence);
51320a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            for (auto cmdbuffer : queue_data->second.untrackedCmdBuffers) {
51330a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                fence_data->second.cmdBuffers.push_back(cmdbuffer);
51345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
51355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            queue_data->second.untrackedCmdBuffers.clear();
51365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
51375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        fence_data->second.cmdBuffers.clear();
51380a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        fence_data->second.priorFences = prior_fences;
51395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        fence_data->second.needsSignaled = true;
51405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        fence_data->second.queue = queue;
51415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        fence_data->second.in_use.fetch_add(1);
51425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
51435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            const VkSubmitInfo *submit = &pSubmits[submit_idx];
51445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t i = 0; i < submit->commandBufferCount; ++i) {
51455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto secondaryCmdBuffer : my_data->commandBufferMap[submit->pCommandBuffers[i]]->secondaryCommandBuffers) {
51465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    fence_data->second.cmdBuffers.push_back(secondaryCmdBuffer);
51475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
51485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                fence_data->second.cmdBuffers.push_back(submit->pCommandBuffers[i]);
51495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
51505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
51515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
51525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (queue_data != my_data->queueMap.end()) {
51535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
51545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                const VkSubmitInfo *submit = &pSubmits[submit_idx];
51555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (uint32_t i = 0; i < submit->commandBufferCount; ++i) {
51565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    for (auto secondaryCmdBuffer : my_data->commandBufferMap[submit->pCommandBuffers[i]]->secondaryCommandBuffers) {
51575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        queue_data->second.untrackedCmdBuffers.push_back(secondaryCmdBuffer);
51585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
51595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    queue_data->second.untrackedCmdBuffers.push_back(submit->pCommandBuffers[i]);
51605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
51615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
51625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
51635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
51645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (queue_data != my_data->queueMap.end()) {
51655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
51665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            const VkSubmitInfo *submit = &pSubmits[submit_idx];
51675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t i = 0; i < submit->commandBufferCount; ++i) {
51685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Add cmdBuffers to both the global set and queue set
51695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto secondaryCmdBuffer : my_data->commandBufferMap[submit->pCommandBuffers[i]]->secondaryCommandBuffers) {
51705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    my_data->globalInFlightCmdBuffers.insert(secondaryCmdBuffer);
51715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    queue_data->second.inFlightCmdBuffers.insert(secondaryCmdBuffer);
51725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
51735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                my_data->globalInFlightCmdBuffers.insert(submit->pCommandBuffers[i]);
51745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                queue_data->second.inFlightCmdBuffers.insert(submit->pCommandBuffers[i]);
51755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
51765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
51775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
51785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
51795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
51805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool validateCommandBufferSimultaneousUse(layer_data *dev_data, GLOBAL_CB_NODE *pCB) {
51815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skip_call = false;
51825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (dev_data->globalInFlightCmdBuffers.count(pCB->commandBuffer) &&
51835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        !(pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
51845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |=
51855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
51865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    __LINE__, DRAWSTATE_INVALID_FENCE, "DS", "Command Buffer %#" PRIx64 " is already in use and is not marked "
51875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                             "for simultaneous use.",
51885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    reinterpret_cast<uint64_t>(pCB->commandBuffer));
51895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
51905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
51915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
51925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
51935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool validateCommandBufferState(layer_data *dev_data, GLOBAL_CB_NODE *pCB) {
51945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
51955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate that cmd buffers have been updated
51965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (CB_RECORDED != pCB->state) {
51975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (CB_INVALID == pCB->state) {
51985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Inform app of reason CB invalid
51995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            bool causeReported = false;
52005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pCB->destroyedSets.empty()) {
52015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                std::stringstream set_string;
52025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto set : pCB->destroyedSets)
52035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    set_string << " " << set;
52045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
52055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
52065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
52075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
52085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "You are submitting command buffer %#" PRIxLEAST64
52095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            " that is invalid because it had the following bound descriptor set(s) destroyed: %s",
52105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(pCB->commandBuffer), set_string.str().c_str());
52115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                causeReported = true;
52125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
52135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pCB->updatedSets.empty()) {
52145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                std::stringstream set_string;
52155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto set : pCB->updatedSets)
52165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    set_string << " " << set;
52175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
52185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
52195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
52205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
52215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "You are submitting command buffer %#" PRIxLEAST64
52225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            " that is invalid because it had the following bound descriptor set(s) updated: %s",
52235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(pCB->commandBuffer), set_string.str().c_str());
52245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                causeReported = true;
52255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
52265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pCB->destroyedFramebuffers.empty()) {
52275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                std::stringstream fb_string;
52285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto fb : pCB->destroyedFramebuffers)
52295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    fb_string << " " << fb;
52305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
52315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
52325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
52335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
52345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "You are submitting command buffer %#" PRIxLEAST64 " that is invalid because it had the following "
52355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "referenced framebuffers destroyed: %s",
52365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            reinterpret_cast<uint64_t &>(pCB->commandBuffer), fb_string.str().c_str());
52375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                causeReported = true;
52385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
52395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO : This is defensive programming to make sure an error is
52405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            //  flagged if we hit this INVALID cmd buffer case and none of the
52415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            //  above cases are hit. As the number of INVALID cases grows, this
52425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            //  code should be updated to seemlessly handle all the cases.
52435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!causeReported) {
52445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |= log_msg(
52455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
52465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
52475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "You are submitting command buffer %#" PRIxLEAST64 " that is invalid due to an unknown cause. Validation "
52485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "should "
52495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "be improved to report the exact cause.",
52505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    reinterpret_cast<uint64_t &>(pCB->commandBuffer));
52515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
52525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else { // Flag error for using CB w/o vkEndCommandBuffer() called
52535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
52545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
52555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_NO_END_COMMAND_BUFFER, "DS",
52565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "You must call vkEndCommandBuffer() on CB %#" PRIxLEAST64 " before this call to vkQueueSubmit()!",
52575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)(pCB->commandBuffer));
52585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
52595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
52605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
52615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
52625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
52635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic VkBool32 validatePrimaryCommandBufferState(layer_data *dev_data, GLOBAL_CB_NODE *pCB) {
52645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Track in-use for resources off of primary and any secondary CBs
52655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = validateAndIncrementResources(dev_data, pCB);
52665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!pCB->secondaryCommandBuffers.empty()) {
52675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto secondaryCmdBuffer : pCB->secondaryCommandBuffers) {
52685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validateAndIncrementResources(dev_data, dev_data->commandBufferMap[secondaryCmdBuffer]);
52695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            GLOBAL_CB_NODE *pSubCB = getCBNode(dev_data, secondaryCmdBuffer);
52705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pSubCB->primaryCommandBuffer != pCB->commandBuffer) {
52715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
52725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        __LINE__, DRAWSTATE_COMMAND_BUFFER_SINGLE_SUBMIT_VIOLATION, "DS",
52735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "CB %#" PRIxLEAST64 " was submitted with secondary buffer %#" PRIxLEAST64
52745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        " but that buffer has subsequently been bound to "
52755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "primary cmd buffer %#" PRIxLEAST64 ".",
52765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        reinterpret_cast<uint64_t>(pCB->commandBuffer), reinterpret_cast<uint64_t>(secondaryCmdBuffer),
52775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        reinterpret_cast<uint64_t>(pSubCB->primaryCommandBuffer));
52785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
52795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
52805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
52815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Verify if this also needs to be checked for secondary command
52825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //  buffers. If so, this block of code can move to
52835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //   validateCommandBufferState() function. vulkan GL106 filed to clarify
52845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if ((pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) && (pCB->submitCount > 1)) {
52855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
52865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            __LINE__, DRAWSTATE_COMMAND_BUFFER_SINGLE_SUBMIT_VIOLATION, "DS",
52875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "CB %#" PRIxLEAST64 " was begun w/ VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT "
52885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "set, but has been submitted %#" PRIxLEAST64 " times.",
52895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(pCB->commandBuffer), pCB->submitCount);
52905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
52915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validateCommandBufferState(dev_data, pCB);
52925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // If USAGE_SIMULTANEOUS_USE_BIT not set then CB cannot already be executing
52935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // on device
52945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validateCommandBufferSimultaneousUse(dev_data, pCB);
52955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
52965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
52975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
52985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
52995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence) {
53005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
53015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = NULL;
53025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
53035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
53045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
53055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
53065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Need to track fence and clear mem references when fence clears
53075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : Merge this code with code below to avoid duplicating efforts
53085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    MT_CB_INFO *pCBInfo = NULL;
53095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint64_t fenceId = 0;
53105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = add_fence_info(dev_data, fence, queue, &fenceId);
53115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
53125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    print_mem_list(dev_data, queue);
53135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    printCBList(dev_data, queue);
53145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
53155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkSubmitInfo *submit = &pSubmits[submit_idx];
53165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < submit->commandBufferCount; i++) {
53175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCBInfo = get_cmd_buf_info(dev_data, submit->pCommandBuffers[i]);
53185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pCBInfo) {
53195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pCBInfo->fenceId = fenceId;
53205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pCBInfo->lastSubmittedFence = fence;
53215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pCBInfo->lastSubmittedQueue = queue;
53225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto &function : pCBInfo->validate_functions) {
53235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= function();
53245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
53255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
53265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
53275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
53285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < submit->waitSemaphoreCount; i++) {
53295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkSemaphore sem = submit->pWaitSemaphores[i];
53305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
53315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dev_data->semaphoreMap.find(sem) != dev_data->semaphoreMap.end()) {
53325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (dev_data->semaphoreMap[sem].state != MEMTRACK_SEMAPHORE_STATE_SIGNALLED) {
53335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall =
53345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
53355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)sem, __LINE__, MEMTRACK_NONE, "SEMAPHORE",
53365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkQueueSubmit: Semaphore must be in signaled state before passing to pWaitSemaphores");
53375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
53385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->semaphoreMap[sem].state = MEMTRACK_SEMAPHORE_STATE_WAIT;
53395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
53405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
53415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < submit->signalSemaphoreCount; i++) {
53425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkSemaphore sem = submit->pSignalSemaphores[i];
53435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
53445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dev_data->semaphoreMap.find(sem) != dev_data->semaphoreMap.end()) {
53455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (dev_data->semaphoreMap[sem].state != MEMTRACK_SEMAPHORE_STATE_UNSET) {
53465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
53475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                       VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT, (uint64_t)sem, __LINE__, MEMTRACK_NONE,
53485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                       "SEMAPHORE", "vkQueueSubmit: Semaphore must not be currently signaled or in a wait state");
53495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
53505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->semaphoreMap[sem].state = MEMTRACK_SEMAPHORE_STATE_SIGNALLED;
53515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
53525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
53535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
53545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
53555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // First verify that fence is not in use
53565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if ((fence != VK_NULL_HANDLE) && (submitCount != 0) && dev_data->fenceMap[fence].in_use.load()) {
53575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
53585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(fence), __LINE__, DRAWSTATE_INVALID_FENCE, "DS",
53595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Fence %#" PRIx64 " is already in use by another submission.", (uint64_t)(fence));
53605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
53615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Now verify each individual submit
53620a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine    std::unordered_set<VkQueue> processed_other_queues;
53635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
53645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkSubmitInfo *submit = &pSubmits[submit_idx];
53655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        vector<VkSemaphore> semaphoreList;
53665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < submit->waitSemaphoreCount; ++i) {
53670a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            const VkSemaphore &semaphore = submit->pWaitSemaphores[i];
53680a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            semaphoreList.push_back(semaphore);
53690a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            if (dev_data->semaphoreMap[semaphore].signaled) {
53700a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                dev_data->semaphoreMap[semaphore].signaled = 0;
53715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
53725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
53735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS,
53745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "DS", "Queue %#" PRIx64 " is waiting on semaphore %#" PRIx64 " that has no way to be signaled.",
53750a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                                    reinterpret_cast<uint64_t &>(queue), reinterpret_cast<const uint64_t &>(semaphore));
53760a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            }
53770a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            const VkQueue &other_queue = dev_data->semaphoreMap[semaphore].queue;
53780a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            if (other_queue != VK_NULL_HANDLE && !processed_other_queues.count(other_queue)) {
53790a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                updateTrackedCommandBuffers(dev_data, queue, other_queue, fence);
53800a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                processed_other_queues.insert(other_queue);
53815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
53825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
53835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < submit->signalSemaphoreCount; ++i) {
53840a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            const VkSemaphore &semaphore = submit->pSignalSemaphores[i];
53850a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            semaphoreList.push_back(semaphore);
53860a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            if (dev_data->semaphoreMap[semaphore].signaled) {
53870a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
53880a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                                    VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS,
53890a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                                    "DS", "Queue %#" PRIx64 " is signaling semaphore %#" PRIx64
53900a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                                          " that has already been signaled but not waited on by queue %#" PRIx64 ".",
53910a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                                    reinterpret_cast<uint64_t &>(queue), reinterpret_cast<const uint64_t &>(semaphore),
53920a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                                    reinterpret_cast<uint64_t &>(dev_data->semaphoreMap[semaphore].queue));
53930a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            } else {
53940a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                dev_data->semaphoreMap[semaphore].signaled = 1;
53950a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine                dev_data->semaphoreMap[semaphore].queue = queue;
53960a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine            }
53975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
53985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < submit->commandBufferCount; i++) {
53995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= ValidateCmdBufImageLayouts(submit->pCommandBuffers[i]);
54005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB = getCBNode(dev_data, submit->pCommandBuffers[i]);
54015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->semaphores = semaphoreList;
54025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->submitCount++; // increment submit count
54035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validatePrimaryCommandBufferState(dev_data, pCB);
54045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
54055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
54065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Update cmdBuffer-related data structs and mark fence in-use
54075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    trackCommandBuffers(dev_data, queue, submitCount, pSubmits, fence);
54085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
54095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
54105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->QueueSubmit(queue, submitCount, pSubmits, fence);
54115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
54125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
54135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
54145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkSubmitInfo *submit = &pSubmits[submit_idx];
54155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < submit->waitSemaphoreCount; i++) {
54165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkSemaphore sem = submit->pWaitSemaphores[i];
54175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
54185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dev_data->semaphoreMap.find(sem) != dev_data->semaphoreMap.end()) {
54195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->semaphoreMap[sem].state = MEMTRACK_SEMAPHORE_STATE_UNSET;
54205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
54215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
54225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
54235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
54245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
54255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
54265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
54275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
54285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
54295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkAllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo,
54305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMemory) {
54315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
54325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = my_data->device_dispatch_table->AllocateMemory(device, pAllocateInfo, pAllocator, pMemory);
54335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Track allocations and overall size here
54345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
54355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    add_mem_obj_info(my_data, device, *pMemory, pAllocateInfo);
54365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    print_mem_list(my_data, device);
54375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
54385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
54395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
54405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
54415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
54425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkFreeMemory(VkDevice device, VkDeviceMemory mem, const VkAllocationCallbacks *pAllocator) {
54435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
54445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
54455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // From spec : A memory object is freed by calling vkFreeMemory() when it is no longer needed.
54465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Before freeing a memory object, an application must ensure the memory object is no longer
54475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // in use by the device—for example by command buffers queued for execution. The memory need
54485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // not yet be unbound from all images and buffers, but any further use of those images or
54495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // buffers (on host or device) for anything other than destroying those objects will result in
54505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // undefined behavior.
54515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
545258070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
54535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    freeMemObjInfo(my_data, device, mem, VK_FALSE);
54545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    print_mem_list(my_data, device);
54555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    printCBList(my_data, device);
54565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
54575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->device_dispatch_table->FreeMemory(device, mem, pAllocator);
54585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
54595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
54605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 validateMemRange(layer_data *my_data, VkDeviceMemory mem, VkDeviceSize offset, VkDeviceSize size) {
54615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
54625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
54635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (size == 0) {
54645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO: a size of 0 is not listed as an invalid use in the spec, should it be?
54655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
54665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MAP, "MEM",
54675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           "VkMapMemory: Attempting to map memory range of size zero");
54685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
54695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
54705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto mem_element = my_data->memObjMap.find(mem);
54715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (mem_element != my_data->memObjMap.end()) {
54725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // It is an application error to call VkMapMemory on an object that is already mapped
54735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (mem_element->second.memRange.size != 0) {
54745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
54755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MAP, "MEM",
54765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               "VkMapMemory: Attempting to map memory on an already-mapped object %#" PRIxLEAST64, (uint64_t)mem);
54775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
54785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
54795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Validate that offset + size is within object's allocationSize
54805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (size == VK_WHOLE_SIZE) {
54815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (offset >= mem_element->second.allocInfo.allocationSize) {
54825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
54835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MAP,
54845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   "MEM", "Mapping Memory from %" PRIu64 " to %" PRIu64 " with total array size %" PRIu64, offset,
54855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   mem_element->second.allocInfo.allocationSize, mem_element->second.allocInfo.allocationSize);
54865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
54875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
54885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if ((offset + size) > mem_element->second.allocInfo.allocationSize) {
54895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
54905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MAP,
54915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   "MEM", "Mapping Memory from %" PRIu64 " to %" PRIu64 " with total array size %" PRIu64, offset,
54925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   size + offset, mem_element->second.allocInfo.allocationSize);
54935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
54945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
54955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
54965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
54975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
54985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
54995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid storeMemRanges(layer_data *my_data, VkDeviceMemory mem, VkDeviceSize offset, VkDeviceSize size) {
55005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto mem_element = my_data->memObjMap.find(mem);
55015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (mem_element != my_data->memObjMap.end()) {
55025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        MemRange new_range;
55035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        new_range.offset = offset;
55045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        new_range.size = size;
55055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        mem_element->second.memRange = new_range;
55065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
55075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
55085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
55095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 deleteMemRanges(layer_data *my_data, VkDeviceMemory mem) {
55105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
55115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto mem_element = my_data->memObjMap.find(mem);
55125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (mem_element != my_data->memObjMap.end()) {
55135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!mem_element->second.memRange.size) {
55145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Valid Usage: memory must currently be mapped
55155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall = log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
55165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               (uint64_t)mem, __LINE__, MEMTRACK_INVALID_MAP, "MEM",
55175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               "Unmapping Memory without memory being mapped: mem obj %#" PRIxLEAST64, (uint64_t)mem);
55185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
55195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        mem_element->second.memRange.size = 0;
55205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (mem_element->second.pData) {
55215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            free(mem_element->second.pData);
55225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            mem_element->second.pData = 0;
55235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
55245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
55255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
55265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
55275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
55285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic char NoncoherentMemoryFillValue = 0xb;
55295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
55305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid initializeAndTrackMemory(layer_data *my_data, VkDeviceMemory mem, VkDeviceSize size, void **ppData) {
55315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto mem_element = my_data->memObjMap.find(mem);
55325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (mem_element != my_data->memObjMap.end()) {
55335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        mem_element->second.pDriverData = *ppData;
55345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t index = mem_element->second.allocInfo.memoryTypeIndex;
55355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (memProps.memoryTypes[index].propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
55365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            mem_element->second.pData = 0;
55375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
55385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (size == VK_WHOLE_SIZE) {
55395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                size = mem_element->second.allocInfo.allocationSize;
55405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
55415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            size_t convSize = (size_t)(size);
55425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            mem_element->second.pData = malloc(2 * convSize);
55435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memset(mem_element->second.pData, NoncoherentMemoryFillValue, 2 * convSize);
55445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            *ppData = static_cast<char *>(mem_element->second.pData) + (convSize / 2);
55455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
55465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
55475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
55485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
55495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Note: This function assumes that the global lock is held by the calling
55505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// thread.
55515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 cleanInFlightCmdBuffer(layer_data *my_data, VkCommandBuffer cmdBuffer) {
55525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
55535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(my_data, cmdBuffer);
55545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
55555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto queryEventsPair : pCB->waitedEventsBeforeQueryReset) {
55565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (auto event : queryEventsPair.second) {
55575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (my_data->eventMap[event].needsSignaled) {
55585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
55595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, 0, DRAWSTATE_INVALID_QUERY, "DS",
55605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         "Cannot get query results on queryPool %" PRIu64
55615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         " with index %d which was guarded by unsignaled event %" PRIu64 ".",
55625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         (uint64_t)(queryEventsPair.first.pool), queryEventsPair.first.index, (uint64_t)(event));
55635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
55645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
55655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
55665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
55675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
55685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
55695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Remove given cmd_buffer from the global inFlight set.
55705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  Also, if given queue is valid, then remove the cmd_buffer from that queues
55715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  inFlightCmdBuffer set. Finally, check all other queues and if given cmd_buffer
55725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//  is still in flight on another queue, add it back into the global set.
55735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Note: This function assumes that the global lock is held by the calling
55745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// thread.
55755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic inline void removeInFlightCmdBuffer(layer_data *dev_data, VkCommandBuffer cmd_buffer, VkQueue queue) {
55765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Pull it off of global list initially, but if we find it in any other queue list, add it back in
55775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->globalInFlightCmdBuffers.erase(cmd_buffer);
55785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (dev_data->queueMap.find(queue) != dev_data->queueMap.end()) {
55795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->queueMap[queue].inFlightCmdBuffers.erase(cmd_buffer);
55805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto q : dev_data->queues) {
55815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if ((q != queue) &&
55825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                (dev_data->queueMap[q].inFlightCmdBuffers.find(cmd_buffer) != dev_data->queueMap[q].inFlightCmdBuffers.end())) {
55835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->globalInFlightCmdBuffers.insert(cmd_buffer);
55845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
55855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
55865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
55875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
55885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
55895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
55905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic inline bool verifyFenceStatus(VkDevice device, VkFence fence, const char *apiCall) {
55915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
55925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = false;
55935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto pFenceInfo = my_data->fenceMap.find(fence);
55945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pFenceInfo != my_data->fenceMap.end()) {
55955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pFenceInfo->second.firstTimeFlag != VK_TRUE) {
55965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if ((pFenceInfo->second.createInfo.flags & VK_FENCE_CREATE_SIGNALED_BIT) &&
55975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pFenceInfo->second.firstTimeFlag != VK_TRUE) {
55985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
55995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(my_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
56005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)fence, __LINE__, MEMTRACK_INVALID_FENCE_STATE, "MEM",
56015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "%s specified fence %#" PRIxLEAST64 " already in SIGNALED state.", apiCall, (uint64_t)fence);
56025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
56035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pFenceInfo->second.queue && !pFenceInfo->second.swapchain) { // Checking status of unsubmitted fence
56045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
56055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    reinterpret_cast<uint64_t &>(fence), __LINE__, MEMTRACK_INVALID_FENCE_STATE, "MEM",
56065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "%s called for fence %#" PRIxLEAST64 " which has not been submitted on a Queue or during "
56075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "acquire next image.",
56085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    apiCall, reinterpret_cast<uint64_t &>(fence));
56095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
56105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
56115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pFenceInfo->second.firstTimeFlag = VK_FALSE;
56125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
56135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
56145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
56155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
56165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
56175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
56185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkWaitForFences(VkDevice device, uint32_t fenceCount, const VkFence *pFences, VkBool32 waitAll, uint64_t timeout) {
56195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
56205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
56215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
56225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Verify fence status of submitted fences
56235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
56245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < fenceCount; i++) {
56255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= verifyFenceStatus(device, pFences[i], "vkWaitForFences");
56265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
56275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
56285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (skip_call)
56295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
56305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
56315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->WaitForFences(device, fenceCount, pFences, waitAll, timeout);
56325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
56335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (result == VK_SUCCESS) {
56345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
56355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // When we know that all fences are complete we can clean/remove their CBs
56365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (waitAll || fenceCount == 1) {
56375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t i = 0; i < fenceCount; ++i) {
56385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
56395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                update_fence_tracking(dev_data, pFences[i]);
56405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
56415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkQueue fence_queue = dev_data->fenceMap[pFences[i]].queue;
56425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto cmdBuffer : dev_data->fenceMap[pFences[i]].cmdBuffers) {
56435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
56445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    removeInFlightCmdBuffer(dev_data, cmdBuffer, fence_queue);
56455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
56465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
56475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            decrementResources(dev_data, fenceCount, pFences);
56485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
56495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // NOTE : Alternate case not handled here is when some fences have completed. In
56505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        //  this case for app to guarantee which fences completed it will have to call
56515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        //  vkGetFenceStatus() at which point we'll clean/remove their CBs if complete.
56525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
56535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
56545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE != skip_call)
56555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
56565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
56575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
56585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
56595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkGetFenceStatus(VkDevice device, VkFence fence) {
56605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
56615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
56625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
56635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
56645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
56655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = verifyFenceStatus(device, fence, "vkGetFenceStatus");
56665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
56675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (skipCall)
56685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return result;
56695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
56705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    result = dev_data->device_dispatch_table->GetFenceStatus(device, fence);
56715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
56725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
56735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (result == VK_SUCCESS) {
56745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
56755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        update_fence_tracking(dev_data, fence);
56765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
56775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto fence_queue = dev_data->fenceMap[fence].queue;
56785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto cmdBuffer : dev_data->fenceMap[fence].cmdBuffers) {
56795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
56805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            removeInFlightCmdBuffer(dev_data, cmdBuffer, fence_queue);
56815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
56825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        decrementResources(dev_data, 1, &fence);
56835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
56845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
56855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE != skip_call)
56865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
56875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
56885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
56895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
56905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
56915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkGetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue *pQueue) {
56925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
56935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->device_dispatch_table->GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
56945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
56955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->queues.push_back(*pQueue);
56965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    QUEUE_NODE *pQNode = &dev_data->queueMap[*pQueue];
56975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pQNode->device = device;
56985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
56995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pQNode->lastRetiredId = 0;
57005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pQNode->lastSubmittedId = 0;
57015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
57025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
57035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
57045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
57055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueueWaitIdle(VkQueue queue) {
57065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
57075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    decrementResources(dev_data, queue);
57085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
57095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
57105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Iterate over local set since we erase set members as we go in for loop
57115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto local_cb_set = dev_data->queueMap[queue].inFlightCmdBuffers;
57125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto cmdBuffer : local_cb_set) {
57135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
57145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        removeInFlightCmdBuffer(dev_data, cmdBuffer, queue);
57155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
57165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->queueMap[queue].inFlightCmdBuffers.clear();
57175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
57185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE != skip_call)
57195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
57205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->QueueWaitIdle(queue);
57215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
57225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
57235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
57245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        retire_queue_fences(dev_data, queue);
57255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
57265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
57275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
57285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
57295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
57305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
57315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkDeviceWaitIdle(VkDevice device) {
57325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
57335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
57345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
57355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto queue : dev_data->queues) {
57365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        decrementResources(dev_data, queue);
57375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (dev_data->queueMap.find(queue) != dev_data->queueMap.end()) {
57385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Clear all of the queue inFlightCmdBuffers (global set cleared below)
57395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->queueMap[queue].inFlightCmdBuffers.clear();
57405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
57415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
57425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto cmdBuffer : dev_data->globalInFlightCmdBuffers) {
57435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= cleanInFlightCmdBuffer(dev_data, cmdBuffer);
57445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
57455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->globalInFlightCmdBuffers.clear();
57465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
57475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE != skip_call)
57485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
57495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->DeviceWaitIdle(device);
57505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
57515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
57525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
57535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        retire_device_fences(dev_data, device);
57545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
57555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
57565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
57575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
57585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
57595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
57605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyFence(VkDevice device, VkFence fence, const VkAllocationCallbacks *pAllocator) {
57615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
57625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
57635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
57645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (dev_data->fenceMap[fence].in_use.load()) {
57655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
57665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(fence), __LINE__, DRAWSTATE_INVALID_FENCE, "DS",
57675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Fence %#" PRIx64 " is in use by a command buffer.", (uint64_t)(fence));
57685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
57695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
57705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    delete_fence_info(dev_data, fence);
57715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto item = dev_data->fenceMap.find(fence);
57725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (item != dev_data->fenceMap.end()) {
57735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->fenceMap.erase(item);
57745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
57755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
57765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
57775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skipCall)
57785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->DestroyFence(device, fence, pAllocator);
57795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
57805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
57815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
57825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroySemaphore(VkDevice device, VkSemaphore semaphore, const VkAllocationCallbacks *pAllocator) {
57835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
57845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->device_dispatch_table->DestroySemaphore(device, semaphore, pAllocator);
57855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
57865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto item = dev_data->semaphoreMap.find(semaphore);
57875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (item != dev_data->semaphoreMap.end()) {
57885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (item->second.in_use.load()) {
57895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
57905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    reinterpret_cast<uint64_t &>(semaphore), __LINE__, DRAWSTATE_INVALID_SEMAPHORE, "DS",
57915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "Cannot delete semaphore %" PRIx64 " which is in use.", reinterpret_cast<uint64_t &>(semaphore));
57925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
57935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->semaphoreMap.erase(semaphore);
57945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
57955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
57965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Clean up any internal data structures using this obj.
57975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
57985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
57995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
58005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
58015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skip_call = false;
58025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
58035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto event_data = dev_data->eventMap.find(event);
58045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (event_data != dev_data->eventMap.end()) {
58055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (event_data->second.in_use.load()) {
58065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(
58075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
58085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                reinterpret_cast<uint64_t &>(event), __LINE__, DRAWSTATE_INVALID_EVENT, "DS",
58095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                "Cannot delete event %" PRIx64 " which is in use by a command buffer.", reinterpret_cast<uint64_t &>(event));
58105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
58115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->eventMap.erase(event_data);
58125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
58135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
58145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skip_call)
58155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->DestroyEvent(device, event, pAllocator);
58165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Clean up any internal data structures using this obj.
58175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
58185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
58195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
58205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyQueryPool(VkDevice device, VkQueryPool queryPool, const VkAllocationCallbacks *pAllocator) {
58215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    get_my_data_ptr(get_dispatch_key(device), layer_data_map)
58225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ->device_dispatch_table->DestroyQueryPool(device, queryPool, pAllocator);
58235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Clean up any internal data structures using this obj.
58245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
58255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
58265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL vkGetQueryPoolResults(VkDevice device, VkQueryPool queryPool, uint32_t firstQuery,
58275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     uint32_t queryCount, size_t dataSize, void *pData, VkDeviceSize stride,
58285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     VkQueryResultFlags flags) {
58295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
58305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_map<QueryObject, vector<VkCommandBuffer>> queriesInFlight;
58315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = nullptr;
58325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
58335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto cmdBuffer : dev_data->globalInFlightCmdBuffers) {
58345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB = getCBNode(dev_data, cmdBuffer);
58355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto queryStatePair : pCB->queryToStateMap) {
58365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            queriesInFlight[queryStatePair.first].push_back(cmdBuffer);
58375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
58385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
58395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
58405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < queryCount; ++i) {
58415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        QueryObject query = {queryPool, firstQuery + i};
58425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto queryElement = queriesInFlight.find(query);
58435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto queryToStateElement = dev_data->queryToStateMap.find(query);
58445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (queryToStateElement != dev_data->queryToStateMap.end()) {
58455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
58465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Available and in flight
58475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (queryElement != queriesInFlight.end() && queryToStateElement != dev_data->queryToStateMap.end() &&
58485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            queryToStateElement->second) {
58495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (auto cmdBuffer : queryElement->second) {
58505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pCB = getCBNode(dev_data, cmdBuffer);
58515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto queryEventElement = pCB->waitedEventsBeforeQueryReset.find(query);
58525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (queryEventElement == pCB->waitedEventsBeforeQueryReset.end()) {
58535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
58545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
58555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         "Cannot get query results on queryPool %" PRIu64 " with index %d which is in flight.",
58565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         (uint64_t)(queryPool), firstQuery + i);
58575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else {
58585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    for (auto event : queryEventElement->second) {
58595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        dev_data->eventMap[event].needsSignaled = true;
58605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
58615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
58625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
58635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Unavailable and in flight
58645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (queryElement != queriesInFlight.end() && queryToStateElement != dev_data->queryToStateMap.end() &&
58655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   !queryToStateElement->second) {
58665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO : Can there be the same query in use by multiple command buffers in flight?
58675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            bool make_available = false;
58685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (auto cmdBuffer : queryElement->second) {
58695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pCB = getCBNode(dev_data, cmdBuffer);
58705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                make_available |= pCB->queryToStateMap[query];
58715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
58725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!(((flags & VK_QUERY_RESULT_PARTIAL_BIT) || (flags & VK_QUERY_RESULT_WAIT_BIT)) && make_available)) {
58735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
58745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT, 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
58755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     "Cannot get query results on queryPool %" PRIu64 " with index %d which is unavailable.",
58765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     (uint64_t)(queryPool), firstQuery + i);
58775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
58785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Unavailable
58795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (queryToStateElement != dev_data->queryToStateMap.end() && !queryToStateElement->second) {
58805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT,
58815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
58825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 "Cannot get query results on queryPool %" PRIu64 " with index %d which is unavailable.",
58835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 (uint64_t)(queryPool), firstQuery + i);
58845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Unitialized
58855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (queryToStateElement == dev_data->queryToStateMap.end()) {
58865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT,
58875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 0, __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
58885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 "Cannot get query results on queryPool %" PRIu64 " with index %d which is uninitialized.",
58895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 (uint64_t)(queryPool), firstQuery + i);
58905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
58915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
58925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
58935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (skip_call)
58945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
58955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return dev_data->device_dispatch_table->GetQueryPoolResults(device, queryPool, firstQuery, queryCount, dataSize, pData, stride,
58965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                flags);
58975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
58985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
58995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 validateIdleBuffer(const layer_data *my_data, VkBuffer buffer) {
59005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
59015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto buffer_data = my_data->bufferMap.find(buffer);
59025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (buffer_data == my_data->bufferMap.end()) {
59035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
59045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             (uint64_t)(buffer), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
59055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             "Cannot free buffer %" PRIxLEAST64 " that has not been allocated.", (uint64_t)(buffer));
59065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
59075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (buffer_data->second.in_use.load()) {
59085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
59095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 (uint64_t)(buffer), __LINE__, DRAWSTATE_OBJECT_INUSE, "DS",
59105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 "Cannot free buffer %" PRIxLEAST64 " that is in use by a command buffer.", (uint64_t)(buffer));
59115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
59125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
59135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
59145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
59155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
59165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
59175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) {
59185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
59195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
59205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
59215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
59225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto item = dev_data->bufferBindingMap.find((uint64_t)buffer);
59235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (item != dev_data->bufferBindingMap.end()) {
59245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = clear_object_binding(dev_data, device, (uint64_t)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT);
59255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->bufferBindingMap.erase(item);
59265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
59275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
59285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!validateIdleBuffer(dev_data, buffer) && (VK_FALSE == skipCall)) {
59295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
59305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->DestroyBuffer(device, buffer, pAllocator);
59315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
59325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
59335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->bufferMap.erase(buffer);
59345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
59355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
59365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
59375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
59385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyBufferView(VkDevice device, VkBufferView bufferView, const VkAllocationCallbacks *pAllocator) {
59395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
59405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->device_dispatch_table->DestroyBufferView(device, bufferView, pAllocator);
59415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
59425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto item = dev_data->bufferViewMap.find(bufferView);
59435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (item != dev_data->bufferViewMap.end()) {
59445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->bufferViewMap.erase(item);
59455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
59465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
59475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
59485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
59495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
59505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
59515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
59525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
59535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
59545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto item = dev_data->imageBindingMap.find((uint64_t)image);
59555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (item != dev_data->imageBindingMap.end()) {
59565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = clear_object_binding(dev_data, device, (uint64_t)image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT);
59575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->imageBindingMap.erase(item);
59585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
59595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
59605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
59615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
59625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->DestroyImage(device, image, pAllocator);
59635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
59645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
59655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const auto& entry = dev_data->imageMap.find(image);
59665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (entry != dev_data->imageMap.end()) {
596758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        // Clear any memory mapping for this image
596858070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        const auto &mem_entry = dev_data->memObjMap.find(entry->second.mem);
596958070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        if (mem_entry != dev_data->memObjMap.end())
597058070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            mem_entry->second.image = VK_NULL_HANDLE;
597158070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis
597258070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        // Remove image from imageMap
59735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->imageMap.erase(entry);
59745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
59755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const auto& subEntry = dev_data->imageSubresourceMap.find(image);
59765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (subEntry != dev_data->imageSubresourceMap.end()) {
59775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (const auto& pair : subEntry->second) {
59785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->imageLayoutMap.erase(pair);
59795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
59805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->imageSubresourceMap.erase(subEntry);
59815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
59825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
59835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
59845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
598558070a671b39e2058a799c7b9c932f3f03f2e66dTobin EhlisVkBool32 print_memory_range_error(layer_data *dev_data, const uint64_t object_handle, const uint64_t other_handle,
59865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  VkDebugReportObjectTypeEXT object_type) {
59875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (object_type == VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT) {
598858070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, object_type, object_handle, 0,
59895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       MEMTRACK_INVALID_ALIASING, "MEM", "Buffer %" PRIx64 " is alised with image %" PRIx64, object_handle,
59905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       other_handle);
59915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
599258070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, object_type, object_handle, 0,
59935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       MEMTRACK_INVALID_ALIASING, "MEM", "Image %" PRIx64 " is alised with buffer %" PRIx64, object_handle,
59945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       other_handle);
59955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
59965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
59975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
599858070a671b39e2058a799c7b9c932f3f03f2e66dTobin EhlisVkBool32 validate_memory_range(layer_data *dev_data, const vector<MEMORY_RANGE> &ranges, const MEMORY_RANGE &new_range,
599958070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                               VkDebugReportObjectTypeEXT object_type) {
60005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = false;
600158070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis
60025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto range : ranges) {
600358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        if ((range.end & ~(dev_data->physDevProperties.properties.limits.bufferImageGranularity - 1)) <
600458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            (new_range.start & ~(dev_data->physDevProperties.properties.limits.bufferImageGranularity - 1)))
60055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
600658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        if ((range.start & ~(dev_data->physDevProperties.properties.limits.bufferImageGranularity - 1)) >
600758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis            (new_range.end & ~(dev_data->physDevProperties.properties.limits.bufferImageGranularity - 1)))
60085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
600958070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        skip_call |= print_memory_range_error(dev_data, new_range.handle, range.handle, object_type);
60105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
60115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
60125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
60135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
601458070a671b39e2058a799c7b9c932f3f03f2e66dTobin EhlisVkBool32 validate_buffer_image_aliasing(layer_data *dev_data, uint64_t handle, VkDeviceMemory mem, VkDeviceSize memoryOffset,
601558070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                                        VkMemoryRequirements memRequirements, vector<MEMORY_RANGE> &ranges,
601658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                                        const vector<MEMORY_RANGE> &other_ranges, VkDebugReportObjectTypeEXT object_type) {
60175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    MEMORY_RANGE range;
60185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    range.handle = handle;
60195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    range.memory = mem;
60205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    range.start = memoryOffset;
60215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    range.end = memoryOffset + memRequirements.size - 1;
602258070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    ranges.push_back(range);
602358070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    return validate_memory_range(dev_data, other_ranges, range, object_type);
60245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
60255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
60265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
60275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory mem, VkDeviceSize memoryOffset) {
602858070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
60295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
60305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
60315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Track objects tied to memory
60325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint64_t buffer_handle = (uint64_t)(buffer);
60335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall =
603458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        set_mem_binding(dev_data, device, mem, buffer_handle, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, "vkBindBufferMemory");
603558070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    add_object_binding_info(dev_data, buffer_handle, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, mem);
60365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    {
60375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkMemoryRequirements memRequirements;
60385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // MTMTODO : Shouldn't this call down the chain?
60395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
604058070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        skipCall |= validate_buffer_image_aliasing(dev_data, buffer_handle, mem, memoryOffset, memRequirements,
604158070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                                                   dev_data->memObjMap[mem].bufferRanges, dev_data->memObjMap[mem].imageRanges,
604258070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                                                   VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT);
60435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
604458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    print_mem_list(dev_data, device);
60455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
60465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
604758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        result = dev_data->device_dispatch_table->BindBufferMemory(device, buffer, mem, memoryOffset);
60485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
60495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
60505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
60515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
60525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
60535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements) {
60545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
60555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : What to track here?
60565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //   Could potentially save returned mem requirements and validate values passed into BindBufferMemory
60575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->device_dispatch_table->GetBufferMemoryRequirements(device, buffer, pMemoryRequirements);
60585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
60595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
60605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
60615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkGetImageMemoryRequirements(VkDevice device, VkImage image, VkMemoryRequirements *pMemoryRequirements) {
60625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
60635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : What to track here?
60645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //   Could potentially save returned mem requirements and validate values passed into BindImageMemory
60655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->device_dispatch_table->GetImageMemoryRequirements(device, image, pMemoryRequirements);
60665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
60675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
60685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
60695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyImageView(VkDevice device, VkImageView imageView, const VkAllocationCallbacks *pAllocator) {
60705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    get_my_data_ptr(get_dispatch_key(device), layer_data_map)
60715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ->device_dispatch_table->DestroyImageView(device, imageView, pAllocator);
60725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Clean up any internal data structures using this obj.
60735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
60745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
60755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
60765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyShaderModule(VkDevice device, VkShaderModule shaderModule, const VkAllocationCallbacks *pAllocator) {
60775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    get_my_data_ptr(get_dispatch_key(device), layer_data_map)
60785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ->device_dispatch_table->DestroyShaderModule(device, shaderModule, pAllocator);
60795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Clean up any internal data structures using this obj.
60805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
60815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
60825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
60835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) {
60845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroyPipeline(device, pipeline, pAllocator);
60855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Clean up any internal data structures using this obj.
60865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
60875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
60885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
60895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyPipelineLayout(VkDevice device, VkPipelineLayout pipelineLayout, const VkAllocationCallbacks *pAllocator) {
60905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    get_my_data_ptr(get_dispatch_key(device), layer_data_map)
60915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ->device_dispatch_table->DestroyPipelineLayout(device, pipelineLayout, pAllocator);
60925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Clean up any internal data structures using this obj.
60935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
60945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
60955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
60965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroySampler(VkDevice device, VkSampler sampler, const VkAllocationCallbacks *pAllocator) {
60975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    get_my_data_ptr(get_dispatch_key(device), layer_data_map)->device_dispatch_table->DestroySampler(device, sampler, pAllocator);
60985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Clean up any internal data structures using this obj.
60995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
61005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
61025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyDescriptorSetLayout(VkDevice device, VkDescriptorSetLayout descriptorSetLayout, const VkAllocationCallbacks *pAllocator) {
61035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    get_my_data_ptr(get_dispatch_key(device), layer_data_map)
61045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ->device_dispatch_table->DestroyDescriptorSetLayout(device, descriptorSetLayout, pAllocator);
61055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Clean up any internal data structures using this obj.
61065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
61075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
61095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, const VkAllocationCallbacks *pAllocator) {
61105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    get_my_data_ptr(get_dispatch_key(device), layer_data_map)
61115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ->device_dispatch_table->DestroyDescriptorPool(device, descriptorPool, pAllocator);
61125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Clean up any internal data structures using this obj.
61135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
61145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
61165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers) {
61175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
61185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skip_call = false;
61205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
61215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < commandBufferCount; i++) {
61225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
61235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= delete_cmd_buf_info(dev_data, commandPool, pCommandBuffers[i]);
61245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
61255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (dev_data->globalInFlightCmdBuffers.count(pCommandBuffers[i])) {
61265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
61275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
61285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        reinterpret_cast<uint64_t>(pCommandBuffers[i]), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER_RESET, "DS",
61295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Attempt to free command buffer (%#" PRIxLEAST64 ") which is in use.",
61305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        reinterpret_cast<uint64_t>(pCommandBuffers[i]));
61315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
61325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Delete CB information structure, and remove from commandBufferMap
61335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto cb = dev_data->commandBufferMap.find(pCommandBuffers[i]);
61345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (cb != dev_data->commandBufferMap.end()) {
61355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // reset prior to delete for data clean-up
61365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            resetCB(dev_data, (*cb).second->commandBuffer);
61375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            delete (*cb).second;
61385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->commandBufferMap.erase(cb);
61395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
61405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Remove commandBuffer reference from commandPoolMap
61425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->commandPoolMap[commandPool].commandBuffers.remove(pCommandBuffers[i]);
61435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
61445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
61455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    printCBList(dev_data, device);
61465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
61475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
61485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skip_call)
61505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->FreeCommandBuffers(device, commandPool, commandBufferCount, pCommandBuffers);
61515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
61525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo *pCreateInfo,
61545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                   const VkAllocationCallbacks *pAllocator,
61555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                   VkCommandPool *pCommandPool) {
61565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
61575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateCommandPool(device, pCreateInfo, pAllocator, pCommandPool);
61595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
61615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
61625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->commandPoolMap[*pCommandPool].createFlags = pCreateInfo->flags;
61635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->commandPoolMap[*pCommandPool].queueFamilyIndex = pCreateInfo->queueFamilyIndex;
61645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
61655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
61665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
61675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
61685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateQueryPool(VkDevice device, const VkQueryPoolCreateInfo *pCreateInfo,
61705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                 const VkAllocationCallbacks *pAllocator, VkQueryPool *pQueryPool) {
61715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
61735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateQueryPool(device, pCreateInfo, pAllocator, pQueryPool);
61745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (result == VK_SUCCESS) {
61755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
61765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->queryPoolMap[*pQueryPool].createInfo = *pCreateInfo;
61775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
61785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
61795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
61805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
61815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 validateCommandBuffersNotInUse(const layer_data *dev_data, VkCommandPool commandPool) {
61835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
61845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto pool_data = dev_data->commandPoolMap.find(commandPool);
61855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pool_data != dev_data->commandPoolMap.end()) {
61865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto cmdBuffer : pool_data->second.commandBuffers) {
61875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dev_data->globalInFlightCmdBuffers.count(cmdBuffer)) {
61885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
61895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_POOL_EXT,
61905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(commandPool), __LINE__, DRAWSTATE_OBJECT_INUSE, "DS",
61915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Cannot reset command pool %" PRIx64 " when allocated command buffer %" PRIx64 " is in use.",
61925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(commandPool), (uint64_t)(cmdBuffer));
61935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
61945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
61955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
61965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
61975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
61985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
61995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Destroy commandPool along with all of the commandBuffers allocated from that pool
62005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
62015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) {
62025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
62035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool commandBufferComplete = false;
62045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
62055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
62065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
62075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Verify that command buffers in pool are complete (not in-flight)
62085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : Merge this with code below (separate *NotInUse() call)
62095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto it = dev_data->commandPoolMap[commandPool].commandBuffers.begin();
62105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         it != dev_data->commandPoolMap[commandPool].commandBuffers.end(); it++) {
62115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        commandBufferComplete = VK_FALSE;
62125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = checkCBCompleted(dev_data, *it, &commandBufferComplete);
62135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (VK_FALSE == commandBufferComplete) {
62145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
62155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)(*it), __LINE__, MEMTRACK_RESET_CB_WHILE_IN_FLIGHT, "MEM",
62165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Destroying Command Pool 0x%" PRIxLEAST64 " before "
62175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "its command buffer (0x%" PRIxLEAST64 ") has completed.",
62185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)(commandPool), reinterpret_cast<uint64_t>(*it));
62195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
62205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
62215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
62225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Must remove cmdpool from cmdpoolmap, after removing all cmdbuffers in its list from the commandPoolMap
62235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (dev_data->commandPoolMap.find(commandPool) != dev_data->commandPoolMap.end()) {
62245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto poolCb = dev_data->commandPoolMap[commandPool].commandBuffers.begin();
62255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             poolCb != dev_data->commandPoolMap[commandPool].commandBuffers.end();) {
62265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto del_cb = dev_data->commandBufferMap.find(*poolCb);
62275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            delete (*del_cb).second;                  // delete CB info structure
62285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->commandBufferMap.erase(del_cb); // Remove this command buffer from cbMap
62295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            poolCb = dev_data->commandPoolMap[commandPool].commandBuffers.erase(
62305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                poolCb); // Remove CB reference from commandPoolMap's list
62315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
62325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
62335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->commandPoolMap.erase(commandPool);
62345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
62355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
62365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
62375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_TRUE == validateCommandBuffersNotInUse(dev_data, commandPool))
62385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
62395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
62405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skipCall)
62415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->DestroyCommandPool(device, commandPool, pAllocator);
62425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
62435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
62445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto item = dev_data->commandPoolMap[commandPool].commandBuffers.begin();
62455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Remove command buffers from command buffer map
62465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while (item != dev_data->commandPoolMap[commandPool].commandBuffers.end()) {
62475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto del_item = item++;
62485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete_cmd_buf_info(dev_data, commandPool, *del_item);
62495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
62505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->commandPoolMap.erase(commandPool);
62515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
62525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
62535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
62545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
62555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
62565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags) {
62575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
62585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool commandBufferComplete = false;
62595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
62605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
62615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
62625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : Merge this with *NotInUse() call below
62635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
62645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto it = dev_data->commandPoolMap[commandPool].commandBuffers.begin();
62655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Verify that CB's in pool are complete (not in-flight)
62665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    while (it != dev_data->commandPoolMap[commandPool].commandBuffers.end()) {
62675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = checkCBCompleted(dev_data, (*it), &commandBufferComplete);
62685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!commandBufferComplete) {
62695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
62705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)(*it), __LINE__, MEMTRACK_RESET_CB_WHILE_IN_FLIGHT, "MEM",
62715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Resetting CB %p before it has completed. You must check CB "
62725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "flag before calling vkResetCommandBuffer().",
62735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (*it));
62745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
62755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Clear memory references at this point.
62765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= clear_cmd_buf_and_mem_references(dev_data, (*it));
62775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
62785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ++it;
62795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
62805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
62815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
62825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_TRUE == validateCommandBuffersNotInUse(dev_data, commandPool))
62835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
62845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
62855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skipCall)
62865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->ResetCommandPool(device, commandPool, flags);
62875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
62885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Reset all of the CBs allocated from this pool
62895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
62905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
62915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto it = dev_data->commandPoolMap[commandPool].commandBuffers.begin();
62925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        while (it != dev_data->commandPoolMap[commandPool].commandBuffers.end()) {
62935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            resetCB(dev_data, (*it));
62945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            ++it;
62955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
62965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
62975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
62985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
62995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
63005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
63015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkResetFences(VkDevice device, uint32_t fenceCount, const VkFence *pFences) {
63025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
63035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
63045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
63055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
63065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < fenceCount; ++i) {
63075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
63085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Reset fence state in fenceCreateInfo structure
63095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // MTMTODO : Merge with code below
63105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto fence_item = dev_data->fenceMap.find(pFences[i]);
63115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (fence_item != dev_data->fenceMap.end()) {
63125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Validate fences in SIGNALED state
63135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!(fence_item->second.createInfo.flags & VK_FENCE_CREATE_SIGNALED_BIT)) {
63145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // TODO: I don't see a Valid Usage section for ResetFences. This behavior should be documented there.
63155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
63165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   (uint64_t)pFences[i], __LINE__, MEMTRACK_INVALID_FENCE_STATE, "MEM",
63175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   "Fence %#" PRIxLEAST64 " submitted to VkResetFences in UNSIGNALED STATE", (uint64_t)pFences[i]);
63185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
63195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                fence_item->second.createInfo.flags =
63205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    static_cast<VkFenceCreateFlags>(fence_item->second.createInfo.flags & ~VK_FENCE_CREATE_SIGNALED_BIT);
63215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
63225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
63235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
63245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (dev_data->fenceMap[pFences[i]].in_use.load()) {
63255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
63265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT,
63275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        reinterpret_cast<const uint64_t &>(pFences[i]), __LINE__, DRAWSTATE_INVALID_FENCE, "DS",
63285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Fence %#" PRIx64 " is in use by a command buffer.", reinterpret_cast<const uint64_t &>(pFences[i]));
63295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
63305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
63315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
63325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skipCall)
63335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->ResetFences(device, fenceCount, pFences);
63345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
63355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
63365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
63375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
63385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer, const VkAllocationCallbacks *pAllocator) {
63395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
63405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
63415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : Merge with code below
63425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
63435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto item = dev_data->fbMap.find(framebuffer);
63445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (item != dev_data->fbMap.end()) {
63455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->fbMap.erase(framebuffer);
63465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
63475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
63485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
63495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto fbNode = dev_data->frameBufferMap.find(framebuffer);
63505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (fbNode != dev_data->frameBufferMap.end()) {
63515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto cb : fbNode->second.referencingCmdBuffers) {
63525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto cbNode = dev_data->commandBufferMap.find(cb);
63535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (cbNode != dev_data->commandBufferMap.end()) {
63545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Set CB as invalid and record destroyed framebuffer
63555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                cbNode->second->state = CB_INVALID;
63567e6ee472f8858eeca9f38d29b7ed0054ec615537Tony Barbour                loader_platform_thread_lock_mutex(&globalLock);
63575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                cbNode->second->destroyedFramebuffers.insert(framebuffer);
63587e6ee472f8858eeca9f38d29b7ed0054ec615537Tony Barbour                loader_platform_thread_unlock_mutex(&globalLock);
63595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
63605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
63615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->frameBufferMap.erase(framebuffer);
63625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
63635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->device_dispatch_table->DestroyFramebuffer(device, framebuffer, pAllocator);
63645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
63655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
63665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
63675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyRenderPass(VkDevice device, VkRenderPass renderPass, const VkAllocationCallbacks *pAllocator) {
63685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
63695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->device_dispatch_table->DestroyRenderPass(device, renderPass, pAllocator);
63705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
63715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->renderPassMap.erase(renderPass);
63725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->passMap.erase(renderPass);
63735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
63745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
63755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
63765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 validate_queue_family_indices(layer_data *dev_data, const char *function_name, const uint32_t count,
63775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                       const uint32_t *indices) {
63785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
63795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto i = 0; i < count; i++) {
63805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (indices[i] >= dev_data->physDevProperties.queue_family_properties.size()) {
63815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
63825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INVALID_QUEUE_INDEX, "DS",
63835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "%s has QueueFamilyIndex greater than the number of QueueFamilies (" PRINTF_SIZE_T_SPECIFIER
63845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                ") for this device.",
63855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                function_name, dev_data->physDevProperties.queue_family_properties.size());
63865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
63875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
63885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
63895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
63905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
63915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
63925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer) {
63935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
63945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
63955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = validate_queue_family_indices(dev_data, "vkCreateBuffer", pCreateInfo->queueFamilyIndexCount,
63965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  pCreateInfo->pQueueFamilyIndices);
63975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skipCall) {
63985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->CreateBuffer(device, pCreateInfo, pAllocator, pBuffer);
63995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
64005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
64015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
64025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
64035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
64045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        add_object_create_info(dev_data, (uint64_t)*pBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, pCreateInfo);
64055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
64065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : This doesn't create deep copy of pQueueFamilyIndices so need to fix that if/when we want that data to be valid
64075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->bufferMap[*pBuffer].create_info = unique_ptr<VkBufferCreateInfo>(new VkBufferCreateInfo(*pCreateInfo));
64085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->bufferMap[*pBuffer].in_use.store(0);
64095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
64105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
64115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
64125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
64135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
64145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateBufferView(VkDevice device, const VkBufferViewCreateInfo *pCreateInfo,
64155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  const VkAllocationCallbacks *pAllocator, VkBufferView *pView) {
64165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
64175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateBufferView(device, pCreateInfo, pAllocator, pView);
64185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
64195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
64205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->bufferViewMap[*pView] = VkBufferViewCreateInfo(*pCreateInfo);
64215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
64225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // In order to create a valid buffer view, the buffer must have been created with at least one of the
64235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // following flags:  UNIFORM_TEXEL_BUFFER_BIT or STORAGE_TEXEL_BUFFER_BIT
64245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        validate_buffer_usage_flags(dev_data, device, pCreateInfo->buffer,
64255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT, VK_FALSE,
64265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "vkCreateBufferView()", "VK_BUFFER_USAGE_[STORAGE|UNIFORM]_TEXEL_BUFFER_BIT");
64275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
64285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
64295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
64305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
64315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
64325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
64335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
64345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCreateImage(VkDevice device, const VkImageCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
64355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
64365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
64375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = validate_queue_family_indices(dev_data, "vkCreateImage", pCreateInfo->queueFamilyIndexCount,
64385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  pCreateInfo->pQueueFamilyIndices);
64395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skipCall) {
64405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->CreateImage(device, pCreateInfo, pAllocator, pImage);
64415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
64425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
64435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
6444e5bc02759d9ab768d67b2f45ec53d29fb59a4705Tony Barbour        loader_platform_thread_lock_mutex(&globalLock);
64455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
64465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        add_object_create_info(dev_data, (uint64_t)*pImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, pCreateInfo);
64475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
64485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        IMAGE_LAYOUT_NODE image_node;
64495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        image_node.layout = pCreateInfo->initialLayout;
64505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        image_node.format = pCreateInfo->format;
64515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->imageMap[*pImage].createInfo = *pCreateInfo;
64525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ImageSubresourcePair subpair = {*pImage, false, VkImageSubresource()};
64535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->imageSubresourceMap[*pImage].push_back(subpair);
64545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->imageLayoutMap[subpair] = image_node;
64555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
64565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
64575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
64585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
64595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
64605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void ResolveRemainingLevelsLayers(layer_data *dev_data, VkImageSubresourceRange *range, VkImage image) {
64615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* expects globalLock to be held by caller */
64625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
64635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto image_node_it = dev_data->imageMap.find(image);
64645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (image_node_it != dev_data->imageMap.end()) {
64655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* If the caller used the special values VK_REMAINING_MIP_LEVELS and
64665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * VK_REMAINING_ARRAY_LAYERS, resolve them now in our internal state to
64675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         * the actual values.
64685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis         */
64695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
64705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            range->levelCount = image_node_it->second.createInfo.mipLevels - range->baseMipLevel;
64715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
64725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
64735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
64745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            range->layerCount = image_node_it->second.createInfo.arrayLayers - range->baseArrayLayer;
64755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
64765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
64775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
64785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
64795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Return the correct layer/level counts if the caller used the special
64805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// values VK_REMAINING_MIP_LEVELS or VK_REMAINING_ARRAY_LAYERS.
64815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void ResolveRemainingLevelsLayers(layer_data *dev_data, uint32_t *levels, uint32_t *layers, VkImageSubresourceRange range,
64825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         VkImage image) {
64835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    /* expects globalLock to be held by caller */
64845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
64855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    *levels = range.levelCount;
64865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    *layers = range.layerCount;
64875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto image_node_it = dev_data->imageMap.find(image);
64885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (image_node_it != dev_data->imageMap.end()) {
64895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (range.levelCount == VK_REMAINING_MIP_LEVELS) {
64905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            *levels = image_node_it->second.createInfo.mipLevels - range.baseMipLevel;
64915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
64925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (range.layerCount == VK_REMAINING_ARRAY_LAYERS) {
64935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            *layers = image_node_it->second.createInfo.arrayLayers - range.baseArrayLayer;
64945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
64955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
64965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
64975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
64985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateImageView(VkDevice device, const VkImageViewCreateInfo *pCreateInfo,
64995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                 const VkAllocationCallbacks *pAllocator, VkImageView *pView) {
65005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
65015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateImageView(device, pCreateInfo, pAllocator, pView);
65025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
65035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
65045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkImageViewCreateInfo localCI = VkImageViewCreateInfo(*pCreateInfo);
65055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ResolveRemainingLevelsLayers(dev_data, &localCI.subresourceRange, pCreateInfo->image);
65065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->imageViewMap[*pView] = localCI;
65075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
65085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Validate that img has correct usage flags set
65095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        validate_image_usage_flags(dev_data, device, pCreateInfo->image,
65105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT |
65115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                       VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
65125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   VK_FALSE, "vkCreateImageView()", "VK_IMAGE_USAGE_[SAMPLED|STORAGE|COLOR_ATTACHMENT]_BIT");
65135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
65145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
65155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
65165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
65175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
65185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
65195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
65205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCreateFence(VkDevice device, const VkFenceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkFence *pFence) {
65215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
65225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateFence(device, pCreateInfo, pAllocator, pFence);
65235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
65245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
65255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        FENCE_NODE *pFN = &dev_data->fenceMap[*pFence];
65265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
65275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memset(pFN, 0, sizeof(MT_FENCE_INFO));
65285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy(&(pFN->createInfo), pCreateInfo, sizeof(VkFenceCreateInfo));
65295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
65305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pFN->firstTimeFlag = VK_TRUE;
65315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
65325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
65335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pFN->in_use.store(0);
65345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
65355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
65365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
65375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
65385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
65395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// TODO handle pipeline caches
65405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL vkCreatePipelineCache(VkDevice device, const VkPipelineCacheCreateInfo *pCreateInfo,
65415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     const VkAllocationCallbacks *pAllocator, VkPipelineCache *pPipelineCache) {
65425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
65435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreatePipelineCache(device, pCreateInfo, pAllocator, pPipelineCache);
65445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
65455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
65465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
65475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR void VKAPI_CALL
65485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroyPipelineCache(VkDevice device, VkPipelineCache pipelineCache, const VkAllocationCallbacks *pAllocator) {
65495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
65505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->device_dispatch_table->DestroyPipelineCache(device, pipelineCache, pAllocator);
65515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
65525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
65535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL
65545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkGetPipelineCacheData(VkDevice device, VkPipelineCache pipelineCache, size_t *pDataSize, void *pData) {
65555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
65565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->GetPipelineCacheData(device, pipelineCache, pDataSize, pData);
65575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
65585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
65595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
65605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL
65615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkMergePipelineCaches(VkDevice device, VkPipelineCache dstCache, uint32_t srcCacheCount, const VkPipelineCache *pSrcCaches) {
65625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
65635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->MergePipelineCaches(device, dstCache, srcCacheCount, pSrcCaches);
65645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
65655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
65665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
65675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
65685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
65695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                          const VkGraphicsPipelineCreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator,
65705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                          VkPipeline *pPipelines) {
65715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_SUCCESS;
65725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO What to do with pipelineCache?
65735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // The order of operations here is a little convoluted but gets the job done
65745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //  1. Pipeline create state is first shadowed into PIPELINE_NODE struct
65755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //  2. Create state is then validated (which uses flags setup during shadowing)
65765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //  3. If everything looks good, we'll then create the pipeline and add NODE to pipelineMap
65775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
65785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Improve this data struct w/ unique_ptrs so cleanup below is automatic
65795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    vector<PIPELINE_NODE *> pPipeNode(count);
65805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
65815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
65825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t i = 0;
65835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
65845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
65855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (i = 0; i < count; i++) {
65865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeNode[i] = initGraphicsPipeline(dev_data, &pCreateInfos[i]);
65875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= verifyPipelineCreateState(dev_data, device, pPipeNode, i);
65885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
65895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
65905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
65915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
65925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator,
65935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                          pPipelines);
65945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
65955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < count; i++) {
65965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeNode[i]->pipeline = pPipelines[i];
65975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->pipelineMap[pPipeNode[i]->pipeline] = pPipeNode[i];
65985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
65995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
66005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
66015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < count; i++) {
66025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pPipeNode[i]) {
66035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // If we allocated a pipeNode, need to clean it up here
66045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                delete[] pPipeNode[i]->pVertexBindingDescriptions;
66055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                delete[] pPipeNode[i]->pVertexAttributeDescriptions;
66065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                delete[] pPipeNode[i]->pAttachments;
66075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                delete pPipeNode[i];
66085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
66095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
66105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
66115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
66125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
66135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
66145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
66155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
66165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
66175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
66185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                         const VkComputePipelineCreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator,
66195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                         VkPipeline *pPipelines) {
66205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_SUCCESS;
66215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
66225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
66235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Improve this data struct w/ unique_ptrs so cleanup below is automatic
66245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    vector<PIPELINE_NODE *> pPipeNode(count);
66255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
66265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
66275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t i = 0;
66285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
66295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (i = 0; i < count; i++) {
66305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO: Verify compute stage bits
66315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
66325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Create and initialize internal tracking data structure
66335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pPipeNode[i] = new PIPELINE_NODE;
66345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy(&pPipeNode[i]->computePipelineCI, (const void *)&pCreateInfos[i], sizeof(VkComputePipelineCreateInfo));
66355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
66365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO: Add Compute Pipeline Verification
66375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // skipCall |= verifyPipelineCreateState(dev_data, device, pPipeNode[i]);
66385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
66395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
66405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
66415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
66425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->CreateComputePipelines(device, pipelineCache, count, pCreateInfos, pAllocator,
66435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                         pPipelines);
66445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
66455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < count; i++) {
66465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pPipeNode[i]->pipeline = pPipelines[i];
66475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->pipelineMap[pPipeNode[i]->pipeline] = pPipeNode[i];
66485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
66495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
66505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
66515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < count; i++) {
66525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Clean up any locally allocated data structures
66535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            delete pPipeNode[i];
66545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
66555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
66565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
66575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
66585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
66595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
66605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
66615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateSampler(VkDevice device, const VkSamplerCreateInfo *pCreateInfo,
66625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                               const VkAllocationCallbacks *pAllocator, VkSampler *pSampler) {
66635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
66645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateSampler(device, pCreateInfo, pAllocator, pSampler);
66655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
66665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
66675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->sampleMap[*pSampler] = unique_ptr<SAMPLER_NODE>(new SAMPLER_NODE(pSampler, pCreateInfo));
66685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
66695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
66705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
66715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
66725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
66735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
66745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCreateDescriptorSetLayout(VkDevice device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
66755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            const VkAllocationCallbacks *pAllocator, VkDescriptorSetLayout *pSetLayout) {
66765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
66775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout);
66785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
66795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODOSC : Capture layout bindings set
66805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        LAYOUT_NODE *pNewNode = new LAYOUT_NODE;
66815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (NULL == pNewNode) {
66825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
66835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)*pSetLayout, __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
66845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Out of memory while attempting to allocate LAYOUT_NODE in vkCreateDescriptorSetLayout()"))
66855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return VK_ERROR_VALIDATION_FAILED_EXT;
66865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
66875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)&pNewNode->createInfo, pCreateInfo, sizeof(VkDescriptorSetLayoutCreateInfo));
66885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pNewNode->createInfo.pBindings = new VkDescriptorSetLayoutBinding[pCreateInfo->bindingCount];
66895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy((void *)pNewNode->createInfo.pBindings, pCreateInfo->pBindings,
66905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis               sizeof(VkDescriptorSetLayoutBinding) * pCreateInfo->bindingCount);
66915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // g++ does not like reserve with size 0
66925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCreateInfo->bindingCount)
66935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pNewNode->bindingToIndexMap.reserve(pCreateInfo->bindingCount);
66945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t totalCount = 0;
66955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {
66965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pNewNode->bindingToIndexMap.emplace(pCreateInfo->pBindings[i].binding, i).second) {
66975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
66985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t)*pSetLayout, __LINE__,
66995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_LAYOUT, "DS", "duplicated binding number in "
67005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                            "VkDescriptorSetLayoutBinding"))
67015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    return VK_ERROR_VALIDATION_FAILED_EXT;
67025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
67035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pNewNode->bindingToIndexMap[pCreateInfo->pBindings[i].binding] = i;
67045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
67055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            totalCount += pCreateInfo->pBindings[i].descriptorCount;
67065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pCreateInfo->pBindings[i].pImmutableSamplers) {
67075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkSampler **ppIS = (VkSampler **)&pNewNode->createInfo.pBindings[i].pImmutableSamplers;
67085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                *ppIS = new VkSampler[pCreateInfo->pBindings[i].descriptorCount];
67095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                memcpy(*ppIS, pCreateInfo->pBindings[i].pImmutableSamplers,
67105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       pCreateInfo->pBindings[i].descriptorCount * sizeof(VkSampler));
67115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
67125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
67135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pNewNode->layout = *pSetLayout;
67145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pNewNode->startIndex = 0;
67155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (totalCount > 0) {
67165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pNewNode->descriptorTypes.resize(totalCount);
67175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pNewNode->stageFlags.resize(totalCount);
67185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t offset = 0;
67195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t j = 0;
67205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkDescriptorType dType;
67215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {
67225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dType = pCreateInfo->pBindings[i].descriptorType;
67235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (j = 0; j < pCreateInfo->pBindings[i].descriptorCount; j++) {
67245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pNewNode->descriptorTypes[offset + j] = dType;
67255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pNewNode->stageFlags[offset + j] = pCreateInfo->pBindings[i].stageFlags;
67265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if ((dType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
67275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (dType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
67285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        pNewNode->dynamicDescriptorCount++;
67295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
67305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
67315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                offset += j;
67325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
67335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pNewNode->endIndex = pNewNode->startIndex + totalCount - 1;
67345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else { // no descriptors
67355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pNewNode->endIndex = 0;
67365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
67375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Put new node at Head of global Layer list
67385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
67395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->descriptorSetLayoutMap[*pSetLayout] = pNewNode;
67405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
67415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
67425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
67435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
67445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
67455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic bool validatePushConstantSize(const layer_data *dev_data, const uint32_t offset, const uint32_t size,
67465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     const char *caller_name) {
67475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
67485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if ((offset + size) > dev_data->physDevProperties.properties.limits.maxPushConstantsSize) {
67495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
67505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           DRAWSTATE_PUSH_CONSTANTS_ERROR, "DS", "%s call has push constants with offset %u and size %u that "
67515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                 "exceeds this device's maxPushConstantSize of %u.",
67525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           caller_name, offset, size, dev_data->physDevProperties.properties.limits.maxPushConstantsSize);
67535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
67545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
67555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
67565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
67575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL vkCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
67585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                      const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout) {
67595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
67605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
67615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t i = 0;
67625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (i = 0; i < pCreateInfo->pushConstantRangeCount; ++i) {
67635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validatePushConstantSize(dev_data, pCreateInfo->pPushConstantRanges[i].offset,
67645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                             pCreateInfo->pPushConstantRanges[i].size, "vkCreatePipelineLayout()");
67655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((pCreateInfo->pPushConstantRanges[i].size == 0) || ((pCreateInfo->pPushConstantRanges[i].size & 0x3) != 0)) {
67665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
67675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
67685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_PUSH_CONSTANTS_ERROR, "DS", "vkCreatePipelineLayout() call has push constant index %u with "
67695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                              "size %u. Size must be greater than zero and a multiple of 4.",
67705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        i, pCreateInfo->pPushConstantRanges[i].size);
67715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
67725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Add warning if ranges overlap
67735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
67745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout);
67755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
67765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
67775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODOSC : Merge capture of the setLayouts per pipeline
67785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        PIPELINE_LAYOUT_NODE &plNode = dev_data->pipelineLayoutMap[*pPipelineLayout];
67795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        plNode.descriptorSetLayouts.resize(pCreateInfo->setLayoutCount);
67805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < pCreateInfo->setLayoutCount; ++i) {
67815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            plNode.descriptorSetLayouts[i] = pCreateInfo->pSetLayouts[i];
67825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
67835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        plNode.pushConstantRanges.resize(pCreateInfo->pushConstantRangeCount);
67845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (i = 0; i < pCreateInfo->pushConstantRangeCount; ++i) {
67855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            plNode.pushConstantRanges[i] = pCreateInfo->pPushConstantRanges[i];
67865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
67875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
67885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
67895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
67905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
67915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
67925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
67935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCreateDescriptorPool(VkDevice device, const VkDescriptorPoolCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
67945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       VkDescriptorPool *pDescriptorPool) {
67955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
67965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateDescriptorPool(device, pCreateInfo, pAllocator, pDescriptorPool);
67975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
67985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Insert this pool into Global Pool LL at head
67995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
68005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (uint64_t)*pDescriptorPool, __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS", "Created Descriptor Pool %#" PRIxLEAST64,
68015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (uint64_t)*pDescriptorPool))
68025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_ERROR_VALIDATION_FAILED_EXT;
68035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        DESCRIPTOR_POOL_NODE *pNewNode = new DESCRIPTOR_POOL_NODE(*pDescriptorPool, pCreateInfo);
68045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (NULL == pNewNode) {
68055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
68065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)*pDescriptorPool, __LINE__, DRAWSTATE_OUT_OF_MEMORY, "DS",
68075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Out of memory while attempting to allocate DESCRIPTOR_POOL_NODE in vkCreateDescriptorPool()"))
68085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return VK_ERROR_VALIDATION_FAILED_EXT;
68095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
68105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            loader_platform_thread_lock_mutex(&globalLock);
68115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->descriptorPoolMap[*pDescriptorPool] = pNewNode;
68125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            loader_platform_thread_unlock_mutex(&globalLock);
68135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
68145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
68155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Need to do anything if pool create fails?
68165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
68175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
68185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
68195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
68205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
68215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, VkDescriptorPoolResetFlags flags) {
68225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
68235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->ResetDescriptorPool(device, descriptorPool, flags);
68245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
68255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
68265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        clearDescriptorPool(dev_data, device, descriptorPool, flags);
68275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
68285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
68295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
68305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
68315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
68325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
68335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo, VkDescriptorSet *pDescriptorSets) {
68345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
68355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
68365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
68375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
68385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Verify that requested descriptorSets are available in pool
68395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    DESCRIPTOR_POOL_NODE *pPoolNode = getPoolNode(dev_data, pAllocateInfo->descriptorPool);
68405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!pPoolNode) {
68415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
68425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)pAllocateInfo->descriptorPool, __LINE__, DRAWSTATE_INVALID_POOL, "DS",
68435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Unable to find pool node for pool %#" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
68445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)pAllocateInfo->descriptorPool);
68455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else { // Make sure pool has all the available descriptors before calling down chain
68465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validate_descriptor_availability_in_pool(dev_data, pPoolNode, pAllocateInfo->descriptorSetCount,
68475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                             pAllocateInfo->pSetLayouts);
68485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
68495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
68505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (skipCall)
68515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
68525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->AllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets);
68535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
68545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
68555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        DESCRIPTOR_POOL_NODE *pPoolNode = getPoolNode(dev_data, pAllocateInfo->descriptorPool);
68565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pPoolNode) {
68575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pAllocateInfo->descriptorSetCount == 0) {
68585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
68595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        pAllocateInfo->descriptorSetCount, __LINE__, DRAWSTATE_NONE, "DS",
68605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "AllocateDescriptorSets called with 0 count");
68615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
68625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
68635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
68645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)pDescriptorSets[i], __LINE__, DRAWSTATE_NONE, "DS", "Created Descriptor Set %#" PRIxLEAST64,
68655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)pDescriptorSets[i]);
68665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Create new set node and add to head of pool nodes
68675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                SET_NODE *pNewNode = new SET_NODE;
68685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (NULL == pNewNode) {
68695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
68705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i], __LINE__,
68715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_OUT_OF_MEMORY, "DS",
68725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Out of memory while attempting to allocate SET_NODE in vkAllocateDescriptorSets()"))
68735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        return VK_ERROR_VALIDATION_FAILED_EXT;
68745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else {
68755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // TODO : Pool should store a total count of each type of Descriptor available
68765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    //  When descriptors are allocated, decrement the count and validate here
68775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    //  that the count doesn't go below 0. One reset/free need to bump count back up.
68785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // Insert set at head of Set LL for this pool
68795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pNewNode->pNext = pPoolNode->pSets;
68805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pNewNode->in_use.store(0);
68815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pPoolNode->pSets = pNewNode;
68825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    LAYOUT_NODE *pLayout = getLayoutNode(dev_data, pAllocateInfo->pSetLayouts[i]);
68835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (NULL == pLayout) {
68845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
68855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, (uint64_t)pAllocateInfo->pSetLayouts[i],
68865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
68875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "Unable to find set layout node for layout %#" PRIxLEAST64
68885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    " specified in vkAllocateDescriptorSets() call",
68895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    (uint64_t)pAllocateInfo->pSetLayouts[i]))
68905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            return VK_ERROR_VALIDATION_FAILED_EXT;
68915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
68925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pNewNode->pLayout = pLayout;
68935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pNewNode->pool = pAllocateInfo->descriptorPool;
68945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pNewNode->set = pDescriptorSets[i];
68955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pNewNode->descriptorCount = (pLayout->createInfo.bindingCount != 0) ? pLayout->endIndex + 1 : 0;
68965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (pNewNode->descriptorCount) {
68975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        size_t descriptorArraySize = sizeof(GENERIC_HEADER *) * pNewNode->descriptorCount;
68985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        pNewNode->ppDescriptors = new GENERIC_HEADER *[descriptorArraySize];
68995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        memset(pNewNode->ppDescriptors, 0, descriptorArraySize);
69005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
69015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    dev_data->setMap[pDescriptorSets[i]] = pNewNode;
69025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
69035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
69045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
69055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
69065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
69075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
69085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
69095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
69105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
69115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t count, const VkDescriptorSet *pDescriptorSets) {
69125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
69135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
69145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Make sure that no sets being destroyed are in-flight
69155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
69165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < count; ++i)
69175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validateIdleDescriptorSet(dev_data, pDescriptorSets[i], "vkFreeDesriptorSets");
69185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    DESCRIPTOR_POOL_NODE *pPoolNode = getPoolNode(dev_data, descriptorPool);
69195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pPoolNode && !(VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT & pPoolNode->createInfo.flags)) {
69205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Can't Free from a NON_FREE pool
69215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
69225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)device, __LINE__, DRAWSTATE_CANT_FREE_FROM_NON_FREE_POOL, "DS",
69235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "It is invalid to call vkFreeDescriptorSets() with a pool created without setting "
69245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT.");
69255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
69265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
69275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE != skipCall)
69285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
69295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->FreeDescriptorSets(device, descriptorPool, count, pDescriptorSets);
69305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
69315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // For each freed descriptor add it back into the pool as available
69325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
69335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < count; ++i) {
69345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            SET_NODE *pSet = dev_data->setMap[pDescriptorSets[i]]; // getSetNode() without locking
69355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            invalidateBoundCmdBuffers(dev_data, pSet);
69365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            LAYOUT_NODE *pLayout = pSet->pLayout;
69375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t typeIndex = 0, poolSizeCount = 0;
69385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t j = 0; j < pLayout->createInfo.bindingCount; ++j) {
69395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                typeIndex = static_cast<uint32_t>(pLayout->createInfo.pBindings[j].descriptorType);
69405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                poolSizeCount = pLayout->createInfo.pBindings[j].descriptorCount;
69415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pPoolNode->availableDescriptorTypeCount[typeIndex] += poolSizeCount;
69425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
69435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
69445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
69455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
69465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Any other clean-up or book-keeping to do here?
69475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
69485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
69495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
69505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
69515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pDescriptorWrites,
69525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       uint32_t descriptorCopyCount, const VkCopyDescriptorSet *pDescriptorCopies) {
69535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // dsUpdate will return VK_TRUE only if a bailout error occurs, so we want to call down tree when update returns VK_FALSE
69545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
69555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
69565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
69575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : Merge this in with existing update code below and handle descriptor copies case
69585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t j = 0;
69595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < descriptorWriteCount; ++i) {
69605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pDescriptorWrites[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
69615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (j = 0; j < pDescriptorWrites[i].descriptorCount; ++j) {
69625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->descriptorSetMap[pDescriptorWrites[i].dstSet].images.push_back(
69635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pDescriptorWrites[i].pImageInfo[j].imageView);
69645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
69655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (pDescriptorWrites[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) {
69665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (j = 0; j < pDescriptorWrites[i].descriptorCount; ++j) {
69675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->descriptorSetMap[pDescriptorWrites[i].dstSet].buffers.push_back(
69685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    dev_data->bufferViewMap[pDescriptorWrites[i].pTexelBufferView[j]].buffer);
69695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
69705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (pDescriptorWrites[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
69715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   pDescriptorWrites[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
69725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (j = 0; j < pDescriptorWrites[i].descriptorCount; ++j) {
69735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->descriptorSetMap[pDescriptorWrites[i].dstSet].buffers.push_back(
69745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pDescriptorWrites[i].pBufferInfo[j].buffer);
69755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
69765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
69775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
69785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
69795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 rtn = dsUpdate(dev_data, device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount, pDescriptorCopies);
69805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
69815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!rtn) {
69825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->UpdateDescriptorSets(device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount,
69835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                              pDescriptorCopies);
69845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
69855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
69865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
69875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
69885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pCreateInfo, VkCommandBuffer *pCommandBuffer) {
69895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
69905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->AllocateCommandBuffers(device, pCreateInfo, pCommandBuffer);
69915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
69925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
69935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < pCreateInfo->commandBufferCount; i++) {
69945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
69955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            add_cmd_buf_info(dev_data, pCreateInfo->commandPool, pCommandBuffer[i]);
69965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
69975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Validate command pool
69985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dev_data->commandPoolMap.find(pCreateInfo->commandPool) != dev_data->commandPoolMap.end()) {
69995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Add command buffer to its commandPool map
7000aeaf18290b786324f35b462651928c00e4066bfaMark Lobodzinski#if !MTMERGE
70015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->commandPoolMap[pCreateInfo->commandPool].commandBuffers.push_back(pCommandBuffer[i]);
7002aeaf18290b786324f35b462651928c00e4066bfaMark Lobodzinski#endif
70035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                GLOBAL_CB_NODE *pCB = new GLOBAL_CB_NODE;
70045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Add command buffer to map
70055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->commandBufferMap[pCommandBuffer[i]] = pCB;
70065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                resetCB(dev_data, pCommandBuffer[i]);
70075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pCB->createInfo = *pCreateInfo;
70085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pCB->device = device;
70095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
70105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
70115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
70125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        printCBList(dev_data, device);
70135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
70145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
70155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
70165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
70175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
70185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
70195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
70205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo) {
70215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
70225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
70235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
70245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate command buffer level
70255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
70265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
70275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
70285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        bool commandBufferComplete = false;
70295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // MTMTODO : Merge this with code below
70305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // This implicitly resets the Cmd Buffer so make sure any fence is done and then clear memory references
70315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall = checkCBCompleted(dev_data, commandBuffer, &commandBufferComplete);
70325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
70335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!commandBufferComplete) {
70345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
70355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)commandBuffer, __LINE__, MEMTRACK_RESET_CB_WHILE_IN_FLIGHT, "MEM",
70365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Calling vkBeginCommandBuffer() on active CB %p before it has completed. "
70375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "You must check CB flag before this call.",
70385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                commandBuffer);
70395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
70405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
70415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->createInfo.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
70425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Secondary Command Buffer
70435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            const VkCommandBufferInheritanceInfo *pInfo = pBeginInfo->pInheritanceInfo;
70445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pInfo) {
70455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
70465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
70475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
70485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "vkBeginCommandBuffer(): Secondary Command Buffer (%p) must have inheritance info.",
70495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            reinterpret_cast<void *>(commandBuffer));
70505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
70515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
70525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (!pInfo->renderPass) { // renderpass should NOT be null for an Secondary CB
70535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        skipCall |= log_msg(
70545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
70555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
70565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "vkBeginCommandBuffer(): Secondary Command Buffers (%p) must specify a valid renderpass parameter.",
70575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            reinterpret_cast<void *>(commandBuffer));
70585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
70595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (!pInfo->framebuffer) { // framebuffer may be null for an Secondary CB, but this affects perf
70605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT,
70615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
70625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE,
70635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "DS", "vkBeginCommandBuffer(): Secondary Command Buffers (%p) may perform better if a "
70645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  "valid framebuffer parameter is specified.",
70655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            reinterpret_cast<void *>(commandBuffer));
70665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    } else {
70675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        string errorString = "";
70685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        auto fbNode = dev_data->frameBufferMap.find(pInfo->framebuffer);
70695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (fbNode != dev_data->frameBufferMap.end()) {
70705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            VkRenderPass fbRP = fbNode->second.createInfo.renderPass;
70715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            if (!verify_renderpass_compatibility(dev_data, fbRP, pInfo->renderPass, errorString)) {
70725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                // renderPass that framebuffer was created with
70735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                // must
70745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                // be compatible with local renderPass
70755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                skipCall |=
70765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
70775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
70785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_RENDERPASS_INCOMPATIBLE,
70795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "DS", "vkBeginCommandBuffer(): Secondary Command "
70805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  "Buffer (%p) renderPass (%#" PRIxLEAST64 ") is incompatible w/ framebuffer "
70815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  "(%#" PRIxLEAST64 ") w/ render pass (%#" PRIxLEAST64 ") due to: %s",
70825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            reinterpret_cast<void *>(commandBuffer), (uint64_t)(pInfo->renderPass),
70835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            (uint64_t)(pInfo->framebuffer), (uint64_t)(fbRP), errorString.c_str());
70845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            }
70855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            // Connect this framebuffer to this cmdBuffer
70865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            fbNode->second.referencingCmdBuffers.insert(pCB->commandBuffer);
70875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
70885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
70895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
70905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if ((pInfo->occlusionQueryEnable == VK_FALSE ||
70915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     dev_data->physDevProperties.features.occlusionQueryPrecise == VK_FALSE) &&
70925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (pInfo->queryFlags & VK_QUERY_CONTROL_PRECISE_BIT)) {
70935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
70945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(commandBuffer),
70955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
70965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "vkBeginCommandBuffer(): Secondary Command Buffer (%p) must not have "
70975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "VK_QUERY_CONTROL_PRECISE_BIT if occulusionQuery is disabled or the device does not "
70985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "support precise occlusion queries.",
70995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        reinterpret_cast<void *>(commandBuffer));
71005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
71015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
71025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pInfo && pInfo->renderPass != VK_NULL_HANDLE) {
71035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto rp_data = dev_data->renderPassMap.find(pInfo->renderPass);
71045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (rp_data != dev_data->renderPassMap.end() && rp_data->second && rp_data->second->pCreateInfo) {
71055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (pInfo->subpass >= rp_data->second->pCreateInfo->subpassCount) {
71065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
71075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__,
71085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
71095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "vkBeginCommandBuffer(): Secondary Command Buffers (%p) must has a subpass index (%d) "
71105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "that is less than the number of subpasses (%d).",
71115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            (void *)commandBuffer, pInfo->subpass, rp_data->second->pCreateInfo->subpassCount);
71125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
71135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
71145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
71155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
71165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (CB_RECORDING == pCB->state) {
71175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
71185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
71195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)commandBuffer, __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
71205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkBeginCommandBuffer(): Cannot call Begin on CB (%#" PRIxLEAST64
71215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        ") in the RECORDING state. Must first call vkEndCommandBuffer().",
71225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)commandBuffer);
71235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (CB_RECORDED == pCB->state) {
71245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkCommandPool cmdPool = pCB->createInfo.commandPool;
71255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT & dev_data->commandPoolMap[cmdPool].createFlags)) {
71265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
71275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
71285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER_RESET, "DS",
71295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Call to vkBeginCommandBuffer() on command buffer (%#" PRIxLEAST64
71305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            ") attempts to implicitly reset cmdBuffer created from command pool (%#" PRIxLEAST64
71315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            ") that does NOT have the VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT bit set.",
71325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)commandBuffer, (uint64_t)cmdPool);
71335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
71345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            resetCB(dev_data, commandBuffer);
71355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
71365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Set updated state here in case implicit reset occurs above
71375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->state = CB_RECORDING;
71385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->beginInfo = *pBeginInfo;
71395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->beginInfo.pInheritanceInfo) {
71405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->inheritanceInfo = *(pCB->beginInfo.pInheritanceInfo);
71415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->beginInfo.pInheritanceInfo = &pCB->inheritanceInfo;
71425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
71435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
71445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
71455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
71465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "In vkBeginCommandBuffer() and unable to find CommandBuffer Node for CB %p!", (void *)commandBuffer);
71475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
71485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
71495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE != skipCall) {
71505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
71515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
71525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->BeginCommandBuffer(commandBuffer, pBeginInfo);
71535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
71545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
71555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    clear_cmd_buf_and_mem_references(dev_data, commandBuffer);
71565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
71575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
71585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
71595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
71605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
71615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEndCommandBuffer(VkCommandBuffer commandBuffer) {
71625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
71635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_SUCCESS;
71645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
71655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
71665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
71675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
71685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->state != CB_RECORDING) {
71695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkEndCommandBuffer()");
71705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
71715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto query : pCB->activeQueries) {
71725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
71735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INVALID_QUERY, "DS",
71745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Ending command buffer with in progress query: queryPool %" PRIu64 ", index %d",
71755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)(query.pool), query.index);
71765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
71775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
71785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
71795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
71805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->EndCommandBuffer(commandBuffer);
71815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
71825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (VK_SUCCESS == result) {
71835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->state = CB_RECORDED;
71845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Reset CB status flags
71855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->status = 0;
71865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            printCB(dev_data, commandBuffer);
71875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
71885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
71895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = VK_ERROR_VALIDATION_FAILED_EXT;
71905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
71915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
71925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
71935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
71945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
71955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
71965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkResetCommandBuffer(VkCommandBuffer commandBuffer, VkCommandBufferResetFlags flags) {
71975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
71985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
71995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
72005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
72015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool commandBufferComplete = false;
72025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Verify that CB is complete (not in-flight)
72035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = checkCBCompleted(dev_data, commandBuffer, &commandBufferComplete);
72045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!commandBufferComplete) {
72055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
72065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)commandBuffer, __LINE__, MEMTRACK_RESET_CB_WHILE_IN_FLIGHT, "MEM",
72075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Resetting CB %p before it has completed. You must check CB "
72085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "flag before calling vkResetCommandBuffer().",
72095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            commandBuffer);
72105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
72115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Clear memory references as this point.
72125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= clear_cmd_buf_and_mem_references(dev_data, commandBuffer);
72135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
72145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
72155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkCommandPool cmdPool = pCB->createInfo.commandPool;
72165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT & dev_data->commandPoolMap[cmdPool].createFlags)) {
72175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
72185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER_RESET, "DS",
72195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Attempt to reset command buffer (%#" PRIxLEAST64 ") created from command pool (%#" PRIxLEAST64
72205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            ") that does NOT have the VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT bit set.",
72215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)commandBuffer, (uint64_t)cmdPool);
72225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
72235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (dev_data->globalInFlightCmdBuffers.count(commandBuffer)) {
72245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
72255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER_RESET, "DS",
72265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Attempt to reset command buffer (%#" PRIxLEAST64 ") which is in use.",
72275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            reinterpret_cast<uint64_t>(commandBuffer));
72285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
72295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
72305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (skipCall != VK_FALSE)
72315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
72325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->ResetCommandBuffer(commandBuffer, flags);
72335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
72345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
72355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        resetCB(dev_data, commandBuffer);
72365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
72375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
72385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
72395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
72405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
72415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// TODO : For any vkCmdBind* calls that include an object which has mem bound to it,
72425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis//    need to account for that mem now having binding to given commandBuffer
72435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
72445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
72455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline) {
72465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
72475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
72485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
72495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
72505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : Pulled this dead code in during merge, figure out what to do with it
72515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if 0 // FIXME: NEED TO FIX THE FOLLOWING CODE AND REMOVE THIS #if 0
72525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : If memory bound to pipeline, then need to tie that mem to commandBuffer
72535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (getPipeline(pipeline)) {
72545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        MT_CB_INFO *pCBInfo = get_cmd_buf_info(my_data, commandBuffer);
72555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCBInfo) {
72565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCBInfo->pipelines[pipelineBindPoint] = pipeline;
72575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
72585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
72595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    else {
72605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                "Attempt to bind Pipeline %p that doesn't exist!", (void*)pipeline);
72615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        layerCbMsg(VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, pipeline, __LINE__, MEMTRACK_INVALID_OBJECT, (char *) "DS", (char *) str);
72625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
72635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
72645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
72655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
72665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
72675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_BINDPIPELINE, "vkCmdBindPipeline()");
72685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((VK_PIPELINE_BIND_POINT_COMPUTE == pipelineBindPoint) && (pCB->activeRenderPass)) {
72695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
72705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
72715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)pipeline, __LINE__, DRAWSTATE_INVALID_RENDERPASS_CMD, "DS",
72725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Incorrectly binding compute pipeline (%#" PRIxLEAST64 ") during active RenderPass (%#" PRIxLEAST64 ")",
72735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)pipeline, (uint64_t)pCB->activeRenderPass);
72745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
72755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
72765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        PIPELINE_NODE *pPN = getPipeline(dev_data, pipeline);
72775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pPN) {
72785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->lastBoundPipeline = pipeline;
72795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_cb_pso_status(pCB, pPN);
72805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validatePipelineState(dev_data, pCB, pipelineBindPoint, pipeline);
72815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
72825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
72835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)pipeline, __LINE__, DRAWSTATE_INVALID_PIPELINE, "DS",
72845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Attempt to bind Pipeline %#" PRIxLEAST64 " that doesn't exist!", (uint64_t)(pipeline));
72855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
72865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
72875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
72885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
72895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline);
72905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
72915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
72925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
72935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdSetViewport(VkCommandBuffer commandBuffer, uint32_t firstViewport, uint32_t viewportCount, const VkViewport *pViewports) {
72945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
72955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
72965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
72975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
72985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
72995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_SETVIEWPORTSTATE, "vkCmdSetViewport()");
73005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_VIEWPORT_SET;
73015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->viewports.resize(viewportCount);
73025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy(pCB->viewports.data(), pViewports, viewportCount * sizeof(VkViewport));
73035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
73045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
73055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
73065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdSetViewport(commandBuffer, firstViewport, viewportCount, pViewports);
73075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
73085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
73095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
73105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdSetScissor(VkCommandBuffer commandBuffer, uint32_t firstScissor, uint32_t scissorCount, const VkRect2D *pScissors) {
73115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
73125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
73135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
73145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
73155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
73165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_SETSCISSORSTATE, "vkCmdSetScissor()");
73175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_SCISSOR_SET;
73185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->scissors.resize(scissorCount);
73195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy(pCB->scissors.data(), pScissors, scissorCount * sizeof(VkRect2D));
73205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
73215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
73225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
73235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdSetScissor(commandBuffer, firstScissor, scissorCount, pScissors);
73245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
73255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
73265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetLineWidth(VkCommandBuffer commandBuffer, float lineWidth) {
73275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
73285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
73295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
73305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
73315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
73325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_SETLINEWIDTHSTATE, "vkCmdSetLineWidth()");
73335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_LINE_WIDTH_SET;
73345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->lineWidth = lineWidth;
73355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
73365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
73375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
73385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdSetLineWidth(commandBuffer, lineWidth);
73395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
73405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
73415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
73425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdSetDepthBias(VkCommandBuffer commandBuffer, float depthBiasConstantFactor, float depthBiasClamp, float depthBiasSlopeFactor) {
73435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
73445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
73455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
73465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
73475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
73485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_SETDEPTHBIASSTATE, "vkCmdSetDepthBias()");
73495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_DEPTH_BIAS_SET;
73505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->depthBiasConstantFactor = depthBiasConstantFactor;
73515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->depthBiasClamp = depthBiasClamp;
73525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->depthBiasSlopeFactor = depthBiasSlopeFactor;
73535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
73545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
73555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
73565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdSetDepthBias(commandBuffer, depthBiasConstantFactor, depthBiasClamp,
73575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         depthBiasSlopeFactor);
73585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
73595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
73605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdSetBlendConstants(VkCommandBuffer commandBuffer, const float blendConstants[4]) {
73615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
73625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
73635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
73645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
73655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
73665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_SETBLENDSTATE, "vkCmdSetBlendConstants()");
73675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_BLEND_SET;
73685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        memcpy(pCB->blendConstants, blendConstants, 4 * sizeof(float));
73695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
73705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
73715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
73725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdSetBlendConstants(commandBuffer, blendConstants);
73735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
73745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
73755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
73765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdSetDepthBounds(VkCommandBuffer commandBuffer, float minDepthBounds, float maxDepthBounds) {
73775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
73785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
73795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
73805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
73815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
73825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_SETDEPTHBOUNDSSTATE, "vkCmdSetDepthBounds()");
73835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_DEPTH_BOUNDS_SET;
73845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->minDepthBounds = minDepthBounds;
73855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->maxDepthBounds = maxDepthBounds;
73865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
73875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
73885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
73895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdSetDepthBounds(commandBuffer, minDepthBounds, maxDepthBounds);
73905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
73915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
73925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
73935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdSetStencilCompareMask(VkCommandBuffer commandBuffer, VkStencilFaceFlags faceMask, uint32_t compareMask) {
73945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
73955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
73965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
73975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
73985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
73995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_SETSTENCILREADMASKSTATE, "vkCmdSetStencilCompareMask()");
74005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
74015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->front.compareMask = compareMask;
74025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
74035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
74045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->back.compareMask = compareMask;
74055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
74065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* TODO: Do we need to track front and back separately? */
74075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        /* TODO: We aren't capturing the faceMask, do we need to? */
74085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_STENCIL_READ_MASK_SET;
74095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
74105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
74115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
74125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdSetStencilCompareMask(commandBuffer, faceMask, compareMask);
74135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
74145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
74155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
74165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdSetStencilWriteMask(VkCommandBuffer commandBuffer, VkStencilFaceFlags faceMask, uint32_t writeMask) {
74175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
74185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
74195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
74205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
74215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
74225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_SETSTENCILWRITEMASKSTATE, "vkCmdSetStencilWriteMask()");
74235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
74245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->front.writeMask = writeMask;
74255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
74265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
74275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->back.writeMask = writeMask;
74285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
74295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_STENCIL_WRITE_MASK_SET;
74305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
74315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
74325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
74335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdSetStencilWriteMask(commandBuffer, faceMask, writeMask);
74345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
74355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
74365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
74375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdSetStencilReference(VkCommandBuffer commandBuffer, VkStencilFaceFlags faceMask, uint32_t reference) {
74385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
74395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
74405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
74415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
74425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
74435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_SETSTENCILREFERENCESTATE, "vkCmdSetStencilReference()");
74445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
74455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->front.reference = reference;
74465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
74475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
74485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->back.reference = reference;
74495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
74505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_STENCIL_REFERENCE_SET;
74515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
74525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
74535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
74545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdSetStencilReference(commandBuffer, faceMask, reference);
74555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
74565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
74575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
74585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdBindDescriptorSets(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout,
74595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        uint32_t firstSet, uint32_t setCount, const VkDescriptorSet *pDescriptorSets, uint32_t dynamicOffsetCount,
74605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        const uint32_t *pDynamicOffsets) {
74615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
74625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
74635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
74645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
74655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : Merge this with code below
74665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
74675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
74685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::vector<VkDescriptorSet> &activeDescriptorSets = cb_data->second.activeDescriptorSets;
74695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (activeDescriptorSets.size() < (setCount + firstSet)) {
74705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            activeDescriptorSets.resize(setCount + firstSet);
74715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
74725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < setCount; ++i) {
74735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            activeDescriptorSets[i + firstSet] = pDescriptorSets[i];
74745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
74755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
74765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Somewhere need to verify that all textures referenced by shaders in DS are in some type of *SHADER_READ* state
74775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
74785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
74795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
74805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->state == CB_RECORDING) {
7481787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            // Track total count of dynamic descriptor types to make sure we have an offset for each one
7482787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            uint32_t totalDynamicDescriptors = 0;
7483787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            string errorString = "";
7484787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            uint32_t lastSetIndex = firstSet + setCount - 1;
7485787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            if (lastSetIndex >= pCB->boundDescriptorSets.size())
7486787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                pCB->boundDescriptorSets.resize(lastSetIndex + 1);
7487787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            VkDescriptorSet oldFinalBoundSet = pCB->boundDescriptorSets[lastSetIndex];
7488787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            for (uint32_t i = 0; i < setCount; i++) {
7489787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                SET_NODE *pSet = getSetNode(dev_data, pDescriptorSets[i]);
7490787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                if (pSet) {
7491787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    pCB->uniqueBoundSets.insert(pDescriptorSets[i]);
7492787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    pSet->boundCmdBuffers.insert(commandBuffer);
7493787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    pCB->lastBoundDescriptorSet = pDescriptorSets[i];
7494787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    pCB->lastBoundPipelineLayout = layout;
7495787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    pCB->boundDescriptorSets[i + firstSet] = pDescriptorSets[i];
7496787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
7497787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i], __LINE__,
7498787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        DRAWSTATE_NONE, "DS", "DS %#" PRIxLEAST64 " bound on pipeline %s",
7499787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        (uint64_t)pDescriptorSets[i], string_VkPipelineBindPoint(pipelineBindPoint));
7500787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    if (!pSet->pUpdateStructs && (pSet->descriptorCount != 0)) {
7501787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT,
7502787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i],
7503787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            __LINE__, DRAWSTATE_DESCRIPTOR_SET_NOT_UPDATED, "DS",
7504787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            "DS %#" PRIxLEAST64
7505787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            " bound but it was never updated. You may want to either update it or not bind it.",
7506787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            (uint64_t)pDescriptorSets[i]);
7507787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    }
7508787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    // Verify that set being bound is compatible with overlapping setLayout of pipelineLayout
7509787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    if (!verify_set_layout_compatibility(dev_data, pSet, layout, i + firstSet, errorString)) {
7510787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7511787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i],
7512787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            __LINE__, DRAWSTATE_PIPELINE_LAYOUTS_INCOMPATIBLE, "DS",
7513787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            "descriptorSet #%u being bound is not compatible with overlapping layout in "
7514787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            "pipelineLayout due to: %s",
7515787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            i, errorString.c_str());
7516787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    }
7517787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    if (pSet->pLayout->dynamicDescriptorCount) {
7518787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                        // First make sure we won't overstep bounds of pDynamicOffsets array
7519787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                        if ((totalDynamicDescriptors + pSet->pLayout->dynamicDescriptorCount) > dynamicOffsetCount) {
7520787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            skipCall |=
7521787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7522787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i], __LINE__,
7523787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        DRAWSTATE_INVALID_DYNAMIC_OFFSET_COUNT, "DS",
7524787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        "descriptorSet #%u (%#" PRIxLEAST64
7525787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        ") requires %u dynamicOffsets, but only %u dynamicOffsets are left in pDynamicOffsets "
7526787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        "array. There must be one dynamic offset for each dynamic descriptor being bound.",
7527787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        i, (uint64_t)pDescriptorSets[i], pSet->pLayout->dynamicDescriptorCount,
7528787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        (dynamicOffsetCount - totalDynamicDescriptors));
7529787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                        } else { // Validate and store dynamic offsets with the set
7530787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            // Validate Dynamic Offset Minimums
7531787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            uint32_t cur_dyn_offset = totalDynamicDescriptors;
7532787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            for (uint32_t d = 0; d < pSet->descriptorCount; d++) {
7533787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                if (pSet->pLayout->descriptorTypes[d] == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
7534787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                    if (vk_safe_modulo(
7535787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            pDynamicOffsets[cur_dyn_offset],
7536787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            dev_data->physDevProperties.properties.limits.minUniformBufferOffsetAlignment) !=
7537787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        0) {
7538787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        skipCall |= log_msg(
7539787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7540787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__,
7541787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            DRAWSTATE_INVALID_UNIFORM_BUFFER_OFFSET, "DS",
7542787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            "vkCmdBindDescriptorSets(): pDynamicOffsets[%d] is %d but must be a multiple of "
7543787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            "device limit minUniformBufferOffsetAlignment %#" PRIxLEAST64,
7544787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            cur_dyn_offset, pDynamicOffsets[cur_dyn_offset],
7545787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            dev_data->physDevProperties.properties.limits.minUniformBufferOffsetAlignment);
7546787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                    }
7547787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                    cur_dyn_offset++;
7548787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                } else if (pSet->pLayout->descriptorTypes[d] == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
7549787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                    if (vk_safe_modulo(
7550787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            pDynamicOffsets[cur_dyn_offset],
7551787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            dev_data->physDevProperties.properties.limits.minStorageBufferOffsetAlignment) !=
7552787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        0) {
7553787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        skipCall |= log_msg(
7554787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7555787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT, 0, __LINE__,
7556787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            DRAWSTATE_INVALID_STORAGE_BUFFER_OFFSET, "DS",
7557787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            "vkCmdBindDescriptorSets(): pDynamicOffsets[%d] is %d but must be a multiple of "
7558787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            "device limit minStorageBufferOffsetAlignment %#" PRIxLEAST64,
7559787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            cur_dyn_offset, pDynamicOffsets[cur_dyn_offset],
7560787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                            dev_data->physDevProperties.properties.limits.minStorageBufferOffsetAlignment);
75615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    }
7562787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                    cur_dyn_offset++;
75635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                }
75645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            }
7565787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            // Keep running total of dynamic descriptor count to verify at the end
7566787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            totalDynamicDescriptors += pSet->pLayout->dynamicDescriptorCount;
75675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
75685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
7569787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                } else {
7570787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7571787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pDescriptorSets[i], __LINE__,
7572787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        DRAWSTATE_INVALID_SET, "DS", "Attempt to bind DS %#" PRIxLEAST64 " that doesn't exist!",
7573787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                        (uint64_t)pDescriptorSets[i]);
75745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
7575787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            }
7576787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            skipCall |= addCmd(dev_data, pCB, CMD_BINDDESCRIPTORSETS, "vkCmdBindDescrsiptorSets()");
7577787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            // For any previously bound sets, need to set them to "invalid" if they were disturbed by this update
7578787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            if (firstSet > 0) { // Check set #s below the first bound set
7579787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                for (uint32_t i = 0; i < firstSet; ++i) {
7580787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    if (pCB->boundDescriptorSets[i] &&
7581787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                        !verify_set_layout_compatibility(dev_data, dev_data->setMap[pCB->boundDescriptorSets[i]], layout, i,
75825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         errorString)) {
7583787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                        skipCall |= log_msg(
7584787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            dev_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
7585787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)pCB->boundDescriptorSets[i], __LINE__,
7586787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            DRAWSTATE_NONE, "DS",
7587787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            "DescriptorSetDS %#" PRIxLEAST64
7588787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            " previously bound as set #%u was disturbed by newly bound pipelineLayout (%#" PRIxLEAST64 ")",
7589787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                            (uint64_t)pCB->boundDescriptorSets[i], i, (uint64_t)layout);
7590787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                        pCB->boundDescriptorSets[i] = VK_NULL_HANDLE;
75915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
75925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
7593787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            }
7594787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            // Check if newly last bound set invalidates any remaining bound sets
7595787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            if ((pCB->boundDescriptorSets.size() - 1) > (lastSetIndex)) {
7596787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                if (oldFinalBoundSet &&
7597787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    !verify_set_layout_compatibility(dev_data, dev_data->setMap[oldFinalBoundSet], layout, lastSetIndex,
7598787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                                     errorString)) {
7599787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    skipCall |=
7600787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                        log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
7601787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, (uint64_t)oldFinalBoundSet, __LINE__,
7602787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                DRAWSTATE_NONE, "DS", "DescriptorSetDS %#" PRIxLEAST64
7603787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                                      " previously bound as set #%u is incompatible with set %#" PRIxLEAST64
7604787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                                      " newly bound as set #%u so set #%u and any subsequent sets were "
7605787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                                      "disturbed by newly bound pipelineLayout (%#" PRIxLEAST64 ")",
7606787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                (uint64_t)oldFinalBoundSet, lastSetIndex, (uint64_t)pCB->boundDescriptorSets[lastSetIndex],
7607787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                lastSetIndex, lastSetIndex + 1, (uint64_t)layout);
7608787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                    pCB->boundDescriptorSets.resize(lastSetIndex + 1);
76095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
76105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
7611787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            //  dynamicOffsetCount must equal the total number of dynamic descriptors in the sets being bound
7612787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            if (totalDynamicDescriptors != dynamicOffsetCount) {
7613787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
7614787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                    VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__,
7615787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                    DRAWSTATE_INVALID_DYNAMIC_OFFSET_COUNT, "DS",
7616787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                    "Attempting to bind %u descriptorSets with %u dynamic descriptors, but dynamicOffsetCount "
7617787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                    "is %u. It should exactly match the number of dynamic descriptors.",
7618787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                                    setCount, totalDynamicDescriptors, dynamicOffsetCount);
7619787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            }
7620787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            // Save dynamicOffsets bound to this CB
7621787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            for (uint32_t i = 0; i < dynamicOffsetCount; i++) {
7622787f29d93dee194c015789cf61c079504c980572Tobin Ehlis                pCB->dynamicOffsets.emplace_back(pDynamicOffsets[i]);
7623787f29d93dee194c015789cf61c079504c980572Tobin Ehlis            }
76245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
76255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdBindDescriptorSets()");
76265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
76275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
76285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
76295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
76305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdBindDescriptorSets(commandBuffer, pipelineBindPoint, layout, firstSet, setCount,
76315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                               pDescriptorSets, dynamicOffsetCount, pDynamicOffsets);
76325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
76335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
76345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
76355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdBindIndexBuffer(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType) {
76365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
76375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
76385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
76395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
76405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
76415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall =
76425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)(buffer), VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
76435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
76445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
76455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdBindIndexBuffer()"); };
76465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
76475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
76485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Somewhere need to verify that IBs have correct usage state flagged
76495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
76505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
76515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
76525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_BINDINDEXBUFFER, "vkCmdBindIndexBuffer()");
76535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkDeviceSize offset_align = 0;
76545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        switch (indexType) {
76555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_INDEX_TYPE_UINT16:
76565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            offset_align = 2;
76575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
76585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        case VK_INDEX_TYPE_UINT32:
76595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            offset_align = 4;
76605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
76615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        default:
76625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // ParamChecker should catch bad enum, we'll also throw alignment error below if offset_align stays 0
76635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            break;
76645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
76655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!offset_align || (offset % offset_align)) {
76665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
76675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_VTX_INDEX_ALIGNMENT_ERROR, "DS",
76685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkCmdBindIndexBuffer() offset (%#" PRIxLEAST64 ") does not fall on alignment (%s) boundary.",
76695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                offset, string_VkIndexType(indexType));
76705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
76715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->status |= CBSTATUS_INDEX_BUFFER_BOUND;
76725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
76735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
76745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
76755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdBindIndexBuffer(commandBuffer, buffer, offset, indexType);
76765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
76775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
76785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid updateResourceTracking(GLOBAL_CB_NODE *pCB, uint32_t firstBinding, uint32_t bindingCount, const VkBuffer *pBuffers) {
76795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t end = firstBinding + bindingCount;
76805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB->currentDrawData.buffers.size() < end) {
76815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->currentDrawData.buffers.resize(end);
76825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
76835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < bindingCount; ++i) {
76845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->currentDrawData.buffers[i + firstBinding] = pBuffers[i];
76855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
76865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
76875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
76885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid updateResourceTrackingOnDraw(GLOBAL_CB_NODE *pCB) { pCB->drawData.push_back(pCB->currentDrawData); }
76895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
76905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdBindVertexBuffers(VkCommandBuffer commandBuffer, uint32_t firstBinding,
76915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  uint32_t bindingCount, const VkBuffer *pBuffers,
76925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  const VkDeviceSize *pOffsets) {
76935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
76945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
76955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
76965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
76975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < bindingCount; ++i) {
76985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkDeviceMemory mem;
76995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)(pBuffers[i]),
77005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
77015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto cb_data = dev_data->cbMap.find(commandBuffer);
77025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (cb_data != dev_data->cbMap.end()) {
77035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            std::function<VkBool32()> function =
77045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdBindVertexBuffers()"); };
77055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            cb_data->second.validate_functions.push_back(function);
77065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
77075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
77085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Somewhere need to verify that VBs have correct usage state flagged
77095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
77105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
77115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
77125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        addCmd(dev_data, pCB, CMD_BINDVERTEXBUFFER, "vkCmdBindVertexBuffer()");
77135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        updateResourceTracking(pCB, firstBinding, bindingCount, pBuffers);
77145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
77155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdBindVertexBuffer()");
77165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
77175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
77185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
77195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdBindVertexBuffers(commandBuffer, firstBinding, bindingCount, pBuffers, pOffsets);
77205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
77215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
77225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
77235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis/* expects globalLock to be held by caller */
77245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool markStoreImagesAndBuffersAsWritten(VkCommandBuffer commandBuffer) {
77255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skip_call = false;
77265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
77275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = my_data->cbMap.find(commandBuffer);
77285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data == my_data->cbMap.end())
77295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return skip_call;
77305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::vector<VkDescriptorSet> &activeDescriptorSets = cb_data->second.activeDescriptorSets;
77315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto descriptorSet : activeDescriptorSets) {
77325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto ds_data = my_data->descriptorSetMap.find(descriptorSet);
77335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (ds_data == my_data->descriptorSetMap.end())
77345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
77355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::vector<VkImageView> images = ds_data->second.images;
77365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::vector<VkBuffer> buffers = ds_data->second.buffers;
77375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto imageView : images) {
77385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto iv_data = my_data->imageViewMap.find(imageView);
77395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (iv_data == my_data->imageViewMap.end())
77405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                continue;
77415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkImage image = iv_data->second.image;
77425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkDeviceMemory mem;
77435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
77445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                get_mem_binding_from_object(my_data, commandBuffer, (uint64_t)image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
77455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            std::function<VkBool32()> function = [=]() {
77465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                set_memory_valid(my_data, mem, true, image);
77475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return VK_FALSE;
77485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            };
77495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            cb_data->second.validate_functions.push_back(function);
77505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
77515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto buffer : buffers) {
77525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkDeviceMemory mem;
77535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
77545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                get_mem_binding_from_object(my_data, commandBuffer, (uint64_t)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
77555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            std::function<VkBool32()> function = [=]() {
77565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                set_memory_valid(my_data, mem, true);
77575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return VK_FALSE;
77585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            };
77595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            cb_data->second.validate_functions.push_back(function);
77605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
77615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
77625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
77635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
77645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
77655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
77665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
77675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     uint32_t firstVertex, uint32_t firstInstance) {
77685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
77695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
77705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
77715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
77725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : merge with code below
77735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = markStoreImagesAndBuffersAsWritten(commandBuffer);
77745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
77755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
77765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
77775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_DRAW, "vkCmdDraw()");
77785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->drawCount[DRAW]++;
77795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validate_draw_state(dev_data, pCB, VK_FALSE);
77805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Need to pass commandBuffer as srcObj here
77815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |=
77825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
77835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    __LINE__, DRAWSTATE_NONE, "DS", "vkCmdDraw() call #%" PRIu64 ", reporting DS state:", g_drawCount[DRAW]++);
77845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
77855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (VK_FALSE == skipCall) {
77865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            updateResourceTrackingOnDraw(pCB);
77875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
77885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDraw");
77895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
77905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
77915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
77925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
77935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
77945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
77955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount,
77965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                            uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset,
77975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                            uint32_t firstInstance) {
77985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
77995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
78005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
78015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
78025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : merge with code below
78035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = markStoreImagesAndBuffersAsWritten(commandBuffer);
78045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
78055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
78065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
78075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_DRAWINDEXED, "vkCmdDrawIndexed()");
78085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->drawCount[DRAW_INDEXED]++;
78095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validate_draw_state(dev_data, pCB, VK_TRUE);
78105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Need to pass commandBuffer as srcObj here
78115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
78125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_NONE, "DS",
78135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "vkCmdDrawIndexed() call #%" PRIu64 ", reporting DS state:", g_drawCount[DRAW_INDEXED]++);
78145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
78155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (VK_FALSE == skipCall) {
78165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            updateResourceTrackingOnDraw(pCB);
78175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
78185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDrawIndexed");
78195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
78205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
78215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
78225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset,
78235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                        firstInstance);
78245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
78255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
78265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
78275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count, uint32_t stride) {
78285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
78295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
78305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
78315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
78325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
78335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : merge with code below
78345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall =
78355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
78365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdDrawIndirect");
78375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= markStoreImagesAndBuffersAsWritten(commandBuffer);
78385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
78395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
78405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
78415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_DRAWINDIRECT, "vkCmdDrawIndirect()");
78425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->drawCount[DRAW_INDIRECT]++;
78435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validate_draw_state(dev_data, pCB, VK_FALSE);
78445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Need to pass commandBuffer as srcObj here
78455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
78465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, __LINE__, DRAWSTATE_NONE, "DS",
78475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "vkCmdDrawIndirect() call #%" PRIu64 ", reporting DS state:", g_drawCount[DRAW_INDIRECT]++);
78485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
78495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (VK_FALSE == skipCall) {
78505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            updateResourceTrackingOnDraw(pCB);
78515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
78525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDrawIndirect");
78535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
78545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
78555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
78565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdDrawIndirect(commandBuffer, buffer, offset, count, stride);
78575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
78585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
78595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
78605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count, uint32_t stride) {
78615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
78625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
78635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
78645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
78655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
78665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // MTMTODO : merge with code below
78675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall =
78685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
78695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdDrawIndexedIndirect");
78705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= markStoreImagesAndBuffersAsWritten(commandBuffer);
78715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
78725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
78735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
78745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_DRAWINDEXEDINDIRECT, "vkCmdDrawIndexedIndirect()");
78755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->drawCount[DRAW_INDEXED_INDIRECT]++;
78765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
78775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validate_draw_state(dev_data, pCB, VK_TRUE);
78785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
78795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO : Need to pass commandBuffer as srcObj here
78805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |=
78815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            log_msg(dev_data->report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
78825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    __LINE__, DRAWSTATE_NONE, "DS", "vkCmdDrawIndexedIndirect() call #%" PRIu64 ", reporting DS state:",
78835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    g_drawCount[DRAW_INDEXED_INDIRECT]++);
78845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= synchAndPrintDSConfig(dev_data, commandBuffer);
78855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (VK_FALSE == skipCall) {
78865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            updateResourceTrackingOnDraw(pCB);
78875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
78885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdDrawIndexedIndirect");
78895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
78905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
78915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
78925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride);
78935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
78945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
78955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
78965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
78975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
78985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
78995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
79005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = markStoreImagesAndBuffersAsWritten(commandBuffer);
79015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
79025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
79035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
79045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_DISPATCH, "vkCmdDispatch()");
79055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdDispatch");
79065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
79075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
79085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
79095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdDispatch(commandBuffer, x, y, z);
79105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
79115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
79125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
79135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
79145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
79155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
79165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
79175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
79185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
79195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall =
79205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
79215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdDispatchIndirect");
79225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= markStoreImagesAndBuffersAsWritten(commandBuffer);
79235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
79245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
79255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
79265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_DISPATCHINDIRECT, "vkCmdDispatchIndirect()");
79275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdDispatchIndirect");
79285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
79295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
79305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
79315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdDispatchIndirect(commandBuffer, buffer, offset);
79325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
79335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
79345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
79355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                           uint32_t regionCount, const VkBufferCopy *pRegions) {
79365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
79375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
79385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
79395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
79405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
79415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
79425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
79435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall =
79445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)srcBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
79455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
79465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdCopyBuffer()"); };
79475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
79485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
79495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyBuffer");
79505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |=
79515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)dstBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
79525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
79535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
79545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true);
79555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
79565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
79575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
79585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
79595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyBuffer");
79605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate that SRC & DST buffers have correct usage flags set
79615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_buffer_usage_flags(dev_data, commandBuffer, srcBuffer, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true,
79625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
79635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_buffer_usage_flags(dev_data, commandBuffer, dstBuffer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true,
79645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
79655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
79665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
79675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
79685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_COPYBUFFER, "vkCmdCopyBuffer()");
79695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyBuffer");
79705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
79715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
79725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
79735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, regionCount, pRegions);
79745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
79755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
79765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 VerifySourceImageLayout(VkCommandBuffer cmdBuffer, VkImage srcImage, VkImageSubresourceLayers subLayers,
79775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 VkImageLayout srcImageLayout) {
79785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
79795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
79805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
79815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
79825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
79835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t layer = i + subLayers.baseArrayLayer;
79845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
79855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        IMAGE_CMD_BUF_LAYOUT_NODE node;
79865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!FindLayout(pCB, srcImage, sub, node)) {
79875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            SetLayout(pCB, srcImage, sub, {srcImageLayout, srcImageLayout});
79885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
79895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
79905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (node.layout != srcImageLayout) {
79915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO: Improve log message in the next pass
79925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
79935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
79945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Cannot copy from an image whose source layout is %s "
79955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                        "and doesn't match the current layout %s.",
79965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        string_VkImageLayout(srcImageLayout), string_VkImageLayout(node.layout));
79975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
79985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
79995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (srcImageLayout != VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL) {
80005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (srcImageLayout == VK_IMAGE_LAYOUT_GENERAL) {
80015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
80025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0,
80035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
80045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 "Layout for input image should be TRANSFER_SRC_OPTIMAL instead of GENERAL.");
80055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
80065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
80075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Layout for input image is %s but can only be "
80085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                       "TRANSFER_SRC_OPTIMAL or GENERAL.",
80095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 string_VkImageLayout(srcImageLayout));
80105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
80115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
80125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
80135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
80145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
80155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 VerifyDestImageLayout(VkCommandBuffer cmdBuffer, VkImage destImage, VkImageSubresourceLayers subLayers,
80165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               VkImageLayout destImageLayout) {
80175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
80185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
80195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
80205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
80215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
80225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t layer = i + subLayers.baseArrayLayer;
80235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
80245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        IMAGE_CMD_BUF_LAYOUT_NODE node;
80255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!FindLayout(pCB, destImage, sub, node)) {
80265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            SetLayout(pCB, destImage, sub, {destImageLayout, destImageLayout});
80275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
80285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
80295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (node.layout != destImageLayout) {
80305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
80315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
80325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Cannot copy from an image whose dest layout is %s and "
80335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                        "doesn't match the current layout %s.",
80345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        string_VkImageLayout(destImageLayout), string_VkImageLayout(node.layout));
80355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
80365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
80375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (destImageLayout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
80385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (destImageLayout == VK_IMAGE_LAYOUT_GENERAL) {
80395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
80405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0,
80415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
80425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 "Layout for output image should be TRANSFER_DST_OPTIMAL instead of GENERAL.");
80435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
80445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
80455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Layout for output image is %s but can only be "
80465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                       "TRANSFER_DST_OPTIMAL or GENERAL.",
80475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 string_VkImageLayout(destImageLayout));
80485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
80495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
80505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
80515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
80525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
80535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
80545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
80555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis               VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageCopy *pRegions) {
80565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
80575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
80585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
80595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
80605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
80615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
80625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate that src & dst images have correct usage flags set
80635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)srcImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
80645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
80655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdCopyImage()", srcImage); };
80665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
80675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
80685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyImage");
80695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |=
80705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)dstImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
80715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
80725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
80735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true, dstImage);
80745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
80755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
80765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
80775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
80785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyImage");
80795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_image_usage_flags(dev_data, commandBuffer, srcImage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true,
80805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
80815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_image_usage_flags(dev_data, commandBuffer, dstImage, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true,
80825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
80835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
80845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
80855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
80865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_COPYIMAGE, "vkCmdCopyImage()");
80875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyImage");
80885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < regionCount; ++i) {
80895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= VerifySourceImageLayout(commandBuffer, srcImage, pRegions[i].srcSubresource, srcImageLayout);
80905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= VerifyDestImageLayout(commandBuffer, dstImage, pRegions[i].dstSubresource, dstImageLayout);
80915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
80925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
80935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
80945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
80955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdCopyImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout,
80965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                      regionCount, pRegions);
80975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
80985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
80995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
81005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
81015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis               VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) {
81025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
81035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
81045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
81055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
81065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
81075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
81085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate that src & dst images have correct usage flags set
81095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)srcImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
81105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
81115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdBlitImage()", srcImage); };
81125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
81135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
81145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdBlitImage");
81155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |=
81165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)dstImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
81175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
81185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
81195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true, dstImage);
81205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
81215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
81225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
81235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
81245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdBlitImage");
81255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_image_usage_flags(dev_data, commandBuffer, srcImage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true,
81265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
81275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_image_usage_flags(dev_data, commandBuffer, dstImage, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true,
81285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
81295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
81305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
81315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
81325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_BLITIMAGE, "vkCmdBlitImage()");
81335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdBlitImage");
81345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
81355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
81365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
81375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout,
81385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                      regionCount, pRegions, filter);
81395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
81405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
81415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer,
81425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  VkImage dstImage, VkImageLayout dstImageLayout,
81435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  uint32_t regionCount, const VkBufferImageCopy *pRegions) {
81445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
81455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
81465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
81475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
81485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
81495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
81505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)dstImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
81515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
81525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
81535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true, dstImage);
81545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
81555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
81565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
81575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
81585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyBufferToImage");
81595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |=
81605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)srcBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
81615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
81625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdCopyBufferToImage()"); };
81635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
81645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
81655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyBufferToImage");
81665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate that src buff & dst image have correct usage flags set
81675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_buffer_usage_flags(dev_data, commandBuffer, srcBuffer, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true,
81685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "vkCmdCopyBufferToImage()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
81695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_image_usage_flags(dev_data, commandBuffer, dstImage, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true,
81705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           "vkCmdCopyBufferToImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
81715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
81725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
81735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
81745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_COPYBUFFERTOIMAGE, "vkCmdCopyBufferToImage()");
81755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyBufferToImage");
81765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < regionCount; ++i) {
81775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= VerifyDestImageLayout(commandBuffer, dstImage, pRegions[i].imageSubresource, dstImageLayout);
81785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
81795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
81805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
81815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
81825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount,
81835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                              pRegions);
81845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
81855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
81865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
81875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  VkImageLayout srcImageLayout, VkBuffer dstBuffer,
81885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  uint32_t regionCount, const VkBufferImageCopy *pRegions) {
81895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
81905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
81915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
81925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
81935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
81945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
81955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)srcImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
81965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
81975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function =
81985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdCopyImageToBuffer()", srcImage); };
81995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
82005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
82015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyImageToBuffer");
82025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |=
82035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)dstBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
82045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
82055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
82065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true);
82075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
82085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
82095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
82105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
82115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyImageToBuffer");
82125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate that dst buff & src image have correct usage flags set
82135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_image_usage_flags(dev_data, commandBuffer, srcImage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true,
82145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           "vkCmdCopyImageToBuffer()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
82155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_buffer_usage_flags(dev_data, commandBuffer, dstBuffer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true,
82165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "vkCmdCopyImageToBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
82175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
82185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
82195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
82205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_COPYIMAGETOBUFFER, "vkCmdCopyImageToBuffer()");
82215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyImageToBuffer");
82225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < regionCount; ++i) {
82235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= VerifySourceImageLayout(commandBuffer, srcImage, pRegions[i].imageSubresource, srcImageLayout);
82245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
82255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
82265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
82275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
82285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount,
82295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                              pRegions);
82305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
82315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
82325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer,
82335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                             VkDeviceSize dstOffset, VkDeviceSize dataSize, const uint32_t *pData) {
82345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
82355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
82365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
82375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
82385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
82395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
82405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall =
82415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)dstBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
82425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
82435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
82445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true);
82455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
82465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
82475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
82485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
82495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdUpdateBuffer");
82505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate that dst buff has correct usage flags set
82515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_buffer_usage_flags(dev_data, commandBuffer, dstBuffer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true,
82525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "vkCmdUpdateBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
82535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
82545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
82555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
82565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_UPDATEBUFFER, "vkCmdUpdateBuffer()");
82575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyUpdateBuffer");
82585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
82595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
82605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
82615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
82625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
82635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
82645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
82655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize size, uint32_t data) {
82665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
82675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
82685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
82695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
82705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
82715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
82725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall =
82735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)dstBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
82745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
82755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
82765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true);
82775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
82785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
82795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
82805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
82815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdFillBuffer");
82825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate that dst buff has correct usage flags set
82835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_buffer_usage_flags(dev_data, commandBuffer, dstBuffer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true,
82845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "vkCmdFillBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
82855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
82865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
82875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
82885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_FILLBUFFER, "vkCmdFillBuffer()");
82895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyFillBuffer");
82905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
82915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
82925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
82935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
82945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
82955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
82965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdClearAttachments(VkCommandBuffer commandBuffer, uint32_t attachmentCount,
82975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                 const VkClearAttachment *pAttachments, uint32_t rectCount,
82985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                 const VkClearRect *pRects) {
82995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
83005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
83015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
83025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
83035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
83045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_CLEARATTACHMENTS, "vkCmdClearAttachments()");
83055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
83065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!hasDrawCmd(pCB) && (pCB->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
83075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            (pCB->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
83085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO : commandBuffer should be srcObj
83095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
83105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // 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
83115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // call CmdClearAttachments
83125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Otherwise this seems more like a performance warning.
83135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
83145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0, 0, DRAWSTATE_CLEAR_CMD_BEFORE_DRAW, "DS",
83155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkCmdClearAttachments() issued on CB object 0x%" PRIxLEAST64 " prior to any Draw Cmds."
83165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                " It is recommended you use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
83175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)(commandBuffer));
83185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
83195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdClearAttachments");
83205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
83215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
83225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate that attachment is in reference list of active subpass
83235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB->activeRenderPass) {
83245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkRenderPassCreateInfo *pRPCI = dev_data->renderPassMap[pCB->activeRenderPass]->pCreateInfo;
83255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkSubpassDescription *pSD = &pRPCI->pSubpasses[pCB->activeSubpass];
83265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
83275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t attachment_idx = 0; attachment_idx < attachmentCount; attachment_idx++) {
83285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            const VkClearAttachment *attachment = &pAttachments[attachment_idx];
83295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (attachment->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
83305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkBool32 found = VK_FALSE;
83315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (uint32_t i = 0; i < pSD->colorAttachmentCount; i++) {
83325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (attachment->colorAttachment == pSD->pColorAttachments[i].attachment) {
83335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        found = VK_TRUE;
83345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        break;
83355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
83365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
83375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (VK_FALSE == found) {
83385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(
83395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
83405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)commandBuffer, __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
83415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkCmdClearAttachments() attachment index %d not found in attachment reference array of active subpass %d",
83425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        attachment->colorAttachment, pCB->activeSubpass);
83435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
83445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else if (attachment->aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
83455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (!pSD->pDepthStencilAttachment || // Says no DS will be used in active subpass
83465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (pSD->pDepthStencilAttachment->attachment ==
83475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     VK_ATTACHMENT_UNUSED)) { // Says no DS will be used in active subpass
83485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
83495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(
83505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
83515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)commandBuffer, __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
83525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkCmdClearAttachments() attachment index %d does not match depthStencilAttachment.attachment (%d) found "
83535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "in active subpass %d",
83545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        attachment->colorAttachment,
83555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (pSD->pDepthStencilAttachment) ? pSD->pDepthStencilAttachment->attachment : VK_ATTACHMENT_UNUSED,
83565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        pCB->activeSubpass);
83575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
83585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
83595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
83605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
83615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
83625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
83635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdClearAttachments(commandBuffer, attachmentCount, pAttachments, rectCount, pRects);
83645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
83655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
83665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image,
83675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                VkImageLayout imageLayout, const VkClearColorValue *pColor,
83685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
83695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
83705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
83715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
83725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
83735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
83745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
83755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
83765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
83775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
83785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
83795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true, image);
83805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
83815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
83825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
83835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
83845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdClearColorImage");
83855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
83865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
83875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
83885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_CLEARCOLORIMAGE, "vkCmdClearColorImage()");
83895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdClearColorImage");
83905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
83915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
83925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
83935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
83945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
83955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
83965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
83975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
83985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
83995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            const VkImageSubresourceRange *pRanges) {
84005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
84015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
84025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
84035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
84045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
84055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
84065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
84075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
84085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
84095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
84105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true, image);
84115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
84125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
84135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
84145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
84155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdClearDepthStencilImage");
84165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
84175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
84185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
84195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_CLEARDEPTHSTENCILIMAGE, "vkCmdClearDepthStencilImage()");
84205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdClearDepthStencilImage");
84215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
84225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
84235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
84245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount,
84255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                   pRanges);
84265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
84275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
84285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
84295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
84305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                  VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageResolve *pRegions) {
84315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
84325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
84335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
84345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
84355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
84365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
84375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall = get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)srcImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
84385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
84395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function =
84405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            [=]() { return validate_memory_is_valid(dev_data, mem, "vkCmdResolveImage()", srcImage); };
84415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
84425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
84435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdResolveImage");
84445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |=
84455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)dstImage, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
84465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
84475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
84485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true, dstImage);
84495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
84505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
84515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
84525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
84535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdResolveImage");
84545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
84555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
84565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
84575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_RESOLVEIMAGE, "vkCmdResolveImage()");
84585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdResolveImage");
84595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
84605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
84615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
84625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout,
84635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         regionCount, pRegions);
84645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
84655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
84665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
84675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
84685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
84695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
84705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
84715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
84725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
84735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_SETEVENT, "vkCmdSetEvent()");
84745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdSetEvent");
84755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->events.push_back(event);
84765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->eventToStageMap[event] = stageMask;
84775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
84785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
84795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
84805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdSetEvent(commandBuffer, event, stageMask);
84815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
84825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
84835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
84845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
84855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
84865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
84875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
84885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
84895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
84905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_RESETEVENT, "vkCmdResetEvent()");
84915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdResetEvent");
84925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->events.push_back(event);
84935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
84945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
84955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
84965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdResetEvent(commandBuffer, event, stageMask);
84975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
84985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
84995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 TransitionImageLayouts(VkCommandBuffer cmdBuffer, uint32_t memBarrierCount, const VkImageMemoryBarrier *pImgMemBarriers) {
85005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
85015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
85025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip = VK_FALSE;
85035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t levelCount = 0;
85045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t layerCount = 0;
85055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
85065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < memBarrierCount; ++i) {
85075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto mem_barrier = &pImgMemBarriers[i];
85085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!mem_barrier)
85095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
85105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO: Do not iterate over every possibility - consolidate where
85115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // possible
85125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        ResolveRemainingLevelsLayers(dev_data, &levelCount, &layerCount, mem_barrier->subresourceRange, mem_barrier->image);
85135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
85145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < levelCount; j++) {
85155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t level = mem_barrier->subresourceRange.baseMipLevel + j;
85165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t k = 0; k < layerCount; k++) {
85175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                uint32_t layer = mem_barrier->subresourceRange.baseArrayLayer + k;
85185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkImageSubresource sub = {mem_barrier->subresourceRange.aspectMask, level, layer};
85195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                IMAGE_CMD_BUF_LAYOUT_NODE node;
85205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (!FindLayout(pCB, mem_barrier->image, sub, node)) {
85215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    SetLayout(pCB, mem_barrier->image, sub, {mem_barrier->oldLayout, mem_barrier->newLayout});
85225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    continue;
85235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
85245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
85255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // TODO: Set memory invalid which is in mem_tracker currently
85265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else if (node.layout != mem_barrier->oldLayout) {
85275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
85285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "You cannot transition the layout from %s "
85295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                                    "when current layout is %s.",
85305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    string_VkImageLayout(mem_barrier->oldLayout), string_VkImageLayout(node.layout));
85315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
85325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                SetLayout(pCB, mem_barrier->image, sub, mem_barrier->newLayout);
85335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
85345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
85355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
85365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip;
85375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
85385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
85395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Print readable FlagBits in FlagMask
85405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstd::string string_VkAccessFlags(VkAccessFlags accessMask) {
85415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::string result;
85425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::string separator;
85435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
85445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (accessMask == 0) {
85455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = "[None]";
85465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
85475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = "[";
85485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto i = 0; i < 32; i++) {
85495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (accessMask & (1 << i)) {
85505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                result = result + separator + string_VkAccessFlagBits((VkAccessFlagBits)(1 << i));
85515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                separator = " | ";
85525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
85535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
85545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = result + "]";
85555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
85565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
85575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
85585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
85595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// AccessFlags MUST have 'required_bit' set, and may have one or more of 'optional_bits' set.
85605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// If required_bit is zero, accessMask must have at least one of 'optional_bits' set
85615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// TODO: Add tracking to ensure that at least one barrier has been set for these layout transitions
85625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 ValidateMaskBits(const layer_data *my_data, VkCommandBuffer cmdBuffer, const VkAccessFlags &accessMask,
85635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                          const VkImageLayout &layout, VkAccessFlags required_bit, VkAccessFlags optional_bits, const char *type) {
85645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
85655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
85665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if ((accessMask & required_bit) || (!required_bit && (accessMask & optional_bits))) {
85675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (accessMask & !(required_bit | optional_bits)) {
85685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO: Verify against Valid Use
85695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
85705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
85715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_BARRIER, "DS", "Additional bits in %s accessMask %d %s are specified when layout is %s.",
85725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        type, accessMask, string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
85735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
85745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
85755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!required_bit) {
85765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
85775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 DRAWSTATE_INVALID_BARRIER, "DS", "%s AccessMask %d %s must contain at least one of access bits %d "
85785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  "%s when layout is %s, unless the app has previously added a "
85795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  "barrier for this transition.",
85805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 type, accessMask, string_VkAccessFlags(accessMask).c_str(), optional_bits,
85815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 string_VkAccessFlags(optional_bits).c_str(), string_VkImageLayout(layout));
85825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
85835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            std::string opt_bits;
85845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (optional_bits != 0) {
85855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                std::stringstream ss;
85865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                ss << optional_bits;
85875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                opt_bits = "and may have optional bits " + ss.str() + ' ' + string_VkAccessFlags(optional_bits);
85885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
85895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
85905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 DRAWSTATE_INVALID_BARRIER, "DS", "%s AccessMask %d %s must have required access bit %d %s %s when "
85915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  "layout is %s, unless the app has previously added a barrier for "
85925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  "this transition.",
85935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 type, accessMask, string_VkAccessFlags(accessMask).c_str(), required_bit,
85945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 string_VkAccessFlags(required_bit).c_str(), opt_bits.c_str(), string_VkImageLayout(layout));
85955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
85965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
85975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
85985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
85995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
86005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 ValidateMaskBitsFromLayouts(const layer_data *my_data, VkCommandBuffer cmdBuffer, const VkAccessFlags &accessMask,
86015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     const VkImageLayout &layout, const char *type) {
86025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
86035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    switch (layout) {
86045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: {
86055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
86065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      VK_ACCESS_COLOR_ATTACHMENT_READ_BIT, type);
86075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
86085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
86095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: {
86105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
86115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, type);
86125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
86135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
86145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: {
86155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_WRITE_BIT, 0, type);
86165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
86175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
86185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_IMAGE_LAYOUT_PREINITIALIZED: {
86195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_HOST_WRITE_BIT, 0, type);
86205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
86215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
86225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: {
86235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, 0,
86245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
86255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
86265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
86275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: {
86285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, 0,
86295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                      VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
86305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
86315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
86325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: {
86335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= ValidateMaskBits(my_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_READ_BIT, 0, type);
86345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
86355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
86365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_IMAGE_LAYOUT_UNDEFINED: {
86375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (accessMask != 0) {
86385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO: Verify against Valid Use section spec
86395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
86405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
86415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_BARRIER, "DS", "Additional bits in %s accessMask %d %s are specified when layout is %s.",
86425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        type, accessMask, string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
86435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
86445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        break;
86455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
86465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    case VK_IMAGE_LAYOUT_GENERAL:
86475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    default: { break; }
86485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
86495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
86505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
86515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
86525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 ValidateBarriers(const char *funcName, VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
86535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                          const VkMemoryBarrier *pMemBarriers, uint32_t bufferBarrierCount,
86545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                          const VkBufferMemoryBarrier *pBufferMemBarriers, uint32_t imageMemBarrierCount,
86555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                          const VkImageMemoryBarrier *pImageMemBarriers) {
86565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
86575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
86585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
86595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB->activeRenderPass && memBarrierCount) {
86605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!dev_data->renderPassMap[pCB->activeRenderPass]->hasSelfDependency[pCB->activeSubpass]) {
86615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
86625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 DRAWSTATE_INVALID_BARRIER, "DS", "%s: Barriers cannot be set during subpass %d "
86635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  "with no self dependency specified.",
86645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 funcName, pCB->activeSubpass);
86655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
86665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
86675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < imageMemBarrierCount; ++i) {
86685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto mem_barrier = &pImageMemBarriers[i];
86695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto image_data = dev_data->imageMap.find(mem_barrier->image);
86705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (image_data != dev_data->imageMap.end()) {
86715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t src_q_f_index = mem_barrier->srcQueueFamilyIndex;
86725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t dst_q_f_index = mem_barrier->dstQueueFamilyIndex;
86735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (image_data->second.createInfo.sharingMode == VK_SHARING_MODE_CONCURRENT) {
86745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // srcQueueFamilyIndex and dstQueueFamilyIndex must both
86755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // be VK_QUEUE_FAMILY_IGNORED
86765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if ((src_q_f_index != VK_QUEUE_FAMILY_IGNORED) || (dst_q_f_index != VK_QUEUE_FAMILY_IGNORED)) {
86775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
86785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         __LINE__, DRAWSTATE_INVALID_QUEUE_INDEX, "DS",
86795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         "%s: Image Barrier for image 0x%" PRIx64 " was created with sharingMode of "
86805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         "VK_SHARING_MODE_CONCURRENT.  Src and dst "
86815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         " queueFamilyIndices must be VK_QUEUE_FAMILY_IGNORED.",
86825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         funcName, reinterpret_cast<const uint64_t &>(mem_barrier->image));
86835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
86845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
86855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Sharing mode is VK_SHARING_MODE_EXCLUSIVE. srcQueueFamilyIndex and
86865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // dstQueueFamilyIndex must either both be VK_QUEUE_FAMILY_IGNORED,
86875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // or both be a valid queue family
86885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (((src_q_f_index == VK_QUEUE_FAMILY_IGNORED) || (dst_q_f_index == VK_QUEUE_FAMILY_IGNORED)) &&
86895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (src_q_f_index != dst_q_f_index)) {
86905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call |=
86915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
86925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INVALID_QUEUE_INDEX, "DS", "%s: Image 0x%" PRIx64 " was created with sharingMode "
86935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                     "of VK_SHARING_MODE_EXCLUSIVE. If one of src- or "
86945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                     "dstQueueFamilyIndex is VK_QUEUE_FAMILY_IGNORED, both "
86955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                     "must be.",
86965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                funcName, reinterpret_cast<const uint64_t &>(mem_barrier->image));
86975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else if (((src_q_f_index != VK_QUEUE_FAMILY_IGNORED) && (dst_q_f_index != VK_QUEUE_FAMILY_IGNORED)) &&
86985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                           ((src_q_f_index >= dev_data->physDevProperties.queue_family_properties.size()) ||
86995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (dst_q_f_index >= dev_data->physDevProperties.queue_family_properties.size()))) {
87005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
87015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         __LINE__, DRAWSTATE_INVALID_QUEUE_INDEX, "DS",
87025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         "%s: Image 0x%" PRIx64 " was created with sharingMode "
87035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         "of VK_SHARING_MODE_EXCLUSIVE, but srcQueueFamilyIndex %d"
87045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         " or dstQueueFamilyIndex %d is greater than " PRINTF_SIZE_T_SPECIFIER
87055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         "queueFamilies crated for this device.",
87065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         funcName, reinterpret_cast<const uint64_t &>(mem_barrier->image), src_q_f_index,
87075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         dst_q_f_index, dev_data->physDevProperties.queue_family_properties.size());
87085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
87095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
87105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
87115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
87125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (mem_barrier) {
87135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
87145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                ValidateMaskBitsFromLayouts(dev_data, cmdBuffer, mem_barrier->srcAccessMask, mem_barrier->oldLayout, "Source");
87155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
87165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                ValidateMaskBitsFromLayouts(dev_data, cmdBuffer, mem_barrier->dstAccessMask, mem_barrier->newLayout, "Dest");
87175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (mem_barrier->newLayout == VK_IMAGE_LAYOUT_UNDEFINED || mem_barrier->newLayout == VK_IMAGE_LAYOUT_PREINITIALIZED) {
87185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
87195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_BARRIER, "DS", "%s: Image Layout cannot be transitioned to UNDEFINED or "
87205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                         "PREINITIALIZED.",
87215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        funcName);
87225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
87235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto image_data = dev_data->imageMap.find(mem_barrier->image);
87245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkFormat format;
87255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t arrayLayers, mipLevels;
87265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            bool imageFound = false;
87275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (image_data != dev_data->imageMap.end()) {
87285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                format = image_data->second.createInfo.format;
87295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                arrayLayers = image_data->second.createInfo.arrayLayers;
87305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                mipLevels = image_data->second.createInfo.mipLevels;
87315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                imageFound = true;
87325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else if (dev_data->device_extensions.wsi_enabled) {
87335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto imageswap_data = dev_data->device_extensions.imageToSwapchainMap.find(mem_barrier->image);
87345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (imageswap_data != dev_data->device_extensions.imageToSwapchainMap.end()) {
87355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    auto swapchain_data = dev_data->device_extensions.swapchainMap.find(imageswap_data->second);
87365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (swapchain_data != dev_data->device_extensions.swapchainMap.end()) {
87375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        format = swapchain_data->second->createInfo.imageFormat;
87385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        arrayLayers = swapchain_data->second->createInfo.imageArrayLayers;
87395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        mipLevels = 1;
87405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        imageFound = true;
87415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
87425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
87435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
87445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (imageFound) {
87455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (vk_format_is_depth_and_stencil(format) &&
87465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (!(mem_barrier->subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) ||
87475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     !(mem_barrier->subresourceRange.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT))) {
87485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
87495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_BARRIER, "DS", "%s: Image is a depth and stencil format and thus must "
87505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                             "have both VK_IMAGE_ASPECT_DEPTH_BIT and "
87515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                             "VK_IMAGE_ASPECT_STENCIL_BIT set.",
87525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            funcName);
87535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
8754f0d091deca3d4b90e1f31d75e062684dca160ad1Michael Lentine                int layerCount = (mem_barrier->subresourceRange.layerCount == VK_REMAINING_ARRAY_LAYERS)
8755f0d091deca3d4b90e1f31d75e062684dca160ad1Michael Lentine                                     ? 1
8756f0d091deca3d4b90e1f31d75e062684dca160ad1Michael Lentine                                     : mem_barrier->subresourceRange.layerCount;
8757f0d091deca3d4b90e1f31d75e062684dca160ad1Michael Lentine                if ((mem_barrier->subresourceRange.baseArrayLayer + layerCount) > arrayLayers) {
87585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
87595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_BARRIER, "DS", "%s: Subresource must have the sum of the "
87605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                             "baseArrayLayer (%d) and layerCount (%d) be less "
87615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                             "than or equal to the total number of layers (%d).",
87625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            funcName, mem_barrier->subresourceRange.baseArrayLayer, mem_barrier->subresourceRange.layerCount,
87635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            arrayLayers);
87645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
8765f0d091deca3d4b90e1f31d75e062684dca160ad1Michael Lentine                int levelCount = (mem_barrier->subresourceRange.levelCount == VK_REMAINING_MIP_LEVELS)
8766f0d091deca3d4b90e1f31d75e062684dca160ad1Michael Lentine                                     ? 1
8767f0d091deca3d4b90e1f31d75e062684dca160ad1Michael Lentine                                     : mem_barrier->subresourceRange.levelCount;
8768f0d091deca3d4b90e1f31d75e062684dca160ad1Michael Lentine                if ((mem_barrier->subresourceRange.baseMipLevel + levelCount) > mipLevels) {
87695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
87705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_BARRIER, "DS", "%s: Subresource must have the sum of the baseMipLevel "
87715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                             "(%d) and levelCount (%d) be less than or equal to "
87725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                             "the total number of levels (%d).",
87735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            funcName, mem_barrier->subresourceRange.baseMipLevel, mem_barrier->subresourceRange.levelCount,
87745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            mipLevels);
87755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
87765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
87775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
87785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
87795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < bufferBarrierCount; ++i) {
87805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto mem_barrier = &pBufferMemBarriers[i];
87815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->activeRenderPass) {
87825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
87835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
87845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_BARRIER, "DS", "%s: Buffer Barriers cannot be used during a render pass.", funcName);
87855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
87865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!mem_barrier)
87875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
87885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
87895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Validate buffer barrier queue family indices
87905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((mem_barrier->srcQueueFamilyIndex != VK_QUEUE_FAMILY_IGNORED &&
87915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             mem_barrier->srcQueueFamilyIndex >= dev_data->physDevProperties.queue_family_properties.size()) ||
87925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            (mem_barrier->dstQueueFamilyIndex != VK_QUEUE_FAMILY_IGNORED &&
87935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis             mem_barrier->dstQueueFamilyIndex >= dev_data->physDevProperties.queue_family_properties.size())) {
87945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
87955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 DRAWSTATE_INVALID_QUEUE_INDEX, "DS",
87965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 "%s: Buffer Barrier 0x%" PRIx64 " has QueueFamilyIndex greater "
87975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 "than the number of QueueFamilies (" PRINTF_SIZE_T_SPECIFIER ") for this device.",
87985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 funcName, reinterpret_cast<const uint64_t &>(mem_barrier->buffer),
87995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 dev_data->physDevProperties.queue_family_properties.size());
88005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
88015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
88025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto buffer_data = dev_data->bufferMap.find(mem_barrier->buffer);
88035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint64_t buffer_size =
88045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            buffer_data->second.create_info ? reinterpret_cast<uint64_t &>(buffer_data->second.create_info->size) : 0;
88055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (buffer_data != dev_data->bufferMap.end()) {
88065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (mem_barrier->offset >= buffer_size) {
88075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |=
88085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
88095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_BARRIER, "DS", "%s: Buffer Barrier 0x%" PRIx64 " has offset %" PRIu64
88105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                             " whose sum is not less than total size %" PRIu64 ".",
88115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            funcName, reinterpret_cast<const uint64_t &>(mem_barrier->buffer),
88125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            reinterpret_cast<const uint64_t &>(mem_barrier->offset), buffer_size);
88135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else if (mem_barrier->size != VK_WHOLE_SIZE && (mem_barrier->offset + mem_barrier->size > buffer_size)) {
88145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
88155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
88165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     "%s: Buffer Barrier 0x%" PRIx64 " has offset %" PRIu64 " and size %" PRIu64
88175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     " whose sum is greater than total size %" PRIu64 ".",
88185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     funcName, reinterpret_cast<const uint64_t &>(mem_barrier->buffer),
88195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     reinterpret_cast<const uint64_t &>(mem_barrier->offset),
88205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     reinterpret_cast<const uint64_t &>(mem_barrier->size), buffer_size);
88215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
88225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
88235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
88245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
88255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
88265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
88275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
88285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents, VkPipelineStageFlags sourceStageMask,
88295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkPipelineStageFlags dstStageMask, uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
88305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
88315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) {
88325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
88335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
88345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
88355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
88365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
88375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkPipelineStageFlags stageMask = 0;
88385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < eventCount; ++i) {
88395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->waitedEvents.push_back(pEvents[i]);
88405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->events.push_back(pEvents[i]);
88415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto event_data = pCB->eventToStageMap.find(pEvents[i]);
88425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (event_data != pCB->eventToStageMap.end()) {
88435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                stageMask |= event_data->second;
88445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
88455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto global_event_data = dev_data->eventMap.find(pEvents[i]);
88465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (global_event_data == dev_data->eventMap.end()) {
88475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT,
88485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        reinterpret_cast<const uint64_t &>(pEvents[i]), __LINE__, DRAWSTATE_INVALID_FENCE, "DS",
88495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "Fence 0x%" PRIx64 " cannot be waited on if it has never been set.",
88505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        reinterpret_cast<const uint64_t &>(pEvents[i]));
88515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else {
88525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    stageMask |= global_event_data->second.stageMask;
88535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
88545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
88555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
88565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (sourceStageMask != stageMask) {
88575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
88585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INVALID_FENCE, "DS", "srcStageMask in vkCmdWaitEvents must be the bitwise OR of the "
88595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                               "stageMask parameters used in calls to vkCmdSetEvent and "
88605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                               "VK_PIPELINE_STAGE_HOST_BIT if used with vkSetEvent.");
88615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
88625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->state == CB_RECORDING) {
88635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= addCmd(dev_data, pCB, CMD_WAITEVENTS, "vkCmdWaitEvents()");
88645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
88655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdWaitEvents()");
88665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
88675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= TransitionImageLayouts(commandBuffer, imageMemoryBarrierCount, pImageMemoryBarriers);
88685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |=
88695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            ValidateBarriers("vkCmdWaitEvents", commandBuffer, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
88705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
88715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
88725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
88735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
88745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdWaitEvents(commandBuffer, eventCount, pEvents, sourceStageMask, dstStageMask,
88755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                       memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
88765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                       pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
88775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
88785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
88795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
88805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
88815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
88825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
88835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                     uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) {
88845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
88855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
88865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
88875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
88885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
88895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_PIPELINEBARRIER, "vkCmdPipelineBarrier()");
88905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= TransitionImageLayouts(commandBuffer, imageMemoryBarrierCount, pImageMemoryBarriers);
88915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |=
88925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            ValidateBarriers("vkCmdPipelineBarrier", commandBuffer, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
88935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
88945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
88955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
88965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
88975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags,
88985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                            memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
88995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                            pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
89005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
89015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
89025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
89035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdBeginQuery(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t slot, VkFlags flags) {
89045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
89055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
89065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
89075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
89085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
89095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        QueryObject query = {queryPool, slot};
89105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->activeQueries.insert(query);
89115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!pCB->startedQueries.count(query)) {
89125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->startedQueries.insert(query);
89135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
89145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_BEGINQUERY, "vkCmdBeginQuery()");
89155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
89165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
89175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
89185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdBeginQuery(commandBuffer, queryPool, slot, flags);
89195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
89205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
89215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdEndQuery(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t slot) {
89225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
89235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
89245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
89255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
89265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
89275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        QueryObject query = {queryPool, slot};
89285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!pCB->activeQueries.count(query)) {
89295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
89305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
89315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_QUERY, "DS", "Ending a query before it was started: queryPool %" PRIu64 ", index %d",
89325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)(queryPool), slot);
89335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
89345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->activeQueries.erase(query);
89355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
89365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->queryToStateMap[query] = 1;
89375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->state == CB_RECORDING) {
89385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= addCmd(dev_data, pCB, CMD_ENDQUERY, "VkCmdEndQuery()");
89395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
89405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdEndQuery()");
89415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
89425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
89435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
89445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
89455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdEndQuery(commandBuffer, queryPool, slot);
89465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
89475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
89485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
89495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdResetQueryPool(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount) {
89505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
89515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
89525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
89535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
89545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
89555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < queryCount; i++) {
89565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            QueryObject query = {queryPool, firstQuery + i};
89575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->waitedEventsBeforeQueryReset[query] = pCB->waitedEvents;
89585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->queryToStateMap[query] = 0;
89595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
89605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->state == CB_RECORDING) {
89615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= addCmd(dev_data, pCB, CMD_RESETQUERYPOOL, "VkCmdResetQueryPool()");
89625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
89635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdResetQueryPool()");
89645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
89655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdQueryPool");
89665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
89675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
89685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
89695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdResetQueryPool(commandBuffer, queryPool, firstQuery, queryCount);
89705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
89715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
89725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
89735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount,
89745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                          VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize stride, VkQueryResultFlags flags) {
89755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
89765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
89775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
89785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
89795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
89805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkDeviceMemory mem;
89815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
89825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |=
89835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        get_mem_binding_from_object(dev_data, commandBuffer, (uint64_t)dstBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, &mem);
89845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
89855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::function<VkBool32()> function = [=]() {
89865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            set_memory_valid(dev_data, mem, true);
89875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_FALSE;
89885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        };
89895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        cb_data->second.validate_functions.push_back(function);
89905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
89915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= update_cmd_buf_and_mem_references(dev_data, commandBuffer, mem, "vkCmdCopyQueryPoolResults");
89925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate that DST buffer has correct usage flags set
89935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validate_buffer_usage_flags(dev_data, commandBuffer, dstBuffer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true,
89945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            "vkCmdCopyQueryPoolResults()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
89955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
89965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
89975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < queryCount; i++) {
89985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            QueryObject query = {queryPool, firstQuery + i};
89995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pCB->queryToStateMap[query]) {
90005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
90015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    __LINE__, DRAWSTATE_INVALID_QUERY, "DS",
90025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "Requesting a copy from query to buffer with invalid query: queryPool %" PRIu64 ", index %d",
90035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    (uint64_t)(queryPool), firstQuery + i);
90045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
90055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
90065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->state == CB_RECORDING) {
90075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= addCmd(dev_data, pCB, CMD_COPYQUERYPOOLRESULTS, "vkCmdCopyQueryPoolResults()");
90085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
90095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdCopyQueryPoolResults()");
90105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
90115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= insideRenderPass(dev_data, pCB, "vkCmdCopyQueryPoolResults");
90125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
90135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
90145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
90155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer,
90165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                 dstOffset, stride, flags);
90175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
90185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
90195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdPushConstants(VkCommandBuffer commandBuffer, VkPipelineLayout layout,
90205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                              VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size,
90215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                              const void *pValues) {
90225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
90235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
90245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
90255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
90265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
90275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->state == CB_RECORDING) {
90285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= addCmd(dev_data, pCB, CMD_PUSHCONSTANTS, "vkCmdPushConstants()");
90295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
90305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdPushConstants()");
90315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
90325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
90335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if ((offset + size) > dev_data->physDevProperties.properties.limits.maxPushConstantsSize) {
90345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validatePushConstantSize(dev_data, offset, size, "vkCmdPushConstants()");
90355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
90365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // TODO : Add warning if push constant update doesn't align with range
90375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
90385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skipCall)
90395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdPushConstants(commandBuffer, layout, stageFlags, offset, size, pValues);
90405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
90415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
90425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
90435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage, VkQueryPool queryPool, uint32_t slot) {
90445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
90455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
90465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
90475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
90485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
90495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        QueryObject query = {queryPool, slot};
90505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->queryToStateMap[query] = 1;
90515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->state == CB_RECORDING) {
90525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= addCmd(dev_data, pCB, CMD_WRITETIMESTAMP, "vkCmdWriteTimestamp()");
90535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
90545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= report_error_no_cb_begin(dev_data, commandBuffer, "vkCmdWriteTimestamp()");
90555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
90565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
90575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
90585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
90595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdWriteTimestamp(commandBuffer, pipelineStage, queryPool, slot);
90605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
90615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
90625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo *pCreateInfo,
90635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                   const VkAllocationCallbacks *pAllocator,
90645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                   VkFramebuffer *pFramebuffer) {
90655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
90665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateFramebuffer(device, pCreateInfo, pAllocator, pFramebuffer);
90675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
90685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Shadow create info and store in map
90695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkFramebufferCreateInfo *localFBCI = new VkFramebufferCreateInfo(*pCreateInfo);
90705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCreateInfo->pAttachments) {
90715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            localFBCI->pAttachments = new VkImageView[localFBCI->attachmentCount];
90725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy((void *)localFBCI->pAttachments, pCreateInfo->pAttachments, localFBCI->attachmentCount * sizeof(VkImageView));
90735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
90745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        FRAMEBUFFER_NODE fbNode = {};
90755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        fbNode.createInfo = *localFBCI;
90765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::pair<VkFramebuffer, FRAMEBUFFER_NODE> fbPair(*pFramebuffer, fbNode);
90775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
90785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
90795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
90805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkImageView view = pCreateInfo->pAttachments[i];
90815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto view_data = dev_data->imageViewMap.find(view);
90825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (view_data == dev_data->imageViewMap.end()) {
90835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                continue;
90845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
90855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            MT_FB_ATTACHMENT_INFO fb_info;
90865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            get_mem_binding_from_object(dev_data, device, (uint64_t)(view_data->second.image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
90875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        &fb_info.mem);
90885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            fb_info.image = view_data->second.image;
90895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->fbMap[*pFramebuffer].attachments.push_back(fb_info);
90905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
90915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
90925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->frameBufferMap.insert(fbPair);
90935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
90945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
90955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
90965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
90975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
90985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 FindDependency(const int index, const int dependent, const std::vector<DAGNode> &subpass_to_node,
90995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        std::unordered_set<uint32_t> &processed_nodes) {
91005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // If we have already checked this node we have not found a dependency path so return false.
91015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (processed_nodes.count(index))
91025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_FALSE;
91035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    processed_nodes.insert(index);
91045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const DAGNode &node = subpass_to_node[index];
91055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Look for a dependency path. If one exists return true else recurse on the previous nodes.
91065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (std::find(node.prev.begin(), node.prev.end(), dependent) == node.prev.end()) {
91075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto elem : node.prev) {
91085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (FindDependency(elem, dependent, subpass_to_node, processed_nodes))
91095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                return VK_TRUE;
91105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
91115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    } else {
91125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_TRUE;
91135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
91145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return VK_FALSE;
91155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
91165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
91175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 CheckDependencyExists(const layer_data *my_data, const int subpass, const std::vector<uint32_t> &dependent_subpasses,
91185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               const std::vector<DAGNode> &subpass_to_node, VkBool32 &skip_call) {
91195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 result = VK_TRUE;
91205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Loop through all subpasses that share the same attachment and make sure a dependency exists
91215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t k = 0; k < dependent_subpasses.size(); ++k) {
91225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (subpass == dependent_subpasses[k])
91235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            continue;
91245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const DAGNode &node = subpass_to_node[subpass];
91255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Check for a specified dependency between the two nodes. If one exists we are done.
91265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto prev_elem = std::find(node.prev.begin(), node.prev.end(), dependent_subpasses[k]);
91275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto next_elem = std::find(node.next.begin(), node.next.end(), dependent_subpasses[k]);
91285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (prev_elem == node.prev.end() && next_elem == node.next.end()) {
91295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // If no dependency exits an implicit dependency still might. If so, warn and if not throw an error.
91305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            std::unordered_set<uint32_t> processed_nodes;
91315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (FindDependency(subpass, dependent_subpasses[k], subpass_to_node, processed_nodes) ||
91325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                FindDependency(dependent_subpasses[k], subpass, subpass_to_node, processed_nodes)) {
91335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // TODO: Verify against Valid Use section of spec
91345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
91355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
91365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     "A dependency between subpasses %d and %d must exist but only an implicit one is specified.",
91375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     subpass, dependent_subpasses[k]);
91385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
91395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
91405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
91415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     "A dependency between subpasses %d and %d must exist but one is not specified.", subpass,
91425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     dependent_subpasses[k]);
91435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                result = VK_FALSE;
91445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
91455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
91465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
91475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
91485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
91495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
91505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 CheckPreserved(const layer_data *my_data, const VkRenderPassCreateInfo *pCreateInfo, const int index,
91515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        const uint32_t attachment, const std::vector<DAGNode> &subpass_to_node, int depth, VkBool32 &skip_call) {
91525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const DAGNode &node = subpass_to_node[index];
91535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // If this node writes to the attachment return true as next nodes need to preserve the attachment.
91545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[index];
91555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
91565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (attachment == subpass.pColorAttachments[j].attachment)
91575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_TRUE;
91585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
91595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
91605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (attachment == subpass.pDepthStencilAttachment->attachment)
91615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return VK_TRUE;
91625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
91635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 result = VK_FALSE;
91645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Loop through previous nodes and see if any of them write to the attachment.
91655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto elem : node.prev) {
91665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result |= CheckPreserved(my_data, pCreateInfo, elem, attachment, subpass_to_node, depth + 1, skip_call);
91675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
91685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // If the attachment was written to by a previous node than this node needs to preserve it.
91695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (result && depth > 0) {
91705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[index];
91715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkBool32 has_preserved = VK_FALSE;
91725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < subpass.preserveAttachmentCount; ++j) {
91735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (subpass.pPreserveAttachments[j] == attachment) {
91745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                has_preserved = VK_TRUE;
91755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                break;
91765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
91775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
91785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (has_preserved == VK_FALSE) {
91795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
91805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
91815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_RENDERPASS, "DS",
91825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Attachment %d is used by a later subpass and must be preserved in subpass %d.", attachment, index);
91835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
91845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
91855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
91865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
91875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
91885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlistemplate <class T> bool isRangeOverlapping(T offset1, T size1, T offset2, T size2) {
91895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return (((offset1 + size1) > offset2) && ((offset1 + size1) < (offset2 + size2))) ||
91905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis           ((offset1 > offset2) && (offset1 < (offset2 + size2)));
91915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
91925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
91935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool isRegionOverlapping(VkImageSubresourceRange range1, VkImageSubresourceRange range2) {
91945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return (isRangeOverlapping(range1.baseMipLevel, range1.levelCount, range2.baseMipLevel, range2.levelCount) &&
91955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            isRangeOverlapping(range1.baseArrayLayer, range1.layerCount, range2.baseArrayLayer, range2.layerCount));
91965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
91975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
91985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 ValidateDependencies(const layer_data *my_data, const VkRenderPassBeginInfo *pRenderPassBegin,
91995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                              const std::vector<DAGNode> &subpass_to_node) {
92005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
92015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkFramebufferCreateInfo *pFramebufferInfo = &my_data->frameBufferMap.at(pRenderPassBegin->framebuffer).createInfo;
92025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkRenderPassCreateInfo *pCreateInfo = my_data->renderPassMap.at(pRenderPassBegin->renderPass)->pCreateInfo;
92035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::vector<std::vector<uint32_t>> output_attachment_to_subpass(pCreateInfo->attachmentCount);
92045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::vector<std::vector<uint32_t>> input_attachment_to_subpass(pCreateInfo->attachmentCount);
92055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::vector<std::vector<uint32_t>> overlapping_attachments(pCreateInfo->attachmentCount);
92065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Find overlapping attachments
92075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
92085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = i + 1; j < pCreateInfo->attachmentCount; ++j) {
92095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkImageView viewi = pFramebufferInfo->pAttachments[i];
92105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkImageView viewj = pFramebufferInfo->pAttachments[j];
92115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (viewi == viewj) {
92125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                overlapping_attachments[i].push_back(j);
92135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                overlapping_attachments[j].push_back(i);
92145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                continue;
92155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
92165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto view_data_i = my_data->imageViewMap.find(viewi);
92175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto view_data_j = my_data->imageViewMap.find(viewj);
92185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (view_data_i == my_data->imageViewMap.end() || view_data_j == my_data->imageViewMap.end()) {
92195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                continue;
92205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
92215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (view_data_i->second.image == view_data_j->second.image &&
92225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                isRegionOverlapping(view_data_i->second.subresourceRange, view_data_j->second.subresourceRange)) {
92235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                overlapping_attachments[i].push_back(j);
92245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                overlapping_attachments[j].push_back(i);
92255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                continue;
92265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
92275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto image_data_i = my_data->imageMap.find(view_data_i->second.image);
92285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto image_data_j = my_data->imageMap.find(view_data_j->second.image);
92295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (image_data_i == my_data->imageMap.end() || image_data_j == my_data->imageMap.end()) {
92305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                continue;
92315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
92325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (image_data_i->second.mem == image_data_j->second.mem &&
92335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                isRangeOverlapping(image_data_i->second.memOffset, image_data_i->second.memSize, image_data_j->second.memOffset,
92345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                   image_data_j->second.memSize)) {
92355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                overlapping_attachments[i].push_back(j);
92365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                overlapping_attachments[j].push_back(i);
92375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
92385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
92395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
92405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < overlapping_attachments.size(); ++i) {
92415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t attachment = i;
92425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (auto other_attachment : overlapping_attachments[i]) {
92435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!(pCreateInfo->pAttachments[attachment].flags & VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT)) {
92445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |=
92455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
92465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_RENDERPASS, "DS", "Attachment %d aliases attachment %d but doesn't "
92475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                "set VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT.",
92485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            attachment, other_attachment);
92495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
92505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!(pCreateInfo->pAttachments[other_attachment].flags & VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT)) {
92515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |=
92525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
92535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_RENDERPASS, "DS", "Attachment %d aliases attachment %d but doesn't "
92545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                "set VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT.",
92555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            other_attachment, attachment);
92565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
92575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
92585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
92595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Find for each attachment the subpasses that use them.
92605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
92615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
92625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
92635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t attachment = subpass.pInputAttachments[j].attachment;
92645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            input_attachment_to_subpass[attachment].push_back(i);
92655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (auto overlapping_attachment : overlapping_attachments[attachment]) {
92665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                input_attachment_to_subpass[overlapping_attachment].push_back(i);
92675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
92685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
92695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
92705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t attachment = subpass.pColorAttachments[j].attachment;
92715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            output_attachment_to_subpass[attachment].push_back(i);
92725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (auto overlapping_attachment : overlapping_attachments[attachment]) {
92735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                output_attachment_to_subpass[overlapping_attachment].push_back(i);
92745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
92755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
92765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
92775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t attachment = subpass.pDepthStencilAttachment->attachment;
92785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            output_attachment_to_subpass[attachment].push_back(i);
92795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (auto overlapping_attachment : overlapping_attachments[attachment]) {
92805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                output_attachment_to_subpass[overlapping_attachment].push_back(i);
92815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
92825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
92835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
92845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // If there is a dependency needed make sure one exists
92855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
92865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
92875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // If the attachment is an input then all subpasses that output must have a dependency relationship
92885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
92895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            const uint32_t &attachment = subpass.pInputAttachments[j].attachment;
92905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            CheckDependencyExists(my_data, i, output_attachment_to_subpass[attachment], subpass_to_node, skip_call);
92915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
92925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // If the attachment is an output then all subpasses that use the attachment must have a dependency relationship
92935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
92945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            const uint32_t &attachment = subpass.pColorAttachments[j].attachment;
92955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            CheckDependencyExists(my_data, i, output_attachment_to_subpass[attachment], subpass_to_node, skip_call);
92965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            CheckDependencyExists(my_data, i, input_attachment_to_subpass[attachment], subpass_to_node, skip_call);
92975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
92985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
92995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            const uint32_t &attachment = subpass.pDepthStencilAttachment->attachment;
93005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            CheckDependencyExists(my_data, i, output_attachment_to_subpass[attachment], subpass_to_node, skip_call);
93015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            CheckDependencyExists(my_data, i, input_attachment_to_subpass[attachment], subpass_to_node, skip_call);
93025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
93035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
93045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Loop through implicit dependencies, if this pass reads make sure the attachment is preserved for all passes after it was
93055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // written.
93065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
93075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
93085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
93095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            CheckPreserved(my_data, pCreateInfo, i, subpass.pInputAttachments[j].attachment, subpass_to_node, 0, skip_call);
93105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
93115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
93125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
93135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
93145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
93155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 ValidateLayouts(const layer_data *my_data, VkDevice device, const VkRenderPassCreateInfo *pCreateInfo) {
93165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip = VK_FALSE;
93175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
93185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
93195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
93205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
93215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (subpass.pInputAttachments[j].layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL &&
93225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                subpass.pInputAttachments[j].layout != VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
93235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (subpass.pInputAttachments[j].layout == VK_IMAGE_LAYOUT_GENERAL) {
93245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // TODO: Verify Valid Use in spec. I believe this is allowed (valid) but may not be optimal performance
93255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
93265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
93275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "Layout for input attachment is GENERAL but should be READ_ONLY_OPTIMAL.");
93285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else {
93295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
93305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
93315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "Layout for input attachment is %s but can only be READ_ONLY_OPTIMAL or GENERAL.",
93325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    string_VkImageLayout(subpass.pInputAttachments[j].layout));
93335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
93345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
93355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
93365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
93375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (subpass.pColorAttachments[j].layout != VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
93385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (subpass.pColorAttachments[j].layout == VK_IMAGE_LAYOUT_GENERAL) {
93395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // TODO: Verify Valid Use in spec. I believe this is allowed (valid) but may not be optimal performance
93405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
93415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
93425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "Layout for color attachment is GENERAL but should be COLOR_ATTACHMENT_OPTIMAL.");
93435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else {
93445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
93455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
93465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "Layout for color attachment is %s but can only be COLOR_ATTACHMENT_OPTIMAL or GENERAL.",
93475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    string_VkImageLayout(subpass.pColorAttachments[j].layout));
93485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
93495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
93505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
93515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((subpass.pDepthStencilAttachment != NULL) && (subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
93525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (subpass.pDepthStencilAttachment->layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
93535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (subpass.pDepthStencilAttachment->layout == VK_IMAGE_LAYOUT_GENERAL) {
93545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // TODO: Verify Valid Use in spec. I believe this is allowed (valid) but may not be optimal performance
93555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip |= log_msg(my_data->report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
93565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    (VkDebugReportObjectTypeEXT)0, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
93575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "Layout for depth attachment is GENERAL but should be DEPTH_STENCIL_ATTACHMENT_OPTIMAL.");
93585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else {
93595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip |=
93605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
93615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
93625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "Layout for depth attachment is %s but can only be DEPTH_STENCIL_ATTACHMENT_OPTIMAL or GENERAL.",
93635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                string_VkImageLayout(subpass.pDepthStencilAttachment->layout));
93645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
93655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
93665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
93675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
93685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip;
93695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
93705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
93715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 CreatePassDAG(const layer_data *my_data, VkDevice device, const VkRenderPassCreateInfo *pCreateInfo,
93725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                       std::vector<DAGNode> &subpass_to_node, std::vector<bool> &has_self_dependency) {
93735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
93745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
93755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        DAGNode &subpass_node = subpass_to_node[i];
93765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        subpass_node.pass = i;
93775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
93785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pCreateInfo->dependencyCount; ++i) {
93795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkSubpassDependency &dependency = pCreateInfo->pDependencies[i];
93805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (dependency.srcSubpass > dependency.dstSubpass && dependency.srcSubpass != VK_SUBPASS_EXTERNAL &&
93815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dependency.dstSubpass != VK_SUBPASS_EXTERNAL) {
93825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
93835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 DRAWSTATE_INVALID_RENDERPASS, "DS",
93845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 "Depedency graph must be specified such that an earlier pass cannot depend on a later pass.");
93855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (dependency.srcSubpass == VK_SUBPASS_EXTERNAL && dependency.dstSubpass == VK_SUBPASS_EXTERNAL) {
93865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
93875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 DRAWSTATE_INVALID_RENDERPASS, "DS", "The src and dest subpasses cannot both be external.");
93885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else if (dependency.srcSubpass == dependency.dstSubpass) {
93895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            has_self_dependency[dependency.srcSubpass] = true;
93905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
93915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (dependency.dstSubpass != VK_SUBPASS_EXTERNAL) {
93925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            subpass_to_node[dependency.dstSubpass].prev.push_back(dependency.srcSubpass);
93935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
93945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (dependency.srcSubpass != VK_SUBPASS_EXTERNAL) {
93955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            subpass_to_node[dependency.srcSubpass].next.push_back(dependency.dstSubpass);
93965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
93975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
93985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
93995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
94005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// TODOSC : Add intercept of vkCreateShaderModule
94015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
94025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
94035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                    const VkAllocationCallbacks *pAllocator,
94045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                    VkShaderModule *pShaderModule) {
94055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
94065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
94075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!shader_is_spirv(pCreateInfo)) {
94085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
94095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             /* dev */ 0, __LINE__, SHADER_CHECKER_NON_SPIRV_SHADER, "SC", "Shader is not SPIR-V");
94105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
94115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
94125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE != skip_call)
94135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
94145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
94155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule);
94165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
94175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (res == VK_SUCCESS) {
94185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
94195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->shaderModuleMap[*pShaderModule] = new shader_module(pCreateInfo);
94205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
94215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
94225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return res;
94235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
94245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
94255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo *pCreateInfo,
94265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  const VkAllocationCallbacks *pAllocator,
94275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                  VkRenderPass *pRenderPass) {
94285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
94295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
94305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
94315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Create DAG
94325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::vector<bool> has_self_dependency(pCreateInfo->subpassCount);
94335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    std::vector<DAGNode> subpass_to_node(pCreateInfo->subpassCount);
94345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skip_call |= CreatePassDAG(dev_data, device, pCreateInfo, subpass_to_node, has_self_dependency);
94355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Validate
94365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skip_call |= ValidateLayouts(dev_data, device, pCreateInfo);
94375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE != skip_call) {
94385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return VK_ERROR_VALIDATION_FAILED_EXT;
94395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
94405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
94415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
94425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
94435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
94445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
94455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // MTMTODO : Merge with code from below to eliminate duplication
94465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
94475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkAttachmentDescription desc = pCreateInfo->pAttachments[i];
94485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            MT_PASS_ATTACHMENT_INFO pass_info;
94495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pass_info.load_op = desc.loadOp;
94505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pass_info.store_op = desc.storeOp;
94515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pass_info.attachment = i;
94525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->passMap[*pRenderPass].attachments.push_back(pass_info);
94535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
94545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO: Maybe fill list and then copy instead of locking
94555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::unordered_map<uint32_t, bool> &attachment_first_read = dev_data->passMap[*pRenderPass].attachment_first_read;
94565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::unordered_map<uint32_t, VkImageLayout> &attachment_first_layout = dev_data->passMap[*pRenderPass].attachment_first_layout;
94575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
94585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
94595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
94605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                uint32_t attachment = subpass.pInputAttachments[j].attachment;
94615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (attachment_first_read.count(attachment))
94625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    continue;
94635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                attachment_first_read.insert(std::make_pair(attachment, true));
94645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                attachment_first_layout.insert(std::make_pair(attachment, subpass.pInputAttachments[j].layout));
94655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
94665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
94675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                uint32_t attachment = subpass.pColorAttachments[j].attachment;
94685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (attachment_first_read.count(attachment))
94695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    continue;
94705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                attachment_first_read.insert(std::make_pair(attachment, false));
94715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                attachment_first_layout.insert(std::make_pair(attachment, subpass.pColorAttachments[j].layout));
94725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
94735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
94745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                uint32_t attachment = subpass.pDepthStencilAttachment->attachment;
94755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (attachment_first_read.count(attachment))
94765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    continue;
94775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                attachment_first_read.insert(std::make_pair(attachment, false));
94785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                attachment_first_layout.insert(std::make_pair(attachment, subpass.pDepthStencilAttachment->layout));
94795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
94805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
94815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
94825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODOSC : Merge in tracking of renderpass from shader_checker
94835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Shadow create info and store in map
94845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkRenderPassCreateInfo *localRPCI = new VkRenderPassCreateInfo(*pCreateInfo);
94855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCreateInfo->pAttachments) {
94865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            localRPCI->pAttachments = new VkAttachmentDescription[localRPCI->attachmentCount];
94875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy((void *)localRPCI->pAttachments, pCreateInfo->pAttachments,
94885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   localRPCI->attachmentCount * sizeof(VkAttachmentDescription));
94895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
94905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCreateInfo->pSubpasses) {
94915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            localRPCI->pSubpasses = new VkSubpassDescription[localRPCI->subpassCount];
94925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy((void *)localRPCI->pSubpasses, pCreateInfo->pSubpasses, localRPCI->subpassCount * sizeof(VkSubpassDescription));
94935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
94945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t i = 0; i < localRPCI->subpassCount; i++) {
94955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkSubpassDescription *subpass = (VkSubpassDescription *)&localRPCI->pSubpasses[i];
94965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                const uint32_t attachmentCount = subpass->inputAttachmentCount +
94975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 subpass->colorAttachmentCount * (1 + (subpass->pResolveAttachments ? 1 : 0)) +
94985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 ((subpass->pDepthStencilAttachment) ? 1 : 0) + subpass->preserveAttachmentCount;
94995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkAttachmentReference *attachments = new VkAttachmentReference[attachmentCount];
95005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
95015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                memcpy(attachments, subpass->pInputAttachments, sizeof(attachments[0]) * subpass->inputAttachmentCount);
95025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                subpass->pInputAttachments = attachments;
95035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                attachments += subpass->inputAttachmentCount;
95045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
95055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                memcpy(attachments, subpass->pColorAttachments, sizeof(attachments[0]) * subpass->colorAttachmentCount);
95065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                subpass->pColorAttachments = attachments;
95075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                attachments += subpass->colorAttachmentCount;
95085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
95095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (subpass->pResolveAttachments) {
95105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    memcpy(attachments, subpass->pResolveAttachments, sizeof(attachments[0]) * subpass->colorAttachmentCount);
95115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    subpass->pResolveAttachments = attachments;
95125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    attachments += subpass->colorAttachmentCount;
95135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
95145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
95155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (subpass->pDepthStencilAttachment) {
95165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    memcpy(attachments, subpass->pDepthStencilAttachment, sizeof(attachments[0]) * 1);
95175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    subpass->pDepthStencilAttachment = attachments;
95185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    attachments += 1;
95195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
95205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
95215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                memcpy(attachments, subpass->pPreserveAttachments, sizeof(attachments[0]) * subpass->preserveAttachmentCount);
95225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                subpass->pPreserveAttachments = &attachments->attachment;
95235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
95245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
95255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCreateInfo->pDependencies) {
95265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            localRPCI->pDependencies = new VkSubpassDependency[localRPCI->dependencyCount];
95275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            memcpy((void *)localRPCI->pDependencies, pCreateInfo->pDependencies,
95285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   localRPCI->dependencyCount * sizeof(VkSubpassDependency));
95295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
95305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->renderPassMap[*pRenderPass] = new RENDER_PASS_NODE(localRPCI);
95315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->renderPassMap[*pRenderPass]->hasSelfDependency = has_self_dependency;
95325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->renderPassMap[*pRenderPass]->subpassToNode = subpass_to_node;
95335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
95345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
95355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
95365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
95375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis// Free the renderpass shadow
95385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisstatic void deleteRenderPasses(layer_data *my_data) {
95395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (my_data->renderPassMap.size() <= 0)
95405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
95415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto ii = my_data->renderPassMap.begin(); ii != my_data->renderPassMap.end(); ++ii) {
95425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkRenderPassCreateInfo *pRenderPassInfo = (*ii).second->pCreateInfo;
95435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete[] pRenderPassInfo->pAttachments;
95445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pRenderPassInfo->pSubpasses) {
95455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t i = 0; i < pRenderPassInfo->subpassCount; ++i) {
95465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // Attachements are all allocated in a block, so just need to
95475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                //  find the first non-null one to delete
95485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pRenderPassInfo->pSubpasses[i].pInputAttachments) {
95495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    delete[] pRenderPassInfo->pSubpasses[i].pInputAttachments;
95505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else if (pRenderPassInfo->pSubpasses[i].pColorAttachments) {
95515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    delete[] pRenderPassInfo->pSubpasses[i].pColorAttachments;
95525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else if (pRenderPassInfo->pSubpasses[i].pResolveAttachments) {
95535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    delete[] pRenderPassInfo->pSubpasses[i].pResolveAttachments;
95545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else if (pRenderPassInfo->pSubpasses[i].pPreserveAttachments) {
95555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    delete[] pRenderPassInfo->pSubpasses[i].pPreserveAttachments;
95565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
95575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
95585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            delete[] pRenderPassInfo->pSubpasses;
95595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
95605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete[] pRenderPassInfo->pDependencies;
95615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete pRenderPassInfo;
95625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete (*ii).second;
95635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
95645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->renderPassMap.clear();
95655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
95665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
95675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 VerifyFramebufferAndRenderPassLayouts(VkCommandBuffer cmdBuffer, const VkRenderPassBeginInfo *pRenderPassBegin) {
95685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
95695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
95705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
95715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkRenderPassCreateInfo *pRenderPassInfo = dev_data->renderPassMap[pRenderPassBegin->renderPass]->pCreateInfo;
95725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkFramebufferCreateInfo framebufferInfo = dev_data->frameBufferMap[pRenderPassBegin->framebuffer].createInfo;
95735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pRenderPassInfo->attachmentCount != framebufferInfo.attachmentCount) {
95745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
95755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             DRAWSTATE_INVALID_RENDERPASS, "DS", "You cannot start a render pass using a framebuffer "
95765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                 "with a different number of attachments.");
95775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
95785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
95795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkImageView &image_view = framebufferInfo.pAttachments[i];
95805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto image_data = dev_data->imageViewMap.find(image_view);
95815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        assert(image_data != dev_data->imageViewMap.end());
95825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkImage &image = image_data->second.image;
95835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkImageSubresourceRange &subRange = image_data->second.subresourceRange;
95845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        IMAGE_CMD_BUF_LAYOUT_NODE newNode = {pRenderPassInfo->pAttachments[i].initialLayout,
95855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                             pRenderPassInfo->pAttachments[i].initialLayout};
95865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // TODO: Do not iterate over every possibility - consolidate where possible
95875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < subRange.levelCount; j++) {
95885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            uint32_t level = subRange.baseMipLevel + j;
95895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t k = 0; k < subRange.layerCount; k++) {
95905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                uint32_t layer = subRange.baseArrayLayer + k;
95915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkImageSubresource sub = {subRange.aspectMask, level, layer};
95925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                IMAGE_CMD_BUF_LAYOUT_NODE node;
95935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (!FindLayout(pCB, image, sub, node)) {
95945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    SetLayout(pCB, image, sub, newNode);
95955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    continue;
95965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
95975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (newNode.layout != node.layout) {
95985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call |=
95995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
96005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                DRAWSTATE_INVALID_RENDERPASS, "DS", "You cannot start a render pass using attachment %i "
96015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                    "where the "
96025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                    "intial layout differs from the starting layout.",
96035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                i);
96045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
96055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
96065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
96075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
96085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
96095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
96105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
96115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid TransitionSubpassLayouts(VkCommandBuffer cmdBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, const int subpass_index) {
96125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
96135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
96145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto render_pass_data = dev_data->renderPassMap.find(pRenderPassBegin->renderPass);
96155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (render_pass_data == dev_data->renderPassMap.end()) {
96165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
96175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
96185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkRenderPassCreateInfo *pRenderPassInfo = render_pass_data->second->pCreateInfo;
96195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto framebuffer_data = dev_data->frameBufferMap.find(pRenderPassBegin->framebuffer);
96205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (framebuffer_data == dev_data->frameBufferMap.end()) {
96215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
96225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
96235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkFramebufferCreateInfo framebufferInfo = framebuffer_data->second.createInfo;
96245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkSubpassDescription &subpass = pRenderPassInfo->pSubpasses[subpass_index];
96255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
96265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkImageView &image_view = framebufferInfo.pAttachments[subpass.pInputAttachments[j].attachment];
96275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SetLayout(dev_data, pCB, image_view, subpass.pInputAttachments[j].layout);
96285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
96295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
96305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkImageView &image_view = framebufferInfo.pAttachments[subpass.pColorAttachments[j].attachment];
96315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SetLayout(dev_data, pCB, image_view, subpass.pColorAttachments[j].layout);
96325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
96335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if ((subpass.pDepthStencilAttachment != NULL) && (subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
96345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkImageView &image_view = framebufferInfo.pAttachments[subpass.pDepthStencilAttachment->attachment];
96355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SetLayout(dev_data, pCB, image_view, subpass.pDepthStencilAttachment->layout);
96365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
96375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
96385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
96395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 validatePrimaryCommandBuffer(const layer_data *my_data, const GLOBAL_CB_NODE *pCB, const std::string &cmd_name) {
96405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
96415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB->createInfo.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
96425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
96435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             DRAWSTATE_INVALID_COMMAND_BUFFER, "DS", "Cannot execute command %s on a secondary command buffer.",
96445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             cmd_name.c_str());
96455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
96465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
96475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
96485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
96495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisvoid TransitionFinalSubpassLayouts(VkCommandBuffer cmdBuffer, const VkRenderPassBeginInfo *pRenderPassBegin) {
96505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(cmdBuffer), layer_data_map);
96515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, cmdBuffer);
96525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto render_pass_data = dev_data->renderPassMap.find(pRenderPassBegin->renderPass);
96535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (render_pass_data == dev_data->renderPassMap.end()) {
96545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
96555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
96565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkRenderPassCreateInfo *pRenderPassInfo = render_pass_data->second->pCreateInfo;
96575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto framebuffer_data = dev_data->frameBufferMap.find(pRenderPassBegin->framebuffer);
96585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (framebuffer_data == dev_data->frameBufferMap.end()) {
96595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return;
96605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
96615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkFramebufferCreateInfo framebufferInfo = framebuffer_data->second.createInfo;
96625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
96635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkImageView &image_view = framebufferInfo.pAttachments[i];
96645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SetLayout(dev_data, pCB, image_view, pRenderPassInfo->pAttachments[i].finalLayout);
96655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
96665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
96675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
96685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
96695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin, VkSubpassContents contents) {
96705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
96715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
96725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
96735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
96745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
96755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pRenderPassBegin && pRenderPassBegin->renderPass) {
96765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
96775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto pass_data = dev_data->passMap.find(pRenderPassBegin->renderPass);
96785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (pass_data != dev_data->passMap.end()) {
96795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                MT_PASS_INFO &pass_info = pass_data->second;
96805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pass_info.fb = pRenderPassBegin->framebuffer;
96815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto cb_data = dev_data->cbMap.find(commandBuffer);
96825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (size_t i = 0; i < pass_info.attachments.size(); ++i) {
96835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    MT_FB_ATTACHMENT_INFO &fb_info = dev_data->fbMap[pass_info.fb].attachments[i];
96845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (pass_info.attachments[i].load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
96855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (cb_data != dev_data->cbMap.end()) {
96865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            std::function<VkBool32()> function = [=]() {
96875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                set_memory_valid(dev_data, fb_info.mem, true, fb_info.image);
96885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                return VK_FALSE;
96895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            };
96905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            cb_data->second.validate_functions.push_back(function);
96915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
96925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        VkImageLayout &attachment_layout = pass_info.attachment_first_layout[pass_info.attachments[i].attachment];
96935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (attachment_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL ||
96945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            attachment_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
96955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            skipCall |=
96965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
96975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT, (uint64_t)(pRenderPassBegin->renderPass), __LINE__,
96985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        MEMTRACK_INVALID_LAYOUT, "MEM", "Cannot clear attachment %d with invalid first layout %d.",
96995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        pass_info.attachments[i].attachment, attachment_layout);
97005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
97015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    } else if (pass_info.attachments[i].load_op == VK_ATTACHMENT_LOAD_OP_DONT_CARE) {
97025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (cb_data != dev_data->cbMap.end()) {
97035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            std::function<VkBool32()> function = [=]() {
97045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                set_memory_valid(dev_data, fb_info.mem, false, fb_info.image);
97055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                return VK_FALSE;
97065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            };
97075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            cb_data->second.validate_functions.push_back(function);
97085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
97095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    } else if (pass_info.attachments[i].load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
97105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (cb_data != dev_data->cbMap.end()) {
97115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            std::function<VkBool32()> function = [=]() {
97125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                return validate_memory_is_valid(dev_data, fb_info.mem, "vkCmdBeginRenderPass()", fb_info.image);
97135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            };
97145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            cb_data->second.validate_functions.push_back(function);
97155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
97165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
97175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (pass_info.attachment_first_read[pass_info.attachments[i].attachment]) {
97185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (cb_data != dev_data->cbMap.end()) {
97195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            std::function<VkBool32()> function = [=]() {
97205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                return validate_memory_is_valid(dev_data, fb_info.mem, "vkCmdBeginRenderPass()", fb_info.image);
97215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            };
97225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            cb_data->second.validate_functions.push_back(function);
97235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
97245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
97255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
97265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (cb_data != dev_data->cbMap.end()) {
97275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    cb_data->second.pass = pRenderPassBegin->renderPass;
97285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
97295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
97305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
97315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= VerifyFramebufferAndRenderPassLayouts(commandBuffer, pRenderPassBegin);
97325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto render_pass_data = dev_data->renderPassMap.find(pRenderPassBegin->renderPass);
97335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (render_pass_data != dev_data->renderPassMap.end()) {
97345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |= ValidateDependencies(dev_data, pRenderPassBegin, render_pass_data->second->subpassToNode);
97355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
97365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= insideRenderPass(dev_data, pCB, "vkCmdBeginRenderPass");
97375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdBeginRenderPass");
97385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= addCmd(dev_data, pCB, CMD_BEGINRENDERPASS, "vkCmdBeginRenderPass()");
97395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->activeRenderPass = pRenderPassBegin->renderPass;
97405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // This is a shallow copy as that is all that is needed for now
97415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->activeRenderPassBeginInfo = *pRenderPassBegin;
97425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->activeSubpass = 0;
97435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->activeSubpassContents = contents;
97445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->framebuffer = pRenderPassBegin->framebuffer;
97455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Connect this framebuffer to this cmdBuffer
97465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->frameBufferMap[pCB->framebuffer].referencingCmdBuffers.insert(pCB->commandBuffer);
97475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        } else {
97485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
97495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
97505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_RENDERPASS, "DS", "You cannot use a NULL RenderPass object in vkCmdBeginRenderPass()");
97515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
97525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
97535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
97545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
97555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
97565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
97575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // This is a shallow copy as that is all that is needed for now
97585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->renderPassBeginInfo = *pRenderPassBegin;
97595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->currentSubpass = 0;
97605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
97615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
97625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
97635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
97645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
97655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
97665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
97675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
97685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
97695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    TransitionSubpassLayouts(commandBuffer, &dev_data->renderPassBeginInfo, ++dev_data->currentSubpass);
97705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
97715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdNextSubpass");
97725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_NEXTSUBPASS, "vkCmdNextSubpass()");
97735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->activeSubpass++;
97745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->activeSubpassContents = contents;
97755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        TransitionSubpassLayouts(commandBuffer, &pCB->activeRenderPassBeginInfo, pCB->activeSubpass);
97765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pCB->lastBoundPipeline) {
97775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validatePipelineState(dev_data, pCB, VK_PIPELINE_BIND_POINT_GRAPHICS, pCB->lastBoundPipeline);
97785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
97795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdNextSubpass");
97805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
97815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
97825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
97835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdNextSubpass(commandBuffer, contents);
97845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
97855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
97865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkCmdEndRenderPass(VkCommandBuffer commandBuffer) {
97875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
97885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
97895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
97905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
97915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto cb_data = dev_data->cbMap.find(commandBuffer);
97925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (cb_data != dev_data->cbMap.end()) {
97935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto pass_data = dev_data->passMap.find(cb_data->second.pass);
97945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pass_data != dev_data->passMap.end()) {
97955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            MT_PASS_INFO &pass_info = pass_data->second;
97965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (size_t i = 0; i < pass_info.attachments.size(); ++i) {
97975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                MT_FB_ATTACHMENT_INFO &fb_info = dev_data->fbMap[pass_info.fb].attachments[i];
97985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pass_info.attachments[i].store_op == VK_ATTACHMENT_STORE_OP_STORE) {
97995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (cb_data != dev_data->cbMap.end()) {
98005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        std::function<VkBool32()> function = [=]() {
98015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            set_memory_valid(dev_data, fb_info.mem, true, fb_info.image);
98025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            return VK_FALSE;
98035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        };
98045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        cb_data->second.validate_functions.push_back(function);
98055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
98065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else if (pass_info.attachments[i].store_op == VK_ATTACHMENT_STORE_OP_DONT_CARE) {
98075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (cb_data != dev_data->cbMap.end()) {
98085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        std::function<VkBool32()> function = [=]() {
98095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            set_memory_valid(dev_data, fb_info.mem, false, fb_info.image);
98105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            return VK_FALSE;
98115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        };
98125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        cb_data->second.validate_functions.push_back(function);
98135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
98145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
98155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
98165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
98175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
98185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
98195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
98205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    TransitionFinalSubpassLayouts(commandBuffer, &dev_data->renderPassBeginInfo);
98215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
98225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= outsideRenderPass(dev_data, pCB, "vkCmdEndRenderpass");
98235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdEndRenderPass");
98245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_ENDRENDERPASS, "vkCmdEndRenderPass()");
98255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        TransitionFinalSubpassLayouts(commandBuffer, &pCB->activeRenderPassBeginInfo);
98265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->activeRenderPass = 0;
98275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pCB->activeSubpass = 0;
98285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
98295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
98305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
98315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdEndRenderPass(commandBuffer);
98325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
98335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
98345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool logInvalidAttachmentMessage(layer_data *dev_data, VkCommandBuffer secondaryBuffer, VkRenderPass secondaryPass,
98355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 VkRenderPass primaryPass, uint32_t primaryAttach, uint32_t secondaryAttach, const char *msg) {
98365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
98375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
98385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p which has a render pass %" PRIx64
98395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   " that is not compatible with the current render pass %" PRIx64 "."
98405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   "Attachment %" PRIu32 " is not compatable with %" PRIu32 ". %s",
98415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   (void *)secondaryBuffer, (uint64_t)(secondaryPass), (uint64_t)(primaryPass), primaryAttach, secondaryAttach,
98425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                   msg);
98435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
98445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
98455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool validateAttachmentCompatibility(layer_data *dev_data, VkCommandBuffer primaryBuffer, VkRenderPass primaryPass,
98465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     uint32_t primaryAttach, VkCommandBuffer secondaryBuffer, VkRenderPass secondaryPass,
98475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     uint32_t secondaryAttach, bool is_multi) {
98485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skip_call = false;
98495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto primary_data = dev_data->renderPassMap.find(primaryPass);
98505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto secondary_data = dev_data->renderPassMap.find(secondaryPass);
98515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primary_data->second->pCreateInfo->attachmentCount <= primaryAttach) {
98525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        primaryAttach = VK_ATTACHMENT_UNUSED;
98535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
98545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (secondary_data->second->pCreateInfo->attachmentCount <= secondaryAttach) {
98555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        secondaryAttach = VK_ATTACHMENT_UNUSED;
98565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
98575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primaryAttach == VK_ATTACHMENT_UNUSED && secondaryAttach == VK_ATTACHMENT_UNUSED) {
98585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return skip_call;
98595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
98605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primaryAttach == VK_ATTACHMENT_UNUSED) {
98615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= logInvalidAttachmentMessage(dev_data, secondaryBuffer, secondaryPass, primaryPass, primaryAttach,
98625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 secondaryAttach, "The first is unused while the second is not.");
98635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return skip_call;
98645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
98655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (secondaryAttach == VK_ATTACHMENT_UNUSED) {
98665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= logInvalidAttachmentMessage(dev_data, secondaryBuffer, secondaryPass, primaryPass, primaryAttach,
98675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 secondaryAttach, "The second is unused while the first is not.");
98685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return skip_call;
98695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
98705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primary_data->second->pCreateInfo->pAttachments[primaryAttach].format !=
98715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        secondary_data->second->pCreateInfo->pAttachments[secondaryAttach].format) {
98725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= logInvalidAttachmentMessage(dev_data, secondaryBuffer, secondaryPass, primaryPass, primaryAttach,
98735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 secondaryAttach, "They have different formats.");
98745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
98755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primary_data->second->pCreateInfo->pAttachments[primaryAttach].samples !=
98765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        secondary_data->second->pCreateInfo->pAttachments[secondaryAttach].samples) {
98775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= logInvalidAttachmentMessage(dev_data, secondaryBuffer, secondaryPass, primaryPass, primaryAttach,
98785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 secondaryAttach, "They have different samples.");
98795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
98805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (is_multi &&
98815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        primary_data->second->pCreateInfo->pAttachments[primaryAttach].flags !=
98825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            secondary_data->second->pCreateInfo->pAttachments[secondaryAttach].flags) {
98835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= logInvalidAttachmentMessage(dev_data, secondaryBuffer, secondaryPass, primaryPass, primaryAttach,
98845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 secondaryAttach, "They have different flags.");
98855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
98865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
98875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
98885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
98895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool validateSubpassCompatibility(layer_data *dev_data, VkCommandBuffer primaryBuffer, VkRenderPass primaryPass,
98905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                  VkCommandBuffer secondaryBuffer, VkRenderPass secondaryPass, const int subpass, bool is_multi) {
98915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skip_call = false;
98925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto primary_data = dev_data->renderPassMap.find(primaryPass);
98935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto secondary_data = dev_data->renderPassMap.find(secondaryPass);
98945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkSubpassDescription &primary_desc = primary_data->second->pCreateInfo->pSubpasses[subpass];
98955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    const VkSubpassDescription &secondary_desc = secondary_data->second->pCreateInfo->pSubpasses[subpass];
98965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t maxInputAttachmentCount = std::max(primary_desc.inputAttachmentCount, secondary_desc.inputAttachmentCount);
98975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < maxInputAttachmentCount; ++i) {
98985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t primary_input_attach = VK_ATTACHMENT_UNUSED, secondary_input_attach = VK_ATTACHMENT_UNUSED;
98995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (i < primary_desc.inputAttachmentCount) {
99005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            primary_input_attach = primary_desc.pInputAttachments[i].attachment;
99015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
99025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (i < secondary_desc.inputAttachmentCount) {
99035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            secondary_input_attach = secondary_desc.pInputAttachments[i].attachment;
99045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
99055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= validateAttachmentCompatibility(dev_data, primaryBuffer, primaryPass, primary_input_attach, secondaryBuffer,
99065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     secondaryPass, secondary_input_attach, is_multi);
99075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
99085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t maxColorAttachmentCount = std::max(primary_desc.colorAttachmentCount, secondary_desc.colorAttachmentCount);
99095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < maxColorAttachmentCount; ++i) {
99105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t primary_color_attach = VK_ATTACHMENT_UNUSED, secondary_color_attach = VK_ATTACHMENT_UNUSED;
99115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (i < primary_desc.colorAttachmentCount) {
99125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            primary_color_attach = primary_desc.pColorAttachments[i].attachment;
99135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
99145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (i < secondary_desc.colorAttachmentCount) {
99155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            secondary_color_attach = secondary_desc.pColorAttachments[i].attachment;
99165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
99175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= validateAttachmentCompatibility(dev_data, primaryBuffer, primaryPass, primary_color_attach, secondaryBuffer,
99185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     secondaryPass, secondary_color_attach, is_multi);
99195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        uint32_t primary_resolve_attach = VK_ATTACHMENT_UNUSED, secondary_resolve_attach = VK_ATTACHMENT_UNUSED;
99205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (i < primary_desc.colorAttachmentCount && primary_desc.pResolveAttachments) {
99215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            primary_resolve_attach = primary_desc.pResolveAttachments[i].attachment;
99225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
99235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (i < secondary_desc.colorAttachmentCount && secondary_desc.pResolveAttachments) {
99245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            secondary_resolve_attach = secondary_desc.pResolveAttachments[i].attachment;
99255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
99265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= validateAttachmentCompatibility(dev_data, primaryBuffer, primaryPass, primary_resolve_attach, secondaryBuffer,
99275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     secondaryPass, secondary_resolve_attach, is_multi);
99285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
99295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint32_t primary_depthstencil_attach = VK_ATTACHMENT_UNUSED, secondary_depthstencil_attach = VK_ATTACHMENT_UNUSED;
99305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primary_desc.pDepthStencilAttachment) {
99315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        primary_depthstencil_attach = primary_desc.pDepthStencilAttachment[0].attachment;
99325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
99335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (secondary_desc.pDepthStencilAttachment) {
99345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        secondary_depthstencil_attach = secondary_desc.pDepthStencilAttachment[0].attachment;
99355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
99365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skip_call |= validateAttachmentCompatibility(dev_data, primaryBuffer, primaryPass, primary_depthstencil_attach, secondaryBuffer,
99375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 secondaryPass, secondary_depthstencil_attach, is_multi);
99385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
99395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
99405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
99415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool validateRenderPassCompatibility(layer_data *dev_data, VkCommandBuffer primaryBuffer, VkRenderPass primaryPass,
99425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                     VkCommandBuffer secondaryBuffer, VkRenderPass secondaryPass) {
99435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skip_call = false;
99445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Early exit if renderPass objects are identical (and therefore compatible)
99455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primaryPass == secondaryPass)
99465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return skip_call;
99475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto primary_data = dev_data->renderPassMap.find(primaryPass);
99485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto secondary_data = dev_data->renderPassMap.find(secondaryPass);
99495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primary_data == dev_data->renderPassMap.end() || primary_data->second == nullptr) {
99505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |=
99515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
99525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
99535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "vkCmdExecuteCommands() called w/ invalid current Cmd Buffer %p which has invalid render pass %" PRIx64 ".",
99545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (void *)primaryBuffer, (uint64_t)(primaryPass));
99555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return skip_call;
99565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
99575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (secondary_data == dev_data->renderPassMap.end() || secondary_data->second == nullptr) {
99585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |=
99595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
99605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
99615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "vkCmdExecuteCommands() called w/ invalid secondary Cmd Buffer %p which has invalid render pass %" PRIx64 ".",
99625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (void *)secondaryBuffer, (uint64_t)(secondaryPass));
99635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return skip_call;
99645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
99655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (primary_data->second->pCreateInfo->subpassCount != secondary_data->second->pCreateInfo->subpassCount) {
99665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
99675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
99685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p which has a render pass %" PRIx64
99695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             " that is not compatible with the current render pass %" PRIx64 "."
99705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             "They have a different number of subpasses.",
99715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                             (void *)secondaryBuffer, (uint64_t)(secondaryPass), (uint64_t)(primaryPass));
99725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return skip_call;
99735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
99745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool is_multi = primary_data->second->pCreateInfo->subpassCount > 1;
99755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < primary_data->second->pCreateInfo->subpassCount; ++i) {
99765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |=
99775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            validateSubpassCompatibility(dev_data, primaryBuffer, primaryPass, secondaryBuffer, secondaryPass, i, is_multi);
99785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
99795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
99805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
99815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
99825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool validateFramebuffer(layer_data *dev_data, VkCommandBuffer primaryBuffer, const GLOBAL_CB_NODE *pCB,
99835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                         VkCommandBuffer secondaryBuffer, const GLOBAL_CB_NODE *pSubCB) {
99845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skip_call = false;
99855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!pSubCB->beginInfo.pInheritanceInfo) {
99865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return skip_call;
99875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
99885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkFramebuffer primary_fb = pCB->framebuffer;
99895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkFramebuffer secondary_fb = pSubCB->beginInfo.pInheritanceInfo->framebuffer;
99905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (secondary_fb != VK_NULL_HANDLE) {
99915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (primary_fb != secondary_fb) {
99925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
99935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
99945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p which has a framebuffer %" PRIx64
99955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 " that is not compatible with the current framebuffer %" PRIx64 ".",
99965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                 (void *)secondaryBuffer, (uint64_t)(secondary_fb), (uint64_t)(primary_fb));
99975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
99985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto fb_data = dev_data->frameBufferMap.find(secondary_fb);
99995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (fb_data == dev_data->frameBufferMap.end()) {
100005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call |=
100015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
100025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS", "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p "
100035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                          "which has invalid framebuffer %" PRIx64 ".",
100045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (void *)secondaryBuffer, (uint64_t)(secondary_fb));
100055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return skip_call;
100065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
100075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skip_call |= validateRenderPassCompatibility(dev_data, secondaryBuffer, fb_data->second.createInfo.renderPass,
100085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     secondaryBuffer, pSubCB->beginInfo.pInheritanceInfo->renderPass);
100095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
100105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
100115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
100125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
100135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlisbool validateSecondaryCommandBufferState(layer_data *dev_data, GLOBAL_CB_NODE *pCB, GLOBAL_CB_NODE *pSubCB) {
100145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
100155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    unordered_set<int> activeTypes;
100165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto queryObject : pCB->activeQueries) {
100175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto queryPoolData = dev_data->queryPoolMap.find(queryObject.pool);
100185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (queryPoolData != dev_data->queryPoolMap.end()) {
100195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (queryPoolData->second.createInfo.queryType == VK_QUERY_TYPE_PIPELINE_STATISTICS &&
100205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pSubCB->beginInfo.pInheritanceInfo) {
100215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkQueryPipelineStatisticFlags cmdBufStatistics = pSubCB->beginInfo.pInheritanceInfo->pipelineStatistics;
100225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if ((cmdBufStatistics & queryPoolData->second.createInfo.pipelineStatistics) != cmdBufStatistics) {
100235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(
100245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
100255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
100265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p "
100275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "which has invalid active query pool %" PRIx64 ". Pipeline statistics is being queried so the command "
100285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "buffer must have all bits set on the queryPool.",
100295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        reinterpret_cast<void *>(pCB->commandBuffer), reinterpret_cast<const uint64_t &>(queryPoolData->first));
100305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
100315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
100325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            activeTypes.insert(queryPoolData->second.createInfo.queryType);
100335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
100345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
100355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (auto queryObject : pSubCB->startedQueries) {
100365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto queryPoolData = dev_data->queryPoolMap.find(queryObject.pool);
100375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (queryPoolData != dev_data->queryPoolMap.end() && activeTypes.count(queryPoolData->second.createInfo.queryType)) {
100385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |=
100395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
100405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
100415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p "
100425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "which has invalid active query pool %" PRIx64 "of type %d but a query of that type has been started on "
100435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "secondary Cmd Buffer %p.",
100445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        reinterpret_cast<void *>(pCB->commandBuffer), reinterpret_cast<const uint64_t &>(queryPoolData->first),
100455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        queryPoolData->second.createInfo.queryType, reinterpret_cast<void *>(pSubCB->commandBuffer));
100465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
100475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
100485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
100495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
100505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
100515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
100525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBuffersCount, const VkCommandBuffer *pCommandBuffers) {
100535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
100545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(commandBuffer), layer_data_map);
100555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
100565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    GLOBAL_CB_NODE *pCB = getCBNode(dev_data, commandBuffer);
100575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pCB) {
100585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        GLOBAL_CB_NODE *pSubCB = NULL;
100595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < commandBuffersCount; i++) {
100605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pSubCB = getCBNode(dev_data, pCommandBuffers[i]);
100615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pSubCB) {
100625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
100635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
100645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
100655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "vkCmdExecuteCommands() called w/ invalid Cmd Buffer %p in element %u of pCommandBuffers array.",
100665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (void *)pCommandBuffers[i], i);
100675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else if (VK_COMMAND_BUFFER_LEVEL_PRIMARY == pSubCB->createInfo.level) {
100685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
100695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    __LINE__, DRAWSTATE_INVALID_SECONDARY_COMMAND_BUFFER, "DS",
100705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "vkCmdExecuteCommands() called w/ Primary Cmd Buffer %p in element %u of pCommandBuffers "
100715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    "array. All cmd buffers in pCommandBuffers array must be secondary.",
100725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    (void *)pCommandBuffers[i], i);
100735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else if (pCB->activeRenderPass) { // Secondary CB w/i RenderPass must have *CONTINUE_BIT set
100745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (!(pSubCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
100755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(
100765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
100775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)pCommandBuffers[i], __LINE__, DRAWSTATE_BEGIN_CB_INVALID_STATE, "DS",
100785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkCmdExecuteCommands(): Secondary Command Buffer (%p) executed within render pass (%#" PRIxLEAST64
100795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        ") must have had vkBeginCommandBuffer() called w/ VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT set.",
100805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (void *)pCommandBuffers[i], (uint64_t)pCB->activeRenderPass);
100815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                } else {
100825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // Make sure render pass is compatible with parent command buffer pass if has continue
100835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= validateRenderPassCompatibility(dev_data, commandBuffer, pCB->activeRenderPass, pCommandBuffers[i],
100845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                pSubCB->beginInfo.pInheritanceInfo->renderPass);
100855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= validateFramebuffer(dev_data, commandBuffer, pCB, pCommandBuffers[i], pSubCB);
100865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
100875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                string errorString = "";
100885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (!verify_renderpass_compatibility(dev_data, pCB->activeRenderPass,
100895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     pSubCB->beginInfo.pInheritanceInfo->renderPass, errorString)) {
100905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(
100915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
100925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)pCommandBuffers[i], __LINE__, DRAWSTATE_RENDERPASS_INCOMPATIBLE, "DS",
100935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkCmdExecuteCommands(): Secondary Command Buffer (%p) w/ render pass (%#" PRIxLEAST64
100945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        ") is incompatible w/ primary command buffer (%p) w/ render pass (%#" PRIxLEAST64 ") due to: %s",
100955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (void *)pCommandBuffers[i], (uint64_t)pSubCB->beginInfo.pInheritanceInfo->renderPass, (void *)commandBuffer,
100965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)pCB->activeRenderPass, errorString.c_str());
100975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
100985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                //  If framebuffer for secondary CB is not NULL, then it must match FB from vkCmdBeginRenderPass()
100995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                //   that this CB will be executed in AND framebuffer must have been created w/ RP compatible w/ renderpass
101005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pSubCB->beginInfo.pInheritanceInfo->framebuffer) {
101015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (pSubCB->beginInfo.pInheritanceInfo->framebuffer != pCB->activeRenderPassBeginInfo.framebuffer) {
101025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        skipCall |= log_msg(
101035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
101045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)pCommandBuffers[i], __LINE__, DRAWSTATE_FRAMEBUFFER_INCOMPATIBLE, "DS",
101055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "vkCmdExecuteCommands(): Secondary Command Buffer (%p) references framebuffer (%#" PRIxLEAST64
101065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            ") that does not match framebuffer (%#" PRIxLEAST64 ") in active renderpass (%#" PRIxLEAST64 ").",
101075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (void *)pCommandBuffers[i], (uint64_t)pSubCB->beginInfo.pInheritanceInfo->framebuffer,
101085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)pCB->activeRenderPassBeginInfo.framebuffer, (uint64_t)pCB->activeRenderPass);
101095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
101105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
101115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
101125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // TODO(mlentine): Move more logic into this method
101135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validateSecondaryCommandBufferState(dev_data, pCB, pSubCB);
101145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall |= validateCommandBufferState(dev_data, pSubCB);
101155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // Secondary cmdBuffers are considered pending execution starting w/
101165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            // being recorded
101175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!(pSubCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
101185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (dev_data->globalInFlightCmdBuffers.find(pSubCB->commandBuffer) != dev_data->globalInFlightCmdBuffers.end()) {
101195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(
101205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
101215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_CB_SIMULTANEOUS_USE, "DS",
101225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Attempt to simultaneously execute CB %#" PRIxLEAST64 " w/o VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT "
101235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "set!",
101245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)(pCB->commandBuffer));
101255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
101265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (pCB->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
101275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    // Warn that non-simultaneous secondary cmd buffer renders primary non-simultaneous
101285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skipCall |= log_msg(
101295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        dev_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
101305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)(pCommandBuffers[i]), __LINE__, DRAWSTATE_INVALID_CB_SIMULTANEOUS_USE, "DS",
101315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkCmdExecuteCommands(): Secondary Command Buffer (%#" PRIxLEAST64
101325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        ") does not have VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT set and will cause primary command buffer "
101335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "(%#" PRIxLEAST64 ") to be treated as if it does not have VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT "
101345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                          "set, even though it does.",
101355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)(pCommandBuffers[i]), (uint64_t)(pCB->commandBuffer));
101365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    pCB->beginInfo.flags &= ~VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
101375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
101385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
101395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (!pCB->activeQueries.empty() && !dev_data->physDevProperties.features.inheritedQueries) {
101405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |=
101415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
101425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            reinterpret_cast<uint64_t>(pCommandBuffers[i]), __LINE__, DRAWSTATE_INVALID_COMMAND_BUFFER, "DS",
101435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "vkCmdExecuteCommands(): Secondary Command Buffer "
101445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "(%#" PRIxLEAST64 ") cannot be submitted with a query in "
101455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "flight and inherited queries not "
101465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "supported on this device.",
101475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            reinterpret_cast<uint64_t>(pCommandBuffers[i]));
101485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
101495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pSubCB->primaryCommandBuffer = pCB->commandBuffer;
101505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            pCB->secondaryCommandBuffers.insert(pSubCB->commandBuffer);
101515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->globalInFlightCmdBuffers.insert(pSubCB->commandBuffer);
101525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
101535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= validatePrimaryCommandBuffer(dev_data, pCB, "vkCmdExecuteComands");
101545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        skipCall |= addCmd(dev_data, pCB, CMD_EXECUTECOMMANDS, "vkCmdExecuteComands()");
101555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
101565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
101575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall)
101585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->CmdExecuteCommands(commandBuffer, commandBuffersCount, pCommandBuffers);
101595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
101605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
101615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 ValidateMapImageLayouts(VkDevice device, VkDeviceMemory mem) {
101625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
101635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
1016458070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    auto mem_data = dev_data->memObjMap.find(mem);
1016558070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    if ((mem_data != dev_data->memObjMap.end()) && (mem_data->second.image != VK_NULL_HANDLE)) {
101665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        std::vector<VkImageLayout> layouts;
1016758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        if (FindLayouts(dev_data, mem_data->second.image, layouts)) {
101685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (auto layout : layouts) {
101695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (layout != VK_IMAGE_LAYOUT_PREINITIALIZED && layout != VK_IMAGE_LAYOUT_GENERAL) {
101705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call |= log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0,
101715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS", "Cannot map an image with layout %s. Only "
101725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                                         "GENERAL or PREINITIALIZED are supported.",
101735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                         string_VkImageLayout(layout));
101745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
101755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
101765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
101775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
101785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skip_call;
101795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
101805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
101815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
101825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkMapMemory(VkDevice device, VkDeviceMemory mem, VkDeviceSize offset, VkDeviceSize size, VkFlags flags, void **ppData) {
101835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
101845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
101855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
101865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
101875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
101885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
1018958070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    DEVICE_MEM_INFO *pMemObj = get_mem_obj_info(dev_data, mem);
101905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pMemObj) {
101915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        pMemObj->valid = true;
101925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if ((memProps.memoryTypes[pMemObj->allocInfo.memoryTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0) {
101935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skip_call =
101945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
101955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)mem, __LINE__, MEMTRACK_INVALID_STATE, "MEM",
101965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "Mapping Memory without VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT set: mem obj %#" PRIxLEAST64, (uint64_t)mem);
101975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
101985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
101995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skip_call |= validateMemRange(dev_data, mem, offset, size);
102005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    storeMemRanges(dev_data, mem, offset, size);
102015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
1020258070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis    skip_call |= ValidateMapImageLayouts(device, mem);
102035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
102045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
102055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skip_call) {
102065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->MapMemory(device, mem, offset, size, flags, ppData);
102075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
102085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        initializeAndTrackMemory(dev_data, mem, size, ppData);
102095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
102105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
102115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
102125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
102135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
102145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
102155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkUnmapMemory(VkDevice device, VkDeviceMemory mem) {
102165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
102175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
102185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
102195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
102205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= deleteMemRanges(my_data, mem);
102215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
102225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
102235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        my_data->device_dispatch_table->UnmapMemory(device, mem);
102245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
102255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
102265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
102275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 validateMemoryIsMapped(layer_data *my_data, const char *funcName, uint32_t memRangeCount,
102285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                const VkMappedMemoryRange *pMemRanges) {
102295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
102305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < memRangeCount; ++i) {
102315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto mem_element = my_data->memObjMap.find(pMemRanges[i].memory);
102325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (mem_element != my_data->memObjMap.end()) {
102335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (mem_element->second.memRange.offset > pMemRanges[i].offset) {
102345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |= log_msg(
102355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
102365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    (uint64_t)pMemRanges[i].memory, __LINE__, MEMTRACK_INVALID_MAP, "MEM",
102375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "%s: Flush/Invalidate offset (" PRINTF_SIZE_T_SPECIFIER ") is less than Memory Object's offset "
102385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    "(" PRINTF_SIZE_T_SPECIFIER ").",
102395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    funcName, static_cast<size_t>(pMemRanges[i].offset), static_cast<size_t>(mem_element->second.memRange.offset));
102405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
102415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if ((mem_element->second.memRange.size != VK_WHOLE_SIZE) &&
102425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                ((mem_element->second.memRange.offset + mem_element->second.memRange.size) <
102435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                 (pMemRanges[i].offset + pMemRanges[i].size))) {
102445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
102455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)pMemRanges[i].memory, __LINE__,
102465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    MEMTRACK_INVALID_MAP, "MEM", "%s: Flush/Invalidate upper-bound (" PRINTF_SIZE_T_SPECIFIER
102475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                 ") exceeds the Memory Object's upper-bound "
102485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                 "(" PRINTF_SIZE_T_SPECIFIER ").",
102495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    funcName, static_cast<size_t>(pMemRanges[i].offset + pMemRanges[i].size),
102505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                    static_cast<size_t>(mem_element->second.memRange.offset + mem_element->second.memRange.size));
102515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
102525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
102535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
102545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
102555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
102565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
102575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVkBool32 validateAndCopyNoncoherentMemoryToDriver(layer_data *my_data, uint32_t memRangeCount,
102585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                  const VkMappedMemoryRange *pMemRanges) {
102595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
102605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < memRangeCount; ++i) {
102615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        auto mem_element = my_data->memObjMap.find(pMemRanges[i].memory);
102625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (mem_element != my_data->memObjMap.end()) {
102635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (mem_element->second.pData) {
102645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkDeviceSize size = mem_element->second.memRange.size;
102655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkDeviceSize half_size = (size / 2);
102665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                char *data = static_cast<char *>(mem_element->second.pData);
102675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto j = 0; j < half_size; ++j) {
102685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (data[j] != NoncoherentMemoryFillValue) {
102695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
102705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)pMemRanges[i].memory, __LINE__,
102715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            MEMTRACK_INVALID_MAP, "MEM", "Memory overflow was detected on mem obj %" PRIxLEAST64,
102725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            (uint64_t)pMemRanges[i].memory);
102735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
102745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
102755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                for (auto j = size + half_size; j < 2 * size; ++j) {
102765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    if (data[j] != NoncoherentMemoryFillValue) {
102775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        skipCall |= log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
102785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT, (uint64_t)pMemRanges[i].memory, __LINE__,
102795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            MEMTRACK_INVALID_MAP, "MEM", "Memory overflow was detected on mem obj %" PRIxLEAST64,
102805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                            (uint64_t)pMemRanges[i].memory);
102815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
102825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
102835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                memcpy(mem_element->second.pDriverData, static_cast<void *>(data + (size_t)(half_size)), (size_t)(size));
102845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
102855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
102865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
102875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return skipCall;
102885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
102895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
102905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VkResult VKAPI_CALL
102915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkFlushMappedMemoryRanges(VkDevice device, uint32_t memRangeCount, const VkMappedMemoryRange *pMemRanges) {
102925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
102935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
102945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
102955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
102965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
102975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validateAndCopyNoncoherentMemoryToDriver(my_data, memRangeCount, pMemRanges);
102985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validateMemoryIsMapped(my_data, "vkFlushMappedMemoryRanges", memRangeCount, pMemRanges);
102995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
103005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
103015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = my_data->device_dispatch_table->FlushMappedMemoryRanges(device, memRangeCount, pMemRanges);
103025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
103035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
103045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
103055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
103065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VkResult VKAPI_CALL
103075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkInvalidateMappedMemoryRanges(VkDevice device, uint32_t memRangeCount, const VkMappedMemoryRange *pMemRanges) {
103085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
103095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall = VK_FALSE;
103105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
103115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
103125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
103135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    skipCall |= validateMemoryIsMapped(my_data, "vkInvalidateMappedMemoryRanges", memRangeCount, pMemRanges);
103145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
103155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
103165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = my_data->device_dispatch_table->InvalidateMappedMemoryRanges(device, memRangeCount, pMemRanges);
103175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
103185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
103195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
103205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
103215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
103225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL vkBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory mem, VkDeviceSize memoryOffset) {
103235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
103245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
103255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
103265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
103275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Track objects tied to memory
103285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    uint64_t image_handle = (uint64_t)(image);
103295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skipCall =
103305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        set_mem_binding(dev_data, device, mem, image_handle, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, "vkBindImageMemory");
103315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    add_object_binding_info(dev_data, image_handle, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, mem);
103325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    {
103335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkMemoryRequirements memRequirements;
103345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        vkGetImageMemoryRequirements(device, image, &memRequirements);
1033558070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        skipCall |= validate_buffer_image_aliasing(dev_data, image_handle, mem, memoryOffset, memRequirements,
1033658070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                                                   dev_data->memObjMap[mem].imageRanges, dev_data->memObjMap[mem].bufferRanges,
1033758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis                                                   VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT);
103385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
103395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    print_mem_list(dev_data, device);
103405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
103415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
103425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skipCall) {
103435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->BindImageMemory(device, image, mem, memoryOffset);
103445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkMemoryRequirements memRequirements;
103455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->GetImageMemoryRequirements(device, image, &memRequirements);
103465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
1034758070a671b39e2058a799c7b9c932f3f03f2e66dTobin Ehlis        dev_data->memObjMap[mem].image = image;
103485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->imageMap[image].mem = mem;
103495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->imageMap[image].memOffset = memoryOffset;
103505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->imageMap[image].memSize = memRequirements.size;
103515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
103525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
103535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
103545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
103555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
103565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL vkSetEvent(VkDevice device, VkEvent event) {
103575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
103585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
103595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->eventMap[event].needsSignaled = false;
103605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->eventMap[event].stageMask = VK_PIPELINE_STAGE_HOST_BIT;
103615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
103625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->SetEvent(device, event);
103635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
103645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
103655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
103665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL
103675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo *pBindInfo, VkFence fence) {
103685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
103695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
103705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkBool32 skip_call = VK_FALSE;
103715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
103725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    //MTMTODO : Merge this code with the checks below
103735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
103745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
103755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < bindInfoCount; i++) {
103765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkBindSparseInfo *bindInfo = &pBindInfo[i];
103775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Track objects tied to memory
103785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < bindInfo->bufferBindCount; j++) {
103795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t k = 0; k < bindInfo->pBufferBinds[j].bindCount; k++) {
103805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (set_sparse_mem_binding(dev_data, queue, bindInfo->pBufferBinds[j].pBinds[k].memory,
103815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           (uint64_t)bindInfo->pBufferBinds[j].buffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
103825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           "vkQueueBindSparse"))
103835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call = VK_TRUE;
103845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
103855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
103865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < bindInfo->imageOpaqueBindCount; j++) {
103875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t k = 0; k < bindInfo->pImageOpaqueBinds[j].bindCount; k++) {
103885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (set_sparse_mem_binding(dev_data, queue, bindInfo->pImageOpaqueBinds[j].pBinds[k].memory,
103895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           (uint64_t)bindInfo->pImageOpaqueBinds[j].image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
103905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           "vkQueueBindSparse"))
103915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call = VK_TRUE;
103925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
103935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
103945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t j = 0; j < bindInfo->imageBindCount; j++) {
103955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (uint32_t k = 0; k < bindInfo->pImageBinds[j].bindCount; k++) {
103965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (set_sparse_mem_binding(dev_data, queue, bindInfo->pImageBinds[j].pBinds[k].memory,
103975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           (uint64_t)bindInfo->pImageBinds[j].image, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
103985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                           "vkQueueBindSparse"))
103995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call = VK_TRUE;
104005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
104015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
104025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // Validate semaphore state
104035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < bindInfo->waitSemaphoreCount; i++) {
104045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkSemaphore sem = bindInfo->pWaitSemaphores[i];
104055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
104065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dev_data->semaphoreMap.find(sem) != dev_data->semaphoreMap.end()) {
104075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (dev_data->semaphoreMap[sem].state != MEMTRACK_SEMAPHORE_STATE_SIGNALLED) {
104085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call =
104095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
104105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)sem, __LINE__, MEMTRACK_NONE, "SEMAPHORE",
104115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkQueueBindSparse: Semaphore must be in signaled state before passing to pWaitSemaphores");
104125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
104135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->semaphoreMap[sem].state = MEMTRACK_SEMAPHORE_STATE_WAIT;
104145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
104155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
104165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < bindInfo->signalSemaphoreCount; i++) {
104175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkSemaphore sem = bindInfo->pSignalSemaphores[i];
104185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
104195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dev_data->semaphoreMap.find(sem) != dev_data->semaphoreMap.end()) {
104205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (dev_data->semaphoreMap[sem].state != MEMTRACK_SEMAPHORE_STATE_UNSET) {
104215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    skip_call =
104225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
104235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                (uint64_t)sem, __LINE__, MEMTRACK_NONE, "SEMAPHORE",
104245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                "vkQueueBindSparse: Semaphore must not be currently signaled or in a wait state");
104255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
104265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->semaphoreMap[sem].state = MEMTRACK_SEMAPHORE_STATE_SIGNALLED;
104275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
104285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
104295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
104305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
104315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    print_mem_list(dev_data, queue);
104325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
104335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
104345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
104355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; ++bindIdx) {
104365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkBindSparseInfo &bindInfo = pBindInfo[bindIdx];
104375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < bindInfo.waitSemaphoreCount; ++i) {
104385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dev_data->semaphoreMap[bindInfo.pWaitSemaphores[i]].signaled) {
104395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->semaphoreMap[bindInfo.pWaitSemaphores[i]].signaled = 0;
104405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
104415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |=
104425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
104435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
104445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Queue %#" PRIx64 " is waiting on semaphore %#" PRIx64 " that has no way to be signaled.",
104455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(queue), (uint64_t)(bindInfo.pWaitSemaphores[i]));
104465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
104475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
104485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < bindInfo.signalSemaphoreCount; ++i) {
104495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->semaphoreMap[bindInfo.pSignalSemaphores[i]].signaled = 1;
104505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
104515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
104525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
104535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
104545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_FALSE == skip_call)
104555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return dev_data->device_dispatch_table->QueueBindSparse(queue, bindInfoCount, pBindInfo, fence);
104565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
104575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // Update semaphore state
104585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
104595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t bind_info_idx = 0; bind_info_idx < bindInfoCount; bind_info_idx++) {
104605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const VkBindSparseInfo *bindInfo = &pBindInfo[bind_info_idx];
104615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < bindInfo->waitSemaphoreCount; i++) {
104625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            VkSemaphore sem = bindInfo->pWaitSemaphores[i];
104635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
104645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dev_data->semaphoreMap.find(sem) != dev_data->semaphoreMap.end()) {
104655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->semaphoreMap[sem].state = MEMTRACK_SEMAPHORE_STATE_UNSET;
104665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
104675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
104685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
104695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
104705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
104715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
104725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
104735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
104745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
104755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL vkCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo *pCreateInfo,
104765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                 const VkAllocationCallbacks *pAllocator, VkSemaphore *pSemaphore) {
104775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
104785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateSemaphore(device, pCreateInfo, pAllocator, pSemaphore);
104795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (result == VK_SUCCESS) {
104805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
104815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        SEMAPHORE_NODE* sNode = &dev_data->semaphoreMap[*pSemaphore];
104825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        sNode->signaled = 0;
104830a32ed7bdf03e7bda914e03c0bfa6ebf400cdf5bMichael Lentine        sNode->queue = VK_NULL_HANDLE;
104845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        sNode->in_use.store(0);
104855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        sNode->state = MEMTRACK_SEMAPHORE_STATE_UNSET;
104865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
104875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
104885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
104895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
104905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
104915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL
104925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCreateEvent(VkDevice device, const VkEventCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkEvent *pEvent) {
104935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
104945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateEvent(device, pCreateInfo, pAllocator, pEvent);
104955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (result == VK_SUCCESS) {
104965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
104975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->eventMap[*pEvent].needsSignaled = false;
104985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->eventMap[*pEvent].in_use.store(0);
104995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->eventMap[*pEvent].stageMask = VkPipelineStageFlags(0);
105005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
105015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
105025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
105035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
105045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
105055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo,
105065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                    const VkAllocationCallbacks *pAllocator,
105075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                    VkSwapchainKHR *pSwapchain) {
105085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
105095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
105105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
105115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == result) {
105120801763633180d277d26a90343464bd11646056fTobin Ehlis        SWAPCHAIN_NODE *psc_node = new SWAPCHAIN_NODE(pCreateInfo);
105135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
105140801763633180d277d26a90343464bd11646056fTobin Ehlis        dev_data->device_extensions.swapchainMap[*pSwapchain] = psc_node;
105155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
105165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
105175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
105185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
105195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
105205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
105215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
105225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator) {
105235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
105245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
105255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
105265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
105275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto swapchain_data = dev_data->device_extensions.swapchainMap.find(swapchain);
105285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (swapchain_data != dev_data->device_extensions.swapchainMap.end()) {
105295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (swapchain_data->second->images.size() > 0) {
105305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            for (auto swapchain_image : swapchain_data->second->images) {
105315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                auto image_sub = dev_data->imageSubresourceMap.find(swapchain_image);
105325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (image_sub != dev_data->imageSubresourceMap.end()) {
105335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    for (auto imgsubpair : image_sub->second) {
105345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        auto image_item = dev_data->imageLayoutMap.find(imgsubpair);
105355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (image_item != dev_data->imageLayoutMap.end()) {
105365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            dev_data->imageLayoutMap.erase(image_item);
105375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
105385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
105395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    dev_data->imageSubresourceMap.erase(image_sub);
105405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
105410801763633180d277d26a90343464bd11646056fTobin Ehlis                skipCall = clear_object_binding(dev_data, device, (uint64_t)swapchain_image,
105420801763633180d277d26a90343464bd11646056fTobin Ehlis                                                VK_DEBUG_REPORT_OBJECT_TYPE_SWAPCHAIN_KHR_EXT);
105430801763633180d277d26a90343464bd11646056fTobin Ehlis                dev_data->imageBindingMap.erase((uint64_t)swapchain_image);
105445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
105455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
105465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        delete swapchain_data->second;
105475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_extensions.swapchainMap.erase(swapchain);
105485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
105495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
105505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skipCall)
105515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->device_dispatch_table->DestroySwapchainKHR(device, swapchain, pAllocator);
105525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
105535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
105545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
105555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pCount, VkImage *pSwapchainImages) {
105565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
105575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = dev_data->device_dispatch_table->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages);
105585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
105595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (result == VK_SUCCESS && pSwapchainImages != NULL) {
105605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        // This should never happen and is checked by param checker.
105615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!pCount)
105625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return result;
105635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
105645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        const size_t count = *pCount;
105650801763633180d277d26a90343464bd11646056fTobin Ehlis        auto swapchain_node = dev_data->device_extensions.swapchainMap[swapchain];
105660801763633180d277d26a90343464bd11646056fTobin Ehlis        if (!swapchain_node->images.empty()) {
105670801763633180d277d26a90343464bd11646056fTobin Ehlis            // TODO : Not sure I like the memcmp here, but it works
105680801763633180d277d26a90343464bd11646056fTobin Ehlis            const bool mismatch = (swapchain_node->images.size() != count ||
105690801763633180d277d26a90343464bd11646056fTobin Ehlis                                   memcmp(&swapchain_node->images[0], pSwapchainImages, sizeof(swapchain_node->images[0]) * count));
105705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (mismatch) {
105715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                // TODO: Verify against Valid Usage section of extension
105725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                log_msg(dev_data->report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SWAPCHAIN_KHR_EXT,
105735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)swapchain, __LINE__, MEMTRACK_NONE, "SWAP_CHAIN",
105745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        "vkGetSwapchainInfoKHR(%" PRIu64
105755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        ", VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_KHR) returned mismatching data",
105765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        (uint64_t)(swapchain));
105775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
105785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
105795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < *pCount; ++i) {
105805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            IMAGE_LAYOUT_NODE image_layout_node;
105815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            image_layout_node.layout = VK_IMAGE_LAYOUT_UNDEFINED;
105825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            image_layout_node.format = swapchain_node->createInfo.imageFormat;
105835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->imageMap[pSwapchainImages[i]].createInfo.mipLevels = 1;
105845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->imageMap[pSwapchainImages[i]].createInfo.arrayLayers = swapchain_node->createInfo.imageArrayLayers;
105855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            swapchain_node->images.push_back(pSwapchainImages[i]);
105865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            ImageSubresourcePair subpair = {pSwapchainImages[i], false, VkImageSubresource()};
105875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->imageSubresourceMap[pSwapchainImages[i]].push_back(subpair);
105885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->imageLayoutMap[subpair] = image_layout_node;
105895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->device_extensions.imageToSwapchainMap[pSwapchainImages[i]] = swapchain;
105905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
105910801763633180d277d26a90343464bd11646056fTobin Ehlis        if (!swapchain_node->images.empty()) {
105920801763633180d277d26a90343464bd11646056fTobin Ehlis            for (auto image : swapchain_node->images) {
105930801763633180d277d26a90343464bd11646056fTobin Ehlis                // Add image object binding, then insert the new Mem Object and then bind it to created image
105940801763633180d277d26a90343464bd11646056fTobin Ehlis                add_object_create_info(dev_data, (uint64_t)image, VK_DEBUG_REPORT_OBJECT_TYPE_SWAPCHAIN_KHR_EXT,
105950801763633180d277d26a90343464bd11646056fTobin Ehlis                                       &swapchain_node->createInfo);
105960801763633180d277d26a90343464bd11646056fTobin Ehlis            }
105970801763633180d277d26a90343464bd11646056fTobin Ehlis        }
105985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
105995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
106005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
106015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
106025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
106035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo) {
106045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(queue), layer_data_map);
106055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
106065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skip_call = false;
106075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
106085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pPresentInfo) {
106095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
106105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < pPresentInfo->waitSemaphoreCount; ++i) {
106115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (dev_data->semaphoreMap[pPresentInfo->pWaitSemaphores[i]].signaled) {
106125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                dev_data->semaphoreMap[pPresentInfo->pWaitSemaphores[i]].signaled = 0;
106135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            } else {
106145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                skip_call |=
106155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
106165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            __LINE__, DRAWSTATE_QUEUE_FORWARD_PROGRESS, "DS",
106175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            "Queue %#" PRIx64 " is waiting on semaphore %#" PRIx64 " that has no way to be signaled.",
106185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            (uint64_t)(queue), (uint64_t)(pPresentInfo->pWaitSemaphores[i]));
106195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
106205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
106210801763633180d277d26a90343464bd11646056fTobin Ehlis        VkDeviceMemory mem;
106225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
106235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            auto swapchain_data = dev_data->device_extensions.swapchainMap.find(pPresentInfo->pSwapchains[i]);
106245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            if (swapchain_data != dev_data->device_extensions.swapchainMap.end() &&
106255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                pPresentInfo->pImageIndices[i] < swapchain_data->second->images.size()) {
106265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                VkImage image = swapchain_data->second->images[pPresentInfo->pImageIndices[i]];
106270801763633180d277d26a90343464bd11646056fTobin Ehlis                skip_call |=
106280801763633180d277d26a90343464bd11646056fTobin Ehlis                    get_mem_binding_from_object(dev_data, queue, (uint64_t)(image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, &mem);
106290801763633180d277d26a90343464bd11646056fTobin Ehlis                skip_call |= validate_memory_is_valid(dev_data, mem, "vkQueuePresentKHR()", image);
106305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                vector<VkImageLayout> layouts;
106315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                if (FindLayouts(dev_data, image, layouts)) {
106325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    for (auto layout : layouts) {
106335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        if (layout != VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
106345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                            skip_call |=
106355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT,
106365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        reinterpret_cast<uint64_t &>(queue), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
106375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "Images passed to present must be in layout "
106385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        "PRESENT_SOURCE_KHR but is in %s",
106395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                        string_VkImageLayout(layout));
106405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        }
106415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                    }
106425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                }
106435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            }
106445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
106455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
106465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
106475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
106485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skip_call)
106495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result = dev_data->device_dispatch_table->QueuePresentKHR(queue, pPresentInfo);
106505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
106515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
106525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    for (uint32_t i = 0; i < pPresentInfo->waitSemaphoreCount; i++) {
106535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        VkSemaphore sem = pPresentInfo->pWaitSemaphores[i];
106545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (dev_data->semaphoreMap.find(sem) != dev_data->semaphoreMap.end()) {
106555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->semaphoreMap[sem].state = MEMTRACK_SEMAPHORE_STATE_UNSET;
106565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
106575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
106585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
106595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
106605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
106615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
106625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
106635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVKAPI_ATTR VkResult VKAPI_CALL vkAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
106645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                     VkSemaphore semaphore, VkFence fence, uint32_t *pImageIndex) {
106655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
106665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult result = VK_ERROR_VALIDATION_FAILED_EXT;
106675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    bool skipCall = false;
106685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
106695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
106705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (dev_data->semaphoreMap.find(semaphore) != dev_data->semaphoreMap.end()) {
106715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (dev_data->semaphoreMap[semaphore].state != MEMTRACK_SEMAPHORE_STATE_UNSET) {
106725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            skipCall = log_msg(dev_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT,
106735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               (uint64_t)semaphore, __LINE__, MEMTRACK_NONE, "SEMAPHORE",
106745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               "vkAcquireNextImageKHR: Semaphore must not be currently signaled or in a wait state");
106755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        }
106765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        dev_data->semaphoreMap[semaphore].state = MEMTRACK_SEMAPHORE_STATE_SIGNALLED;
106775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
106785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    auto fence_data = dev_data->fenceMap.find(fence);
106795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (fence_data != dev_data->fenceMap.end()) {
106805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        fence_data->second.swapchain = swapchain;
106815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
106825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
106835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
106845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!skipCall) {
106855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        result =
106865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            dev_data->device_dispatch_table->AcquireNextImageKHR(device, swapchain, timeout, semaphore, fence, pImageIndex);
106875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
106885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
106895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    // FIXME/TODO: Need to add some thing code the "fence" parameter
106905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data->semaphoreMap[semaphore].signaled = 1;
106915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
106925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return result;
106935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
106945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
106955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
106965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkCreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
106975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                               const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pMsgCallback) {
106985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
106995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
107005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkResult res = pTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);
107015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (VK_SUCCESS == res) {
107025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_lock_mutex(&globalLock);
107035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        res = layer_create_msg_callback(my_data->report_data, pCreateInfo, pAllocator, pMsgCallback);
107045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        loader_platform_thread_unlock_mutex(&globalLock);
107055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
107065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return res;
107075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
107085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
107095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackEXT(VkInstance instance,
107105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                           VkDebugReportCallbackEXT msgCallback,
107115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                                           const VkAllocationCallbacks *pAllocator) {
107125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
107135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
107145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    pTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);
107155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_lock_mutex(&globalLock);
107165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_destroy_msg_callback(my_data->report_data, msgCallback, pAllocator);
107175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    loader_platform_thread_unlock_mutex(&globalLock);
107185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
107195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
107205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL
107215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisvkDebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objType, uint64_t object,
107225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                        size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
107235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
107245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data->instance_dispatch_table->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix,
107255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis                                                            pMsg);
107265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
107275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
107285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName) {
107295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkGetDeviceProcAddr"))
107305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
107315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyDevice"))
107325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyDevice;
107335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkQueueSubmit"))
107345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkQueueSubmit;
107355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkWaitForFences"))
107365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkWaitForFences;
107375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkGetFenceStatus"))
107385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkGetFenceStatus;
107395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkQueueWaitIdle"))
107405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkQueueWaitIdle;
107415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDeviceWaitIdle"))
107425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDeviceWaitIdle;
107435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkGetDeviceQueue"))
107445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkGetDeviceQueue;
107455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyInstance"))
107465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyInstance;
107475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyDevice"))
107485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyDevice;
107495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyFence"))
107505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyFence;
107515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkResetFences"))
107525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkResetFences;
107535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroySemaphore"))
107545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroySemaphore;
107555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyEvent"))
107565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyEvent;
107575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyQueryPool"))
107585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyQueryPool;
107595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyBuffer"))
107605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyBuffer;
107615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyBufferView"))
107625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyBufferView;
107635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyImage"))
107645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyImage;
107655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyImageView"))
107665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyImageView;
107675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyShaderModule"))
107685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyShaderModule;
107695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyPipeline"))
107705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyPipeline;
107715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyPipelineLayout"))
107725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyPipelineLayout;
107735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroySampler"))
107745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroySampler;
107755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyDescriptorSetLayout"))
107765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyDescriptorSetLayout;
107775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyDescriptorPool"))
107785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyDescriptorPool;
107795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyFramebuffer"))
107805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyFramebuffer;
107815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyRenderPass"))
107825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyRenderPass;
107835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateBuffer"))
107845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateBuffer;
107855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateBufferView"))
107865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateBufferView;
107875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateImage"))
107885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateImage;
107895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateImageView"))
107905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateImageView;
107915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateFence"))
107925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateFence;
107935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "CreatePipelineCache"))
107945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreatePipelineCache;
107955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "DestroyPipelineCache"))
107965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyPipelineCache;
107975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "GetPipelineCacheData"))
107985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkGetPipelineCacheData;
107995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "MergePipelineCaches"))
108005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkMergePipelineCaches;
108015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateGraphicsPipelines"))
108025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateGraphicsPipelines;
108035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateComputePipelines"))
108045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateComputePipelines;
108055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateSampler"))
108065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateSampler;
108075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateDescriptorSetLayout"))
108085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateDescriptorSetLayout;
108095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreatePipelineLayout"))
108105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreatePipelineLayout;
108115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateDescriptorPool"))
108125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateDescriptorPool;
108135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkResetDescriptorPool"))
108145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkResetDescriptorPool;
108155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkAllocateDescriptorSets"))
108165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkAllocateDescriptorSets;
108175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkFreeDescriptorSets"))
108185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkFreeDescriptorSets;
108195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkUpdateDescriptorSets"))
108205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkUpdateDescriptorSets;
108215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateCommandPool"))
108225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateCommandPool;
108235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyCommandPool"))
108245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyCommandPool;
108255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkResetCommandPool"))
108265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkResetCommandPool;
108275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateQueryPool"))
108285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateQueryPool;
108295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkAllocateCommandBuffers"))
108305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkAllocateCommandBuffers;
108315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkFreeCommandBuffers"))
108325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkFreeCommandBuffers;
108335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkBeginCommandBuffer"))
108345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkBeginCommandBuffer;
108355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkEndCommandBuffer"))
108365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkEndCommandBuffer;
108375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkResetCommandBuffer"))
108385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkResetCommandBuffer;
108395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdBindPipeline"))
108405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdBindPipeline;
108415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdSetViewport"))
108425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdSetViewport;
108435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdSetScissor"))
108445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdSetScissor;
108455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdSetLineWidth"))
108465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdSetLineWidth;
108475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdSetDepthBias"))
108485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdSetDepthBias;
108495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdSetBlendConstants"))
108505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdSetBlendConstants;
108515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdSetDepthBounds"))
108525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdSetDepthBounds;
108535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdSetStencilCompareMask"))
108545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdSetStencilCompareMask;
108555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdSetStencilWriteMask"))
108565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdSetStencilWriteMask;
108575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdSetStencilReference"))
108585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdSetStencilReference;
108595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdBindDescriptorSets"))
108605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdBindDescriptorSets;
108615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdBindVertexBuffers"))
108625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdBindVertexBuffers;
108635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdBindIndexBuffer"))
108645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdBindIndexBuffer;
108655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdDraw"))
108665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdDraw;
108675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdDrawIndexed"))
108685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdDrawIndexed;
108695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdDrawIndirect"))
108705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdDrawIndirect;
108715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdDrawIndexedIndirect"))
108725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdDrawIndexedIndirect;
108735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdDispatch"))
108745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdDispatch;
108755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdDispatchIndirect"))
108765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdDispatchIndirect;
108775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdCopyBuffer"))
108785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdCopyBuffer;
108795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdCopyImage"))
108805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdCopyImage;
108815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdBlitImage"))
108825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdBlitImage;
108835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdCopyBufferToImage"))
108845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdCopyBufferToImage;
108855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdCopyImageToBuffer"))
108865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdCopyImageToBuffer;
108875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdUpdateBuffer"))
108885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdUpdateBuffer;
108895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdFillBuffer"))
108905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdFillBuffer;
108915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdClearColorImage"))
108925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdClearColorImage;
108935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdClearDepthStencilImage"))
108945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdClearDepthStencilImage;
108955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdClearAttachments"))
108965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdClearAttachments;
108975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdResolveImage"))
108985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdResolveImage;
108995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdSetEvent"))
109005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdSetEvent;
109015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdResetEvent"))
109025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdResetEvent;
109035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdWaitEvents"))
109045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdWaitEvents;
109055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdPipelineBarrier"))
109065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdPipelineBarrier;
109075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdBeginQuery"))
109085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdBeginQuery;
109095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdEndQuery"))
109105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdEndQuery;
109115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdResetQueryPool"))
109125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdResetQueryPool;
109135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdCopyQueryPoolResults"))
109145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdCopyQueryPoolResults;
109155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdPushConstants"))
109165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdPushConstants;
109175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdWriteTimestamp"))
109185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdWriteTimestamp;
109195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateFramebuffer"))
109205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateFramebuffer;
109215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateShaderModule"))
109225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateShaderModule;
109235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateRenderPass"))
109245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateRenderPass;
109255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdBeginRenderPass"))
109265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdBeginRenderPass;
109275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdNextSubpass"))
109285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdNextSubpass;
109295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdEndRenderPass"))
109305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdEndRenderPass;
109315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCmdExecuteCommands"))
109325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCmdExecuteCommands;
109335b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkSetEvent"))
109345b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkSetEvent;
109355b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkMapMemory"))
109365b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkMapMemory;
109375b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
109385b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkUnmapMemory"))
109395b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkUnmapMemory;
109405b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkAllocateMemory"))
109415b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkAllocateMemory;
109425b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkFreeMemory"))
109435b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkFreeMemory;
109445b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkFlushMappedMemoryRanges"))
109455b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkFlushMappedMemoryRanges;
109465b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkInvalidateMappedMemoryRanges"))
109475b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkInvalidateMappedMemoryRanges;
109485b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkBindBufferMemory"))
109495b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkBindBufferMemory;
109505b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkGetBufferMemoryRequirements"))
109515b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkGetBufferMemoryRequirements;
109525b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkGetImageMemoryRequirements"))
109535b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkGetImageMemoryRequirements;
109545b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
109555b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkGetQueryPoolResults"))
109565b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkGetQueryPoolResults;
109575b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkBindImageMemory"))
109585b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkBindImageMemory;
109595b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkQueueBindSparse"))
109605b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkQueueBindSparse;
109615b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateSemaphore"))
109625b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateSemaphore;
109635b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateEvent"))
109645b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateEvent;
109655b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
109665b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (dev == NULL)
109675b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return NULL;
109685b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
109695b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *dev_data;
109705b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    dev_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
109715b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
109725b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (dev_data->device_extensions.wsi_enabled) {
109735b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!strcmp(funcName, "vkCreateSwapchainKHR"))
109745b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return (PFN_vkVoidFunction)vkCreateSwapchainKHR;
109755b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!strcmp(funcName, "vkDestroySwapchainKHR"))
109765b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return (PFN_vkVoidFunction)vkDestroySwapchainKHR;
109775b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!strcmp(funcName, "vkGetSwapchainImagesKHR"))
109785b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return (PFN_vkVoidFunction)vkGetSwapchainImagesKHR;
109795b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!strcmp(funcName, "vkAcquireNextImageKHR"))
109805b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return (PFN_vkVoidFunction)vkAcquireNextImageKHR;
109815b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (!strcmp(funcName, "vkQueuePresentKHR"))
109825b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return (PFN_vkVoidFunction)vkQueuePresentKHR;
109835b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
109845b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
109855b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerDispatchTable *pTable = dev_data->device_dispatch_table;
109865b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    {
109875b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        if (pTable->GetDeviceProcAddr == NULL)
109885b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis            return NULL;
109895b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return pTable->GetDeviceProcAddr(dev, funcName);
109905b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    }
109915b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
109925b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
109935b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin EhlisVK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName) {
109945b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkGetInstanceProcAddr"))
109955b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkGetInstanceProcAddr;
109965b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkGetDeviceProcAddr"))
109975b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
109985b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateInstance"))
109995b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateInstance;
110005b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkCreateDevice"))
110015b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkCreateDevice;
110025b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkDestroyInstance"))
110035b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkDestroyInstance;
110045b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#if MTMERGE
110055b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkGetPhysicalDeviceMemoryProperties"))
110065b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkGetPhysicalDeviceMemoryProperties;
110075b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis#endif
110085b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkEnumerateInstanceLayerProperties"))
110095b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkEnumerateInstanceLayerProperties;
110105b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkEnumerateInstanceExtensionProperties"))
110115b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkEnumerateInstanceExtensionProperties;
110125b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkEnumerateDeviceLayerProperties"))
110135b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkEnumerateDeviceLayerProperties;
110145b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (!strcmp(funcName, "vkEnumerateDeviceExtensionProperties"))
110155b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return (PFN_vkVoidFunction)vkEnumerateDeviceExtensionProperties;
110165b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
110175b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (instance == NULL)
110185b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return NULL;
110195b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
110205b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    PFN_vkVoidFunction fptr;
110215b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
110225b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    layer_data *my_data;
110235b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
110245b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
110255b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (fptr)
110265b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return fptr;
110275b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis
110285b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
110295b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    if (pTable->GetInstanceProcAddr == NULL)
110305b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis        return NULL;
110315b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis    return pTable->GetInstanceProcAddr(instance, funcName);
110325b5e7bcfe47d9ed90d0d5edbbe11edf7214a7784Tobin Ehlis}
11033