Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2018-2019 The Khronos Group Inc. |
| 2 | * Copyright (c) 2018-2019 Valve Corporation |
| 3 | * Copyright (c) 2018-2019 LunarG, Inc. |
| 4 | * Copyright (C) 2018-2019 Google Inc. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | // Allow use of STL min and max functions in Windows |
| 21 | #define NOMINMAX |
| 22 | |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 23 | #include "chassis.h" |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 24 | #include "core_validation.h" |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 25 | // This define indicates to build the VMA routines themselves |
| 26 | #define VMA_IMPLEMENTATION |
| 27 | // This define indicates that we will supply Vulkan function pointers at initialization |
| 28 | #define VMA_STATIC_VULKAN_FUNCTIONS 0 |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 29 | #include "gpu_validation.h" |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 30 | #include "shader_validation.h" |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 31 | #include "spirv-tools/libspirv.h" |
| 32 | #include "spirv-tools/optimizer.hpp" |
| 33 | #include "spirv-tools/instrument.hpp" |
| 34 | #include <SPIRV/spirv.hpp> |
| 35 | #include <algorithm> |
| 36 | #include <regex> |
| 37 | |
| 38 | // This is the number of bindings in the debug descriptor set. |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 39 | static const uint32_t kNumBindingsInSet = 2; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 40 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 41 | // Implementation for Descriptor Set Manager class |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 42 | GpuDescriptorSetManager::GpuDescriptorSetManager(CoreChecks *dev_data) { dev_data_ = dev_data; } |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 43 | |
| 44 | GpuDescriptorSetManager::~GpuDescriptorSetManager() { |
| 45 | for (auto &pool : desc_pool_map_) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 46 | DispatchDestroyDescriptorPool(dev_data_->device, pool.first, NULL); |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 47 | } |
| 48 | desc_pool_map_.clear(); |
| 49 | } |
| 50 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 51 | VkResult GpuDescriptorSetManager::GetDescriptorSets(uint32_t count, VkDescriptorPool *pool, |
| 52 | std::vector<VkDescriptorSet> *desc_sets) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 53 | const uint32_t default_pool_size = kItemsPerChunk; |
| 54 | VkResult result = VK_SUCCESS; |
| 55 | VkDescriptorPool pool_to_use = VK_NULL_HANDLE; |
| 56 | |
| 57 | if (0 == count) { |
| 58 | return result; |
| 59 | } |
| 60 | desc_sets->clear(); |
| 61 | desc_sets->resize(count); |
| 62 | |
| 63 | for (auto &pool : desc_pool_map_) { |
| 64 | if (pool.second.used + count < pool.second.size) { |
| 65 | pool_to_use = pool.first; |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | if (VK_NULL_HANDLE == pool_to_use) { |
| 70 | uint32_t pool_count = default_pool_size; |
| 71 | if (count > default_pool_size) { |
| 72 | pool_count = count; |
| 73 | } |
| 74 | const VkDescriptorPoolSize size_counts = { |
| 75 | VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, |
| 76 | pool_count * kNumBindingsInSet, |
| 77 | }; |
| 78 | VkDescriptorPoolCreateInfo desc_pool_info = {}; |
| 79 | desc_pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; |
| 80 | desc_pool_info.pNext = NULL; |
| 81 | desc_pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; |
| 82 | desc_pool_info.maxSets = pool_count; |
| 83 | desc_pool_info.poolSizeCount = 1; |
| 84 | desc_pool_info.pPoolSizes = &size_counts; |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 85 | result = DispatchCreateDescriptorPool(dev_data_->device, &desc_pool_info, NULL, &pool_to_use); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 86 | assert(result == VK_SUCCESS); |
| 87 | if (result != VK_SUCCESS) { |
| 88 | return result; |
| 89 | } |
| 90 | desc_pool_map_[pool_to_use].size = desc_pool_info.maxSets; |
| 91 | desc_pool_map_[pool_to_use].used = 0; |
| 92 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 93 | std::vector<VkDescriptorSetLayout> desc_layouts(count, dev_data_->gpu_validation_state->debug_desc_layout); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 94 | |
| 95 | VkDescriptorSetAllocateInfo alloc_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, NULL, pool_to_use, count, |
| 96 | desc_layouts.data()}; |
| 97 | |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 98 | result = DispatchAllocateDescriptorSets(dev_data_->device, &alloc_info, desc_sets->data()); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 99 | assert(result == VK_SUCCESS); |
| 100 | if (result != VK_SUCCESS) { |
| 101 | return result; |
| 102 | } |
| 103 | *pool = pool_to_use; |
| 104 | desc_pool_map_[pool_to_use].used += count; |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | void GpuDescriptorSetManager::PutBackDescriptorSet(VkDescriptorPool desc_pool, VkDescriptorSet desc_set) { |
| 109 | auto iter = desc_pool_map_.find(desc_pool); |
| 110 | if (iter != desc_pool_map_.end()) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 111 | VkResult result = DispatchFreeDescriptorSets(dev_data_->device, desc_pool, 1, &desc_set); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 112 | assert(result == VK_SUCCESS); |
| 113 | if (result != VK_SUCCESS) { |
| 114 | return; |
| 115 | } |
| 116 | desc_pool_map_[desc_pool].used--; |
| 117 | if (0 == desc_pool_map_[desc_pool].used) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 118 | DispatchDestroyDescriptorPool(dev_data_->device, desc_pool, NULL); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 119 | desc_pool_map_.erase(desc_pool); |
| 120 | } |
| 121 | } |
| 122 | return; |
| 123 | } |
| 124 | |
Tony-LunarG | d85808d | 2019-02-27 16:12:02 -0700 | [diff] [blame] | 125 | void GpuDescriptorSetManager::DestroyDescriptorPools() { |
| 126 | for (auto &pool : desc_pool_map_) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 127 | DispatchDestroyDescriptorPool(dev_data_->device, pool.first, NULL); |
Tony-LunarG | d85808d | 2019-02-27 16:12:02 -0700 | [diff] [blame] | 128 | } |
| 129 | desc_pool_map_.clear(); |
| 130 | } |
| 131 | |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 132 | // Trampolines to make VMA call Dispatch for Vulkan calls |
| 133 | static VKAPI_ATTR void VKAPI_CALL gpuVkGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice, |
| 134 | VkPhysicalDeviceProperties *pProperties) { |
| 135 | DispatchGetPhysicalDeviceProperties(physicalDevice, pProperties); |
| 136 | } |
| 137 | static VKAPI_ATTR void VKAPI_CALL gpuVkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice, |
| 138 | VkPhysicalDeviceMemoryProperties *pMemoryProperties) { |
| 139 | DispatchGetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties); |
| 140 | } |
| 141 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkAllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, |
| 142 | const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMemory) { |
| 143 | return DispatchAllocateMemory(device, pAllocateInfo, pAllocator, pMemory); |
| 144 | } |
| 145 | static VKAPI_ATTR void VKAPI_CALL gpuVkFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks *pAllocator) { |
| 146 | DispatchFreeMemory(device, memory, pAllocator); |
| 147 | } |
| 148 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkMapMemory(VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, |
| 149 | VkMemoryMapFlags flags, void **ppData) { |
| 150 | return DispatchMapMemory(device, memory, offset, size, flags, ppData); |
| 151 | } |
| 152 | static VKAPI_ATTR void VKAPI_CALL gpuVkUnmapMemory(VkDevice device, VkDeviceMemory memory) { DispatchUnmapMemory(device, memory); } |
| 153 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkFlushMappedMemoryRanges(VkDevice device, uint32_t memoryRangeCount, |
| 154 | const VkMappedMemoryRange *pMemoryRanges) { |
| 155 | return DispatchFlushMappedMemoryRanges(device, memoryRangeCount, pMemoryRanges); |
| 156 | } |
| 157 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkInvalidateMappedMemoryRanges(VkDevice device, uint32_t memoryRangeCount, |
| 158 | const VkMappedMemoryRange *pMemoryRanges) { |
| 159 | return DispatchInvalidateMappedMemoryRanges(device, memoryRangeCount, pMemoryRanges); |
| 160 | } |
| 161 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, |
| 162 | VkDeviceSize memoryOffset) { |
| 163 | return DispatchBindBufferMemory(device, buffer, memory, memoryOffset); |
| 164 | } |
| 165 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory, |
| 166 | VkDeviceSize memoryOffset) { |
| 167 | return DispatchBindImageMemory(device, image, memory, memoryOffset); |
| 168 | } |
| 169 | static VKAPI_ATTR void VKAPI_CALL gpuVkGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer, |
| 170 | VkMemoryRequirements *pMemoryRequirements) { |
| 171 | DispatchGetBufferMemoryRequirements(device, buffer, pMemoryRequirements); |
| 172 | } |
| 173 | static VKAPI_ATTR void VKAPI_CALL gpuVkGetImageMemoryRequirements(VkDevice device, VkImage image, |
| 174 | VkMemoryRequirements *pMemoryRequirements) { |
| 175 | DispatchGetImageMemoryRequirements(device, image, pMemoryRequirements); |
| 176 | } |
| 177 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo, |
| 178 | const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer) { |
| 179 | return DispatchCreateBuffer(device, pCreateInfo, pAllocator, pBuffer); |
| 180 | } |
| 181 | static VKAPI_ATTR void VKAPI_CALL gpuVkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) { |
| 182 | return DispatchDestroyBuffer(device, buffer, pAllocator); |
| 183 | } |
| 184 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkCreateImage(VkDevice device, const VkImageCreateInfo *pCreateInfo, |
| 185 | const VkAllocationCallbacks *pAllocator, VkImage *pImage) { |
| 186 | return DispatchCreateImage(device, pCreateInfo, pAllocator, pImage); |
| 187 | } |
| 188 | static VKAPI_ATTR void VKAPI_CALL gpuVkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) { |
| 189 | DispatchDestroyImage(device, image, pAllocator); |
| 190 | } |
| 191 | static VKAPI_ATTR void VKAPI_CALL gpuVkCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, |
| 192 | uint32_t regionCount, const VkBufferCopy *pRegions) { |
| 193 | DispatchCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, regionCount, pRegions); |
| 194 | } |
| 195 | |
| 196 | VkResult CoreChecks::GpuInitializeVma() { |
| 197 | VmaVulkanFunctions functions; |
| 198 | VmaAllocatorCreateInfo allocatorInfo = {}; |
| 199 | allocatorInfo.device = device; |
| 200 | ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(allocatorInfo.device), layer_data_map); |
| 201 | ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeCoreValidation); |
| 202 | CoreChecks *core_checks = static_cast<CoreChecks *>(validation_data); |
| 203 | allocatorInfo.physicalDevice = core_checks->physical_device; |
| 204 | |
| 205 | functions.vkGetPhysicalDeviceProperties = (PFN_vkGetPhysicalDeviceProperties)gpuVkGetPhysicalDeviceProperties; |
| 206 | functions.vkGetPhysicalDeviceMemoryProperties = (PFN_vkGetPhysicalDeviceMemoryProperties)gpuVkGetPhysicalDeviceMemoryProperties; |
| 207 | functions.vkAllocateMemory = (PFN_vkAllocateMemory)gpuVkAllocateMemory; |
| 208 | functions.vkFreeMemory = (PFN_vkFreeMemory)gpuVkFreeMemory; |
| 209 | functions.vkMapMemory = (PFN_vkMapMemory)gpuVkMapMemory; |
| 210 | functions.vkUnmapMemory = (PFN_vkUnmapMemory)gpuVkUnmapMemory; |
| 211 | functions.vkFlushMappedMemoryRanges = (PFN_vkFlushMappedMemoryRanges)gpuVkFlushMappedMemoryRanges; |
| 212 | functions.vkInvalidateMappedMemoryRanges = (PFN_vkInvalidateMappedMemoryRanges)gpuVkInvalidateMappedMemoryRanges; |
| 213 | functions.vkBindBufferMemory = (PFN_vkBindBufferMemory)gpuVkBindBufferMemory; |
| 214 | functions.vkBindImageMemory = (PFN_vkBindImageMemory)gpuVkBindImageMemory; |
| 215 | functions.vkGetBufferMemoryRequirements = (PFN_vkGetBufferMemoryRequirements)gpuVkGetBufferMemoryRequirements; |
| 216 | functions.vkGetImageMemoryRequirements = (PFN_vkGetImageMemoryRequirements)gpuVkGetImageMemoryRequirements; |
| 217 | functions.vkCreateBuffer = (PFN_vkCreateBuffer)gpuVkCreateBuffer; |
| 218 | functions.vkDestroyBuffer = (PFN_vkDestroyBuffer)gpuVkDestroyBuffer; |
| 219 | functions.vkCreateImage = (PFN_vkCreateImage)gpuVkCreateImage; |
| 220 | functions.vkDestroyImage = (PFN_vkDestroyImage)gpuVkDestroyImage; |
| 221 | functions.vkCmdCopyBuffer = (PFN_vkCmdCopyBuffer)gpuVkCmdCopyBuffer; |
| 222 | allocatorInfo.pVulkanFunctions = &functions; |
| 223 | |
| 224 | return vmaCreateAllocator(&allocatorInfo, &gpu_validation_state->vmaAllocator); |
| 225 | } |
| 226 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 227 | // Convenience function for reporting problems with setting up GPU Validation. |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 228 | void CoreChecks::ReportSetupProblem(VkDebugReportObjectTypeEXT object_type, uint64_t object_handle, |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 229 | const char *const specific_message) { |
| 230 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, object_type, object_handle, "UNASSIGNED-GPU-Assisted Validation Error. ", |
| 231 | "Detail: (%s)", specific_message); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // Turn on necessary device features. |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 235 | void CoreChecks::GpuPreCallRecordCreateDevice(VkPhysicalDevice gpu, std::unique_ptr<safe_VkDeviceCreateInfo> &create_info, |
| 236 | VkPhysicalDeviceFeatures *supported_features) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 237 | if (supported_features->fragmentStoresAndAtomics || supported_features->vertexPipelineStoresAndAtomics) { |
Tony-LunarG | 48b478a | 2019-01-15 16:35:22 -0700 | [diff] [blame] | 238 | VkPhysicalDeviceFeatures new_features = {}; |
Mark Lobodzinski | 5eb3c26 | 2019-03-01 16:08:30 -0700 | [diff] [blame] | 239 | if (create_info->pEnabledFeatures) { |
| 240 | new_features = *create_info->pEnabledFeatures; |
Tony-LunarG | 48b478a | 2019-01-15 16:35:22 -0700 | [diff] [blame] | 241 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 242 | new_features.fragmentStoresAndAtomics = supported_features->fragmentStoresAndAtomics; |
| 243 | new_features.vertexPipelineStoresAndAtomics = supported_features->vertexPipelineStoresAndAtomics; |
Mark Lobodzinski | 5eb3c26 | 2019-03-01 16:08:30 -0700 | [diff] [blame] | 244 | delete create_info->pEnabledFeatures; |
| 245 | create_info->pEnabledFeatures = new VkPhysicalDeviceFeatures(new_features); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 246 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Perform initializations that can be done at Create Device time. |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 250 | void CoreChecks::GpuPostCallRecordCreateDevice(const CHECK_ENABLED *enables) { |
| 251 | gpu_validation_state = std::unique_ptr<GpuValidationState>(new GpuValidationState); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 252 | |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 253 | gpu_validation_state->aborted = false; |
| 254 | gpu_validation_state->reserve_binding_slot = false; |
| 255 | gpu_validation_state->barrier_command_pool = VK_NULL_HANDLE; |
| 256 | gpu_validation_state->barrier_command_buffer = VK_NULL_HANDLE; |
| 257 | gpu_validation_state->reserve_binding_slot = enables->gpu_validation_reserve_binding_slot; |
Tony-LunarG | 65f9c49 | 2019-01-17 14:24:42 -0700 | [diff] [blame] | 258 | |
Mark Lobodzinski | 5c04880 | 2019-03-07 10:47:31 -0700 | [diff] [blame] | 259 | if (GetPDProperties()->apiVersion < VK_API_VERSION_1_1) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 260 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 261 | "GPU-Assisted validation requires Vulkan 1.1 or later. GPU-Assisted Validation disabled."); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 262 | gpu_validation_state->aborted = true; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | // Some devices have extremely high limits here, so set a reasonable max because we have to pad |
| 266 | // the pipeline layout with dummy descriptor set layouts. |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 267 | gpu_validation_state->adjusted_max_desc_sets = GetPDProperties()->limits.maxBoundDescriptorSets; |
| 268 | gpu_validation_state->adjusted_max_desc_sets = std::min(33U, gpu_validation_state->adjusted_max_desc_sets); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 269 | |
| 270 | // We can't do anything if there is only one. |
| 271 | // Device probably not a legit Vulkan device, since there should be at least 4. Protect ourselves. |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 272 | if (gpu_validation_state->adjusted_max_desc_sets == 1) { |
| 273 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 274 | "Device can bind only a single descriptor set. GPU-Assisted Validation disabled."); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 275 | gpu_validation_state->aborted = true; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 276 | return; |
| 277 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 278 | gpu_validation_state->desc_set_bind_index = gpu_validation_state->adjusted_max_desc_sets - 1; |
| 279 | log_msg(report_data, VK_DEBUG_REPORT_INFORMATION_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
| 280 | "UNASSIGNED-GPU-Assisted Validation. ", "Shaders using descriptor set at index %d. ", |
| 281 | gpu_validation_state->desc_set_bind_index); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 282 | |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 283 | gpu_validation_state->output_buffer_size = sizeof(uint32_t) * (spvtools::kInstMaxOutCnt + 1); |
| 284 | VkResult result = GpuInitializeVma(); |
| 285 | assert(result == VK_SUCCESS); |
Mark Lobodzinski | 3bf82a5 | 2019-03-11 11:49:34 -0600 | [diff] [blame] | 286 | std::unique_ptr<GpuDescriptorSetManager> desc_set_manager(new GpuDescriptorSetManager(this)); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 287 | |
| 288 | // The descriptor indexing checks require only the first "output" binding. |
| 289 | const VkDescriptorSetLayoutBinding debug_desc_layout_bindings[kNumBindingsInSet] = { |
| 290 | { |
| 291 | 0, // output |
| 292 | VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, |
| 293 | 1, |
| 294 | VK_SHADER_STAGE_ALL_GRAPHICS, |
| 295 | NULL, |
| 296 | }, |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 297 | { |
| 298 | 1, // input |
| 299 | VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, |
| 300 | 1, |
| 301 | VK_SHADER_STAGE_ALL_GRAPHICS, |
| 302 | NULL, |
| 303 | }, |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | const VkDescriptorSetLayoutCreateInfo debug_desc_layout_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, NULL, 0, |
| 307 | kNumBindingsInSet, debug_desc_layout_bindings}; |
| 308 | |
| 309 | const VkDescriptorSetLayoutCreateInfo dummy_desc_layout_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, NULL, 0, 0, |
| 310 | NULL}; |
| 311 | |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 312 | result = DispatchCreateDescriptorSetLayout(device, &debug_desc_layout_info, NULL, &gpu_validation_state->debug_desc_layout); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 313 | |
| 314 | // This is a layout used to "pad" a pipeline layout to fill in any gaps to the selected bind index. |
Mark Lobodzinski | 155d763 | 2019-03-07 11:20:42 -0700 | [diff] [blame] | 315 | VkResult result2 = |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 316 | DispatchCreateDescriptorSetLayout(device, &dummy_desc_layout_info, NULL, &gpu_validation_state->dummy_desc_layout); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 317 | assert((result == VK_SUCCESS) && (result2 == VK_SUCCESS)); |
| 318 | if ((result != VK_SUCCESS) || (result2 != VK_SUCCESS)) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 319 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 320 | "Unable to create descriptor set layout. GPU-Assisted Validation disabled."); |
| 321 | if (result == VK_SUCCESS) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 322 | DispatchDestroyDescriptorSetLayout(device, gpu_validation_state->debug_desc_layout, NULL); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 323 | } |
| 324 | if (result2 == VK_SUCCESS) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 325 | DispatchDestroyDescriptorSetLayout(device, gpu_validation_state->dummy_desc_layout, NULL); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 326 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 327 | gpu_validation_state->debug_desc_layout = VK_NULL_HANDLE; |
| 328 | gpu_validation_state->dummy_desc_layout = VK_NULL_HANDLE; |
| 329 | gpu_validation_state->aborted = true; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 330 | return; |
| 331 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 332 | gpu_validation_state->desc_set_manager = std::move(desc_set_manager); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // Clean up device-related resources |
Mark Lobodzinski | 70f0065 | 2019-03-07 15:22:47 -0700 | [diff] [blame] | 336 | void CoreChecks::GpuPreCallRecordDestroyDevice() { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 337 | if (gpu_validation_state->barrier_command_buffer) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 338 | DispatchFreeCommandBuffers(device, gpu_validation_state->barrier_command_pool, 1, |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 339 | &gpu_validation_state->barrier_command_buffer); |
| 340 | gpu_validation_state->barrier_command_buffer = VK_NULL_HANDLE; |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 341 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 342 | if (gpu_validation_state->barrier_command_pool) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 343 | DispatchDestroyCommandPool(device, gpu_validation_state->barrier_command_pool, NULL); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 344 | gpu_validation_state->barrier_command_pool = VK_NULL_HANDLE; |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 345 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 346 | if (gpu_validation_state->debug_desc_layout) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 347 | DispatchDestroyDescriptorSetLayout(device, gpu_validation_state->debug_desc_layout, NULL); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 348 | gpu_validation_state->debug_desc_layout = VK_NULL_HANDLE; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 349 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 350 | if (gpu_validation_state->dummy_desc_layout) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 351 | DispatchDestroyDescriptorSetLayout(device, gpu_validation_state->dummy_desc_layout, NULL); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 352 | gpu_validation_state->dummy_desc_layout = VK_NULL_HANDLE; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 353 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 354 | gpu_validation_state->desc_set_manager->DestroyDescriptorPools(); |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 355 | vmaDestroyAllocator(gpu_validation_state->vmaAllocator); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 356 | } |
| 357 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 358 | // Modify the pipeline layout to include our debug descriptor set and any needed padding with the dummy descriptor set. |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 359 | bool CoreChecks::GpuPreCallCreatePipelineLayout(const VkPipelineLayoutCreateInfo *pCreateInfo, |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 360 | const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout, |
| 361 | std::vector<VkDescriptorSetLayout> *new_layouts, |
| 362 | VkPipelineLayoutCreateInfo *modified_create_info) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 363 | if (gpu_validation_state->aborted) { |
Mark Lobodzinski | ff7d800 | 2019-02-13 13:01:26 -0700 | [diff] [blame] | 364 | return false; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 365 | } |
Mark Lobodzinski | ff7d800 | 2019-02-13 13:01:26 -0700 | [diff] [blame] | 366 | |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 367 | if (modified_create_info->setLayoutCount >= gpu_validation_state->adjusted_max_desc_sets) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 368 | std::ostringstream strm; |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 369 | strm << "Pipeline Layout conflict with validation's descriptor set at slot " << gpu_validation_state->desc_set_bind_index |
| 370 | << ". " |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 371 | << "Application has too many descriptor sets in the pipeline layout to continue with gpu validation. " |
| 372 | << "Validation is not modifying the pipeline layout. " |
| 373 | << "Instrumented shaders are replaced with non-instrumented shaders."; |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 374 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), strm.str().c_str()); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 375 | } else { |
| 376 | // Modify the pipeline layout by: |
| 377 | // 1. Copying the caller's descriptor set desc_layouts |
| 378 | // 2. Fill in dummy descriptor layouts up to the max binding |
| 379 | // 3. Fill in with the debug descriptor layout at the max binding slot |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 380 | new_layouts->reserve(gpu_validation_state->adjusted_max_desc_sets); |
Mark Lobodzinski | ff7d800 | 2019-02-13 13:01:26 -0700 | [diff] [blame] | 381 | new_layouts->insert(new_layouts->end(), &pCreateInfo->pSetLayouts[0], |
| 382 | &pCreateInfo->pSetLayouts[pCreateInfo->setLayoutCount]); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 383 | for (uint32_t i = pCreateInfo->setLayoutCount; i < gpu_validation_state->adjusted_max_desc_sets - 1; ++i) { |
| 384 | new_layouts->push_back(gpu_validation_state->dummy_desc_layout); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 385 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 386 | new_layouts->push_back(gpu_validation_state->debug_desc_layout); |
Mark Lobodzinski | ff7d800 | 2019-02-13 13:01:26 -0700 | [diff] [blame] | 387 | modified_create_info->pSetLayouts = new_layouts->data(); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 388 | modified_create_info->setLayoutCount = gpu_validation_state->adjusted_max_desc_sets; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 389 | } |
Mark Lobodzinski | ff7d800 | 2019-02-13 13:01:26 -0700 | [diff] [blame] | 390 | return true; |
| 391 | } |
| 392 | |
| 393 | // Clean up GPU validation after the CreatePipelineLayout call is made |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 394 | void CoreChecks::GpuPostCallCreatePipelineLayout(VkResult result) { |
Mark Lobodzinski | ff7d800 | 2019-02-13 13:01:26 -0700 | [diff] [blame] | 395 | // Clean up GPU validation |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 396 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 397 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 398 | "Unable to create pipeline layout. Device could become unstable."); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 399 | gpu_validation_state->aborted = true; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 400 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 401 | } |
| 402 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 403 | // Free the device memory and descriptor set associated with a command buffer. |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 404 | void CoreChecks::GpuPreCallRecordFreeCommandBuffers(uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 405 | if (gpu_validation_state->aborted) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 406 | return; |
| 407 | } |
| 408 | for (uint32_t i = 0; i < commandBufferCount; ++i) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 409 | auto gpu_buffer_list = gpu_validation_state->GetGpuBufferInfo(pCommandBuffers[i]); |
| 410 | for (auto buffer_info : gpu_buffer_list) { |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 411 | vmaDestroyBuffer(gpu_validation_state->vmaAllocator, buffer_info.output_mem_block.buffer, |
| 412 | buffer_info.output_mem_block.allocation); |
| 413 | if (buffer_info.input_mem_block.buffer) { |
| 414 | vmaDestroyBuffer(gpu_validation_state->vmaAllocator, buffer_info.input_mem_block.buffer, |
| 415 | buffer_info.input_mem_block.allocation); |
| 416 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 417 | if (buffer_info.desc_set != VK_NULL_HANDLE) { |
| 418 | gpu_validation_state->desc_set_manager->PutBackDescriptorSet(buffer_info.desc_pool, buffer_info.desc_set); |
| 419 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 420 | } |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 421 | gpu_validation_state->command_buffer_map.erase(pCommandBuffers[i]); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | // Just gives a warning about a possible deadlock. |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 426 | void CoreChecks::GpuPreCallValidateCmdWaitEvents(VkPipelineStageFlags sourceStageMask) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 427 | if (sourceStageMask & VK_PIPELINE_STAGE_HOST_BIT) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 428 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 429 | "CmdWaitEvents recorded with VK_PIPELINE_STAGE_HOST_BIT set. " |
| 430 | "GPU_Assisted validation waits on queue completion. " |
| 431 | "This wait could block the host's signaling of this event, resulting in deadlock."); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Examine the pipelines to see if they use the debug descriptor set binding index. |
| 436 | // If any do, create new non-instrumented shader modules and use them to replace the instrumented |
| 437 | // shaders in the pipeline. Return the (possibly) modified create infos to the caller. |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 438 | std::vector<safe_VkGraphicsPipelineCreateInfo> CoreChecks::GpuPreCallRecordCreateGraphicsPipelines( |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 439 | VkPipelineCache pipelineCache, uint32_t count, const VkGraphicsPipelineCreateInfo *pCreateInfos, |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 440 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, std::vector<std::unique_ptr<PIPELINE_STATE>> &pipe_state) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 441 | std::vector<safe_VkGraphicsPipelineCreateInfo> new_pipeline_create_infos; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 442 | |
| 443 | // Walk through all the pipelines, make a copy of each and flag each pipeline that contains a shader that uses the debug |
| 444 | // descriptor set index. |
| 445 | for (uint32_t pipeline = 0; pipeline < count; ++pipeline) { |
Tony-LunarG | d4da33b | 2019-04-16 16:28:22 -0600 | [diff] [blame] | 446 | bool replace_shaders = false; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 447 | new_pipeline_create_infos.push_back(pipe_state[pipeline]->graphicsPipelineCI); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 448 | if (pipe_state[pipeline]->active_slots.find(gpu_validation_state->desc_set_bind_index) != |
| 449 | pipe_state[pipeline]->active_slots.end()) { |
Tony-LunarG | d4da33b | 2019-04-16 16:28:22 -0600 | [diff] [blame] | 450 | replace_shaders = true; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 451 | } |
Tony-LunarG | d4da33b | 2019-04-16 16:28:22 -0600 | [diff] [blame] | 452 | // If the app requests all available sets, the pipeline layout was not modified at pipeline layout creation and the already |
| 453 | // instrumented shaders need to be replaced with uninstrumented shaders |
| 454 | if (pipe_state[pipeline]->pipeline_layout.set_layouts.size() >= gpu_validation_state->adjusted_max_desc_sets) { |
| 455 | replace_shaders = true; |
| 456 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 457 | |
Tony-LunarG | d4da33b | 2019-04-16 16:28:22 -0600 | [diff] [blame] | 458 | if (replace_shaders) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 459 | for (uint32_t stage = 0; stage < pCreateInfos[pipeline].stageCount; ++stage) { |
Mark Lobodzinski | 9e9da29 | 2019-03-06 16:19:55 -0700 | [diff] [blame] | 460 | const shader_module *shader = GetShaderModuleState(pCreateInfos[pipeline].pStages[stage].module); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 461 | VkShaderModuleCreateInfo create_info = {}; |
| 462 | VkShaderModule shader_module; |
| 463 | create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 464 | create_info.pCode = shader->words.data(); |
| 465 | create_info.codeSize = shader->words.size() * sizeof(uint32_t); |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 466 | VkResult result = DispatchCreateShaderModule(device, &create_info, pAllocator, &shader_module); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 467 | if (result == VK_SUCCESS) { |
| 468 | new_pipeline_create_infos[pipeline].pStages[stage].module = shader_module; |
| 469 | } else { |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 470 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 471 | HandleToUint64(pCreateInfos[pipeline].pStages[stage].module), |
| 472 | "Unable to replace instrumented shader with non-instrumented one. " |
| 473 | "Device could become unstable."); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | return new_pipeline_create_infos; |
| 479 | } |
| 480 | |
| 481 | // For every pipeline: |
| 482 | // - For every shader in a pipeline: |
| 483 | // - If the shader had to be replaced in PreCallRecord (because the pipeline is using the debug desc set index): |
| 484 | // - Destroy it since it has been bound into the pipeline by now. This is our only chance to delete it. |
| 485 | // - Track the shader in the shader_map |
| 486 | // - Save the shader binary if it contains debug code |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 487 | void CoreChecks::GpuPostCallRecordCreateGraphicsPipelines(const uint32_t count, const VkGraphicsPipelineCreateInfo *pCreateInfos, |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 488 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 489 | for (uint32_t pipeline = 0; pipeline < count; ++pipeline) { |
Mark Lobodzinski | fd917d2 | 2019-03-06 16:07:15 -0700 | [diff] [blame] | 490 | auto pipeline_state = GetPipelineState(pPipelines[pipeline]); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 491 | if (nullptr == pipeline_state) continue; |
| 492 | for (uint32_t stage = 0; stage < pipeline_state->graphicsPipelineCI.stageCount; ++stage) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 493 | if (pipeline_state->active_slots.find(gpu_validation_state->desc_set_bind_index) != |
| 494 | pipeline_state->active_slots.end()) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 495 | DispatchDestroyShaderModule(device, pCreateInfos->pStages[stage].module, pAllocator); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 496 | } |
Mark Lobodzinski | 9e9da29 | 2019-03-06 16:19:55 -0700 | [diff] [blame] | 497 | auto shader_state = GetShaderModuleState(pipeline_state->graphicsPipelineCI.pStages[stage].module); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 498 | std::vector<unsigned int> code; |
| 499 | // Save the shader binary if debug info is present. |
| 500 | // The core_validation ShaderModule tracker saves the binary too, but discards it when the ShaderModule |
| 501 | // is destroyed. Applications may destroy ShaderModules after they are placed in a pipeline and before |
| 502 | // the pipeline is used, so we have to keep another copy. |
| 503 | if (shader_state && shader_state->has_valid_spirv) { // really checking for presense of SPIR-V code. |
| 504 | for (auto insn : *shader_state) { |
| 505 | if (insn.opcode() == spv::OpLine) { |
| 506 | code = shader_state->words; |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 511 | gpu_validation_state->shader_map[shader_state->gpu_validation_shader_id].pipeline = pipeline_state->pipeline; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 512 | // Be careful to use the originally bound (instrumented) shader here, even if PreCallRecord had to back it |
| 513 | // out with a non-instrumented shader. The non-instrumented shader (found in pCreateInfo) was destroyed above. |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 514 | gpu_validation_state->shader_map[shader_state->gpu_validation_shader_id].shader_module = |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 515 | pipeline_state->graphicsPipelineCI.pStages[stage].module; |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 516 | gpu_validation_state->shader_map[shader_state->gpu_validation_shader_id].pgm = std::move(code); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | // Remove all the shader trackers associated with this destroyed pipeline. |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 522 | void CoreChecks::GpuPreCallRecordDestroyPipeline(const VkPipeline pipeline) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 523 | for (auto it = gpu_validation_state->shader_map.begin(); it != gpu_validation_state->shader_map.end();) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 524 | if (it->second.pipeline == pipeline) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 525 | it = gpu_validation_state->shader_map.erase(it); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 526 | } else { |
| 527 | ++it; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | // Call the SPIR-V Optimizer to run the instrumentation pass on the shader. |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 533 | bool CoreChecks::GpuInstrumentShader(const VkShaderModuleCreateInfo *pCreateInfo, std::vector<unsigned int> &new_pgm, |
| 534 | uint32_t *unique_shader_id) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 535 | if (gpu_validation_state->aborted) return false; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 536 | if (pCreateInfo->pCode[0] != spv::MagicNumber) return false; |
| 537 | |
| 538 | // Load original shader SPIR-V |
| 539 | uint32_t num_words = static_cast<uint32_t>(pCreateInfo->codeSize / 4); |
| 540 | new_pgm.clear(); |
| 541 | new_pgm.reserve(num_words); |
| 542 | new_pgm.insert(new_pgm.end(), &pCreateInfo->pCode[0], &pCreateInfo->pCode[num_words]); |
| 543 | |
| 544 | // Call the optimizer to instrument the shader. |
| 545 | // Use the unique_shader_module_id as a shader ID so we can look up its handle later in the shader_map. |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 546 | // If descriptor indexing is enabled, enable length checks and updated descriptor checks |
| 547 | const bool descriptor_indexing = GetDeviceExtensions()->vk_ext_descriptor_indexing; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 548 | using namespace spvtools; |
| 549 | spv_target_env target_env = SPV_ENV_VULKAN_1_1; |
| 550 | Optimizer optimizer(target_env); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 551 | optimizer.RegisterPass(CreateInstBindlessCheckPass(gpu_validation_state->desc_set_bind_index, |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 552 | gpu_validation_state->unique_shader_module_id, descriptor_indexing, |
| 553 | descriptor_indexing)); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 554 | optimizer.RegisterPass(CreateAggressiveDCEPass()); |
| 555 | bool pass = optimizer.Run(new_pgm.data(), new_pgm.size(), &new_pgm); |
| 556 | if (!pass) { |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 557 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, VK_NULL_HANDLE, |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 558 | "Failure to instrument shader. Proceeding with non-instrumented shader."); |
| 559 | } |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 560 | *unique_shader_id = gpu_validation_state->unique_shader_module_id++; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 561 | return pass; |
| 562 | } |
| 563 | |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 564 | // Create the instrumented shader data to provide to the driver. |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 565 | bool CoreChecks::GpuPreCallCreateShaderModule(const VkShaderModuleCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, |
| 566 | VkShaderModule *pShaderModule, uint32_t *unique_shader_id, |
| 567 | VkShaderModuleCreateInfo *instrumented_create_info, |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 568 | std::vector<unsigned int> *instrumented_pgm) { |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 569 | bool pass = GpuInstrumentShader(pCreateInfo, *instrumented_pgm, unique_shader_id); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 570 | if (pass) { |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 571 | instrumented_create_info->pCode = instrumented_pgm->data(); |
| 572 | instrumented_create_info->codeSize = instrumented_pgm->size() * sizeof(unsigned int); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 573 | } |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 574 | return pass; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | // Generate the stage-specific part of the message. |
| 578 | static void GenerateStageMessage(const uint32_t *debug_record, std::string &msg) { |
| 579 | using namespace spvtools; |
| 580 | std::ostringstream strm; |
| 581 | switch (debug_record[kInstCommonOutStageIdx]) { |
| 582 | case 0: { |
Tony-LunarG | 6ff8758 | 2019-02-08 10:29:07 -0700 | [diff] [blame] | 583 | strm << "Stage = Vertex. Vertex Index = " << debug_record[kInstVertOutVertexIndex] |
| 584 | << " Instance Index = " << debug_record[kInstVertOutInstanceIndex] << ". "; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 585 | } break; |
| 586 | case 1: { |
| 587 | strm << "Stage = Tessellation Control. Invocation ID = " << debug_record[kInstTessOutInvocationId] << ". "; |
| 588 | } break; |
| 589 | case 2: { |
| 590 | strm << "Stage = Tessellation Eval. Invocation ID = " << debug_record[kInstTessOutInvocationId] << ". "; |
| 591 | } break; |
| 592 | case 3: { |
| 593 | strm << "Stage = Geometry. Primitive ID = " << debug_record[kInstGeomOutPrimitiveId] |
| 594 | << " Invocation ID = " << debug_record[kInstGeomOutInvocationId] << ". "; |
| 595 | } break; |
| 596 | case 4: { |
| 597 | strm << "Stage = Fragment. Fragment coord (x,y) = (" |
| 598 | << *reinterpret_cast<const float *>(&debug_record[kInstFragOutFragCoordX]) << ", " |
| 599 | << *reinterpret_cast<const float *>(&debug_record[kInstFragOutFragCoordY]) << "). "; |
| 600 | } break; |
| 601 | case 5: { |
| 602 | strm << "Stage = Compute. Global invocation ID = " << debug_record[kInstCompOutGlobalInvocationId] << ". "; |
| 603 | } break; |
| 604 | default: { |
| 605 | strm << "Internal Error (unexpected stage = " << debug_record[kInstCommonOutStageIdx] << "). "; |
| 606 | assert(false); |
| 607 | } break; |
| 608 | } |
| 609 | msg = strm.str(); |
| 610 | } |
| 611 | |
| 612 | // Generate the part of the message describing the violation. |
| 613 | static void GenerateValidationMessage(const uint32_t *debug_record, std::string &msg, std::string &vuid_msg) { |
| 614 | using namespace spvtools; |
| 615 | std::ostringstream strm; |
| 616 | switch (debug_record[kInstValidationOutError]) { |
| 617 | case 0: { |
| 618 | strm << "Index of " << debug_record[kInstBindlessOutDescIndex] << " used to index descriptor array of length " |
| 619 | << debug_record[kInstBindlessOutDescBound] << ". "; |
Tony-LunarG | c1d657d | 2019-02-22 14:55:19 -0700 | [diff] [blame] | 620 | vuid_msg = "UNASSIGNED-Descriptor index out of bounds"; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 621 | } break; |
| 622 | case 1: { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 623 | strm << "Descriptor index " << debug_record[kInstBindlessOutDescIndex] << " is uninitialized. "; |
Tony-LunarG | c1d657d | 2019-02-22 14:55:19 -0700 | [diff] [blame] | 624 | vuid_msg = "UNASSIGNED-Descriptor uninitialized"; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 625 | } break; |
| 626 | default: { |
| 627 | strm << "Internal Error (unexpected error type = " << debug_record[kInstValidationOutError] << "). "; |
| 628 | vuid_msg = "UNASSIGNED-Internal Error"; |
| 629 | assert(false); |
| 630 | } break; |
| 631 | } |
| 632 | msg = strm.str(); |
| 633 | } |
| 634 | |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 635 | static std::string LookupDebugUtilsName(const debug_report_data *report_data, const uint64_t object) { |
Mark Lobodzinski | 1d7313a | 2019-02-07 11:04:42 -0700 | [diff] [blame] | 636 | auto object_label = report_data->DebugReportGetUtilsObjectName(object); |
| 637 | if (object_label != "") { |
| 638 | object_label = "(" + object_label + ")"; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 639 | } |
Mark Lobodzinski | 1d7313a | 2019-02-07 11:04:42 -0700 | [diff] [blame] | 640 | return object_label; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | // Generate message from the common portion of the debug report record. |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 644 | static void GenerateCommonMessage(const debug_report_data *report_data, const GLOBAL_CB_NODE *cb_node, const uint32_t *debug_record, |
Tony-LunarG | d589c7c | 2019-01-31 11:23:44 -0700 | [diff] [blame] | 645 | const VkShaderModule shader_module_handle, const VkPipeline pipeline_handle, |
| 646 | const uint32_t draw_index, std::string &msg) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 647 | using namespace spvtools; |
| 648 | std::ostringstream strm; |
| 649 | if (shader_module_handle == VK_NULL_HANDLE) { |
| 650 | strm << std::hex << std::showbase << "Internal Error: Unable to locate information for shader used in command buffer " |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 651 | << LookupDebugUtilsName(report_data, HandleToUint64(cb_node->commandBuffer)) << "(" |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 652 | << HandleToUint64(cb_node->commandBuffer) << "). "; |
| 653 | assert(true); |
| 654 | } else { |
| 655 | strm << std::hex << std::showbase << "Command buffer " |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 656 | << LookupDebugUtilsName(report_data, HandleToUint64(cb_node->commandBuffer)) << "(" |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 657 | << HandleToUint64(cb_node->commandBuffer) << "). " |
Tony-LunarG | d589c7c | 2019-01-31 11:23:44 -0700 | [diff] [blame] | 658 | << "Draw Index " << draw_index << ". " |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 659 | << "Pipeline " << LookupDebugUtilsName(report_data, HandleToUint64(pipeline_handle)) << "(" |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 660 | << HandleToUint64(pipeline_handle) << "). " |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 661 | << "Shader Module " << LookupDebugUtilsName(report_data, HandleToUint64(shader_module_handle)) << "(" |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 662 | << HandleToUint64(shader_module_handle) << "). "; |
| 663 | } |
| 664 | strm << std::dec << std::noshowbase; |
| 665 | strm << "Shader Instruction Index = " << debug_record[kInstCommonOutInstructionIdx] << ". "; |
| 666 | msg = strm.str(); |
| 667 | } |
| 668 | |
| 669 | // Read the contents of the SPIR-V OpSource instruction and any following continuation instructions. |
| 670 | // Split the single string into a vector of strings, one for each line, for easier processing. |
| 671 | static void ReadOpSource(const shader_module &shader, const uint32_t reported_file_id, std::vector<std::string> &opsource_lines) { |
| 672 | for (auto insn : shader) { |
| 673 | if ((insn.opcode() == spv::OpSource) && (insn.len() >= 5) && (insn.word(3) == reported_file_id)) { |
| 674 | std::istringstream in_stream; |
| 675 | std::string cur_line; |
| 676 | in_stream.str((char *)&insn.word(4)); |
| 677 | while (std::getline(in_stream, cur_line)) { |
| 678 | opsource_lines.push_back(cur_line); |
| 679 | } |
| 680 | while ((++insn).opcode() == spv::OpSourceContinued) { |
| 681 | in_stream.str((char *)&insn.word(1)); |
| 682 | while (std::getline(in_stream, cur_line)) { |
| 683 | opsource_lines.push_back(cur_line); |
| 684 | } |
| 685 | } |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | } |
Tony-LunarG | 03059b7 | 2019-02-19 13:57:41 -0700 | [diff] [blame] | 690 | |
| 691 | // The task here is to search the OpSource content to find the #line directive with the |
| 692 | // line number that is closest to, but still prior to the reported error line number and |
| 693 | // still within the reported filename. |
| 694 | // From this known position in the OpSource content we can add the difference between |
| 695 | // the #line line number and the reported error line number to determine the location |
| 696 | // in the OpSource content of the reported error line. |
| 697 | // |
| 698 | // Considerations: |
| 699 | // - Look only at #line directives that specify the reported_filename since |
| 700 | // the reported error line number refers to its location in the reported filename. |
| 701 | // - If a #line directive does not have a filename, the file is the reported filename, or |
| 702 | // the filename found in a prior #line directive. (This is C-preprocessor behavior) |
| 703 | // - It is possible (e.g., inlining) for blocks of code to get shuffled out of their |
| 704 | // original order and the #line directives are used to keep the numbering correct. This |
| 705 | // is why we need to examine the entire contents of the source, instead of leaving early |
| 706 | // when finding a #line line number larger than the reported error line number. |
| 707 | // |
| 708 | |
| 709 | // GCC 4.8 has a problem with std::regex that is fixed in GCC 4.9. Provide fallback code for 4.8 |
| 710 | #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) |
| 711 | |
| 712 | #if defined(__GNUC__) && GCC_VERSION < 40900 |
Tony-LunarG | 16f9b7e | 2019-02-19 13:02:03 -0700 | [diff] [blame] | 713 | static bool GetLineAndFilename(const std::string string, uint32_t *linenumber, std::string &filename) { |
Tony-LunarG | 03059b7 | 2019-02-19 13:57:41 -0700 | [diff] [blame] | 714 | // # line <linenumber> "<filename>" or |
| 715 | // #line <linenumber> "<filename>" |
| 716 | std::vector<std::string> tokens; |
| 717 | std::stringstream stream(string); |
| 718 | std::string temp; |
| 719 | uint32_t line_index = 0; |
| 720 | |
| 721 | while (stream >> temp) tokens.push_back(temp); |
| 722 | auto size = tokens.size(); |
| 723 | if (size > 1) { |
| 724 | if (tokens[0] == "#" && tokens[1] == "line") { |
| 725 | line_index = 2; |
| 726 | } else if (tokens[0] == "#line") { |
| 727 | line_index = 1; |
| 728 | } |
| 729 | } |
| 730 | if (0 == line_index) return false; |
| 731 | *linenumber = std::stoul(tokens[line_index]); |
| 732 | uint32_t filename_index = line_index + 1; |
| 733 | // Remove enclosing double quotes around filename |
| 734 | if (size > filename_index) filename = tokens[filename_index].substr(1, tokens[filename_index].size() - 2); |
| 735 | return true; |
| 736 | } |
| 737 | #else |
| 738 | static bool GetLineAndFilename(const std::string string, uint32_t *linenumber, std::string &filename) { |
Tony-LunarG | 16f9b7e | 2019-02-19 13:02:03 -0700 | [diff] [blame] | 739 | static const std::regex line_regex( // matches #line directives |
| 740 | "^" // beginning of line |
| 741 | "\\s*" // optional whitespace |
| 742 | "#" // required text |
| 743 | "\\s*" // optional whitespace |
| 744 | "line" // required text |
| 745 | "\\s+" // required whitespace |
| 746 | "([0-9]+)" // required first capture - line number |
| 747 | "(\\s+)?" // optional second capture - whitespace |
| 748 | "(\".+\")?" // optional third capture - quoted filename with at least one char inside |
| 749 | ".*"); // rest of line (needed when using std::regex_match since the entire line is tested) |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 750 | |
Tony-LunarG | 16f9b7e | 2019-02-19 13:02:03 -0700 | [diff] [blame] | 751 | std::smatch captures; |
| 752 | |
| 753 | bool found_line = std::regex_match(string, captures, line_regex); |
| 754 | if (!found_line) return false; |
| 755 | |
| 756 | // filename is optional and considered found only if the whitespace and the filename are captured |
| 757 | if (captures[2].matched && captures[3].matched) { |
| 758 | // Remove enclosing double quotes. The regex guarantees the quotes and at least one char. |
| 759 | filename = captures[3].str().substr(1, captures[3].str().size() - 2); |
| 760 | } |
| 761 | *linenumber = std::stoul(captures[1]); |
| 762 | return true; |
| 763 | } |
Tony-LunarG | 03059b7 | 2019-02-19 13:57:41 -0700 | [diff] [blame] | 764 | #endif // GCC_VERSION |
| 765 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 766 | // Extract the filename, line number, and column number from the correct OpLine and build a message string from it. |
| 767 | // Scan the source (from OpSource) to find the line of source at the reported line number and place it in another message string. |
| 768 | static void GenerateSourceMessages(const std::vector<unsigned int> &pgm, const uint32_t *debug_record, std::string &filename_msg, |
| 769 | std::string &source_msg) { |
| 770 | using namespace spvtools; |
| 771 | std::ostringstream filename_stream; |
| 772 | std::ostringstream source_stream; |
| 773 | shader_module shader; |
| 774 | shader.words = pgm; |
| 775 | // Find the OpLine just before the failing instruction indicated by the debug info. |
| 776 | // SPIR-V can only be iterated in the forward direction due to its opcode/length encoding. |
| 777 | uint32_t instruction_index = 0; |
| 778 | uint32_t reported_file_id = 0; |
| 779 | uint32_t reported_line_number = 0; |
| 780 | uint32_t reported_column_number = 0; |
| 781 | if (shader.words.size() > 0) { |
| 782 | for (auto insn : shader) { |
| 783 | if (insn.opcode() == spv::OpLine) { |
| 784 | reported_file_id = insn.word(1); |
| 785 | reported_line_number = insn.word(2); |
| 786 | reported_column_number = insn.word(3); |
| 787 | } |
| 788 | if (instruction_index == debug_record[kInstCommonOutInstructionIdx]) { |
| 789 | break; |
| 790 | } |
| 791 | instruction_index++; |
| 792 | } |
| 793 | } |
| 794 | // Create message with file information obtained from the OpString pointed to by the discovered OpLine. |
| 795 | std::string reported_filename; |
| 796 | if (reported_file_id == 0) { |
| 797 | filename_stream |
| 798 | << "Unable to find SPIR-V OpLine for source information. Build shader with debug info to get source information."; |
| 799 | } else { |
| 800 | bool found_opstring = false; |
| 801 | for (auto insn : shader) { |
| 802 | if ((insn.opcode() == spv::OpString) && (insn.len() >= 3) && (insn.word(1) == reported_file_id)) { |
| 803 | found_opstring = true; |
| 804 | reported_filename = (char *)&insn.word(2); |
| 805 | if (reported_filename.empty()) { |
| 806 | filename_stream << "Shader validation error occurred at line " << reported_line_number; |
| 807 | } else { |
| 808 | filename_stream << "Shader validation error occurred in file: " << reported_filename << " at line " |
| 809 | << reported_line_number; |
| 810 | } |
| 811 | if (reported_column_number > 0) { |
| 812 | filename_stream << ", column " << reported_column_number; |
| 813 | } |
| 814 | filename_stream << "."; |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | if (!found_opstring) { |
| 819 | filename_stream << "Unable to find SPIR-V OpString for file id " << reported_file_id << " from OpLine instruction."; |
| 820 | } |
| 821 | } |
| 822 | filename_msg = filename_stream.str(); |
| 823 | |
| 824 | // Create message to display source code line containing error. |
| 825 | if ((reported_file_id != 0)) { |
| 826 | // Read the source code and split it up into separate lines. |
| 827 | std::vector<std::string> opsource_lines; |
| 828 | ReadOpSource(shader, reported_file_id, opsource_lines); |
| 829 | // Find the line in the OpSource content that corresponds to the reported error file and line. |
| 830 | if (!opsource_lines.empty()) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 831 | uint32_t saved_line_number = 0; |
| 832 | std::string current_filename = reported_filename; // current "preprocessor" filename state. |
| 833 | std::vector<std::string>::size_type saved_opsource_offset = 0; |
| 834 | bool found_best_line = false; |
| 835 | for (auto it = opsource_lines.begin(); it != opsource_lines.end(); ++it) { |
Tony-LunarG | 16f9b7e | 2019-02-19 13:02:03 -0700 | [diff] [blame] | 836 | uint32_t parsed_line_number; |
| 837 | std::string parsed_filename; |
| 838 | bool found_line = GetLineAndFilename(*it, &parsed_line_number, parsed_filename); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 839 | if (!found_line) continue; |
Tony-LunarG | 16f9b7e | 2019-02-19 13:02:03 -0700 | [diff] [blame] | 840 | |
| 841 | bool found_filename = parsed_filename.size() > 0; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 842 | if (found_filename) { |
Tony-LunarG | 16f9b7e | 2019-02-19 13:02:03 -0700 | [diff] [blame] | 843 | current_filename = parsed_filename; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 844 | } |
| 845 | if ((!found_filename) || (current_filename == reported_filename)) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 846 | // Update the candidate best line directive, if the current one is prior and closer to the reported line |
| 847 | if (reported_line_number >= parsed_line_number) { |
| 848 | if (!found_best_line || |
| 849 | (reported_line_number - parsed_line_number <= reported_line_number - saved_line_number)) { |
| 850 | saved_line_number = parsed_line_number; |
| 851 | saved_opsource_offset = std::distance(opsource_lines.begin(), it); |
| 852 | found_best_line = true; |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | if (found_best_line) { |
| 858 | assert(reported_line_number >= saved_line_number); |
| 859 | std::vector<std::string>::size_type opsource_index = |
| 860 | (reported_line_number - saved_line_number) + 1 + saved_opsource_offset; |
| 861 | if (opsource_index < opsource_lines.size()) { |
| 862 | source_stream << "\n" << reported_line_number << ": " << opsource_lines[opsource_index].c_str(); |
| 863 | } else { |
| 864 | source_stream << "Internal error: calculated source line of " << opsource_index << " for source size of " |
| 865 | << opsource_lines.size() << " lines."; |
| 866 | } |
| 867 | } else { |
| 868 | source_stream << "Unable to find suitable #line directive in SPIR-V OpSource."; |
| 869 | } |
| 870 | } else { |
| 871 | source_stream << "Unable to find SPIR-V OpSource."; |
| 872 | } |
| 873 | } |
| 874 | source_msg = source_stream.str(); |
| 875 | } |
| 876 | |
| 877 | // Pull together all the information from the debug record to build the error message strings, |
| 878 | // and then assemble them into a single message string. |
| 879 | // Retrieve the shader program referenced by the unique shader ID provided in the debug record. |
| 880 | // We had to keep a copy of the shader program with the same lifecycle as the pipeline to make |
| 881 | // sure it is available when the pipeline is submitted. (The ShaderModule tracking object also |
| 882 | // keeps a copy, but it can be destroyed after the pipeline is created and before it is submitted.) |
| 883 | // |
Mark Lobodzinski | e377ac3 | 2019-03-07 16:12:46 -0700 | [diff] [blame] | 884 | void CoreChecks::AnalyzeAndReportError(GLOBAL_CB_NODE *cb_node, VkQueue queue, uint32_t draw_index, |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 885 | uint32_t *const debug_output_buffer) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 886 | using namespace spvtools; |
| 887 | const uint32_t total_words = debug_output_buffer[0]; |
| 888 | // A zero here means that the shader instrumentation didn't write anything. |
| 889 | // If you have nothing to say, don't say it here. |
| 890 | if (0 == total_words) { |
| 891 | return; |
| 892 | } |
| 893 | // The first word in the debug output buffer is the number of words that would have |
| 894 | // been written by the shader instrumentation, if there was enough room in the buffer we provided. |
| 895 | // The number of words actually written by the shaders is determined by the size of the buffer |
| 896 | // we provide via the descriptor. So, we process only the number of words that can fit in the |
| 897 | // buffer. |
| 898 | // Each "report" written by the shader instrumentation is considered a "record". This function |
| 899 | // is hard-coded to process only one record because it expects the buffer to be large enough to |
| 900 | // hold only one record. If there is a desire to process more than one record, this function needs |
| 901 | // to be modified to loop over records and the buffer size increased. |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 902 | std::string validation_message; |
| 903 | std::string stage_message; |
| 904 | std::string common_message; |
| 905 | std::string filename_message; |
| 906 | std::string source_message; |
| 907 | std::string vuid_msg; |
| 908 | VkShaderModule shader_module_handle = VK_NULL_HANDLE; |
| 909 | VkPipeline pipeline_handle = VK_NULL_HANDLE; |
| 910 | std::vector<unsigned int> pgm; |
| 911 | // The first record starts at this offset after the total_words. |
| 912 | const uint32_t *debug_record = &debug_output_buffer[kDebugOutputDataOffset]; |
| 913 | // Lookup the VkShaderModule handle and SPIR-V code used to create the shader, using the unique shader ID value returned |
| 914 | // by the instrumented shader. |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 915 | auto it = gpu_validation_state->shader_map.find(debug_record[kInstCommonOutShaderId]); |
| 916 | if (it != gpu_validation_state->shader_map.end()) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 917 | shader_module_handle = it->second.shader_module; |
| 918 | pipeline_handle = it->second.pipeline; |
| 919 | pgm = it->second.pgm; |
| 920 | } |
| 921 | GenerateValidationMessage(debug_record, validation_message, vuid_msg); |
| 922 | GenerateStageMessage(debug_record, stage_message); |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 923 | GenerateCommonMessage(report_data, cb_node, debug_record, shader_module_handle, pipeline_handle, draw_index, common_message); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 924 | GenerateSourceMessages(pgm, debug_record, filename_message, source_message); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 925 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT, HandleToUint64(queue), |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 926 | vuid_msg.c_str(), "%s %s %s %s%s", validation_message.c_str(), common_message.c_str(), stage_message.c_str(), |
| 927 | filename_message.c_str(), source_message.c_str()); |
| 928 | // The debug record at word kInstCommonOutSize is the number of words in the record |
| 929 | // written by the shader. Clear the entire record plus the total_words word at the start. |
| 930 | const uint32_t words_to_clear = 1 + std::min(debug_record[kInstCommonOutSize], (uint32_t)kInstMaxOutCnt); |
| 931 | memset(debug_output_buffer, 0, sizeof(uint32_t) * words_to_clear); |
| 932 | } |
| 933 | |
Tony-LunarG | 5ad1727 | 2019-03-05 12:48:24 -0700 | [diff] [blame] | 934 | // For the given command buffer, map its debug data buffers and read their contents for analysis. |
Mark Lobodzinski | e377ac3 | 2019-03-07 16:12:46 -0700 | [diff] [blame] | 935 | void CoreChecks::ProcessInstrumentationBuffer(VkQueue queue, GLOBAL_CB_NODE *cb_node) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 936 | auto gpu_buffer_list = gpu_validation_state->GetGpuBufferInfo(cb_node->commandBuffer); |
| 937 | if (cb_node && cb_node->hasDrawCmd && gpu_buffer_list.size() > 0) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 938 | VkResult result; |
| 939 | char *pData; |
Tony-LunarG | 5ad1727 | 2019-03-05 12:48:24 -0700 | [diff] [blame] | 940 | uint32_t draw_index = 0; |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 941 | |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 942 | for (auto &buffer_info : gpu_buffer_list) { |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 943 | result = vmaMapMemory(gpu_validation_state->vmaAllocator, buffer_info.output_mem_block.allocation, (void **)&pData); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 944 | // Analyze debug output buffer |
| 945 | if (result == VK_SUCCESS) { |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 946 | AnalyzeAndReportError(cb_node, queue, draw_index, (uint32_t *)pData); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 947 | vmaUnmapMemory(gpu_validation_state->vmaAllocator, buffer_info.output_mem_block.allocation); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 948 | } |
Tony-LunarG | d589c7c | 2019-01-31 11:23:44 -0700 | [diff] [blame] | 949 | draw_index++; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 954 | // For the given command buffer, map its debug data buffers and update the status of any update after bind descriptors |
| 955 | void CoreChecks::UpdateInstrumentationBuffer(GLOBAL_CB_NODE *cb_node) { |
| 956 | auto gpu_buffer_list = gpu_validation_state->GetGpuBufferInfo(cb_node->commandBuffer); |
| 957 | uint32_t *pData; |
| 958 | for (auto &buffer_info : gpu_buffer_list) { |
| 959 | if (buffer_info.input_mem_block.update_at_submit.size() > 0) { |
| 960 | VkResult result = |
| 961 | vmaMapMemory(gpu_validation_state->vmaAllocator, buffer_info.input_mem_block.allocation, (void **)&pData); |
| 962 | if (result == VK_SUCCESS) { |
| 963 | for (auto update : buffer_info.input_mem_block.update_at_submit) { |
| 964 | if (update.second->updated) pData[update.first] = 1; |
| 965 | } |
| 966 | vmaUnmapMemory(gpu_validation_state->vmaAllocator, buffer_info.input_mem_block.allocation); |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 972 | // Submit a memory barrier on graphics queues. |
| 973 | // Lazy-create and record the needed command buffer. |
Mark Lobodzinski | e377ac3 | 2019-03-07 16:12:46 -0700 | [diff] [blame] | 974 | void CoreChecks::SubmitBarrier(VkQueue queue) { |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 975 | uint32_t queue_family_index = 0; |
| 976 | |
Mark Lobodzinski | e377ac3 | 2019-03-07 16:12:46 -0700 | [diff] [blame] | 977 | auto it = queueMap.find(queue); |
| 978 | if (it != queueMap.end()) { |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 979 | queue_family_index = it->second.queueFamilyIndex; |
| 980 | } |
| 981 | |
| 982 | // Pay attention only to queues that support graphics. |
| 983 | // This ensures that the command buffer pool is created so that it can be used on a graphics queue. |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 984 | VkQueueFlags queue_flags = GetPhysicalDeviceState()->queue_family_properties[queue_family_index].queueFlags; |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 985 | if (!(queue_flags & VK_QUEUE_GRAPHICS_BIT)) { |
| 986 | return; |
| 987 | } |
| 988 | |
| 989 | // Lazy-allocate and record the command buffer. |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 990 | if (gpu_validation_state->barrier_command_buffer == VK_NULL_HANDLE) { |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 991 | VkResult result; |
| 992 | VkCommandPoolCreateInfo pool_create_info = {}; |
| 993 | pool_create_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 994 | pool_create_info.queueFamilyIndex = queue_family_index; |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 995 | result = DispatchCreateCommandPool(device, &pool_create_info, nullptr, &gpu_validation_state->barrier_command_pool); |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 996 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 997 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 998 | "Unable to create command pool for barrier CB."); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 999 | gpu_validation_state->barrier_command_pool = VK_NULL_HANDLE; |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | VkCommandBufferAllocateInfo command_buffer_alloc_info = {}; |
| 1004 | command_buffer_alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1005 | command_buffer_alloc_info.commandPool = gpu_validation_state->barrier_command_pool; |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1006 | command_buffer_alloc_info.commandBufferCount = 1; |
| 1007 | command_buffer_alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 1008 | result = DispatchAllocateCommandBuffers(device, &command_buffer_alloc_info, &gpu_validation_state->barrier_command_buffer); |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1009 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1010 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1011 | "Unable to create barrier command buffer."); |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 1012 | DispatchDestroyCommandPool(device, gpu_validation_state->barrier_command_pool, nullptr); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1013 | gpu_validation_state->barrier_command_pool = VK_NULL_HANDLE; |
| 1014 | gpu_validation_state->barrier_command_buffer = VK_NULL_HANDLE; |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1015 | return; |
| 1016 | } |
| 1017 | |
| 1018 | // Hook up command buffer dispatch |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1019 | *((const void **)gpu_validation_state->barrier_command_buffer) = *(void **)(device); |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1020 | |
| 1021 | // Record a global memory barrier to force availability of device memory operations to the host domain. |
| 1022 | VkCommandBufferBeginInfo command_buffer_begin_info = {}; |
| 1023 | command_buffer_begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 1024 | result = DispatchBeginCommandBuffer(gpu_validation_state->barrier_command_buffer, &command_buffer_begin_info); |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1025 | |
| 1026 | if (result == VK_SUCCESS) { |
| 1027 | VkMemoryBarrier memory_barrier = {}; |
| 1028 | memory_barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER; |
| 1029 | memory_barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT; |
| 1030 | memory_barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT; |
| 1031 | |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 1032 | DispatchCmdPipelineBarrier(gpu_validation_state->barrier_command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
Mark Lobodzinski | e514d1a | 2019-03-12 08:47:45 -0600 | [diff] [blame] | 1033 | VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &memory_barrier, 0, nullptr, 0, nullptr); |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 1034 | DispatchEndCommandBuffer(gpu_validation_state->barrier_command_buffer); |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1038 | if (gpu_validation_state->barrier_command_buffer) { |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1039 | VkSubmitInfo submit_info = {}; |
| 1040 | submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 1041 | submit_info.commandBufferCount = 1; |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1042 | submit_info.pCommandBuffers = &gpu_validation_state->barrier_command_buffer; |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 1043 | DispatchQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE); |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1047 | void CoreChecks::GpuPreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence) { |
| 1048 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1049 | const VkSubmitInfo *submit = &pSubmits[submit_idx]; |
| 1050 | for (uint32_t i = 0; i < submit->commandBufferCount; i++) { |
| 1051 | auto cb_node = GetCBNode(submit->pCommandBuffers[i]); |
| 1052 | UpdateInstrumentationBuffer(cb_node); |
| 1053 | for (auto secondaryCmdBuffer : cb_node->linkedCommandBuffers) { |
| 1054 | UpdateInstrumentationBuffer(secondaryCmdBuffer); |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1060 | // Issue a memory barrier to make GPU-written data available to host. |
| 1061 | // Wait for the queue to complete execution. |
| 1062 | // Check the debug buffers for all the command buffers that were submitted. |
Mark Lobodzinski | e377ac3 | 2019-03-07 16:12:46 -0700 | [diff] [blame] | 1063 | void CoreChecks::GpuPostCallQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1064 | if (gpu_validation_state->aborted) return; |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1065 | |
Mark Lobodzinski | e377ac3 | 2019-03-07 16:12:46 -0700 | [diff] [blame] | 1066 | SubmitBarrier(queue); |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1067 | |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 1068 | DispatchQueueWaitIdle(queue); |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1069 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1070 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1071 | const VkSubmitInfo *submit = &pSubmits[submit_idx]; |
| 1072 | for (uint32_t i = 0; i < submit->commandBufferCount; i++) { |
Mark Lobodzinski | 1b3a971 | 2019-03-06 15:51:47 -0700 | [diff] [blame] | 1073 | auto cb_node = GetCBNode(submit->pCommandBuffers[i]); |
Mark Lobodzinski | e377ac3 | 2019-03-07 16:12:46 -0700 | [diff] [blame] | 1074 | ProcessInstrumentationBuffer(queue, cb_node); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1075 | for (auto secondaryCmdBuffer : cb_node->linkedCommandBuffers) { |
Mark Lobodzinski | e377ac3 | 2019-03-07 16:12:46 -0700 | [diff] [blame] | 1076 | ProcessInstrumentationBuffer(queue, secondaryCmdBuffer); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | } |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1081 | |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 1082 | void CoreChecks::GpuAllocateValidationResources(const VkCommandBuffer cmd_buffer, const VkPipelineBindPoint bind_point) { |
andreyg | ca287f2 | 2019-04-10 00:15:33 +0300 | [diff] [blame] | 1083 | // Does GPUAV support VK_PIPELINE_BIND_POINT_RAY_TRACING_NV? |
| 1084 | if (bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS && bind_point != VK_PIPELINE_BIND_POINT_COMPUTE) { |
| 1085 | return; |
| 1086 | } |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1087 | VkResult result; |
| 1088 | |
Mark Lobodzinski | 44da62c | 2019-03-07 10:50:59 -0700 | [diff] [blame] | 1089 | if (!(GetEnables()->gpu_validation)) return; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1090 | |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1091 | if (gpu_validation_state->aborted) return; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1092 | |
| 1093 | std::vector<VkDescriptorSet> desc_sets; |
| 1094 | VkDescriptorPool desc_pool = VK_NULL_HANDLE; |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1095 | result = gpu_validation_state->desc_set_manager->GetDescriptorSets(1, &desc_pool, &desc_sets); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1096 | assert(result == VK_SUCCESS); |
| 1097 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1098 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1099 | "Unable to allocate descriptor sets. Device could become unstable."); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1100 | gpu_validation_state->aborted = true; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1101 | return; |
| 1102 | } |
| 1103 | |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1104 | VkDescriptorBufferInfo output_desc_buffer_info = {}; |
| 1105 | output_desc_buffer_info.range = gpu_validation_state->output_buffer_size; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1106 | |
Mark Lobodzinski | 1b3a971 | 2019-03-06 15:51:47 -0700 | [diff] [blame] | 1107 | auto cb_node = GetCBNode(cmd_buffer); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1108 | if (!cb_node) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1109 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), "Unrecognized command buffer"); |
| 1110 | gpu_validation_state->aborted = true; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1111 | return; |
| 1112 | } |
| 1113 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1114 | // Allocate memory for the output block that the gpu will use to return any error information |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1115 | GpuDeviceMemoryBlock output_block = {}; |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 1116 | VkBufferCreateInfo bufferInfo = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; |
| 1117 | bufferInfo.size = gpu_validation_state->output_buffer_size; |
| 1118 | bufferInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 1119 | VmaAllocationCreateInfo allocInfo = {}; |
| 1120 | allocInfo.usage = VMA_MEMORY_USAGE_GPU_TO_CPU; |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1121 | result = vmaCreateBuffer(gpu_validation_state->vmaAllocator, &bufferInfo, &allocInfo, &output_block.buffer, |
| 1122 | &output_block.allocation, nullptr); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1123 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1124 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1125 | "Unable to allocate device memory. Device could become unstable."); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1126 | gpu_validation_state->aborted = true; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1127 | return; |
| 1128 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1129 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1130 | // Clear the output block to zeros so that only error information from the gpu will be present |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1131 | uint32_t *pData; |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1132 | result = vmaMapMemory(gpu_validation_state->vmaAllocator, output_block.allocation, (void **)&pData); |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 1133 | if (result == VK_SUCCESS) { |
| 1134 | memset(pData, 0, gpu_validation_state->output_buffer_size); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1135 | vmaUnmapMemory(gpu_validation_state->vmaAllocator, output_block.allocation); |
| 1136 | } |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1137 | |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1138 | GpuDeviceMemoryBlock input_block = {}; |
| 1139 | VkWriteDescriptorSet desc_writes[2] = {}; |
| 1140 | uint32_t desc_count = 1; |
| 1141 | auto const &state = cb_node->lastBound[bind_point]; |
| 1142 | uint32_t number_of_sets = (uint32_t)state.boundDescriptorSets.size(); |
| 1143 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1144 | // Figure out how much memory we need for the input block based on how many sets and bindings there are |
| 1145 | // and how big each of the bindings is |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1146 | if (number_of_sets > 0 && GetDeviceExtensions()->vk_ext_descriptor_indexing) { |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1147 | uint32_t descriptor_count = 0; // Number of descriptors, including all array elements |
| 1148 | uint32_t binding_count = 0; // Number of bindings based on the max binding number used |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1149 | for (auto desc : state.boundDescriptorSets) { |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1150 | auto bindings = desc->GetLayout()->GetSortedBindingSet(); |
| 1151 | if (bindings.size() > 0) { |
| 1152 | binding_count += desc->GetLayout()->GetMaxBinding() + 1; |
| 1153 | for (auto binding : bindings) { |
| 1154 | if (binding == desc->GetLayout()->GetMaxBinding() && desc->IsVariableDescriptorCount(binding)) { |
| 1155 | descriptor_count += desc->GetVariableDescriptorCount(); |
| 1156 | } else { |
| 1157 | descriptor_count += desc->GetDescriptorCountFromBinding(binding); |
| 1158 | } |
| 1159 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1163 | // Note that the size of the input buffer is dependent on the maximum binding number, which |
| 1164 | // can be very large. This is because for (set = s, binding = b, index = i), the validation |
| 1165 | // code is going to dereference Input[ i + Input[ b + Input[ s + Input[ Input[0] ] ] ] ] to |
| 1166 | // see if descriptors have been written. In gpu_validation.md, we note this and advise |
| 1167 | // using densely packed bindings as a best practice when using gpu-av with descriptor indexing |
| 1168 | uint32_t words_needed = 1 + (number_of_sets * 2) + (binding_count * 2) + descriptor_count; |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1169 | allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; |
| 1170 | bufferInfo.size = words_needed * 4; |
| 1171 | result = vmaCreateBuffer(gpu_validation_state->vmaAllocator, &bufferInfo, &allocInfo, &input_block.buffer, |
| 1172 | &input_block.allocation, nullptr); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1173 | if (result != VK_SUCCESS) { |
| 1174 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), |
| 1175 | "Unable to allocate device memory. Device could become unstable."); |
| 1176 | gpu_validation_state->aborted = true; |
| 1177 | return; |
| 1178 | } |
| 1179 | |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1180 | // Populate input buffer first with the sizes of every descriptor in every set, then with whether |
| 1181 | // each element of each descriptor has been written or not. See gpu_validation.md for a more thourough |
| 1182 | // outline of the input buffer format |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1183 | result = vmaMapMemory(gpu_validation_state->vmaAllocator, input_block.allocation, (void **)&pData); |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1184 | // Pointer to a sets array that points into the sizes array |
| 1185 | uint32_t *sets_to_sizes = pData + 1; |
| 1186 | // Pointer to the sizes array that contains the array size of the descriptor at each binding |
| 1187 | uint32_t *sizes = sets_to_sizes + number_of_sets; |
| 1188 | // Pointer to another sets array that points into the bindings array that points into the written array |
| 1189 | uint32_t *sets_to_bindings = sizes + binding_count; |
| 1190 | // Pointer to the bindings array that points at the start of the writes in the writes array for each binding |
| 1191 | uint32_t *bindings_to_written = sets_to_bindings + number_of_sets; |
| 1192 | // Index of the next entry in the written array to be updated |
| 1193 | uint32_t written_index = 1 + (number_of_sets * 2) + (binding_count * 2); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1194 | uint32_t bindCounter = number_of_sets + 1; |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1195 | // Index of the start of the sets_to_bindings array |
| 1196 | pData[0] = number_of_sets + binding_count + 1; |
| 1197 | |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1198 | for (auto desc : state.boundDescriptorSets) { |
| 1199 | auto layout = desc->GetLayout(); |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1200 | auto bindings = layout->GetSortedBindingSet(); |
| 1201 | if (bindings.size() > 0) { |
| 1202 | // For each set, fill in index of its bindings sizes in the sizes array |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1203 | *sets_to_sizes++ = bindCounter; |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1204 | // For each set, fill in the index of its bindings in the bindings_to_written array |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1205 | *sets_to_bindings++ = bindCounter + number_of_sets + binding_count; |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1206 | for (auto binding : bindings) { |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1207 | // For each binding, fill in its size in the sizes array |
| 1208 | if (binding == layout->GetMaxBinding() && desc->IsVariableDescriptorCount(binding)) { |
| 1209 | sizes[binding] = desc->GetVariableDescriptorCount(); |
| 1210 | } else { |
| 1211 | sizes[binding] = desc->GetDescriptorCountFromBinding(binding); |
| 1212 | } |
| 1213 | // Fill in the starting index for this binding in the written array in the bindings_to_written array |
| 1214 | bindings_to_written[binding] = written_index; |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1215 | |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1216 | auto index_range = desc->GetGlobalIndexRangeFromBinding(binding, true); |
| 1217 | // For each array element in the binding, update the written array with whether it has been written |
| 1218 | for (uint32_t i = index_range.start; i < index_range.end; ++i) { |
| 1219 | auto *descriptor = desc->GetDescriptorFromGlobalIndex(i); |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1220 | if (descriptor->updated) { |
| 1221 | pData[written_index] = 1; |
| 1222 | } else if (desc->IsUpdateAfterBind(binding)) { |
| 1223 | // If it hasn't been written now and it's update after bind, put it in a list to check at QueueSubmit |
| 1224 | input_block.update_at_submit[written_index] = descriptor; |
| 1225 | } |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1226 | written_index++; |
| 1227 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1228 | } |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1229 | auto last = desc->GetLayout()->GetMaxBinding(); |
| 1230 | bindings_to_written += last + 1; |
| 1231 | bindCounter += last + 1; |
| 1232 | sizes += last + 1; |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1233 | } else { |
| 1234 | *sets_to_sizes++ = 0; |
| 1235 | *sets_to_bindings++ = 0; |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | vmaUnmapMemory(gpu_validation_state->vmaAllocator, input_block.allocation); |
| 1239 | |
| 1240 | VkDescriptorBufferInfo input_desc_buffer_info = {}; |
| 1241 | input_desc_buffer_info.range = (words_needed * 4); |
| 1242 | input_desc_buffer_info.buffer = input_block.buffer; |
| 1243 | input_desc_buffer_info.offset = 0; |
| 1244 | |
| 1245 | desc_writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1246 | desc_writes[1].dstBinding = 1; |
| 1247 | desc_writes[1].descriptorCount = 1; |
| 1248 | desc_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 1249 | desc_writes[1].pBufferInfo = &input_desc_buffer_info; |
| 1250 | desc_writes[1].dstSet = desc_sets[0]; |
| 1251 | |
| 1252 | desc_count = 2; |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 1253 | } |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1254 | |
| 1255 | // Write the descriptor |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1256 | output_desc_buffer_info.buffer = output_block.buffer; |
| 1257 | output_desc_buffer_info.offset = 0; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1258 | |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1259 | desc_writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1260 | desc_writes[0].descriptorCount = 1; |
| 1261 | desc_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 1262 | desc_writes[0].pBufferInfo = &output_desc_buffer_info; |
| 1263 | desc_writes[0].dstSet = desc_sets[0]; |
| 1264 | DispatchUpdateDescriptorSets(device, desc_count, desc_writes, 0, NULL); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1265 | |
andreyg | 25ce262 | 2019-04-05 23:07:59 +0300 | [diff] [blame] | 1266 | auto iter = cb_node->lastBound.find(bind_point); // find() allows read-only access to cb_state |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1267 | if (iter != cb_node->lastBound.end()) { |
| 1268 | auto pipeline_state = iter->second.pipeline_state; |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1269 | if (pipeline_state && (pipeline_state->pipeline_layout.set_layouts.size() <= gpu_validation_state->desc_set_bind_index)) { |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 1270 | DispatchCmdBindDescriptorSets(cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_state->pipeline_layout.layout, |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1271 | gpu_validation_state->desc_set_bind_index, 1, desc_sets.data(), 0, nullptr); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1272 | } |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 1273 | // Record buffer and memory info in CB state tracking |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1274 | gpu_validation_state->GetGpuBufferInfo(cmd_buffer).emplace_back(output_block, input_block, desc_sets[0], desc_pool); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1275 | } else { |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1276 | ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), "Unable to find pipeline state"); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1277 | vmaDestroyBuffer(gpu_validation_state->vmaAllocator, input_block.buffer, input_block.allocation); |
| 1278 | vmaDestroyBuffer(gpu_validation_state->vmaAllocator, output_block.buffer, output_block.allocation); |
Mark Lobodzinski | 2a3ee4a | 2019-03-13 13:11:39 -0600 | [diff] [blame] | 1279 | gpu_validation_state->aborted = true; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1280 | return; |
| 1281 | } |
| 1282 | } |