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 | 04697b0 | 2022-03-23 16:18:12 -0600 | [diff] [blame] | 228 | ReadLockGuard GpuAssistedBase::ReadLock() { |
| 229 | if (fine_grained_locking) { |
| 230 | return ReadLockGuard(validation_object_mutex, std::defer_lock); |
| 231 | } else { |
| 232 | return ReadLockGuard(validation_object_mutex); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | WriteLockGuard GpuAssistedBase::WriteLock() { |
| 237 | if (fine_grained_locking) { |
| 238 | return WriteLockGuard(validation_object_mutex, std::defer_lock); |
| 239 | } else { |
| 240 | return WriteLockGuard(validation_object_mutex); |
| 241 | } |
| 242 | } |
| 243 | |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 244 | void GpuAssistedBase::PreCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo, |
| 245 | const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, void *modified_ci) { |
| 246 | ValidationStateTracker::PreCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, modified_ci); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 247 | VkPhysicalDeviceFeatures *features = nullptr; |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 248 | // Use a local variable to query features since this method runs in the instance validation object. |
| 249 | // To avoid confusion and race conditions about which physical device's features are stored in the |
| 250 | // 'supported_devices' member variable, it will only be set in the device validation objects. |
| 251 | // See CreateDevice() below. |
| 252 | VkPhysicalDeviceFeatures gpu_supported_features; |
| 253 | DispatchGetPhysicalDeviceFeatures(gpu, &gpu_supported_features); |
| 254 | auto modified_create_info = static_cast<VkDeviceCreateInfo *>(modified_ci); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 255 | if (modified_create_info->pEnabledFeatures) { |
| 256 | // If pEnabledFeatures, VkPhysicalDeviceFeatures2 in pNext chain is not allowed |
| 257 | features = const_cast<VkPhysicalDeviceFeatures *>(modified_create_info->pEnabledFeatures); |
| 258 | } else { |
| 259 | VkPhysicalDeviceFeatures2 *features2 = nullptr; |
Mark Lobodzinski | 1f887d3 | 2020-12-30 15:31:33 -0700 | [diff] [blame] | 260 | features2 = const_cast<VkPhysicalDeviceFeatures2 *>(LvlFindInChain<VkPhysicalDeviceFeatures2>(modified_create_info->pNext)); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 261 | if (features2) features = &features2->features; |
| 262 | } |
Tony-LunarG | f0634eb | 2021-01-05 15:11:12 -0700 | [diff] [blame] | 263 | VkPhysicalDeviceFeatures new_features = {}; |
| 264 | VkBool32 *desired = reinterpret_cast<VkBool32 *>(&desired_features); |
| 265 | VkBool32 *feature_ptr; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 266 | if (features) { |
Tony-LunarG | f0634eb | 2021-01-05 15:11:12 -0700 | [diff] [blame] | 267 | feature_ptr = reinterpret_cast<VkBool32 *>(features); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 268 | } else { |
Tony-LunarG | f0634eb | 2021-01-05 15:11:12 -0700 | [diff] [blame] | 269 | feature_ptr = reinterpret_cast<VkBool32 *>(&new_features); |
| 270 | } |
| 271 | VkBool32 *supported = reinterpret_cast<VkBool32 *>(&supported_features); |
| 272 | for (size_t i = 0; i < sizeof(VkPhysicalDeviceFeatures); i += (sizeof(VkBool32))) { |
| 273 | if (*supported && *desired) { |
| 274 | *feature_ptr = true; |
| 275 | } |
| 276 | supported++; |
| 277 | desired++; |
| 278 | feature_ptr++; |
| 279 | } |
| 280 | if (!features) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 281 | delete modified_create_info->pEnabledFeatures; |
| 282 | modified_create_info->pEnabledFeatures = new VkPhysicalDeviceFeatures(new_features); |
| 283 | } |
| 284 | } |
| 285 | |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 286 | void GpuAssistedBase::CreateDevice(const VkDeviceCreateInfo *pCreateInfo) { |
| 287 | ValidationStateTracker::CreateDevice(pCreateInfo); |
| 288 | // If api version 1.1 or later, SetDeviceLoaderData will be in the loader |
| 289 | auto chain_info = get_chain_info(pCreateInfo, VK_LOADER_DATA_CALLBACK); |
| 290 | assert(chain_info->u.pfnSetDeviceLoaderData); |
| 291 | vkSetDeviceLoaderData = chain_info->u.pfnSetDeviceLoaderData; |
| 292 | |
| 293 | // Some devices have extremely high limits here, so set a reasonable max because we have to pad |
| 294 | // the pipeline layout with dummy descriptor set layouts. |
| 295 | adjusted_max_desc_sets = phys_dev_props.limits.maxBoundDescriptorSets; |
| 296 | adjusted_max_desc_sets = std::min(33U, adjusted_max_desc_sets); |
| 297 | |
| 298 | // We can't do anything if there is only one. |
| 299 | // Device probably not a legit Vulkan device, since there should be at least 4. Protect ourselves. |
| 300 | if (adjusted_max_desc_sets == 1) { |
| 301 | ReportSetupProblem(device, "Device can bind only a single descriptor set."); |
| 302 | aborted = true; |
| 303 | return; |
| 304 | } |
| 305 | desc_set_bind_index = adjusted_max_desc_sets - 1; |
| 306 | |
| 307 | VkResult result1 = UtilInitializeVma(physical_device, device, &vmaAllocator); |
| 308 | assert(result1 == VK_SUCCESS); |
| 309 | desc_set_manager = layer_data::make_unique<UtilDescriptorSetManager>(device, static_cast<uint32_t>(bindings_.size())); |
| 310 | |
| 311 | const VkDescriptorSetLayoutCreateInfo debug_desc_layout_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, NULL, 0, |
| 312 | static_cast<uint32_t>(bindings_.size()), bindings_.data()}; |
| 313 | |
| 314 | const VkDescriptorSetLayoutCreateInfo dummy_desc_layout_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, NULL, 0, 0, |
| 315 | NULL}; |
| 316 | |
| 317 | result1 = DispatchCreateDescriptorSetLayout(device, &debug_desc_layout_info, NULL, &debug_desc_layout); |
| 318 | |
| 319 | // This is a layout used to "pad" a pipeline layout to fill in any gaps to the selected bind index. |
| 320 | VkResult result2 = DispatchCreateDescriptorSetLayout(device, &dummy_desc_layout_info, NULL, &dummy_desc_layout); |
| 321 | |
| 322 | assert((result1 == VK_SUCCESS) && (result2 == VK_SUCCESS)); |
| 323 | if ((result1 != VK_SUCCESS) || (result2 != VK_SUCCESS)) { |
| 324 | ReportSetupProblem(device, "Unable to create descriptor set layout."); |
| 325 | if (result1 == VK_SUCCESS) { |
| 326 | DispatchDestroyDescriptorSetLayout(device, debug_desc_layout, NULL); |
| 327 | } |
| 328 | if (result2 == VK_SUCCESS) { |
| 329 | DispatchDestroyDescriptorSetLayout(device, dummy_desc_layout, NULL); |
| 330 | } |
| 331 | debug_desc_layout = VK_NULL_HANDLE; |
| 332 | dummy_desc_layout = VK_NULL_HANDLE; |
| 333 | aborted = true; |
| 334 | return; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void GpuAssistedBase::PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) { |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 339 | if (debug_desc_layout) { |
| 340 | DispatchDestroyDescriptorSetLayout(device, debug_desc_layout, NULL); |
| 341 | debug_desc_layout = VK_NULL_HANDLE; |
| 342 | } |
| 343 | if (dummy_desc_layout) { |
| 344 | DispatchDestroyDescriptorSetLayout(device, dummy_desc_layout, NULL); |
| 345 | dummy_desc_layout = VK_NULL_HANDLE; |
| 346 | } |
| 347 | ValidationStateTracker::PreCallRecordDestroyDevice(device, pAllocator); |
| 348 | // State Tracker can end up making vma calls through callbacks - don't destroy allocator until ST is done |
| 349 | if (vmaAllocator) { |
| 350 | vmaDestroyAllocator(vmaAllocator); |
| 351 | } |
| 352 | desc_set_manager.reset(); |
| 353 | } |
| 354 | |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 355 | gpu_utils_state::Queue::Queue(GpuAssistedBase &state, VkQueue q, uint32_t index, VkDeviceQueueCreateFlags flags) |
| 356 | : QUEUE_STATE(q, index, flags), state_(state) {} |
| 357 | |
| 358 | gpu_utils_state::Queue::~Queue() { |
| 359 | if (barrier_command_buffer_) { |
| 360 | DispatchFreeCommandBuffers(state_.device, barrier_command_pool_, 1, &barrier_command_buffer_); |
| 361 | barrier_command_buffer_ = VK_NULL_HANDLE; |
| 362 | } |
| 363 | if (barrier_command_pool_) { |
| 364 | DispatchDestroyCommandPool(state_.device, barrier_command_pool_, NULL); |
| 365 | barrier_command_pool_ = VK_NULL_HANDLE; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // Submit a memory barrier on graphics queues. |
| 370 | // Lazy-create and record the needed command buffer. |
| 371 | void gpu_utils_state::Queue::SubmitBarrier() { |
| 372 | if (barrier_command_pool_ == VK_NULL_HANDLE) { |
| 373 | VkResult result = VK_SUCCESS; |
| 374 | |
| 375 | auto pool_create_info = LvlInitStruct<VkCommandPoolCreateInfo>(); |
| 376 | pool_create_info.queueFamilyIndex = queueFamilyIndex; |
| 377 | result = DispatchCreateCommandPool(state_.device, &pool_create_info, nullptr, &barrier_command_pool_); |
| 378 | if (result != VK_SUCCESS) { |
| 379 | state_.ReportSetupProblem(state_.device, "Unable to create command pool for barrier CB."); |
| 380 | barrier_command_pool_ = VK_NULL_HANDLE; |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | auto buffer_alloc_info = LvlInitStruct<VkCommandBufferAllocateInfo>(); |
| 385 | buffer_alloc_info.commandPool = barrier_command_pool_; |
| 386 | buffer_alloc_info.commandBufferCount = 1; |
| 387 | buffer_alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 388 | result = DispatchAllocateCommandBuffers(state_.device, &buffer_alloc_info, &barrier_command_buffer_); |
| 389 | if (result != VK_SUCCESS) { |
| 390 | state_.ReportSetupProblem(state_.device, "Unable to create barrier command buffer."); |
| 391 | DispatchDestroyCommandPool(state_.device, barrier_command_pool_, nullptr); |
| 392 | barrier_command_pool_ = VK_NULL_HANDLE; |
| 393 | barrier_command_buffer_ = VK_NULL_HANDLE; |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | // Hook up command buffer dispatch |
| 398 | state_.vkSetDeviceLoaderData(state_.device, barrier_command_buffer_); |
| 399 | |
| 400 | // Record a global memory barrier to force availability of device memory operations to the host domain. |
| 401 | auto command_buffer_begin_info = LvlInitStruct<VkCommandBufferBeginInfo>(); |
| 402 | result = DispatchBeginCommandBuffer(barrier_command_buffer_, &command_buffer_begin_info); |
| 403 | if (result == VK_SUCCESS) { |
| 404 | auto memory_barrier = LvlInitStruct<VkMemoryBarrier>(); |
| 405 | memory_barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT; |
| 406 | memory_barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT; |
| 407 | DispatchCmdPipelineBarrier(barrier_command_buffer_, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, |
| 408 | 1, &memory_barrier, 0, nullptr, 0, nullptr); |
| 409 | DispatchEndCommandBuffer(barrier_command_buffer_); |
| 410 | } |
| 411 | } |
| 412 | if (barrier_command_buffer_ != VK_NULL_HANDLE) { |
| 413 | auto submit_info = LvlInitStruct<VkSubmitInfo>(); |
| 414 | submit_info.commandBufferCount = 1; |
| 415 | submit_info.pCommandBuffers = &barrier_command_buffer_; |
| 416 | DispatchQueueSubmit(QUEUE_STATE::Queue(), 1, &submit_info, VK_NULL_HANDLE); |
| 417 | } |
| 418 | } |
| 419 | |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 420 | bool GpuAssistedBase::CommandBufferNeedsProcessing(VkCommandBuffer command_buffer) const { |
Jeremy Gebben | 04697b0 | 2022-03-23 16:18:12 -0600 | [diff] [blame] | 421 | auto cb_node = GetRead<gpu_utils_state::CommandBuffer>(command_buffer); |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 422 | if (cb_node->NeedsProcessing()) { |
| 423 | return true; |
| 424 | } |
| 425 | for (const auto *secondary_cb : cb_node->linkedCommandBuffers) { |
| 426 | auto secondary_cb_node = static_cast<const gpu_utils_state::CommandBuffer *>(secondary_cb); |
Jeremy Gebben | 04697b0 | 2022-03-23 16:18:12 -0600 | [diff] [blame] | 427 | auto guard = secondary_cb_node->ReadLock(); |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 428 | if (secondary_cb_node->NeedsProcessing()) { |
| 429 | return true; |
| 430 | } |
| 431 | } |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | void GpuAssistedBase::ProcessCommandBuffer(VkQueue queue, VkCommandBuffer command_buffer) { |
Jeremy Gebben | 04697b0 | 2022-03-23 16:18:12 -0600 | [diff] [blame] | 436 | auto cb_node = GetWrite<gpu_utils_state::CommandBuffer>(command_buffer); |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 437 | |
| 438 | cb_node->Process(queue); |
| 439 | for (auto *secondary_cmd_base : cb_node->linkedCommandBuffers) { |
| 440 | auto *secondary_cb_node = static_cast<gpu_utils_state::CommandBuffer *>(secondary_cmd_base); |
Jeremy Gebben | 04697b0 | 2022-03-23 16:18:12 -0600 | [diff] [blame] | 441 | auto guard = secondary_cb_node->WriteLock(); |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 442 | secondary_cb_node->Process(queue); |
| 443 | } |
| 444 | } |
| 445 | |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 446 | // Issue a memory barrier to make GPU-written data available to host. |
| 447 | // Wait for the queue to complete execution. |
| 448 | // Check the debug buffers for all the command buffers that were submitted. |
| 449 | void GpuAssistedBase::PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence, |
| 450 | VkResult result) { |
| 451 | ValidationStateTracker::PostCallRecordQueueSubmit(queue, submitCount, pSubmits, fence, result); |
| 452 | |
| 453 | if (aborted || (result != VK_SUCCESS)) return; |
| 454 | bool buffers_present = false; |
| 455 | // Don't QueueWaitIdle if there's nothing to process |
| 456 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 457 | const VkSubmitInfo *submit = &pSubmits[submit_idx]; |
| 458 | for (uint32_t i = 0; i < submit->commandBufferCount; i++) { |
| 459 | buffers_present |= CommandBufferNeedsProcessing(submit->pCommandBuffers[i]); |
| 460 | } |
| 461 | } |
| 462 | if (!buffers_present) return; |
| 463 | |
| 464 | SubmitBarrier(queue); |
| 465 | |
| 466 | DispatchQueueWaitIdle(queue); |
| 467 | |
| 468 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 469 | const VkSubmitInfo *submit = &pSubmits[submit_idx]; |
| 470 | for (uint32_t i = 0; i < submit->commandBufferCount; i++) { |
| 471 | ProcessCommandBuffer(queue, submit->pCommandBuffers[i]); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | void GpuAssistedBase::RecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence, |
| 477 | VkResult result) { |
| 478 | if (aborted || (result != VK_SUCCESS)) return; |
| 479 | bool buffers_present = false; |
| 480 | // Don't QueueWaitIdle if there's nothing to process |
| 481 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 482 | const VkSubmitInfo2 *submit = &pSubmits[submit_idx]; |
| 483 | for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) { |
| 484 | buffers_present |= CommandBufferNeedsProcessing(submit->pCommandBufferInfos[i].commandBuffer); |
| 485 | } |
| 486 | } |
| 487 | if (!buffers_present) return; |
| 488 | |
| 489 | SubmitBarrier(queue); |
| 490 | |
| 491 | DispatchQueueWaitIdle(queue); |
| 492 | |
| 493 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 494 | const VkSubmitInfo2 *submit = &pSubmits[submit_idx]; |
| 495 | for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) { |
| 496 | ProcessCommandBuffer(queue, submit->pCommandBufferInfos[i].commandBuffer); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void GpuAssistedBase::PostCallRecordQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, |
| 502 | VkFence fence, VkResult result) { |
| 503 | ValidationStateTracker::PostCallRecordQueueSubmit2KHR(queue, submitCount, pSubmits, fence, result); |
| 504 | RecordQueueSubmit2(queue, submitCount, pSubmits, fence, result); |
| 505 | } |
| 506 | |
| 507 | void GpuAssistedBase::PostCallRecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence, |
| 508 | VkResult result) { |
| 509 | ValidationStateTracker::PostCallRecordQueueSubmit2(queue, submitCount, pSubmits, fence, result); |
| 510 | RecordQueueSubmit2(queue, submitCount, pSubmits, fence, result); |
| 511 | } |
| 512 | |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 513 | void GpuAssistedBase::PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo, |
| 514 | const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout, |
| 515 | void *cpl_state_data) { |
| 516 | if (aborted) { |
| 517 | return; |
| 518 | } |
| 519 | auto cpl_state = static_cast<create_pipeline_layout_api_state *>(cpl_state_data); |
| 520 | if (cpl_state->modified_create_info.setLayoutCount >= adjusted_max_desc_sets) { |
| 521 | std::ostringstream strm; |
| 522 | strm << "Pipeline Layout conflict with validation's descriptor set at slot " << desc_set_bind_index << ". " |
| 523 | << "Application has too many descriptor sets in the pipeline layout to continue with gpu validation. " |
| 524 | << "Validation is not modifying the pipeline layout. " |
| 525 | << "Instrumented shaders are replaced with non-instrumented shaders."; |
| 526 | ReportSetupProblem(device, strm.str().c_str()); |
| 527 | } else { |
| 528 | // Modify the pipeline layout by: |
| 529 | // 1. Copying the caller's descriptor set desc_layouts |
| 530 | // 2. Fill in dummy descriptor layouts up to the max binding |
| 531 | // 3. Fill in with the debug descriptor layout at the max binding slot |
| 532 | cpl_state->new_layouts.reserve(adjusted_max_desc_sets); |
| 533 | cpl_state->new_layouts.insert(cpl_state->new_layouts.end(), &pCreateInfo->pSetLayouts[0], |
| 534 | &pCreateInfo->pSetLayouts[pCreateInfo->setLayoutCount]); |
| 535 | for (uint32_t i = pCreateInfo->setLayoutCount; i < adjusted_max_desc_sets - 1; ++i) { |
| 536 | cpl_state->new_layouts.push_back(dummy_desc_layout); |
| 537 | } |
| 538 | cpl_state->new_layouts.push_back(debug_desc_layout); |
| 539 | cpl_state->modified_create_info.pSetLayouts = cpl_state->new_layouts.data(); |
| 540 | cpl_state->modified_create_info.setLayoutCount = adjusted_max_desc_sets; |
| 541 | } |
| 542 | ValidationStateTracker::PreCallRecordCreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout, cpl_state_data); |
| 543 | } |
| 544 | |
| 545 | void GpuAssistedBase::PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo, |
| 546 | const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout, |
| 547 | VkResult result) { |
| 548 | if (result != VK_SUCCESS) { |
| 549 | ReportSetupProblem(device, "Unable to create pipeline layout. Device could become unstable."); |
| 550 | aborted = true; |
| 551 | } |
| 552 | ValidationStateTracker::PostCallRecordCreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout, result); |
| 553 | } |
| 554 | |
| 555 | void GpuAssistedBase::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 556 | const VkGraphicsPipelineCreateInfo *pCreateInfos, |
| 557 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 558 | void *cgpl_state_data) { |
| 559 | if (aborted) return; |
| 560 | std::vector<safe_VkGraphicsPipelineCreateInfo> new_pipeline_create_infos; |
| 561 | create_graphics_pipeline_api_state *cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state *>(cgpl_state_data); |
| 562 | PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, cgpl_state->pipe_state, &new_pipeline_create_infos, |
| 563 | VK_PIPELINE_BIND_POINT_GRAPHICS); |
Tony-LunarG | 806cddb | 2022-05-11 15:32:48 -0600 | [diff] [blame] | 564 | cgpl_state->modified_create_infos = new_pipeline_create_infos; |
| 565 | cgpl_state->pCreateInfos = reinterpret_cast<VkGraphicsPipelineCreateInfo *>(cgpl_state->modified_create_infos.data()); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void GpuAssistedBase::PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 569 | const VkComputePipelineCreateInfo *pCreateInfos, |
| 570 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 571 | void *ccpl_state_data) { |
| 572 | if (aborted) return; |
| 573 | std::vector<safe_VkComputePipelineCreateInfo> new_pipeline_create_infos; |
| 574 | auto *ccpl_state = reinterpret_cast<create_compute_pipeline_api_state *>(ccpl_state_data); |
| 575 | PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, ccpl_state->pipe_state, &new_pipeline_create_infos, |
| 576 | VK_PIPELINE_BIND_POINT_COMPUTE); |
Tony-LunarG | 806cddb | 2022-05-11 15:32:48 -0600 | [diff] [blame] | 577 | ccpl_state->modified_create_infos = new_pipeline_create_infos; |
| 578 | ccpl_state->pCreateInfos = reinterpret_cast<VkComputePipelineCreateInfo *>(ccpl_state->modified_create_infos.data()); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | void GpuAssistedBase::PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 582 | const VkRayTracingPipelineCreateInfoNV *pCreateInfos, |
| 583 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 584 | void *crtpl_state_data) { |
| 585 | if (aborted) return; |
| 586 | std::vector<safe_VkRayTracingPipelineCreateInfoCommon> new_pipeline_create_infos; |
| 587 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_api_state *>(crtpl_state_data); |
| 588 | PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, crtpl_state->pipe_state, &new_pipeline_create_infos, |
| 589 | VK_PIPELINE_BIND_POINT_RAY_TRACING_NV); |
Tony-LunarG | 806cddb | 2022-05-11 15:32:48 -0600 | [diff] [blame] | 590 | crtpl_state->modified_create_infos = new_pipeline_create_infos; |
| 591 | crtpl_state->pCreateInfos = reinterpret_cast<VkRayTracingPipelineCreateInfoNV *>(crtpl_state->modified_create_infos.data()); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | void GpuAssistedBase::PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation, |
| 595 | VkPipelineCache pipelineCache, uint32_t count, |
| 596 | const VkRayTracingPipelineCreateInfoKHR *pCreateInfos, |
| 597 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 598 | void *crtpl_state_data) { |
| 599 | if (aborted) return; |
| 600 | std::vector<safe_VkRayTracingPipelineCreateInfoCommon> new_pipeline_create_infos; |
| 601 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data); |
| 602 | PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, crtpl_state->pipe_state, &new_pipeline_create_infos, |
| 603 | VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR); |
Tony-LunarG | 806cddb | 2022-05-11 15:32:48 -0600 | [diff] [blame] | 604 | crtpl_state->modified_create_infos = new_pipeline_create_infos; |
| 605 | crtpl_state->pCreateInfos = reinterpret_cast<VkRayTracingPipelineCreateInfoKHR *>(crtpl_state->modified_create_infos.data()); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | template <typename CreateInfos, typename SafeCreateInfos> |
| 609 | static void UtilCopyCreatePipelineFeedbackData(const uint32_t count, CreateInfos *pCreateInfos, SafeCreateInfos *pSafeCreateInfos) { |
| 610 | for (uint32_t i = 0; i < count; i++) { |
| 611 | auto src_feedback_struct = LvlFindInChain<VkPipelineCreationFeedbackCreateInfoEXT>(pSafeCreateInfos[i].pNext); |
| 612 | if (!src_feedback_struct) return; |
| 613 | auto dst_feedback_struct = const_cast<VkPipelineCreationFeedbackCreateInfoEXT *>( |
| 614 | LvlFindInChain<VkPipelineCreationFeedbackCreateInfoEXT>(pCreateInfos[i].pNext)); |
| 615 | *dst_feedback_struct->pPipelineCreationFeedback = *src_feedback_struct->pPipelineCreationFeedback; |
| 616 | for (uint32_t j = 0; j < src_feedback_struct->pipelineStageCreationFeedbackCount; j++) { |
| 617 | dst_feedback_struct->pPipelineStageCreationFeedbacks[j] = src_feedback_struct->pPipelineStageCreationFeedbacks[j]; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | void GpuAssistedBase::PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 623 | const VkGraphicsPipelineCreateInfo *pCreateInfos, |
| 624 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 625 | VkResult result, void *cgpl_state_data) { |
| 626 | ValidationStateTracker::PostCallRecordCreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator, |
| 627 | pPipelines, result, cgpl_state_data); |
| 628 | if (aborted) return; |
| 629 | create_graphics_pipeline_api_state *cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state *>(cgpl_state_data); |
Tony-LunarG | 806cddb | 2022-05-11 15:32:48 -0600 | [diff] [blame] | 630 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, cgpl_state->modified_create_infos.data()); |
Tony-LunarG | abe71bd | 2022-05-11 15:58:16 -0600 | [diff] [blame^] | 631 | PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 632 | cgpl_state->modified_create_infos.data()); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void GpuAssistedBase::PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 636 | const VkComputePipelineCreateInfo *pCreateInfos, |
| 637 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 638 | VkResult result, void *ccpl_state_data) { |
| 639 | ValidationStateTracker::PostCallRecordCreateComputePipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines, |
| 640 | result, ccpl_state_data); |
| 641 | if (aborted) return; |
| 642 | create_compute_pipeline_api_state *ccpl_state = reinterpret_cast<create_compute_pipeline_api_state *>(ccpl_state_data); |
Tony-LunarG | 806cddb | 2022-05-11 15:32:48 -0600 | [diff] [blame] | 643 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, ccpl_state->modified_create_infos.data()); |
Tony-LunarG | abe71bd | 2022-05-11 15:58:16 -0600 | [diff] [blame^] | 644 | PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_COMPUTE, |
| 645 | ccpl_state->modified_create_infos.data()); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | void GpuAssistedBase::PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 649 | const VkRayTracingPipelineCreateInfoNV *pCreateInfos, |
| 650 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 651 | VkResult result, void *crtpl_state_data) { |
| 652 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data); |
| 653 | ValidationStateTracker::PostCallRecordCreateRayTracingPipelinesNV(device, pipelineCache, count, pCreateInfos, pAllocator, |
| 654 | pPipelines, result, crtpl_state_data); |
| 655 | if (aborted) return; |
Tony-LunarG | 806cddb | 2022-05-11 15:32:48 -0600 | [diff] [blame] | 656 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, crtpl_state->modified_create_infos.data()); |
Tony-LunarG | abe71bd | 2022-05-11 15:58:16 -0600 | [diff] [blame^] | 657 | PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_RAY_TRACING_NV, |
| 658 | crtpl_state->modified_create_infos.data()); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | void GpuAssistedBase::PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation, |
| 662 | VkPipelineCache pipelineCache, uint32_t count, |
| 663 | const VkRayTracingPipelineCreateInfoKHR *pCreateInfos, |
| 664 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 665 | VkResult result, void *crtpl_state_data) { |
| 666 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data); |
| 667 | ValidationStateTracker::PostCallRecordCreateRayTracingPipelinesKHR( |
| 668 | device, deferredOperation, pipelineCache, count, pCreateInfos, pAllocator, pPipelines, result, crtpl_state_data); |
| 669 | if (aborted) return; |
Tony-LunarG | 806cddb | 2022-05-11 15:32:48 -0600 | [diff] [blame] | 670 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, crtpl_state->modified_create_infos.data()); |
Tony-LunarG | abe71bd | 2022-05-11 15:58:16 -0600 | [diff] [blame^] | 671 | PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, |
| 672 | crtpl_state->modified_create_infos.data()); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // Remove all the shader trackers associated with this destroyed pipeline. |
| 676 | void GpuAssistedBase::PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) { |
Jeremy Gebben | 58cc9d1 | 2022-03-23 17:04:57 -0600 | [diff] [blame] | 677 | auto to_erase = shader_map.snapshot([pipeline](const GpuAssistedShaderTracker &entry) { return entry.pipeline == pipeline; }); |
| 678 | for (const auto &entry : to_erase) { |
| 679 | shader_map.erase(entry.first); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 680 | } |
| 681 | ValidationStateTracker::PreCallRecordDestroyPipeline(device, pipeline, pAllocator); |
| 682 | } |
| 683 | |
| 684 | template <typename CreateInfo> |
| 685 | struct CreatePipelineTraits {}; |
| 686 | template <> |
| 687 | struct CreatePipelineTraits<VkGraphicsPipelineCreateInfo> { |
| 688 | using SafeType = safe_VkGraphicsPipelineCreateInfo; |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 689 | static uint32_t GetStageCount(const VkGraphicsPipelineCreateInfo &createInfo) { return createInfo.stageCount; } |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 690 | static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) { |
| 691 | createInfo->pStages[stage].module = shader_module; |
| 692 | } |
| 693 | }; |
| 694 | |
| 695 | template <> |
| 696 | struct CreatePipelineTraits<VkComputePipelineCreateInfo> { |
| 697 | using SafeType = safe_VkComputePipelineCreateInfo; |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 698 | static uint32_t GetStageCount(const VkComputePipelineCreateInfo &createInfo) { return 1; } |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 699 | static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) { |
| 700 | assert(stage == 0); |
| 701 | createInfo->stage.module = shader_module; |
| 702 | } |
| 703 | }; |
| 704 | |
| 705 | template <> |
| 706 | struct CreatePipelineTraits<VkRayTracingPipelineCreateInfoNV> { |
| 707 | using SafeType = safe_VkRayTracingPipelineCreateInfoCommon; |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 708 | static uint32_t GetStageCount(const VkRayTracingPipelineCreateInfoNV &createInfo) { return createInfo.stageCount; } |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 709 | static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) { |
| 710 | createInfo->pStages[stage].module = shader_module; |
| 711 | } |
| 712 | }; |
| 713 | |
| 714 | template <> |
| 715 | struct CreatePipelineTraits<VkRayTracingPipelineCreateInfoKHR> { |
| 716 | using SafeType = safe_VkRayTracingPipelineCreateInfoCommon; |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 717 | static uint32_t GetStageCount(const VkRayTracingPipelineCreateInfoKHR &createInfo) { return createInfo.stageCount; } |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 718 | static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) { |
| 719 | createInfo->pStages[stage].module = shader_module; |
| 720 | } |
| 721 | }; |
| 722 | |
| 723 | // Examine the pipelines to see if they use the debug descriptor set binding index. |
| 724 | // If any do, create new non-instrumented shader modules and use them to replace the instrumented |
| 725 | // shaders in the pipeline. Return the (possibly) modified create infos to the caller. |
| 726 | template <typename CreateInfo, typename SafeCreateInfo> |
| 727 | void GpuAssistedBase::PreCallRecordPipelineCreations(uint32_t count, const CreateInfo *pCreateInfos, |
| 728 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 729 | std::vector<std::shared_ptr<PIPELINE_STATE>> &pipe_state, |
| 730 | std::vector<SafeCreateInfo> *new_pipeline_create_infos, |
| 731 | const VkPipelineBindPoint bind_point) { |
| 732 | using Accessor = CreatePipelineTraits<CreateInfo>; |
| 733 | if (bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS && bind_point != VK_PIPELINE_BIND_POINT_COMPUTE && |
| 734 | bind_point != VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) { |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | // Walk through all the pipelines, make a copy of each and flag each pipeline that contains a shader that uses the debug |
| 739 | // descriptor set index. |
| 740 | for (uint32_t pipeline = 0; pipeline < count; ++pipeline) { |
| 741 | uint32_t stageCount = Accessor::GetStageCount(pCreateInfos[pipeline]); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 742 | const auto &pipe = pipe_state[pipeline]; |
Nathaniel Cesario | d4d8fce | 2022-05-06 15:01:10 -0600 | [diff] [blame] | 743 | new_pipeline_create_infos->push_back(pipe->GetCreateInfo<CreateInfo>()); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 744 | |
| 745 | if (!pipe->IsGraphicsLibrary()) { |
| 746 | bool replace_shaders = false; |
| 747 | if (pipe->active_slots.find(desc_set_bind_index) != pipe->active_slots.end()) { |
| 748 | replace_shaders = true; |
| 749 | } |
| 750 | // If the app requests all available sets, the pipeline layout was not modified at pipeline layout creation and the |
| 751 | // already instrumented shaders need to be replaced with uninstrumented shaders |
| 752 | const auto pipeline_layout = pipe->PipelineLayoutState(); |
| 753 | if (pipeline_layout->set_layouts.size() >= adjusted_max_desc_sets) { |
| 754 | replace_shaders = true; |
| 755 | } |
| 756 | |
| 757 | if (replace_shaders) { |
| 758 | for (uint32_t stage = 0; stage < stageCount; ++stage) { |
Nathaniel Cesario | d4d8fce | 2022-05-06 15:01:10 -0600 | [diff] [blame] | 759 | const auto module_state = Get<SHADER_MODULE_STATE>(pipe->GetShaderModuleByCIIndex<CreateInfo>(stage)); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 760 | |
| 761 | VkShaderModule shader_module; |
| 762 | auto create_info = LvlInitStruct<VkShaderModuleCreateInfo>(); |
| 763 | create_info.pCode = module_state->words.data(); |
| 764 | create_info.codeSize = module_state->words.size() * sizeof(uint32_t); |
| 765 | VkResult result = DispatchCreateShaderModule(device, &create_info, pAllocator, &shader_module); |
| 766 | if (result == VK_SUCCESS) { |
| 767 | Accessor::SetShaderModule(&(*new_pipeline_create_infos)[pipeline], shader_module, stage); |
| 768 | } else { |
| 769 | ReportSetupProblem(device, |
| 770 | "Unable to replace instrumented shader with non-instrumented one. " |
| 771 | "Device could become unstable."); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | // For every pipeline: |
| 779 | // - For every shader in a pipeline: |
| 780 | // - If the shader had to be replaced in PreCallRecord (because the pipeline is using the debug desc set index): |
| 781 | // - Destroy it since it has been bound into the pipeline by now. This is our only chance to delete it. |
| 782 | // - Track the shader in the shader_map |
| 783 | // - Save the shader binary if it contains debug code |
Tony-LunarG | abe71bd | 2022-05-11 15:58:16 -0600 | [diff] [blame^] | 784 | template <typename CreateInfo, typename SafeCreateInfo> |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 785 | void GpuAssistedBase::PostCallRecordPipelineCreations(const uint32_t count, const CreateInfo *pCreateInfos, |
| 786 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
Tony-LunarG | abe71bd | 2022-05-11 15:58:16 -0600 | [diff] [blame^] | 787 | const VkPipelineBindPoint bind_point, const SafeCreateInfo *pModifiedCreateInfos) { |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 788 | if (bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS && bind_point != VK_PIPELINE_BIND_POINT_COMPUTE && |
| 789 | bind_point != VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) { |
| 790 | return; |
| 791 | } |
| 792 | for (uint32_t pipeline = 0; pipeline < count; ++pipeline) { |
| 793 | auto pipeline_state = Get<PIPELINE_STATE>(pPipelines[pipeline]); |
| 794 | if (!pipeline_state || pipeline_state->IsGraphicsLibrary()) continue; |
| 795 | |
| 796 | const uint32_t stageCount = static_cast<uint32_t>(pipeline_state->stage_state.size()); |
| 797 | assert(stageCount > 0); |
| 798 | |
| 799 | for (uint32_t stage = 0; stage < stageCount; ++stage) { |
| 800 | if (pipeline_state->active_slots.find(desc_set_bind_index) != pipeline_state->active_slots.end()) { |
Nathaniel Cesario | d4d8fce | 2022-05-06 15:01:10 -0600 | [diff] [blame] | 801 | DispatchDestroyShaderModule(device, pipeline_state->GetShaderModuleByCIIndex<CreateInfo>(stage), pAllocator); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 802 | } |
| 803 | |
Nathaniel Cesario | d4d8fce | 2022-05-06 15:01:10 -0600 | [diff] [blame] | 804 | assert((bind_point != VK_PIPELINE_BIND_POINT_COMPUTE) || (stage == 0)); |
| 805 | auto shader_module = pipeline_state->GetShaderModuleByCIIndex<CreateInfo>(stage); |
| 806 | auto module_state = Get<SHADER_MODULE_STATE>(shader_module); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 807 | |
| 808 | std::vector<unsigned int> code; |
| 809 | // Save the shader binary |
| 810 | // The core_validation ShaderModule tracker saves the binary too, but discards it when the ShaderModule |
| 811 | // is destroyed. Applications may destroy ShaderModules after they are placed in a pipeline and before |
| 812 | // the pipeline is used, so we have to keep another copy. |
| 813 | if (module_state && module_state->has_valid_spirv) code = module_state->words; |
| 814 | |
Jeremy Gebben | 58cc9d1 | 2022-03-23 17:04:57 -0600 | [diff] [blame] | 815 | shader_map.insert_or_assign(module_state->gpu_validation_shader_id, pipeline_state->pipeline(), shader_module, |
| 816 | std::move(code)); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 821 | // Generate the stage-specific part of the message. |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 822 | void UtilGenerateStageMessage(const uint32_t *debug_record, std::string &msg) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 823 | using namespace spvtools; |
| 824 | std::ostringstream strm; |
| 825 | switch (debug_record[kInstCommonOutStageIdx]) { |
| 826 | case spv::ExecutionModelVertex: { |
| 827 | strm << "Stage = Vertex. Vertex Index = " << debug_record[kInstVertOutVertexIndex] |
| 828 | << " Instance Index = " << debug_record[kInstVertOutInstanceIndex] << ". "; |
| 829 | } break; |
| 830 | case spv::ExecutionModelTessellationControl: { |
| 831 | strm << "Stage = Tessellation Control. Invocation ID = " << debug_record[kInstTessCtlOutInvocationId] |
| 832 | << ", Primitive ID = " << debug_record[kInstTessCtlOutPrimitiveId]; |
| 833 | } break; |
| 834 | case spv::ExecutionModelTessellationEvaluation: { |
| 835 | strm << "Stage = Tessellation Eval. Primitive ID = " << debug_record[kInstTessEvalOutPrimitiveId] |
| 836 | << ", TessCoord (u, v) = (" << debug_record[kInstTessEvalOutTessCoordU] << ", " |
| 837 | << debug_record[kInstTessEvalOutTessCoordV] << "). "; |
| 838 | } break; |
| 839 | case spv::ExecutionModelGeometry: { |
| 840 | strm << "Stage = Geometry. Primitive ID = " << debug_record[kInstGeomOutPrimitiveId] |
| 841 | << " Invocation ID = " << debug_record[kInstGeomOutInvocationId] << ". "; |
| 842 | } break; |
| 843 | case spv::ExecutionModelFragment: { |
| 844 | strm << "Stage = Fragment. Fragment coord (x,y) = (" |
| 845 | << *reinterpret_cast<const float *>(&debug_record[kInstFragOutFragCoordX]) << ", " |
| 846 | << *reinterpret_cast<const float *>(&debug_record[kInstFragOutFragCoordY]) << "). "; |
| 847 | } break; |
| 848 | case spv::ExecutionModelGLCompute: { |
| 849 | strm << "Stage = Compute. Global invocation ID (x, y, z) = (" << debug_record[kInstCompOutGlobalInvocationIdX] << ", " |
| 850 | << debug_record[kInstCompOutGlobalInvocationIdY] << ", " << debug_record[kInstCompOutGlobalInvocationIdZ] << " )"; |
| 851 | } break; |
| 852 | case spv::ExecutionModelRayGenerationNV: { |
| 853 | strm << "Stage = Ray Generation. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 854 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 855 | } break; |
| 856 | case spv::ExecutionModelIntersectionNV: { |
| 857 | strm << "Stage = Intersection. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 858 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 859 | } break; |
| 860 | case spv::ExecutionModelAnyHitNV: { |
| 861 | strm << "Stage = Any Hit. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 862 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 863 | } break; |
| 864 | case spv::ExecutionModelClosestHitNV: { |
| 865 | strm << "Stage = Closest Hit. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 866 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 867 | } break; |
| 868 | case spv::ExecutionModelMissNV: { |
| 869 | strm << "Stage = Miss. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 870 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 871 | } break; |
| 872 | case spv::ExecutionModelCallableNV: { |
| 873 | strm << "Stage = Callable. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", " |
| 874 | << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). "; |
| 875 | } break; |
Tony-LunarG | c7ed208 | 2020-06-11 14:00:04 -0600 | [diff] [blame] | 876 | case spv::ExecutionModelTaskNV: { |
| 877 | strm << "Stage = Task. Global invocation ID (x, y, z) = (" << debug_record[kInstTaskOutGlobalInvocationIdX] << ", " |
| 878 | << debug_record[kInstTaskOutGlobalInvocationIdY] << ", " << debug_record[kInstTaskOutGlobalInvocationIdZ] << " )"; |
| 879 | } break; |
| 880 | case spv::ExecutionModelMeshNV: { |
| 881 | strm << "Stage = Mesh.Global invocation ID (x, y, z) = (" << debug_record[kInstMeshOutGlobalInvocationIdX] << ", " |
| 882 | << debug_record[kInstMeshOutGlobalInvocationIdY] << ", " << debug_record[kInstMeshOutGlobalInvocationIdZ] << " )"; |
| 883 | } break; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 884 | default: { |
| 885 | strm << "Internal Error (unexpected stage = " << debug_record[kInstCommonOutStageIdx] << "). "; |
| 886 | assert(false); |
| 887 | } break; |
| 888 | } |
| 889 | msg = strm.str(); |
| 890 | } |
| 891 | |
| 892 | std::string LookupDebugUtilsName(const debug_report_data *report_data, const uint64_t object) { |
| 893 | auto object_label = report_data->DebugReportGetUtilsObjectName(object); |
| 894 | if (object_label != "") { |
| 895 | object_label = "(" + object_label + ")"; |
| 896 | } |
| 897 | return object_label; |
| 898 | } |
| 899 | |
| 900 | // Generate message from the common portion of the debug report record. |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 901 | void UtilGenerateCommonMessage(const debug_report_data *report_data, const VkCommandBuffer commandBuffer, |
| 902 | const uint32_t *debug_record, const VkShaderModule shader_module_handle, |
| 903 | const VkPipeline pipeline_handle, const VkPipelineBindPoint pipeline_bind_point, |
| 904 | const uint32_t operation_index, std::string &msg) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 905 | using namespace spvtools; |
| 906 | std::ostringstream strm; |
| 907 | if (shader_module_handle == VK_NULL_HANDLE) { |
| 908 | strm << std::hex << std::showbase << "Internal Error: Unable to locate information for shader used in command buffer " |
| 909 | << LookupDebugUtilsName(report_data, HandleToUint64(commandBuffer)) << "(" << HandleToUint64(commandBuffer) << "). "; |
| 910 | assert(true); |
| 911 | } else { |
| 912 | strm << std::hex << std::showbase << "Command buffer " << LookupDebugUtilsName(report_data, HandleToUint64(commandBuffer)) |
| 913 | << "(" << HandleToUint64(commandBuffer) << "). "; |
| 914 | if (pipeline_bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) { |
| 915 | strm << "Draw "; |
| 916 | } else if (pipeline_bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) { |
| 917 | strm << "Compute "; |
| 918 | } else if (pipeline_bind_point == VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) { |
| 919 | strm << "Ray Trace "; |
| 920 | } else { |
| 921 | assert(false); |
| 922 | strm << "Unknown Pipeline Operation "; |
| 923 | } |
| 924 | strm << "Index " << operation_index << ". " |
| 925 | << "Pipeline " << LookupDebugUtilsName(report_data, HandleToUint64(pipeline_handle)) << "(" |
| 926 | << HandleToUint64(pipeline_handle) << "). " |
| 927 | << "Shader Module " << LookupDebugUtilsName(report_data, HandleToUint64(shader_module_handle)) << "(" |
| 928 | << HandleToUint64(shader_module_handle) << "). "; |
| 929 | } |
| 930 | strm << std::dec << std::noshowbase; |
| 931 | strm << "Shader Instruction Index = " << debug_record[kInstCommonOutInstructionIdx] << ". "; |
| 932 | msg = strm.str(); |
| 933 | } |
| 934 | |
| 935 | // Read the contents of the SPIR-V OpSource instruction and any following continuation instructions. |
| 936 | // 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] | 937 | void ReadOpSource(const SHADER_MODULE_STATE &module_state, const uint32_t reported_file_id, |
| 938 | std::vector<std::string> &opsource_lines) { |
| 939 | for (auto insn : module_state) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 940 | if ((insn.opcode() == spv::OpSource) && (insn.len() >= 5) && (insn.word(3) == reported_file_id)) { |
| 941 | std::istringstream in_stream; |
| 942 | std::string cur_line; |
| 943 | in_stream.str((char *)&insn.word(4)); |
| 944 | while (std::getline(in_stream, cur_line)) { |
| 945 | opsource_lines.push_back(cur_line); |
| 946 | } |
| 947 | while ((++insn).opcode() == spv::OpSourceContinued) { |
| 948 | in_stream.str((char *)&insn.word(1)); |
| 949 | while (std::getline(in_stream, cur_line)) { |
| 950 | opsource_lines.push_back(cur_line); |
| 951 | } |
| 952 | } |
| 953 | break; |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | // The task here is to search the OpSource content to find the #line directive with the |
| 959 | // line number that is closest to, but still prior to the reported error line number and |
| 960 | // still within the reported filename. |
| 961 | // From this known position in the OpSource content we can add the difference between |
| 962 | // the #line line number and the reported error line number to determine the location |
| 963 | // in the OpSource content of the reported error line. |
| 964 | // |
| 965 | // Considerations: |
| 966 | // - Look only at #line directives that specify the reported_filename since |
| 967 | // the reported error line number refers to its location in the reported filename. |
| 968 | // - If a #line directive does not have a filename, the file is the reported filename, or |
| 969 | // the filename found in a prior #line directive. (This is C-preprocessor behavior) |
| 970 | // - It is possible (e.g., inlining) for blocks of code to get shuffled out of their |
| 971 | // original order and the #line directives are used to keep the numbering correct. This |
| 972 | // is why we need to examine the entire contents of the source, instead of leaving early |
| 973 | // when finding a #line line number larger than the reported error line number. |
| 974 | // |
| 975 | |
| 976 | // GCC 4.8 has a problem with std::regex that is fixed in GCC 4.9. Provide fallback code for 4.8 |
| 977 | #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) |
| 978 | |
| 979 | #if defined(__GNUC__) && GCC_VERSION < 40900 |
| 980 | bool GetLineAndFilename(const std::string string, uint32_t *linenumber, std::string &filename) { |
| 981 | // # line <linenumber> "<filename>" or |
| 982 | // #line <linenumber> "<filename>" |
| 983 | std::vector<std::string> tokens; |
| 984 | std::stringstream stream(string); |
| 985 | std::string temp; |
| 986 | uint32_t line_index = 0; |
| 987 | |
| 988 | while (stream >> temp) tokens.push_back(temp); |
| 989 | auto size = tokens.size(); |
| 990 | if (size > 1) { |
| 991 | if (tokens[0] == "#" && tokens[1] == "line") { |
| 992 | line_index = 2; |
| 993 | } else if (tokens[0] == "#line") { |
| 994 | line_index = 1; |
| 995 | } |
| 996 | } |
| 997 | if (0 == line_index) return false; |
Mark Young | 0ec6b06 | 2020-11-19 15:32:17 -0700 | [diff] [blame] | 998 | *linenumber = static_cast<uint32_t>(std::stoul(tokens[line_index])); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 999 | uint32_t filename_index = line_index + 1; |
| 1000 | // Remove enclosing double quotes around filename |
| 1001 | if (size > filename_index) filename = tokens[filename_index].substr(1, tokens[filename_index].size() - 2); |
| 1002 | return true; |
| 1003 | } |
| 1004 | #else |
| 1005 | bool GetLineAndFilename(const std::string string, uint32_t *linenumber, std::string &filename) { |
| 1006 | static const std::regex line_regex( // matches #line directives |
| 1007 | "^" // beginning of line |
| 1008 | "\\s*" // optional whitespace |
| 1009 | "#" // required text |
| 1010 | "\\s*" // optional whitespace |
| 1011 | "line" // required text |
| 1012 | "\\s+" // required whitespace |
| 1013 | "([0-9]+)" // required first capture - line number |
| 1014 | "(\\s+)?" // optional second capture - whitespace |
| 1015 | "(\".+\")?" // optional third capture - quoted filename with at least one char inside |
| 1016 | ".*"); // rest of line (needed when using std::regex_match since the entire line is tested) |
| 1017 | |
| 1018 | std::smatch captures; |
| 1019 | |
| 1020 | bool found_line = std::regex_match(string, captures, line_regex); |
| 1021 | if (!found_line) return false; |
| 1022 | |
| 1023 | // filename is optional and considered found only if the whitespace and the filename are captured |
| 1024 | if (captures[2].matched && captures[3].matched) { |
| 1025 | // Remove enclosing double quotes. The regex guarantees the quotes and at least one char. |
| 1026 | filename = captures[3].str().substr(1, captures[3].str().size() - 2); |
| 1027 | } |
Artem Bolgar | 82d0836 | 2021-06-03 13:11:13 -0700 | [diff] [blame] | 1028 | *linenumber = (uint32_t)std::stoul(captures[1]); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1029 | return true; |
| 1030 | } |
| 1031 | #endif // GCC_VERSION |
| 1032 | |
| 1033 | // Extract the filename, line number, and column number from the correct OpLine and build a message string from it. |
| 1034 | // 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] | 1035 | 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] | 1036 | std::string &filename_msg, std::string &source_msg) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1037 | using namespace spvtools; |
| 1038 | std::ostringstream filename_stream; |
| 1039 | std::ostringstream source_stream; |
Nathaniel Cesario | 77cd59b | 2021-10-11 23:52:24 -0600 | [diff] [blame] | 1040 | SHADER_MODULE_STATE shader(pgm); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1041 | // Find the OpLine just before the failing instruction indicated by the debug info. |
| 1042 | // SPIR-V can only be iterated in the forward direction due to its opcode/length encoding. |
| 1043 | uint32_t instruction_index = 0; |
| 1044 | uint32_t reported_file_id = 0; |
| 1045 | uint32_t reported_line_number = 0; |
| 1046 | uint32_t reported_column_number = 0; |
| 1047 | if (shader.words.size() > 0) { |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1048 | for (const auto &insn : shader) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1049 | if (insn.opcode() == spv::OpLine) { |
| 1050 | reported_file_id = insn.word(1); |
| 1051 | reported_line_number = insn.word(2); |
| 1052 | reported_column_number = insn.word(3); |
| 1053 | } |
| 1054 | if (instruction_index == debug_record[kInstCommonOutInstructionIdx]) { |
| 1055 | break; |
| 1056 | } |
| 1057 | instruction_index++; |
| 1058 | } |
| 1059 | } |
| 1060 | // Create message with file information obtained from the OpString pointed to by the discovered OpLine. |
| 1061 | std::string reported_filename; |
| 1062 | if (reported_file_id == 0) { |
| 1063 | filename_stream |
| 1064 | << "Unable to find SPIR-V OpLine for source information. Build shader with debug info to get source information."; |
| 1065 | } else { |
| 1066 | bool found_opstring = false; |
| 1067 | std::string prefix; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1068 | if (from_printf) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1069 | prefix = "Debug shader printf message generated "; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1070 | } else { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1071 | prefix = "Shader validation error occurred "; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1072 | } |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1073 | for (const auto &insn : shader) { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1074 | if ((insn.opcode() == spv::OpString) && (insn.len() >= 3) && (insn.word(1) == reported_file_id)) { |
| 1075 | found_opstring = true; |
| 1076 | reported_filename = (char *)&insn.word(2); |
| 1077 | if (reported_filename.empty()) { |
| 1078 | filename_stream << prefix << "at line " << reported_line_number; |
| 1079 | } else { |
| 1080 | filename_stream << prefix << "in file " << reported_filename << " at line " << reported_line_number; |
| 1081 | } |
| 1082 | if (reported_column_number > 0) { |
| 1083 | filename_stream << ", column " << reported_column_number; |
| 1084 | } |
| 1085 | filename_stream << "."; |
| 1086 | break; |
| 1087 | } |
| 1088 | } |
| 1089 | if (!found_opstring) { |
Tony-LunarG | 6d195e1 | 2020-10-27 16:54:14 -0600 | [diff] [blame] | 1090 | filename_stream << "Unable to find SPIR-V OpString for file id " << reported_file_id << " from OpLine instruction." |
| 1091 | << std::endl; |
| 1092 | filename_stream << "File ID = " << reported_file_id << ", Line Number = " << reported_line_number |
| 1093 | << ", Column = " << reported_column_number << std::endl; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | filename_msg = filename_stream.str(); |
| 1097 | |
| 1098 | // Create message to display source code line containing error. |
| 1099 | if ((reported_file_id != 0)) { |
| 1100 | // Read the source code and split it up into separate lines. |
| 1101 | std::vector<std::string> opsource_lines; |
| 1102 | ReadOpSource(shader, reported_file_id, opsource_lines); |
| 1103 | // Find the line in the OpSource content that corresponds to the reported error file and line. |
| 1104 | if (!opsource_lines.empty()) { |
| 1105 | uint32_t saved_line_number = 0; |
| 1106 | std::string current_filename = reported_filename; // current "preprocessor" filename state. |
| 1107 | std::vector<std::string>::size_type saved_opsource_offset = 0; |
| 1108 | bool found_best_line = false; |
| 1109 | for (auto it = opsource_lines.begin(); it != opsource_lines.end(); ++it) { |
| 1110 | uint32_t parsed_line_number; |
| 1111 | std::string parsed_filename; |
| 1112 | bool found_line = GetLineAndFilename(*it, &parsed_line_number, parsed_filename); |
| 1113 | if (!found_line) continue; |
| 1114 | |
| 1115 | bool found_filename = parsed_filename.size() > 0; |
| 1116 | if (found_filename) { |
| 1117 | current_filename = parsed_filename; |
| 1118 | } |
| 1119 | if ((!found_filename) || (current_filename == reported_filename)) { |
| 1120 | // Update the candidate best line directive, if the current one is prior and closer to the reported line |
| 1121 | if (reported_line_number >= parsed_line_number) { |
| 1122 | if (!found_best_line || |
| 1123 | (reported_line_number - parsed_line_number <= reported_line_number - saved_line_number)) { |
| 1124 | saved_line_number = parsed_line_number; |
| 1125 | saved_opsource_offset = std::distance(opsource_lines.begin(), it); |
| 1126 | found_best_line = true; |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | if (found_best_line) { |
| 1132 | assert(reported_line_number >= saved_line_number); |
| 1133 | std::vector<std::string>::size_type opsource_index = |
| 1134 | (reported_line_number - saved_line_number) + 1 + saved_opsource_offset; |
| 1135 | if (opsource_index < opsource_lines.size()) { |
| 1136 | source_stream << "\n" << reported_line_number << ": " << opsource_lines[opsource_index].c_str(); |
| 1137 | } else { |
| 1138 | source_stream << "Internal error: calculated source line of " << opsource_index << " for source size of " |
| 1139 | << opsource_lines.size() << " lines."; |
| 1140 | } |
| 1141 | } else { |
| 1142 | source_stream << "Unable to find suitable #line directive in SPIR-V OpSource."; |
| 1143 | } |
| 1144 | } else { |
| 1145 | source_stream << "Unable to find SPIR-V OpSource."; |
| 1146 | } |
| 1147 | } |
| 1148 | source_msg = source_stream.str(); |
| 1149 | } |