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