sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2020-2022 The Khronos Group Inc. |
| 2 | * Copyright (c) 2020-2022 Valve Corporation |
| 3 | * Copyright (c) 2020-2022 LunarG, Inc. |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | * Author: Tony Barbour <tony@lunarg.com> |
| 18 | */ |
| 19 | |
Jeremy Gebben | 5160e03 | 2022-03-28 14:57:43 -0600 | [diff] [blame] | 20 | #include "gpu_utils.h" |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 21 | #include "descriptor_sets.h" |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 22 | #include "spirv-tools/libspirv.h" |
| 23 | #include "spirv-tools/optimizer.hpp" |
| 24 | #include "spirv-tools/instrument.hpp" |
Mark Lobodzinski | 102687e | 2020-04-28 11:03:28 -0600 | [diff] [blame] | 25 | #include <spirv/unified1/spirv.hpp> |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <regex> |
| 28 | |
| 29 | #define VMA_IMPLEMENTATION |
| 30 | // This define indicates that we will supply Vulkan function pointers at initialization |
| 31 | #define VMA_STATIC_VULKAN_FUNCTIONS 0 |
| 32 | #include "vk_mem_alloc.h" |
| 33 | |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 34 | // Implementation for Descriptor Set Manager class |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 35 | UtilDescriptorSetManager::UtilDescriptorSetManager(VkDevice device, uint32_t numBindingsInSet) |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 36 | : device(device), numBindingsInSet(numBindingsInSet) {} |
| 37 | |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 38 | UtilDescriptorSetManager::~UtilDescriptorSetManager() { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 39 | for (auto &pool : desc_pool_map_) { |
| 40 | DispatchDestroyDescriptorPool(device, pool.first, NULL); |
| 41 | } |
| 42 | desc_pool_map_.clear(); |
| 43 | } |
| 44 | |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 45 | VkResult UtilDescriptorSetManager::GetDescriptorSet(VkDescriptorPool *desc_pool, VkDescriptorSetLayout ds_layout, |
| 46 | VkDescriptorSet *desc_set) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 47 | std::vector<VkDescriptorSet> desc_sets; |
| 48 | VkResult result = GetDescriptorSets(1, desc_pool, ds_layout, &desc_sets); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 49 | assert(result == VK_SUCCESS); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 50 | if (result == VK_SUCCESS) { |
| 51 | *desc_set = desc_sets[0]; |
| 52 | } |
| 53 | return result; |
| 54 | } |
| 55 | |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 56 | VkResult UtilDescriptorSetManager::GetDescriptorSets(uint32_t count, VkDescriptorPool *pool, VkDescriptorSetLayout ds_layout, |
| 57 | std::vector<VkDescriptorSet> *desc_sets) { |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 58 | auto guard = Lock(); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 59 | const uint32_t default_pool_size = kItemsPerChunk; |
| 60 | VkResult result = VK_SUCCESS; |
| 61 | VkDescriptorPool pool_to_use = VK_NULL_HANDLE; |
| 62 | |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 63 | assert(count > 0); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 64 | if (0 == count) { |
| 65 | return result; |
| 66 | } |
| 67 | desc_sets->clear(); |
| 68 | desc_sets->resize(count); |
| 69 | |
| 70 | for (auto &pool : desc_pool_map_) { |
| 71 | if (pool.second.used + count < pool.second.size) { |
| 72 | pool_to_use = pool.first; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | if (VK_NULL_HANDLE == pool_to_use) { |
| 77 | uint32_t pool_count = default_pool_size; |
| 78 | if (count > default_pool_size) { |
| 79 | pool_count = count; |
| 80 | } |
| 81 | const VkDescriptorPoolSize size_counts = { |
| 82 | VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, |
| 83 | pool_count * numBindingsInSet, |
| 84 | }; |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 85 | auto desc_pool_info = LvlInitStruct<VkDescriptorPoolCreateInfo>(); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 86 | desc_pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; |
| 87 | desc_pool_info.maxSets = pool_count; |
| 88 | desc_pool_info.poolSizeCount = 1; |
| 89 | desc_pool_info.pPoolSizes = &size_counts; |
| 90 | result = DispatchCreateDescriptorPool(device, &desc_pool_info, NULL, &pool_to_use); |
| 91 | assert(result == VK_SUCCESS); |
| 92 | if (result != VK_SUCCESS) { |
| 93 | return result; |
| 94 | } |
| 95 | desc_pool_map_[pool_to_use].size = desc_pool_info.maxSets; |
| 96 | desc_pool_map_[pool_to_use].used = 0; |
| 97 | } |
| 98 | std::vector<VkDescriptorSetLayout> desc_layouts(count, ds_layout); |
| 99 | |
| 100 | VkDescriptorSetAllocateInfo alloc_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, NULL, pool_to_use, count, |
| 101 | desc_layouts.data()}; |
| 102 | |
| 103 | result = DispatchAllocateDescriptorSets(device, &alloc_info, desc_sets->data()); |
| 104 | assert(result == VK_SUCCESS); |
| 105 | if (result != VK_SUCCESS) { |
| 106 | return result; |
| 107 | } |
| 108 | *pool = pool_to_use; |
| 109 | desc_pool_map_[pool_to_use].used += count; |
| 110 | return result; |
| 111 | } |
| 112 | |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 113 | void UtilDescriptorSetManager::PutBackDescriptorSet(VkDescriptorPool desc_pool, VkDescriptorSet desc_set) { |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 114 | auto guard = Lock(); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 115 | auto iter = desc_pool_map_.find(desc_pool); |
| 116 | if (iter != desc_pool_map_.end()) { |
| 117 | VkResult result = DispatchFreeDescriptorSets(device, desc_pool, 1, &desc_set); |
| 118 | assert(result == VK_SUCCESS); |
| 119 | if (result != VK_SUCCESS) { |
| 120 | return; |
| 121 | } |
| 122 | desc_pool_map_[desc_pool].used--; |
| 123 | if (0 == desc_pool_map_[desc_pool].used) { |
| 124 | DispatchDestroyDescriptorPool(device, desc_pool, NULL); |
| 125 | desc_pool_map_.erase(desc_pool); |
| 126 | } |
| 127 | } |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | // Trampolines to make VMA call Dispatch for Vulkan calls |
| 132 | static VKAPI_ATTR void VKAPI_CALL gpuVkGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice, |
| 133 | VkPhysicalDeviceProperties *pProperties) { |
| 134 | DispatchGetPhysicalDeviceProperties(physicalDevice, pProperties); |
| 135 | } |
| 136 | static VKAPI_ATTR void VKAPI_CALL gpuVkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice, |
| 137 | VkPhysicalDeviceMemoryProperties *pMemoryProperties) { |
| 138 | DispatchGetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties); |
| 139 | } |
| 140 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkAllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, |
| 141 | const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMemory) { |
| 142 | return DispatchAllocateMemory(device, pAllocateInfo, pAllocator, pMemory); |
| 143 | } |
| 144 | static VKAPI_ATTR void VKAPI_CALL gpuVkFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks *pAllocator) { |
| 145 | DispatchFreeMemory(device, memory, pAllocator); |
| 146 | } |
| 147 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkMapMemory(VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, |
| 148 | VkMemoryMapFlags flags, void **ppData) { |
| 149 | return DispatchMapMemory(device, memory, offset, size, flags, ppData); |
| 150 | } |
| 151 | static VKAPI_ATTR void VKAPI_CALL gpuVkUnmapMemory(VkDevice device, VkDeviceMemory memory) { DispatchUnmapMemory(device, memory); } |
| 152 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkFlushMappedMemoryRanges(VkDevice device, uint32_t memoryRangeCount, |
| 153 | const VkMappedMemoryRange *pMemoryRanges) { |
| 154 | return DispatchFlushMappedMemoryRanges(device, memoryRangeCount, pMemoryRanges); |
| 155 | } |
| 156 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkInvalidateMappedMemoryRanges(VkDevice device, uint32_t memoryRangeCount, |
| 157 | const VkMappedMemoryRange *pMemoryRanges) { |
| 158 | return DispatchInvalidateMappedMemoryRanges(device, memoryRangeCount, pMemoryRanges); |
| 159 | } |
| 160 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, |
| 161 | VkDeviceSize memoryOffset) { |
| 162 | return DispatchBindBufferMemory(device, buffer, memory, memoryOffset); |
| 163 | } |
| 164 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory, |
| 165 | VkDeviceSize memoryOffset) { |
| 166 | return DispatchBindImageMemory(device, image, memory, memoryOffset); |
| 167 | } |
| 168 | static VKAPI_ATTR void VKAPI_CALL gpuVkGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer, |
| 169 | VkMemoryRequirements *pMemoryRequirements) { |
| 170 | DispatchGetBufferMemoryRequirements(device, buffer, pMemoryRequirements); |
| 171 | } |
| 172 | static VKAPI_ATTR void VKAPI_CALL gpuVkGetImageMemoryRequirements(VkDevice device, VkImage image, |
| 173 | VkMemoryRequirements *pMemoryRequirements) { |
| 174 | DispatchGetImageMemoryRequirements(device, image, pMemoryRequirements); |
| 175 | } |
| 176 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo, |
| 177 | const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer) { |
| 178 | return DispatchCreateBuffer(device, pCreateInfo, pAllocator, pBuffer); |
| 179 | } |
| 180 | static VKAPI_ATTR void VKAPI_CALL gpuVkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) { |
| 181 | return DispatchDestroyBuffer(device, buffer, pAllocator); |
| 182 | } |
| 183 | static VKAPI_ATTR VkResult VKAPI_CALL gpuVkCreateImage(VkDevice device, const VkImageCreateInfo *pCreateInfo, |
| 184 | const VkAllocationCallbacks *pAllocator, VkImage *pImage) { |
| 185 | return DispatchCreateImage(device, pCreateInfo, pAllocator, pImage); |
| 186 | } |
| 187 | static VKAPI_ATTR void VKAPI_CALL gpuVkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) { |
| 188 | DispatchDestroyImage(device, image, pAllocator); |
| 189 | } |
| 190 | static VKAPI_ATTR void VKAPI_CALL gpuVkCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, |
| 191 | uint32_t regionCount, const VkBufferCopy *pRegions) { |
| 192 | DispatchCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, regionCount, pRegions); |
| 193 | } |
| 194 | |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 195 | VkResult UtilInitializeVma(VkPhysicalDevice physical_device, VkDevice device, VmaAllocator *pAllocator) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 196 | VmaVulkanFunctions functions; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 197 | VmaAllocatorCreateInfo allocator_info = {}; |
| 198 | allocator_info.device = device; |
| 199 | allocator_info.physicalDevice = physical_device; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 200 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 201 | functions.vkGetPhysicalDeviceProperties = static_cast<PFN_vkGetPhysicalDeviceProperties>(gpuVkGetPhysicalDeviceProperties); |
| 202 | functions.vkGetPhysicalDeviceMemoryProperties = |
| 203 | static_cast<PFN_vkGetPhysicalDeviceMemoryProperties>(gpuVkGetPhysicalDeviceMemoryProperties); |
| 204 | functions.vkAllocateMemory = static_cast<PFN_vkAllocateMemory>(gpuVkAllocateMemory); |
| 205 | functions.vkFreeMemory = static_cast<PFN_vkFreeMemory>(gpuVkFreeMemory); |
| 206 | functions.vkMapMemory = static_cast<PFN_vkMapMemory>(gpuVkMapMemory); |
| 207 | functions.vkUnmapMemory = static_cast<PFN_vkUnmapMemory>(gpuVkUnmapMemory); |
| 208 | functions.vkFlushMappedMemoryRanges = static_cast<PFN_vkFlushMappedMemoryRanges>(gpuVkFlushMappedMemoryRanges); |
| 209 | functions.vkInvalidateMappedMemoryRanges = static_cast<PFN_vkInvalidateMappedMemoryRanges>(gpuVkInvalidateMappedMemoryRanges); |
| 210 | functions.vkBindBufferMemory = static_cast<PFN_vkBindBufferMemory>(gpuVkBindBufferMemory); |
| 211 | functions.vkBindImageMemory = static_cast<PFN_vkBindImageMemory>(gpuVkBindImageMemory); |
| 212 | functions.vkGetBufferMemoryRequirements = static_cast<PFN_vkGetBufferMemoryRequirements>(gpuVkGetBufferMemoryRequirements); |
| 213 | functions.vkGetImageMemoryRequirements = static_cast<PFN_vkGetImageMemoryRequirements>(gpuVkGetImageMemoryRequirements); |
| 214 | functions.vkCreateBuffer = static_cast<PFN_vkCreateBuffer>(gpuVkCreateBuffer); |
| 215 | functions.vkDestroyBuffer = static_cast<PFN_vkDestroyBuffer>(gpuVkDestroyBuffer); |
| 216 | functions.vkCreateImage = static_cast<PFN_vkCreateImage>(gpuVkCreateImage); |
| 217 | functions.vkDestroyImage = static_cast<PFN_vkDestroyImage>(gpuVkDestroyImage); |
| 218 | functions.vkCmdCopyBuffer = static_cast<PFN_vkCmdCopyBuffer>(gpuVkCmdCopyBuffer); |
| 219 | allocator_info.pVulkanFunctions = &functions; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 220 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 221 | return vmaCreateAllocator(&allocator_info, pAllocator); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 222 | } |
| 223 | |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame^] | 224 | gpu_utils_state::CommandBuffer::CommandBuffer(GpuAssistedBase *ga, VkCommandBuffer cb, |
| 225 | const VkCommandBufferAllocateInfo *pCreateInfo, const COMMAND_POOL_STATE *pool) |
| 226 | : CMD_BUFFER_STATE(ga, cb, pCreateInfo, pool) {} |
| 227 | |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 228 | void GpuAssistedBase::PreCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo, |
| 229 | const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, void *modified_ci) { |
| 230 | ValidationStateTracker::PreCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, modified_ci); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 231 | VkPhysicalDeviceFeatures *features = nullptr; |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 232 | // Use a local variable to query features since this method runs in the instance validation object. |
| 233 | // To avoid confusion and race conditions about which physical device's features are stored in the |
| 234 | // 'supported_devices' member variable, it will only be set in the device validation objects. |
| 235 | // See CreateDevice() below. |
| 236 | VkPhysicalDeviceFeatures gpu_supported_features; |
| 237 | DispatchGetPhysicalDeviceFeatures(gpu, &gpu_supported_features); |
| 238 | auto modified_create_info = static_cast<VkDeviceCreateInfo *>(modified_ci); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 239 | if (modified_create_info->pEnabledFeatures) { |
| 240 | // If pEnabledFeatures, VkPhysicalDeviceFeatures2 in pNext chain is not allowed |
| 241 | features = const_cast<VkPhysicalDeviceFeatures *>(modified_create_info->pEnabledFeatures); |
| 242 | } else { |
| 243 | VkPhysicalDeviceFeatures2 *features2 = nullptr; |
Mark Lobodzinski | 1f887d3 | 2020-12-30 15:31:33 -0700 | [diff] [blame] | 244 | features2 = const_cast<VkPhysicalDeviceFeatures2 *>(LvlFindInChain<VkPhysicalDeviceFeatures2>(modified_create_info->pNext)); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 245 | if (features2) features = &features2->features; |
| 246 | } |
Tony-LunarG | f0634eb | 2021-01-05 15:11:12 -0700 | [diff] [blame] | 247 | VkPhysicalDeviceFeatures new_features = {}; |
| 248 | VkBool32 *desired = reinterpret_cast<VkBool32 *>(&desired_features); |
| 249 | VkBool32 *feature_ptr; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 250 | if (features) { |
Tony-LunarG | f0634eb | 2021-01-05 15:11:12 -0700 | [diff] [blame] | 251 | feature_ptr = reinterpret_cast<VkBool32 *>(features); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 252 | } else { |
Tony-LunarG | f0634eb | 2021-01-05 15:11:12 -0700 | [diff] [blame] | 253 | feature_ptr = reinterpret_cast<VkBool32 *>(&new_features); |
| 254 | } |
| 255 | VkBool32 *supported = reinterpret_cast<VkBool32 *>(&supported_features); |
| 256 | for (size_t i = 0; i < sizeof(VkPhysicalDeviceFeatures); i += (sizeof(VkBool32))) { |
| 257 | if (*supported && *desired) { |
| 258 | *feature_ptr = true; |
| 259 | } |
| 260 | supported++; |
| 261 | desired++; |
| 262 | feature_ptr++; |
| 263 | } |
| 264 | if (!features) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 265 | delete modified_create_info->pEnabledFeatures; |
| 266 | modified_create_info->pEnabledFeatures = new VkPhysicalDeviceFeatures(new_features); |
| 267 | } |
| 268 | } |
| 269 | |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 270 | void GpuAssistedBase::CreateDevice(const VkDeviceCreateInfo *pCreateInfo) { |
| 271 | ValidationStateTracker::CreateDevice(pCreateInfo); |
| 272 | // If api version 1.1 or later, SetDeviceLoaderData will be in the loader |
| 273 | auto chain_info = get_chain_info(pCreateInfo, VK_LOADER_DATA_CALLBACK); |
| 274 | assert(chain_info->u.pfnSetDeviceLoaderData); |
| 275 | vkSetDeviceLoaderData = chain_info->u.pfnSetDeviceLoaderData; |
| 276 | |
| 277 | // Some devices have extremely high limits here, so set a reasonable max because we have to pad |
| 278 | // the pipeline layout with dummy descriptor set layouts. |
| 279 | adjusted_max_desc_sets = phys_dev_props.limits.maxBoundDescriptorSets; |
| 280 | adjusted_max_desc_sets = std::min(33U, adjusted_max_desc_sets); |
| 281 | |
| 282 | // We can't do anything if there is only one. |
| 283 | // Device probably not a legit Vulkan device, since there should be at least 4. Protect ourselves. |
| 284 | if (adjusted_max_desc_sets == 1) { |
| 285 | ReportSetupProblem(device, "Device can bind only a single descriptor set."); |
| 286 | aborted = true; |
| 287 | return; |
| 288 | } |
| 289 | desc_set_bind_index = adjusted_max_desc_sets - 1; |
| 290 | |
| 291 | VkResult result1 = UtilInitializeVma(physical_device, device, &vmaAllocator); |
| 292 | assert(result1 == VK_SUCCESS); |
| 293 | desc_set_manager = layer_data::make_unique<UtilDescriptorSetManager>(device, static_cast<uint32_t>(bindings_.size())); |
| 294 | |
| 295 | const VkDescriptorSetLayoutCreateInfo debug_desc_layout_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, NULL, 0, |
| 296 | static_cast<uint32_t>(bindings_.size()), bindings_.data()}; |
| 297 | |
| 298 | const VkDescriptorSetLayoutCreateInfo dummy_desc_layout_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, NULL, 0, 0, |
| 299 | NULL}; |
| 300 | |
| 301 | result1 = DispatchCreateDescriptorSetLayout(device, &debug_desc_layout_info, NULL, &debug_desc_layout); |
| 302 | |
| 303 | // This is a layout used to "pad" a pipeline layout to fill in any gaps to the selected bind index. |
| 304 | VkResult result2 = DispatchCreateDescriptorSetLayout(device, &dummy_desc_layout_info, NULL, &dummy_desc_layout); |
| 305 | |
| 306 | assert((result1 == VK_SUCCESS) && (result2 == VK_SUCCESS)); |
| 307 | if ((result1 != VK_SUCCESS) || (result2 != VK_SUCCESS)) { |
| 308 | ReportSetupProblem(device, "Unable to create descriptor set layout."); |
| 309 | if (result1 == VK_SUCCESS) { |
| 310 | DispatchDestroyDescriptorSetLayout(device, debug_desc_layout, NULL); |
| 311 | } |
| 312 | if (result2 == VK_SUCCESS) { |
| 313 | DispatchDestroyDescriptorSetLayout(device, dummy_desc_layout, NULL); |
| 314 | } |
| 315 | debug_desc_layout = VK_NULL_HANDLE; |
| 316 | dummy_desc_layout = VK_NULL_HANDLE; |
| 317 | aborted = true; |
| 318 | return; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void GpuAssistedBase::PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) { |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 323 | if (debug_desc_layout) { |
| 324 | DispatchDestroyDescriptorSetLayout(device, debug_desc_layout, NULL); |
| 325 | debug_desc_layout = VK_NULL_HANDLE; |
| 326 | } |
| 327 | if (dummy_desc_layout) { |
| 328 | DispatchDestroyDescriptorSetLayout(device, dummy_desc_layout, NULL); |
| 329 | dummy_desc_layout = VK_NULL_HANDLE; |
| 330 | } |
| 331 | ValidationStateTracker::PreCallRecordDestroyDevice(device, pAllocator); |
| 332 | // State Tracker can end up making vma calls through callbacks - don't destroy allocator until ST is done |
| 333 | if (vmaAllocator) { |
| 334 | vmaDestroyAllocator(vmaAllocator); |
| 335 | } |
| 336 | desc_set_manager.reset(); |
| 337 | } |
| 338 | |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 339 | gpu_utils_state::Queue::Queue(GpuAssistedBase &state, VkQueue q, uint32_t index, VkDeviceQueueCreateFlags flags) |
| 340 | : QUEUE_STATE(q, index, flags), state_(state) {} |
| 341 | |
| 342 | gpu_utils_state::Queue::~Queue() { |
| 343 | if (barrier_command_buffer_) { |
| 344 | DispatchFreeCommandBuffers(state_.device, barrier_command_pool_, 1, &barrier_command_buffer_); |
| 345 | barrier_command_buffer_ = VK_NULL_HANDLE; |
| 346 | } |
| 347 | if (barrier_command_pool_) { |
| 348 | DispatchDestroyCommandPool(state_.device, barrier_command_pool_, NULL); |
| 349 | barrier_command_pool_ = VK_NULL_HANDLE; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // Submit a memory barrier on graphics queues. |
| 354 | // Lazy-create and record the needed command buffer. |
| 355 | void gpu_utils_state::Queue::SubmitBarrier() { |
| 356 | if (barrier_command_pool_ == VK_NULL_HANDLE) { |
| 357 | VkResult result = VK_SUCCESS; |
| 358 | |
| 359 | auto pool_create_info = LvlInitStruct<VkCommandPoolCreateInfo>(); |
| 360 | pool_create_info.queueFamilyIndex = queueFamilyIndex; |
| 361 | result = DispatchCreateCommandPool(state_.device, &pool_create_info, nullptr, &barrier_command_pool_); |
| 362 | if (result != VK_SUCCESS) { |
| 363 | state_.ReportSetupProblem(state_.device, "Unable to create command pool for barrier CB."); |
| 364 | barrier_command_pool_ = VK_NULL_HANDLE; |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | auto buffer_alloc_info = LvlInitStruct<VkCommandBufferAllocateInfo>(); |
| 369 | buffer_alloc_info.commandPool = barrier_command_pool_; |
| 370 | buffer_alloc_info.commandBufferCount = 1; |
| 371 | buffer_alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 372 | result = DispatchAllocateCommandBuffers(state_.device, &buffer_alloc_info, &barrier_command_buffer_); |
| 373 | if (result != VK_SUCCESS) { |
| 374 | state_.ReportSetupProblem(state_.device, "Unable to create barrier command buffer."); |
| 375 | DispatchDestroyCommandPool(state_.device, barrier_command_pool_, nullptr); |
| 376 | barrier_command_pool_ = VK_NULL_HANDLE; |
| 377 | barrier_command_buffer_ = VK_NULL_HANDLE; |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | // Hook up command buffer dispatch |
| 382 | state_.vkSetDeviceLoaderData(state_.device, barrier_command_buffer_); |
| 383 | |
| 384 | // Record a global memory barrier to force availability of device memory operations to the host domain. |
| 385 | auto command_buffer_begin_info = LvlInitStruct<VkCommandBufferBeginInfo>(); |
| 386 | result = DispatchBeginCommandBuffer(barrier_command_buffer_, &command_buffer_begin_info); |
| 387 | if (result == VK_SUCCESS) { |
| 388 | auto memory_barrier = LvlInitStruct<VkMemoryBarrier>(); |
| 389 | memory_barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT; |
| 390 | memory_barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT; |
| 391 | DispatchCmdPipelineBarrier(barrier_command_buffer_, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, |
| 392 | 1, &memory_barrier, 0, nullptr, 0, nullptr); |
| 393 | DispatchEndCommandBuffer(barrier_command_buffer_); |
| 394 | } |
| 395 | } |
| 396 | if (barrier_command_buffer_ != VK_NULL_HANDLE) { |
| 397 | auto submit_info = LvlInitStruct<VkSubmitInfo>(); |
| 398 | submit_info.commandBufferCount = 1; |
| 399 | submit_info.pCommandBuffers = &barrier_command_buffer_; |
| 400 | DispatchQueueSubmit(QUEUE_STATE::Queue(), 1, &submit_info, VK_NULL_HANDLE); |
| 401 | } |
| 402 | } |
| 403 | |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame^] | 404 | bool GpuAssistedBase::CommandBufferNeedsProcessing(VkCommandBuffer command_buffer) const { |
| 405 | auto cb_node = Get<gpu_utils_state::CommandBuffer>(command_buffer); |
| 406 | if (cb_node->NeedsProcessing()) { |
| 407 | return true; |
| 408 | } |
| 409 | for (const auto *secondary_cb : cb_node->linkedCommandBuffers) { |
| 410 | auto secondary_cb_node = static_cast<const gpu_utils_state::CommandBuffer *>(secondary_cb); |
| 411 | if (secondary_cb_node->NeedsProcessing()) { |
| 412 | return true; |
| 413 | } |
| 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | void GpuAssistedBase::ProcessCommandBuffer(VkQueue queue, VkCommandBuffer command_buffer) { |
| 419 | auto cb_node = Get<gpu_utils_state::CommandBuffer>(command_buffer); |
| 420 | |
| 421 | cb_node->Process(queue); |
| 422 | for (auto *secondary_cmd_base : cb_node->linkedCommandBuffers) { |
| 423 | auto *secondary_cb_node = static_cast<gpu_utils_state::CommandBuffer *>(secondary_cmd_base); |
| 424 | secondary_cb_node->Process(queue); |
| 425 | } |
| 426 | } |
| 427 | |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 428 | // Issue a memory barrier to make GPU-written data available to host. |
| 429 | // Wait for the queue to complete execution. |
| 430 | // Check the debug buffers for all the command buffers that were submitted. |
| 431 | void GpuAssistedBase::PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence, |
| 432 | VkResult result) { |
| 433 | ValidationStateTracker::PostCallRecordQueueSubmit(queue, submitCount, pSubmits, fence, result); |
| 434 | |
| 435 | if (aborted || (result != VK_SUCCESS)) return; |
| 436 | bool buffers_present = false; |
| 437 | // Don't QueueWaitIdle if there's nothing to process |
| 438 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 439 | const VkSubmitInfo *submit = &pSubmits[submit_idx]; |
| 440 | for (uint32_t i = 0; i < submit->commandBufferCount; i++) { |
| 441 | buffers_present |= CommandBufferNeedsProcessing(submit->pCommandBuffers[i]); |
| 442 | } |
| 443 | } |
| 444 | if (!buffers_present) return; |
| 445 | |
| 446 | SubmitBarrier(queue); |
| 447 | |
| 448 | DispatchQueueWaitIdle(queue); |
| 449 | |
| 450 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 451 | const VkSubmitInfo *submit = &pSubmits[submit_idx]; |
| 452 | for (uint32_t i = 0; i < submit->commandBufferCount; i++) { |
| 453 | ProcessCommandBuffer(queue, submit->pCommandBuffers[i]); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void GpuAssistedBase::RecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence, |
| 459 | VkResult result) { |
| 460 | if (aborted || (result != VK_SUCCESS)) return; |
| 461 | bool buffers_present = false; |
| 462 | // Don't QueueWaitIdle if there's nothing to process |
| 463 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 464 | const VkSubmitInfo2 *submit = &pSubmits[submit_idx]; |
| 465 | for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) { |
| 466 | buffers_present |= CommandBufferNeedsProcessing(submit->pCommandBufferInfos[i].commandBuffer); |
| 467 | } |
| 468 | } |
| 469 | if (!buffers_present) return; |
| 470 | |
| 471 | SubmitBarrier(queue); |
| 472 | |
| 473 | DispatchQueueWaitIdle(queue); |
| 474 | |
| 475 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 476 | const VkSubmitInfo2 *submit = &pSubmits[submit_idx]; |
| 477 | for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) { |
| 478 | ProcessCommandBuffer(queue, submit->pCommandBufferInfos[i].commandBuffer); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | void GpuAssistedBase::PostCallRecordQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, |
| 484 | VkFence fence, VkResult result) { |
| 485 | ValidationStateTracker::PostCallRecordQueueSubmit2KHR(queue, submitCount, pSubmits, fence, result); |
| 486 | RecordQueueSubmit2(queue, submitCount, pSubmits, fence, result); |
| 487 | } |
| 488 | |
| 489 | void GpuAssistedBase::PostCallRecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence, |
| 490 | VkResult result) { |
| 491 | ValidationStateTracker::PostCallRecordQueueSubmit2(queue, submitCount, pSubmits, fence, result); |
| 492 | RecordQueueSubmit2(queue, submitCount, pSubmits, fence, result); |
| 493 | } |
| 494 | |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 495 | void GpuAssistedBase::PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo, |
| 496 | const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout, |
| 497 | void *cpl_state_data) { |
| 498 | if (aborted) { |
| 499 | return; |
| 500 | } |
| 501 | auto cpl_state = static_cast<create_pipeline_layout_api_state *>(cpl_state_data); |
| 502 | if (cpl_state->modified_create_info.setLayoutCount >= adjusted_max_desc_sets) { |
| 503 | std::ostringstream strm; |
| 504 | strm << "Pipeline Layout conflict with validation's descriptor set at slot " << desc_set_bind_index << ". " |
| 505 | << "Application has too many descriptor sets in the pipeline layout to continue with gpu validation. " |
| 506 | << "Validation is not modifying the pipeline layout. " |
| 507 | << "Instrumented shaders are replaced with non-instrumented shaders."; |
| 508 | ReportSetupProblem(device, strm.str().c_str()); |
| 509 | } else { |
| 510 | // Modify the pipeline layout by: |
| 511 | // 1. Copying the caller's descriptor set desc_layouts |
| 512 | // 2. Fill in dummy descriptor layouts up to the max binding |
| 513 | // 3. Fill in with the debug descriptor layout at the max binding slot |
| 514 | cpl_state->new_layouts.reserve(adjusted_max_desc_sets); |
| 515 | cpl_state->new_layouts.insert(cpl_state->new_layouts.end(), &pCreateInfo->pSetLayouts[0], |
| 516 | &pCreateInfo->pSetLayouts[pCreateInfo->setLayoutCount]); |
| 517 | for (uint32_t i = pCreateInfo->setLayoutCount; i < adjusted_max_desc_sets - 1; ++i) { |
| 518 | cpl_state->new_layouts.push_back(dummy_desc_layout); |
| 519 | } |
| 520 | cpl_state->new_layouts.push_back(debug_desc_layout); |
| 521 | cpl_state->modified_create_info.pSetLayouts = cpl_state->new_layouts.data(); |
| 522 | cpl_state->modified_create_info.setLayoutCount = adjusted_max_desc_sets; |
| 523 | } |
| 524 | ValidationStateTracker::PreCallRecordCreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout, cpl_state_data); |
| 525 | } |
| 526 | |
| 527 | void GpuAssistedBase::PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo, |
| 528 | const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout, |
| 529 | VkResult result) { |
| 530 | if (result != VK_SUCCESS) { |
| 531 | ReportSetupProblem(device, "Unable to create pipeline layout. Device could become unstable."); |
| 532 | aborted = true; |
| 533 | } |
| 534 | ValidationStateTracker::PostCallRecordCreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout, result); |
| 535 | } |
| 536 | |
| 537 | void GpuAssistedBase::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 538 | const VkGraphicsPipelineCreateInfo *pCreateInfos, |
| 539 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 540 | void *cgpl_state_data) { |
| 541 | if (aborted) return; |
| 542 | std::vector<safe_VkGraphicsPipelineCreateInfo> new_pipeline_create_infos; |
| 543 | create_graphics_pipeline_api_state *cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state *>(cgpl_state_data); |
| 544 | PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, cgpl_state->pipe_state, &new_pipeline_create_infos, |
| 545 | VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 546 | cgpl_state->printf_create_infos = new_pipeline_create_infos; |
| 547 | cgpl_state->pCreateInfos = reinterpret_cast<VkGraphicsPipelineCreateInfo *>(cgpl_state->printf_create_infos.data()); |
| 548 | } |
| 549 | |
| 550 | void GpuAssistedBase::PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 551 | const VkComputePipelineCreateInfo *pCreateInfos, |
| 552 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 553 | void *ccpl_state_data) { |
| 554 | if (aborted) return; |
| 555 | std::vector<safe_VkComputePipelineCreateInfo> new_pipeline_create_infos; |
| 556 | auto *ccpl_state = reinterpret_cast<create_compute_pipeline_api_state *>(ccpl_state_data); |
| 557 | PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, ccpl_state->pipe_state, &new_pipeline_create_infos, |
| 558 | VK_PIPELINE_BIND_POINT_COMPUTE); |
| 559 | ccpl_state->printf_create_infos = new_pipeline_create_infos; |
| 560 | ccpl_state->pCreateInfos = reinterpret_cast<VkComputePipelineCreateInfo *>(ccpl_state->printf_create_infos.data()); |
| 561 | } |
| 562 | |
| 563 | void GpuAssistedBase::PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 564 | const VkRayTracingPipelineCreateInfoNV *pCreateInfos, |
| 565 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 566 | void *crtpl_state_data) { |
| 567 | if (aborted) return; |
| 568 | std::vector<safe_VkRayTracingPipelineCreateInfoCommon> new_pipeline_create_infos; |
| 569 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_api_state *>(crtpl_state_data); |
| 570 | PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, crtpl_state->pipe_state, &new_pipeline_create_infos, |
| 571 | VK_PIPELINE_BIND_POINT_RAY_TRACING_NV); |
| 572 | crtpl_state->printf_create_infos = new_pipeline_create_infos; |
| 573 | crtpl_state->pCreateInfos = reinterpret_cast<VkRayTracingPipelineCreateInfoNV *>(crtpl_state->printf_create_infos.data()); |
| 574 | } |
| 575 | |
| 576 | void GpuAssistedBase::PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation, |
| 577 | VkPipelineCache pipelineCache, uint32_t count, |
| 578 | const VkRayTracingPipelineCreateInfoKHR *pCreateInfos, |
| 579 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 580 | void *crtpl_state_data) { |
| 581 | if (aborted) return; |
| 582 | std::vector<safe_VkRayTracingPipelineCreateInfoCommon> new_pipeline_create_infos; |
| 583 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data); |
| 584 | PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, crtpl_state->pipe_state, &new_pipeline_create_infos, |
| 585 | VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR); |
| 586 | crtpl_state->printf_create_infos = new_pipeline_create_infos; |
| 587 | crtpl_state->pCreateInfos = reinterpret_cast<VkRayTracingPipelineCreateInfoKHR *>(crtpl_state->printf_create_infos.data()); |
| 588 | } |
| 589 | |
| 590 | template <typename CreateInfos, typename SafeCreateInfos> |
| 591 | static void UtilCopyCreatePipelineFeedbackData(const uint32_t count, CreateInfos *pCreateInfos, SafeCreateInfos *pSafeCreateInfos) { |
| 592 | for (uint32_t i = 0; i < count; i++) { |
| 593 | auto src_feedback_struct = LvlFindInChain<VkPipelineCreationFeedbackCreateInfoEXT>(pSafeCreateInfos[i].pNext); |
| 594 | if (!src_feedback_struct) return; |
| 595 | auto dst_feedback_struct = const_cast<VkPipelineCreationFeedbackCreateInfoEXT *>( |
| 596 | LvlFindInChain<VkPipelineCreationFeedbackCreateInfoEXT>(pCreateInfos[i].pNext)); |
| 597 | *dst_feedback_struct->pPipelineCreationFeedback = *src_feedback_struct->pPipelineCreationFeedback; |
| 598 | for (uint32_t j = 0; j < src_feedback_struct->pipelineStageCreationFeedbackCount; j++) { |
| 599 | dst_feedback_struct->pPipelineStageCreationFeedbacks[j] = src_feedback_struct->pPipelineStageCreationFeedbacks[j]; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | void GpuAssistedBase::PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 605 | const VkGraphicsPipelineCreateInfo *pCreateInfos, |
| 606 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 607 | VkResult result, void *cgpl_state_data) { |
| 608 | ValidationStateTracker::PostCallRecordCreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator, |
| 609 | pPipelines, result, cgpl_state_data); |
| 610 | if (aborted) return; |
| 611 | create_graphics_pipeline_api_state *cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state *>(cgpl_state_data); |
| 612 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, cgpl_state->printf_create_infos.data()); |
| 613 | PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 614 | } |
| 615 | |
| 616 | void GpuAssistedBase::PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 617 | const VkComputePipelineCreateInfo *pCreateInfos, |
| 618 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 619 | VkResult result, void *ccpl_state_data) { |
| 620 | ValidationStateTracker::PostCallRecordCreateComputePipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines, |
| 621 | result, ccpl_state_data); |
| 622 | if (aborted) return; |
| 623 | create_compute_pipeline_api_state *ccpl_state = reinterpret_cast<create_compute_pipeline_api_state *>(ccpl_state_data); |
| 624 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, ccpl_state->printf_create_infos.data()); |
| 625 | PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_COMPUTE); |
| 626 | } |
| 627 | |
| 628 | void GpuAssistedBase::PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 629 | const VkRayTracingPipelineCreateInfoNV *pCreateInfos, |
| 630 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 631 | VkResult result, void *crtpl_state_data) { |
| 632 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data); |
| 633 | ValidationStateTracker::PostCallRecordCreateRayTracingPipelinesNV(device, pipelineCache, count, pCreateInfos, pAllocator, |
| 634 | pPipelines, result, crtpl_state_data); |
| 635 | if (aborted) return; |
| 636 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, crtpl_state->printf_create_infos.data()); |
| 637 | PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_RAY_TRACING_NV); |
| 638 | } |
| 639 | |
| 640 | void GpuAssistedBase::PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation, |
| 641 | VkPipelineCache pipelineCache, uint32_t count, |
| 642 | const VkRayTracingPipelineCreateInfoKHR *pCreateInfos, |
| 643 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 644 | VkResult result, void *crtpl_state_data) { |
| 645 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data); |
| 646 | ValidationStateTracker::PostCallRecordCreateRayTracingPipelinesKHR( |
| 647 | device, deferredOperation, pipelineCache, count, pCreateInfos, pAllocator, pPipelines, result, crtpl_state_data); |
| 648 | if (aborted) return; |
| 649 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, crtpl_state->printf_create_infos.data()); |
| 650 | PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR); |
| 651 | } |
| 652 | |
| 653 | // Remove all the shader trackers associated with this destroyed pipeline. |
| 654 | void GpuAssistedBase::PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) { |
| 655 | for (auto it = shader_map.begin(); it != shader_map.end();) { |
| 656 | if (it->second.pipeline == pipeline) { |
| 657 | it = shader_map.erase(it); |
| 658 | } else { |
| 659 | ++it; |
| 660 | } |
| 661 | } |
| 662 | ValidationStateTracker::PreCallRecordDestroyPipeline(device, pipeline, pAllocator); |
| 663 | } |
| 664 | |
| 665 | template <typename CreateInfo> |
| 666 | struct CreatePipelineTraits {}; |
| 667 | template <> |
| 668 | struct CreatePipelineTraits<VkGraphicsPipelineCreateInfo> { |
| 669 | using SafeType = safe_VkGraphicsPipelineCreateInfo; |
| 670 | static const SafeType &GetPipelineCI(const PIPELINE_STATE *pipeline_state) { |
| 671 | return pipeline_state->GetUnifiedCreateInfo().graphics; |
| 672 | } |
| 673 | static uint32_t GetStageCount(const VkGraphicsPipelineCreateInfo &createInfo) { return createInfo.stageCount; } |
| 674 | static VkShaderModule GetShaderModule(const VkGraphicsPipelineCreateInfo &createInfo, uint32_t stage) { |
| 675 | return createInfo.pStages[stage].module; |
| 676 | } |
| 677 | static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) { |
| 678 | createInfo->pStages[stage].module = shader_module; |
| 679 | } |
| 680 | }; |
| 681 | |
| 682 | template <> |
| 683 | struct CreatePipelineTraits<VkComputePipelineCreateInfo> { |
| 684 | using SafeType = safe_VkComputePipelineCreateInfo; |
| 685 | static const SafeType &GetPipelineCI(const PIPELINE_STATE *pipeline_state) { |
| 686 | return pipeline_state->GetUnifiedCreateInfo().compute; |
| 687 | } |
| 688 | static uint32_t GetStageCount(const VkComputePipelineCreateInfo &createInfo) { return 1; } |
| 689 | static VkShaderModule GetShaderModule(const VkComputePipelineCreateInfo &createInfo, uint32_t stage) { |
| 690 | return createInfo.stage.module; |
| 691 | } |
| 692 | static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) { |
| 693 | assert(stage == 0); |
| 694 | createInfo->stage.module = shader_module; |
| 695 | } |
| 696 | }; |
| 697 | |
| 698 | template <> |
| 699 | struct CreatePipelineTraits<VkRayTracingPipelineCreateInfoNV> { |
| 700 | using SafeType = safe_VkRayTracingPipelineCreateInfoCommon; |
| 701 | static const SafeType &GetPipelineCI(const PIPELINE_STATE *pipeline_state) { |
| 702 | return pipeline_state->GetUnifiedCreateInfo().raytracing; |
| 703 | } |
| 704 | static uint32_t GetStageCount(const VkRayTracingPipelineCreateInfoNV &createInfo) { return createInfo.stageCount; } |
| 705 | static VkShaderModule GetShaderModule(const VkRayTracingPipelineCreateInfoNV &createInfo, uint32_t stage) { |
| 706 | return createInfo.pStages[stage].module; |
| 707 | } |
| 708 | static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) { |
| 709 | createInfo->pStages[stage].module = shader_module; |
| 710 | } |
| 711 | }; |
| 712 | |
| 713 | template <> |
| 714 | struct CreatePipelineTraits<VkRayTracingPipelineCreateInfoKHR> { |
| 715 | using SafeType = safe_VkRayTracingPipelineCreateInfoCommon; |
| 716 | static const SafeType &GetPipelineCI(const PIPELINE_STATE *pipeline_state) { |
| 717 | return pipeline_state->GetUnifiedCreateInfo().raytracing; |
| 718 | } |
| 719 | static uint32_t GetStageCount(const VkRayTracingPipelineCreateInfoKHR &createInfo) { return createInfo.stageCount; } |
| 720 | static VkShaderModule GetShaderModule(const VkRayTracingPipelineCreateInfoKHR &createInfo, uint32_t stage) { |
| 721 | return createInfo.pStages[stage].module; |
| 722 | } |
| 723 | static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) { |
| 724 | createInfo->pStages[stage].module = shader_module; |
| 725 | } |
| 726 | }; |
| 727 | |
| 728 | // Examine the pipelines to see if they use the debug descriptor set binding index. |
| 729 | // If any do, create new non-instrumented shader modules and use them to replace the instrumented |
| 730 | // shaders in the pipeline. Return the (possibly) modified create infos to the caller. |
| 731 | template <typename CreateInfo, typename SafeCreateInfo> |
| 732 | void GpuAssistedBase::PreCallRecordPipelineCreations(uint32_t count, const CreateInfo *pCreateInfos, |
| 733 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 734 | std::vector<std::shared_ptr<PIPELINE_STATE>> &pipe_state, |
| 735 | std::vector<SafeCreateInfo> *new_pipeline_create_infos, |
| 736 | const VkPipelineBindPoint bind_point) { |
| 737 | using Accessor = CreatePipelineTraits<CreateInfo>; |
| 738 | if (bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS && bind_point != VK_PIPELINE_BIND_POINT_COMPUTE && |
| 739 | bind_point != VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) { |
| 740 | return; |
| 741 | } |
| 742 | |
| 743 | // Walk through all the pipelines, make a copy of each and flag each pipeline that contains a shader that uses the debug |
| 744 | // descriptor set index. |
| 745 | for (uint32_t pipeline = 0; pipeline < count; ++pipeline) { |
| 746 | uint32_t stageCount = Accessor::GetStageCount(pCreateInfos[pipeline]); |
| 747 | new_pipeline_create_infos->push_back(Accessor::GetPipelineCI(pipe_state[pipeline].get())); |
| 748 | const auto &pipe = pipe_state[pipeline]; |
| 749 | |
| 750 | if (!pipe->IsGraphicsLibrary()) { |
| 751 | bool replace_shaders = false; |
| 752 | if (pipe->active_slots.find(desc_set_bind_index) != pipe->active_slots.end()) { |
| 753 | replace_shaders = true; |
| 754 | } |
| 755 | // If the app requests all available sets, the pipeline layout was not modified at pipeline layout creation and the |
| 756 | // already instrumented shaders need to be replaced with uninstrumented shaders |
| 757 | const auto pipeline_layout = pipe->PipelineLayoutState(); |
| 758 | if (pipeline_layout->set_layouts.size() >= adjusted_max_desc_sets) { |
| 759 | replace_shaders = true; |
| 760 | } |
| 761 | |
| 762 | if (replace_shaders) { |
| 763 | for (uint32_t stage = 0; stage < stageCount; ++stage) { |
| 764 | const auto module_state = Get<SHADER_MODULE_STATE>(Accessor::GetShaderModule(pCreateInfos[pipeline], stage)); |
| 765 | |
| 766 | VkShaderModule shader_module; |
| 767 | auto create_info = LvlInitStruct<VkShaderModuleCreateInfo>(); |
| 768 | create_info.pCode = module_state->words.data(); |
| 769 | create_info.codeSize = module_state->words.size() * sizeof(uint32_t); |
| 770 | VkResult result = DispatchCreateShaderModule(device, &create_info, pAllocator, &shader_module); |
| 771 | if (result == VK_SUCCESS) { |
| 772 | Accessor::SetShaderModule(&(*new_pipeline_create_infos)[pipeline], shader_module, stage); |
| 773 | } else { |
| 774 | ReportSetupProblem(device, |
| 775 | "Unable to replace instrumented shader with non-instrumented one. " |
| 776 | "Device could become unstable."); |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | // For every pipeline: |
| 784 | // - For every shader in a pipeline: |
| 785 | // - If the shader had to be replaced in PreCallRecord (because the pipeline is using the debug desc set index): |
| 786 | // - Destroy it since it has been bound into the pipeline by now. This is our only chance to delete it. |
| 787 | // - Track the shader in the shader_map |
| 788 | // - Save the shader binary if it contains debug code |
| 789 | template <typename CreateInfo> |
| 790 | void GpuAssistedBase::PostCallRecordPipelineCreations(const uint32_t count, const CreateInfo *pCreateInfos, |
| 791 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 792 | const VkPipelineBindPoint bind_point) { |
| 793 | using Accessor = CreatePipelineTraits<CreateInfo>; |
| 794 | if (bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS && bind_point != VK_PIPELINE_BIND_POINT_COMPUTE && |
| 795 | bind_point != VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) { |
| 796 | return; |
| 797 | } |
| 798 | for (uint32_t pipeline = 0; pipeline < count; ++pipeline) { |
| 799 | auto pipeline_state = Get<PIPELINE_STATE>(pPipelines[pipeline]); |
| 800 | if (!pipeline_state || pipeline_state->IsGraphicsLibrary()) continue; |
| 801 | |
| 802 | const uint32_t stageCount = static_cast<uint32_t>(pipeline_state->stage_state.size()); |
| 803 | assert(stageCount > 0); |
| 804 | |
| 805 | for (uint32_t stage = 0; stage < stageCount; ++stage) { |
| 806 | if (pipeline_state->active_slots.find(desc_set_bind_index) != pipeline_state->active_slots.end()) { |
| 807 | DispatchDestroyShaderModule(device, Accessor::GetShaderModule(pCreateInfos[pipeline], stage), pAllocator); |
| 808 | } |
| 809 | |
| 810 | std::shared_ptr<const SHADER_MODULE_STATE> module_state; |
| 811 | if (bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) { |
| 812 | module_state = Get<SHADER_MODULE_STATE>(pipeline_state->GetUnifiedCreateInfo().graphics.pStages[stage].module); |
| 813 | } else if (bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) { |
| 814 | assert(stage == 0); |
| 815 | module_state = Get<SHADER_MODULE_STATE>(pipeline_state->GetUnifiedCreateInfo().compute.stage.module); |
| 816 | } else if (bind_point == VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) { |
| 817 | module_state = Get<SHADER_MODULE_STATE>(pipeline_state->GetUnifiedCreateInfo().raytracing.pStages[stage].module); |
| 818 | } else { |
| 819 | assert(false); |
| 820 | } |
| 821 | |
| 822 | std::vector<unsigned int> code; |
| 823 | // Save the shader binary |
| 824 | // The core_validation ShaderModule tracker saves the binary too, but discards it when the ShaderModule |
| 825 | // is destroyed. Applications may destroy ShaderModules after they are placed in a pipeline and before |
| 826 | // the pipeline is used, so we have to keep another copy. |
| 827 | if (module_state && module_state->has_valid_spirv) code = module_state->words; |
| 828 | |
| 829 | // Be careful to use the originally bound (instrumented) shader here, even if PreCallRecord had to back it |
| 830 | // out with a non-instrumented shader. The non-instrumented shader (found in pCreateInfo) was destroyed above. |
| 831 | VkShaderModule shader_module = VK_NULL_HANDLE; |
| 832 | if (bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) { |
| 833 | shader_module = pipeline_state->GetUnifiedCreateInfo().graphics.pStages[stage].module; |
| 834 | } else if (bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) { |
| 835 | assert(stage == 0); |
| 836 | shader_module = pipeline_state->GetUnifiedCreateInfo().compute.stage.module; |
| 837 | } else if (bind_point == VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) { |
| 838 | shader_module = pipeline_state->GetUnifiedCreateInfo().raytracing.pStages[stage].module; |
| 839 | } else { |
| 840 | assert(false); |
| 841 | } |
| 842 | shader_map.emplace(module_state->gpu_validation_shader_id, GpuAssistedShaderTracker{pipeline_state->pipeline(), shader_module, |
| 843 | std::move(code)}); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 848 | // Generate the stage-specific part of the message. |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 849 | void UtilGenerateStageMessage(const uint32_t *debug_record, std::string &msg) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 850 | using namespace spvtools; |
| 851 | std::ostringstream strm; |
| 852 | switch (debug_record[kInstCommonOutStageIdx]) { |
| 853 | case spv::ExecutionModelVertex: { |
| 854 | strm << "Stage = Vertex. Vertex Index = " << debug_record[kInstVertOutVertexIndex] |
| 855 | << " Instance Index = " << debug_record[kInstVertOutInstanceIndex] << ". "; |
| 856 | } break; |
| 857 | case spv::ExecutionModelTessellationControl: { |
| 858 | strm << "Stage = Tessellation Control. Invocation ID = " << debug_record[kInstTessCtlOutInvocationId] |
| 859 | << ", Primitive ID = " << debug_record[kInstTessCtlOutPrimitiveId]; |
| 860 | } break; |
| 861 | case spv::ExecutionModelTessellationEvaluation: { |
| 862 | strm << "Stage = Tessellation Eval. Primitive ID = " << debug_record[kInstTessEvalOutPrimitiveId] |
| 863 | << ", TessCoord (u, v) = (" << debug_record[kInstTessEvalOutTessCoordU] << ", " |
| 864 | << debug_record[kInstTessEvalOutTessCoordV] << "). "; |
| 865 | } break; |
| 866 | case spv::ExecutionModelGeometry: { |
| 867 | strm << "Stage = Geometry. Primitive ID = " << debug_record[kInstGeomOutPrimitiveId] |
| 868 | << " Invocation ID = " << debug_record[kInstGeomOutInvocationId] << ". "; |
| 869 | } break; |
| 870 | case spv::ExecutionModelFragment: { |
| 871 | strm << "Stage = Fragment. Fragment coord (x,y) = (" |
| 872 | << *reinterpret_cast<const float *>(&debug_record[kInstFragOutFragCoordX]) << ", " |
| 873 | << *reinterpret_cast<const float *>(&debug_record[kInstFragOutFragCoordY]) << "). "; |
| 874 | } break; |
| 875 | case spv::ExecutionModelGLCompute: { |
| 876 | strm << "Stage = Compute. Global invocation ID (x, y, z) = (" << debug_record[kInstCompOutGlobalInvocationIdX] << ", " |
| 877 | << debug_record[kInstCompOutGlobalInvocationIdY] << ", " << debug_record[kInstCompOutGlobalInvocationIdZ] << " )"; |
| 878 | } break; |
| 879 | case spv::ExecutionModelRayGenerationNV: { |
| 880 | strm << "Stage = Ray Generation. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 881 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 882 | } break; |
| 883 | case spv::ExecutionModelIntersectionNV: { |
| 884 | strm << "Stage = Intersection. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 885 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 886 | } break; |
| 887 | case spv::ExecutionModelAnyHitNV: { |
| 888 | strm << "Stage = Any Hit. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 889 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 890 | } break; |
| 891 | case spv::ExecutionModelClosestHitNV: { |
| 892 | strm << "Stage = Closest Hit. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 893 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 894 | } break; |
| 895 | case spv::ExecutionModelMissNV: { |
| 896 | strm << "Stage = Miss. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 897 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 898 | } break; |
| 899 | case spv::ExecutionModelCallableNV: { |
| 900 | strm << "Stage = Callable. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 901 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 902 | } break; |
Tony-LunarG | c7ed208 | 2020-06-11 14:00:04 -0600 | [diff] [blame] | 903 | case spv::ExecutionModelTaskNV: { |
| 904 | strm << "Stage = Task. Global invocation ID (x, y, z) = (" << debug_record[kInstTaskOutGlobalInvocationIdX] << ", " |
| 905 | << debug_record[kInstTaskOutGlobalInvocationIdY] << ", " << debug_record[kInstTaskOutGlobalInvocationIdZ] << " )"; |
| 906 | } break; |
| 907 | case spv::ExecutionModelMeshNV: { |
| 908 | strm << "Stage = Mesh.Global invocation ID (x, y, z) = (" << debug_record[kInstMeshOutGlobalInvocationIdX] << ", " |
| 909 | << debug_record[kInstMeshOutGlobalInvocationIdY] << ", " << debug_record[kInstMeshOutGlobalInvocationIdZ] << " )"; |
| 910 | } break; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 911 | default: { |
| 912 | strm << "Internal Error (unexpected stage = " << debug_record[kInstCommonOutStageIdx] << "). "; |
| 913 | assert(false); |
| 914 | } break; |
| 915 | } |
| 916 | msg = strm.str(); |
| 917 | } |
| 918 | |
| 919 | std::string LookupDebugUtilsName(const debug_report_data *report_data, const uint64_t object) { |
| 920 | auto object_label = report_data->DebugReportGetUtilsObjectName(object); |
| 921 | if (object_label != "") { |
| 922 | object_label = "(" + object_label + ")"; |
| 923 | } |
| 924 | return object_label; |
| 925 | } |
| 926 | |
| 927 | // Generate message from the common portion of the debug report record. |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 928 | void UtilGenerateCommonMessage(const debug_report_data *report_data, const VkCommandBuffer commandBuffer, |
| 929 | const uint32_t *debug_record, const VkShaderModule shader_module_handle, |
| 930 | const VkPipeline pipeline_handle, const VkPipelineBindPoint pipeline_bind_point, |
| 931 | const uint32_t operation_index, std::string &msg) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 932 | using namespace spvtools; |
| 933 | std::ostringstream strm; |
| 934 | if (shader_module_handle == VK_NULL_HANDLE) { |
| 935 | strm << std::hex << std::showbase << "Internal Error: Unable to locate information for shader used in command buffer " |
| 936 | << LookupDebugUtilsName(report_data, HandleToUint64(commandBuffer)) << "(" << HandleToUint64(commandBuffer) << "). "; |
| 937 | assert(true); |
| 938 | } else { |
| 939 | strm << std::hex << std::showbase << "Command buffer " << LookupDebugUtilsName(report_data, HandleToUint64(commandBuffer)) |
| 940 | << "(" << HandleToUint64(commandBuffer) << "). "; |
| 941 | if (pipeline_bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) { |
| 942 | strm << "Draw "; |
| 943 | } else if (pipeline_bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) { |
| 944 | strm << "Compute "; |
| 945 | } else if (pipeline_bind_point == VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) { |
| 946 | strm << "Ray Trace "; |
| 947 | } else { |
| 948 | assert(false); |
| 949 | strm << "Unknown Pipeline Operation "; |
| 950 | } |
| 951 | strm << "Index " << operation_index << ". " |
| 952 | << "Pipeline " << LookupDebugUtilsName(report_data, HandleToUint64(pipeline_handle)) << "(" |
| 953 | << HandleToUint64(pipeline_handle) << "). " |
| 954 | << "Shader Module " << LookupDebugUtilsName(report_data, HandleToUint64(shader_module_handle)) << "(" |
| 955 | << HandleToUint64(shader_module_handle) << "). "; |
| 956 | } |
| 957 | strm << std::dec << std::noshowbase; |
| 958 | strm << "Shader Instruction Index = " << debug_record[kInstCommonOutInstructionIdx] << ". "; |
| 959 | msg = strm.str(); |
| 960 | } |
| 961 | |
| 962 | // Read the contents of the SPIR-V OpSource instruction and any following continuation instructions. |
| 963 | // Split the single string into a vector of strings, one for each line, for easier processing. |
sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 964 | void ReadOpSource(const SHADER_MODULE_STATE &module_state, const uint32_t reported_file_id, |
| 965 | std::vector<std::string> &opsource_lines) { |
| 966 | for (auto insn : module_state) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 967 | if ((insn.opcode() == spv::OpSource) && (insn.len() >= 5) && (insn.word(3) == reported_file_id)) { |
| 968 | std::istringstream in_stream; |
| 969 | std::string cur_line; |
| 970 | in_stream.str((char *)&insn.word(4)); |
| 971 | while (std::getline(in_stream, cur_line)) { |
| 972 | opsource_lines.push_back(cur_line); |
| 973 | } |
| 974 | while ((++insn).opcode() == spv::OpSourceContinued) { |
| 975 | in_stream.str((char *)&insn.word(1)); |
| 976 | while (std::getline(in_stream, cur_line)) { |
| 977 | opsource_lines.push_back(cur_line); |
| 978 | } |
| 979 | } |
| 980 | break; |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | // The task here is to search the OpSource content to find the #line directive with the |
| 986 | // line number that is closest to, but still prior to the reported error line number and |
| 987 | // still within the reported filename. |
| 988 | // From this known position in the OpSource content we can add the difference between |
| 989 | // the #line line number and the reported error line number to determine the location |
| 990 | // in the OpSource content of the reported error line. |
| 991 | // |
| 992 | // Considerations: |
| 993 | // - Look only at #line directives that specify the reported_filename since |
| 994 | // the reported error line number refers to its location in the reported filename. |
| 995 | // - If a #line directive does not have a filename, the file is the reported filename, or |
| 996 | // the filename found in a prior #line directive. (This is C-preprocessor behavior) |
| 997 | // - It is possible (e.g., inlining) for blocks of code to get shuffled out of their |
| 998 | // original order and the #line directives are used to keep the numbering correct. This |
| 999 | // is why we need to examine the entire contents of the source, instead of leaving early |
| 1000 | // when finding a #line line number larger than the reported error line number. |
| 1001 | // |
| 1002 | |
| 1003 | // GCC 4.8 has a problem with std::regex that is fixed in GCC 4.9. Provide fallback code for 4.8 |
| 1004 | #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) |
| 1005 | |
| 1006 | #if defined(__GNUC__) && GCC_VERSION < 40900 |
| 1007 | bool GetLineAndFilename(const std::string string, uint32_t *linenumber, std::string &filename) { |
| 1008 | // # line <linenumber> "<filename>" or |
| 1009 | // #line <linenumber> "<filename>" |
| 1010 | std::vector<std::string> tokens; |
| 1011 | std::stringstream stream(string); |
| 1012 | std::string temp; |
| 1013 | uint32_t line_index = 0; |
| 1014 | |
| 1015 | while (stream >> temp) tokens.push_back(temp); |
| 1016 | auto size = tokens.size(); |
| 1017 | if (size > 1) { |
| 1018 | if (tokens[0] == "#" && tokens[1] == "line") { |
| 1019 | line_index = 2; |
| 1020 | } else if (tokens[0] == "#line") { |
| 1021 | line_index = 1; |
| 1022 | } |
| 1023 | } |
| 1024 | if (0 == line_index) return false; |
Mark Young | 0ec6b06 | 2020-11-19 15:32:17 -0700 | [diff] [blame] | 1025 | *linenumber = static_cast<uint32_t>(std::stoul(tokens[line_index])); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1026 | uint32_t filename_index = line_index + 1; |
| 1027 | // Remove enclosing double quotes around filename |
| 1028 | if (size > filename_index) filename = tokens[filename_index].substr(1, tokens[filename_index].size() - 2); |
| 1029 | return true; |
| 1030 | } |
| 1031 | #else |
| 1032 | bool GetLineAndFilename(const std::string string, uint32_t *linenumber, std::string &filename) { |
| 1033 | static const std::regex line_regex( // matches #line directives |
| 1034 | "^" // beginning of line |
| 1035 | "\\s*" // optional whitespace |
| 1036 | "#" // required text |
| 1037 | "\\s*" // optional whitespace |
| 1038 | "line" // required text |
| 1039 | "\\s+" // required whitespace |
| 1040 | "([0-9]+)" // required first capture - line number |
| 1041 | "(\\s+)?" // optional second capture - whitespace |
| 1042 | "(\".+\")?" // optional third capture - quoted filename with at least one char inside |
| 1043 | ".*"); // rest of line (needed when using std::regex_match since the entire line is tested) |
| 1044 | |
| 1045 | std::smatch captures; |
| 1046 | |
| 1047 | bool found_line = std::regex_match(string, captures, line_regex); |
| 1048 | if (!found_line) return false; |
| 1049 | |
| 1050 | // filename is optional and considered found only if the whitespace and the filename are captured |
| 1051 | if (captures[2].matched && captures[3].matched) { |
| 1052 | // Remove enclosing double quotes. The regex guarantees the quotes and at least one char. |
| 1053 | filename = captures[3].str().substr(1, captures[3].str().size() - 2); |
| 1054 | } |
Artem Bolgar | 82d0836 | 2021-06-03 13:11:13 -0700 | [diff] [blame] | 1055 | *linenumber = (uint32_t)std::stoul(captures[1]); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1056 | return true; |
| 1057 | } |
| 1058 | #endif // GCC_VERSION |
| 1059 | |
| 1060 | // Extract the filename, line number, and column number from the correct OpLine and build a message string from it. |
| 1061 | // Scan the source (from OpSource) to find the line of source at the reported line number and place it in another message string. |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 1062 | void UtilGenerateSourceMessages(const std::vector<uint32_t> &pgm, const uint32_t *debug_record, bool from_printf, |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 1063 | std::string &filename_msg, std::string &source_msg) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1064 | using namespace spvtools; |
| 1065 | std::ostringstream filename_stream; |
| 1066 | std::ostringstream source_stream; |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 1067 | SHADER_MODULE_STATE shader(pgm); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1068 | // Find the OpLine just before the failing instruction indicated by the debug info. |
| 1069 | // SPIR-V can only be iterated in the forward direction due to its opcode/length encoding. |
| 1070 | uint32_t instruction_index = 0; |
| 1071 | uint32_t reported_file_id = 0; |
| 1072 | uint32_t reported_line_number = 0; |
| 1073 | uint32_t reported_column_number = 0; |
| 1074 | if (shader.words.size() > 0) { |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1075 | for (const auto &insn : shader) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1076 | if (insn.opcode() == spv::OpLine) { |
| 1077 | reported_file_id = insn.word(1); |
| 1078 | reported_line_number = insn.word(2); |
| 1079 | reported_column_number = insn.word(3); |
| 1080 | } |
| 1081 | if (instruction_index == debug_record[kInstCommonOutInstructionIdx]) { |
| 1082 | break; |
| 1083 | } |
| 1084 | instruction_index++; |
| 1085 | } |
| 1086 | } |
| 1087 | // Create message with file information obtained from the OpString pointed to by the discovered OpLine. |
| 1088 | std::string reported_filename; |
| 1089 | if (reported_file_id == 0) { |
| 1090 | filename_stream |
| 1091 | << "Unable to find SPIR-V OpLine for source information. Build shader with debug info to get source information."; |
| 1092 | } else { |
| 1093 | bool found_opstring = false; |
| 1094 | std::string prefix; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1095 | if (from_printf) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1096 | prefix = "Debug shader printf message generated "; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1097 | } else { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1098 | prefix = "Shader validation error occurred "; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1099 | } |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1100 | for (const auto &insn : shader) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1101 | if ((insn.opcode() == spv::OpString) && (insn.len() >= 3) && (insn.word(1) == reported_file_id)) { |
| 1102 | found_opstring = true; |
| 1103 | reported_filename = (char *)&insn.word(2); |
| 1104 | if (reported_filename.empty()) { |
| 1105 | filename_stream << prefix << "at line " << reported_line_number; |
| 1106 | } else { |
| 1107 | filename_stream << prefix << "in file " << reported_filename << " at line " << reported_line_number; |
| 1108 | } |
| 1109 | if (reported_column_number > 0) { |
| 1110 | filename_stream << ", column " << reported_column_number; |
| 1111 | } |
| 1112 | filename_stream << "."; |
| 1113 | break; |
| 1114 | } |
| 1115 | } |
| 1116 | if (!found_opstring) { |
Tony-LunarG | 6d195e1 | 2020-10-27 16:54:14 -0600 | [diff] [blame] | 1117 | filename_stream << "Unable to find SPIR-V OpString for file id " << reported_file_id << " from OpLine instruction." |
| 1118 | << std::endl; |
| 1119 | filename_stream << "File ID = " << reported_file_id << ", Line Number = " << reported_line_number |
| 1120 | << ", Column = " << reported_column_number << std::endl; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | filename_msg = filename_stream.str(); |
| 1124 | |
| 1125 | // Create message to display source code line containing error. |
| 1126 | if ((reported_file_id != 0)) { |
| 1127 | // Read the source code and split it up into separate lines. |
| 1128 | std::vector<std::string> opsource_lines; |
| 1129 | ReadOpSource(shader, reported_file_id, opsource_lines); |
| 1130 | // Find the line in the OpSource content that corresponds to the reported error file and line. |
| 1131 | if (!opsource_lines.empty()) { |
| 1132 | uint32_t saved_line_number = 0; |
| 1133 | std::string current_filename = reported_filename; // current "preprocessor" filename state. |
| 1134 | std::vector<std::string>::size_type saved_opsource_offset = 0; |
| 1135 | bool found_best_line = false; |
| 1136 | for (auto it = opsource_lines.begin(); it != opsource_lines.end(); ++it) { |
| 1137 | uint32_t parsed_line_number; |
| 1138 | std::string parsed_filename; |
| 1139 | bool found_line = GetLineAndFilename(*it, &parsed_line_number, parsed_filename); |
| 1140 | if (!found_line) continue; |
| 1141 | |
| 1142 | bool found_filename = parsed_filename.size() > 0; |
| 1143 | if (found_filename) { |
| 1144 | current_filename = parsed_filename; |
| 1145 | } |
| 1146 | if ((!found_filename) || (current_filename == reported_filename)) { |
| 1147 | // Update the candidate best line directive, if the current one is prior and closer to the reported line |
| 1148 | if (reported_line_number >= parsed_line_number) { |
| 1149 | if (!found_best_line || |
| 1150 | (reported_line_number - parsed_line_number <= reported_line_number - saved_line_number)) { |
| 1151 | saved_line_number = parsed_line_number; |
| 1152 | saved_opsource_offset = std::distance(opsource_lines.begin(), it); |
| 1153 | found_best_line = true; |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | if (found_best_line) { |
| 1159 | assert(reported_line_number >= saved_line_number); |
| 1160 | std::vector<std::string>::size_type opsource_index = |
| 1161 | (reported_line_number - saved_line_number) + 1 + saved_opsource_offset; |
| 1162 | if (opsource_index < opsource_lines.size()) { |
| 1163 | source_stream << "\n" << reported_line_number << ": " << opsource_lines[opsource_index].c_str(); |
| 1164 | } else { |
| 1165 | source_stream << "Internal error: calculated source line of " << opsource_index << " for source size of " |
| 1166 | << opsource_lines.size() << " lines."; |
| 1167 | } |
| 1168 | } else { |
| 1169 | source_stream << "Unable to find suitable #line directive in SPIR-V OpSource."; |
| 1170 | } |
| 1171 | } else { |
| 1172 | source_stream << "Unable to find SPIR-V OpSource."; |
| 1173 | } |
| 1174 | } |
| 1175 | source_msg = source_stream.str(); |
| 1176 | } |