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