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