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