blob: 1f39b62bff8a8a6b77093d33b8f59dece754075c [file] [log] [blame]
Karl Schultz7b024b42018-08-30 16:18:18 -06001/* 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 Lobodzinskib56bbb92019-02-18 11:49:59 -070023#include "chassis.h"
Karl Schultz7b024b42018-08-30 16:18:18 -060024#include "core_validation.h"
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070025#include "gpu_validation.h"
Karl Schultz7b024b42018-08-30 16:18:18 -060026#include "shader_validation.h"
Karl Schultz7b024b42018-08-30 16:18:18 -060027#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.
35static const uint32_t kNumBindingsInSet = 1;
36
37// Implementation for Device Memory Manager class
Mark Lobodzinski586d10e2019-03-08 18:19:48 -070038GpuDeviceMemoryManager::GpuDeviceMemoryManager(CoreChecks *dev_data, uint32_t data_size) {
Mark Lobodzinski5c048802019-03-07 10:47:31 -070039 uint32_t align = static_cast<uint32_t>(dev_data->GetPDProperties()->limits.minStorageBufferOffsetAlignment);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070040 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
52GpuDeviceMemoryManager::~GpuDeviceMemoryManager() {
53 for (auto &chunk : chunk_list_) {
54 FreeMemoryChunk(chunk);
55 }
56 chunk_list_.clear();
57}
58
Karl Schultz7b024b42018-08-30 16:18:18 -060059VkResult 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
96void GpuDeviceMemoryManager::PutBackBlock(VkBuffer buffer, VkDeviceMemory memory, uint32_t offset) {
97 GpuDeviceMemoryBlock block = {buffer, memory, offset};
98 PutBackBlock(block);
99}
100
101void 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
117void ResetBlock(GpuDeviceMemoryBlock &block) {
118 block.buffer = VK_NULL_HANDLE;
119 block.memory = VK_NULL_HANDLE;
120 block.offset = 0;
121}
122
123bool BlockUsed(GpuDeviceMemoryBlock &block) { return (block.buffer != VK_NULL_HANDLE) && (block.memory != VK_NULL_HANDLE); }
124
125bool GpuDeviceMemoryManager::MemoryTypeFromProperties(uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
126 // Search memtypes to find first index with those properties
Mark Lobodzinski5c048802019-03-07 10:47:31 -0700127 const VkPhysicalDeviceMemoryProperties *props = dev_data_->GetPhysicalDeviceMemoryProperties();
Karl Schultz7b024b42018-08-30 16:18:18 -0600128 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
142VkResult 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 Schultz7b024b42018-08-30 16:18:18 -0600151
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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600155 result = DispatchCreateBuffer(dev_data_, dev_data_->device, &buffer_create_info, NULL, &buffer);
Karl Schultz7b024b42018-08-30 16:18:18 -0600156 if (result != VK_SUCCESS) {
157 return result;
158 }
159
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600160 DispatchGetBufferMemoryRequirements(dev_data_, dev_data_->device, buffer, &mem_reqs);
Karl Schultz7b024b42018-08-30 16:18:18 -0600161
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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600169 DispatchDestroyBuffer(dev_data_, dev_data_->device, buffer, NULL);
Karl Schultz7b024b42018-08-30 16:18:18 -0600170 return result;
171 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600172 result = DispatchAllocateMemory(dev_data_, dev_data_->device, &mem_alloc, NULL, &memory);
Karl Schultz7b024b42018-08-30 16:18:18 -0600173 if (result != VK_SUCCESS) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600174 DispatchDestroyBuffer(dev_data_, dev_data_->device, buffer, NULL);
Karl Schultz7b024b42018-08-30 16:18:18 -0600175 return result;
176 }
177
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600178 result = DispatchBindBufferMemory(dev_data_, dev_data_->device, buffer, memory, 0);
Karl Schultz7b024b42018-08-30 16:18:18 -0600179 if (result != VK_SUCCESS) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600180 DispatchDestroyBuffer(dev_data_, dev_data_->device, buffer, NULL);
181 DispatchFreeMemory(dev_data_, dev_data_->device, memory, NULL);
Karl Schultz7b024b42018-08-30 16:18:18 -0600182 return result;
183 }
184
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600185 result = DispatchMapMemory(dev_data_, dev_data_->device, memory, 0, mem_alloc.allocationSize, 0, &pData);
Karl Schultz7b024b42018-08-30 16:18:18 -0600186 if (result == VK_SUCCESS) {
187 memset(pData, 0, chunk_size_);
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600188 DispatchUnmapMemory(dev_data_, dev_data_->device, memory);
Karl Schultz7b024b42018-08-30 16:18:18 -0600189 } else {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600190 DispatchDestroyBuffer(dev_data_, dev_data_->device, buffer, NULL);
191 DispatchFreeMemory(dev_data_, dev_data_->device, memory, NULL);
Karl Schultz7b024b42018-08-30 16:18:18 -0600192 return result;
193 }
194 chunk.buffer = buffer;
195 chunk.memory = memory;
196 return result;
197}
198
199void GpuDeviceMemoryManager::FreeMemoryChunk(MemoryChunk &chunk) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600200 DispatchDestroyBuffer(dev_data_, dev_data_->device, chunk.buffer, NULL);
201 DispatchFreeMemory(dev_data_, dev_data_->device, chunk.memory, NULL);
Karl Schultz7b024b42018-08-30 16:18:18 -0600202}
203
Tony-LunarGd85808d2019-02-27 16:12:02 -0700204void GpuDeviceMemoryManager::FreeAllBlocks() {
205 for (auto &chunk : chunk_list_) {
206 FreeMemoryChunk(chunk);
207 }
208 chunk_list_.clear();
209}
210
Karl Schultz7b024b42018-08-30 16:18:18 -0600211// Implementation for Descriptor Set Manager class
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700212GpuDescriptorSetManager::GpuDescriptorSetManager(CoreChecks *dev_data) { dev_data_ = dev_data; }
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700213
214GpuDescriptorSetManager::~GpuDescriptorSetManager() {
215 for (auto &pool : desc_pool_map_) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600216 DispatchDestroyDescriptorPool(dev_data_, dev_data_->device, pool.first, NULL);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700217 }
218 desc_pool_map_.clear();
219}
220
Karl Schultz7b024b42018-08-30 16:18:18 -0600221VkResult GpuDescriptorSetManager::GetDescriptorSets(uint32_t count, VkDescriptorPool *pool,
222 std::vector<VkDescriptorSet> *desc_sets) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600223 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600255 result = DispatchCreateDescriptorPool(dev_data_, dev_data_->device, &desc_pool_info, NULL, &pool_to_use);
Karl Schultz7b024b42018-08-30 16:18:18 -0600256 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600263 std::vector<VkDescriptorSetLayout> desc_layouts(count, dev_data_->gpu_validation_state->debug_desc_layout);
Karl Schultz7b024b42018-08-30 16:18:18 -0600264
265 VkDescriptorSetAllocateInfo alloc_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, NULL, pool_to_use, count,
266 desc_layouts.data()};
267
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600268 result = DispatchAllocateDescriptorSets(dev_data_, dev_data_->device, &alloc_info, desc_sets->data());
Karl Schultz7b024b42018-08-30 16:18:18 -0600269 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
278void 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600281 VkResult result = DispatchFreeDescriptorSets(dev_data_, dev_data_->device, desc_pool, 1, &desc_set);
Karl Schultz7b024b42018-08-30 16:18:18 -0600282 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600288 DispatchDestroyDescriptorPool(dev_data_, dev_data_->device, desc_pool, NULL);
Karl Schultz7b024b42018-08-30 16:18:18 -0600289 desc_pool_map_.erase(desc_pool);
290 }
291 }
292 return;
293}
294
Tony-LunarGd85808d2019-02-27 16:12:02 -0700295void GpuDescriptorSetManager::DestroyDescriptorPools() {
296 for (auto &pool : desc_pool_map_) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600297 DispatchDestroyDescriptorPool(dev_data_, dev_data_->device, pool.first, NULL);
Tony-LunarGd85808d2019-02-27 16:12:02 -0700298 }
299 desc_pool_map_.clear();
300}
301
Karl Schultz7b024b42018-08-30 16:18:18 -0600302// Convenience function for reporting problems with setting up GPU Validation.
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700303void CoreChecks::ReportSetupProblem(VkDebugReportObjectTypeEXT object_type, uint64_t object_handle,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700304 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 Schultz7b024b42018-08-30 16:18:18 -0600307}
308
309// Turn on necessary device features.
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700310void CoreChecks::GpuPreCallRecordCreateDevice(VkPhysicalDevice gpu, std::unique_ptr<safe_VkDeviceCreateInfo> &create_info,
311 VkPhysicalDeviceFeatures *supported_features) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600312 if (supported_features->fragmentStoresAndAtomics || supported_features->vertexPipelineStoresAndAtomics) {
Tony-LunarG48b478a2019-01-15 16:35:22 -0700313 VkPhysicalDeviceFeatures new_features = {};
Mark Lobodzinski5eb3c262019-03-01 16:08:30 -0700314 if (create_info->pEnabledFeatures) {
315 new_features = *create_info->pEnabledFeatures;
Tony-LunarG48b478a2019-01-15 16:35:22 -0700316 }
Karl Schultz7b024b42018-08-30 16:18:18 -0600317 new_features.fragmentStoresAndAtomics = supported_features->fragmentStoresAndAtomics;
318 new_features.vertexPipelineStoresAndAtomics = supported_features->vertexPipelineStoresAndAtomics;
Mark Lobodzinski5eb3c262019-03-01 16:08:30 -0700319 delete create_info->pEnabledFeatures;
320 create_info->pEnabledFeatures = new VkPhysicalDeviceFeatures(new_features);
Karl Schultz7b024b42018-08-30 16:18:18 -0600321 }
Karl Schultz7b024b42018-08-30 16:18:18 -0600322}
323
324// Perform initializations that can be done at Create Device time.
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600325void CoreChecks::GpuPostCallRecordCreateDevice(const CHECK_ENABLED *enables) {
326 gpu_validation_state = std::unique_ptr<GpuValidationState>(new GpuValidationState);
Karl Schultz7b024b42018-08-30 16:18:18 -0600327
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600328 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-LunarG65f9c492019-01-17 14:24:42 -0700333
Mark Lobodzinski5c048802019-03-07 10:47:31 -0700334 if (GetPDProperties()->apiVersion < VK_API_VERSION_1_1) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600335 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device),
Karl Schultz7b024b42018-08-30 16:18:18 -0600336 "GPU-Assisted validation requires Vulkan 1.1 or later. GPU-Assisted Validation disabled.");
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600337 gpu_validation_state->aborted = true;
Karl Schultz7b024b42018-08-30 16:18:18 -0600338 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600342 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 Schultz7b024b42018-08-30 16:18:18 -0600344
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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600347 if (gpu_validation_state->adjusted_max_desc_sets == 1) {
348 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device),
Karl Schultz7b024b42018-08-30 16:18:18 -0600349 "Device can bind only a single descriptor set. GPU-Assisted Validation disabled.");
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600350 gpu_validation_state->aborted = true;
Karl Schultz7b024b42018-08-30 16:18:18 -0600351 return;
352 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600353 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 Schultz7b024b42018-08-30 16:18:18 -0600357
358 std::unique_ptr<GpuDeviceMemoryManager> memory_manager(
Mark Lobodzinski3bf82a52019-03-11 11:49:34 -0600359 new GpuDeviceMemoryManager(this, sizeof(uint32_t) * (spvtools::kInstMaxOutCnt + 1)));
360 std::unique_ptr<GpuDescriptorSetManager> desc_set_manager(new GpuDescriptorSetManager(this));
Karl Schultz7b024b42018-08-30 16:18:18 -0600361
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 Lobodzinski155d7632019-03-07 11:20:42 -0700379 VkResult result =
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600380 DispatchCreateDescriptorSetLayout(this, device, &debug_desc_layout_info, NULL, &gpu_validation_state->debug_desc_layout);
Karl Schultz7b024b42018-08-30 16:18:18 -0600381
382 // This is a layout used to "pad" a pipeline layout to fill in any gaps to the selected bind index.
Mark Lobodzinski155d7632019-03-07 11:20:42 -0700383 VkResult result2 =
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600384 DispatchCreateDescriptorSetLayout(this, device, &dummy_desc_layout_info, NULL, &gpu_validation_state->dummy_desc_layout);
Karl Schultz7b024b42018-08-30 16:18:18 -0600385 assert((result == VK_SUCCESS) && (result2 == VK_SUCCESS));
386 if ((result != VK_SUCCESS) || (result2 != VK_SUCCESS)) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600387 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device),
Karl Schultz7b024b42018-08-30 16:18:18 -0600388 "Unable to create descriptor set layout. GPU-Assisted Validation disabled.");
389 if (result == VK_SUCCESS) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600390 DispatchDestroyDescriptorSetLayout(this, device, gpu_validation_state->debug_desc_layout, NULL);
Karl Schultz7b024b42018-08-30 16:18:18 -0600391 }
392 if (result2 == VK_SUCCESS) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600393 DispatchDestroyDescriptorSetLayout(this, device, gpu_validation_state->dummy_desc_layout, NULL);
Karl Schultz7b024b42018-08-30 16:18:18 -0600394 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600395 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 Schultz7b024b42018-08-30 16:18:18 -0600398 return;
399 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600400 gpu_validation_state->memory_manager = std::move(memory_manager);
401 gpu_validation_state->desc_set_manager = std::move(desc_set_manager);
Karl Schultz7b024b42018-08-30 16:18:18 -0600402}
403
404// Clean up device-related resources
Mark Lobodzinski70f00652019-03-07 15:22:47 -0700405void CoreChecks::GpuPreCallRecordDestroyDevice() {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600406 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 Schultz58674242019-01-22 15:35:02 -0700410 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600411 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 Schultz58674242019-01-22 15:35:02 -0700414 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600415 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 Schultz7b024b42018-08-30 16:18:18 -0600418 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600419 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 Schultz7b024b42018-08-30 16:18:18 -0600422 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600423 gpu_validation_state->memory_manager->FreeAllBlocks();
424 gpu_validation_state->desc_set_manager->DestroyDescriptorPools();
Karl Schultz7b024b42018-08-30 16:18:18 -0600425}
426
Karl Schultz7b024b42018-08-30 16:18:18 -0600427// Modify the pipeline layout to include our debug descriptor set and any needed padding with the dummy descriptor set.
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700428bool CoreChecks::GpuPreCallCreatePipelineLayout(const VkPipelineLayoutCreateInfo *pCreateInfo,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700429 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
430 std::vector<VkDescriptorSetLayout> *new_layouts,
431 VkPipelineLayoutCreateInfo *modified_create_info) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600432 if (gpu_validation_state->aborted) {
Mark Lobodzinskiff7d8002019-02-13 13:01:26 -0700433 return false;
Karl Schultz7b024b42018-08-30 16:18:18 -0600434 }
Mark Lobodzinskiff7d8002019-02-13 13:01:26 -0700435
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600436 if (modified_create_info->setLayoutCount >= gpu_validation_state->adjusted_max_desc_sets) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600437 std::ostringstream strm;
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600438 strm << "Pipeline Layout conflict with validation's descriptor set at slot " << gpu_validation_state->desc_set_bind_index
439 << ". "
Karl Schultz7b024b42018-08-30 16:18:18 -0600440 << "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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600443 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), strm.str().c_str());
Karl Schultz7b024b42018-08-30 16:18:18 -0600444 } 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600449 new_layouts->reserve(gpu_validation_state->adjusted_max_desc_sets);
Mark Lobodzinskiff7d8002019-02-13 13:01:26 -0700450 new_layouts->insert(new_layouts->end(), &pCreateInfo->pSetLayouts[0],
451 &pCreateInfo->pSetLayouts[pCreateInfo->setLayoutCount]);
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600452 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 Schultz7b024b42018-08-30 16:18:18 -0600454 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600455 new_layouts->push_back(gpu_validation_state->debug_desc_layout);
Mark Lobodzinskiff7d8002019-02-13 13:01:26 -0700456 modified_create_info->pSetLayouts = new_layouts->data();
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600457 modified_create_info->setLayoutCount = gpu_validation_state->adjusted_max_desc_sets;
Karl Schultz7b024b42018-08-30 16:18:18 -0600458 }
Mark Lobodzinskiff7d8002019-02-13 13:01:26 -0700459 return true;
460}
461
462// Clean up GPU validation after the CreatePipelineLayout call is made
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700463void CoreChecks::GpuPostCallCreatePipelineLayout(VkResult result) {
Mark Lobodzinskiff7d8002019-02-13 13:01:26 -0700464 // Clean up GPU validation
Karl Schultz7b024b42018-08-30 16:18:18 -0600465 if (result != VK_SUCCESS) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600466 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device),
Karl Schultz7b024b42018-08-30 16:18:18 -0600467 "Unable to create pipeline layout. Device could become unstable.");
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600468 gpu_validation_state->aborted = true;
Karl Schultz7b024b42018-08-30 16:18:18 -0600469 }
Karl Schultz7b024b42018-08-30 16:18:18 -0600470}
471
Karl Schultz7b024b42018-08-30 16:18:18 -0600472// Free the device memory and descriptor set associated with a command buffer.
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700473void CoreChecks::GpuPreCallRecordFreeCommandBuffers(uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600474 if (gpu_validation_state->aborted) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600475 return;
476 }
477 for (uint32_t i = 0; i < commandBufferCount; ++i) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600478 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-LunarGb2501d22019-01-28 09:59:13 -0700483 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600484 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 Schultz7b024b42018-08-30 16:18:18 -0600487 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600488 gpu_buffer_list.clear();
Karl Schultz7b024b42018-08-30 16:18:18 -0600489 }
490}
491
492// Just gives a warning about a possible deadlock.
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700493void CoreChecks::GpuPreCallValidateCmdWaitEvents(VkPipelineStageFlags sourceStageMask) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600494 if (sourceStageMask & VK_PIPELINE_STAGE_HOST_BIT) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600495 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device),
Karl Schultz7b024b42018-08-30 16:18:18 -0600496 "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 Lobodzinskib56bbb92019-02-18 11:49:59 -0700505std::vector<safe_VkGraphicsPipelineCreateInfo> CoreChecks::GpuPreCallRecordCreateGraphicsPipelines(
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700506 VkPipelineCache pipelineCache, uint32_t count, const VkGraphicsPipelineCreateInfo *pCreateInfos,
Karl Schultz7b024b42018-08-30 16:18:18 -0600507 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, std::vector<std::unique_ptr<PIPELINE_STATE>> &pipe_state) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600508 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600515 if (pipe_state[pipeline]->active_slots.find(gpu_validation_state->desc_set_bind_index) !=
516 pipe_state[pipeline]->active_slots.end()) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600517 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 Lobodzinski9e9da292019-03-06 16:19:55 -0700532 const shader_module *shader = GetShaderModuleState(pCreateInfos[pipeline].pStages[stage].module);
Karl Schultz7b024b42018-08-30 16:18:18 -0600533 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600538 VkResult result = DispatchCreateShaderModule(this, device, &create_info, pAllocator, &shader_module);
Karl Schultz7b024b42018-08-30 16:18:18 -0600539 if (result == VK_SUCCESS) {
540 new_pipeline_create_infos[pipeline].pStages[stage].module = shader_module;
541 } else {
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700542 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
Karl Schultz7b024b42018-08-30 16:18:18 -0600543 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 Lobodzinski586d10e2019-03-08 18:19:48 -0700559void CoreChecks::GpuPostCallRecordCreateGraphicsPipelines(const uint32_t count, const VkGraphicsPipelineCreateInfo *pCreateInfos,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700560 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600561 for (uint32_t pipeline = 0; pipeline < count; ++pipeline) {
Mark Lobodzinskifd917d22019-03-06 16:07:15 -0700562 auto pipeline_state = GetPipelineState(pPipelines[pipeline]);
Karl Schultz7b024b42018-08-30 16:18:18 -0600563 if (nullptr == pipeline_state) continue;
564 for (uint32_t stage = 0; stage < pipeline_state->graphicsPipelineCI.stageCount; ++stage) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600565 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 Schultz7b024b42018-08-30 16:18:18 -0600568 }
Mark Lobodzinski9e9da292019-03-06 16:19:55 -0700569 auto shader_state = GetShaderModuleState(pipeline_state->graphicsPipelineCI.pStages[stage].module);
Karl Schultz7b024b42018-08-30 16:18:18 -0600570 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600583 gpu_validation_state->shader_map[shader_state->gpu_validation_shader_id].pipeline = pipeline_state->pipeline;
Karl Schultz7b024b42018-08-30 16:18:18 -0600584 // 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600586 gpu_validation_state->shader_map[shader_state->gpu_validation_shader_id].shader_module =
Karl Schultz7b024b42018-08-30 16:18:18 -0600587 pipeline_state->graphicsPipelineCI.pStages[stage].module;
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600588 gpu_validation_state->shader_map[shader_state->gpu_validation_shader_id].pgm = std::move(code);
Karl Schultz7b024b42018-08-30 16:18:18 -0600589 }
590 }
591}
592
593// Remove all the shader trackers associated with this destroyed pipeline.
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700594void CoreChecks::GpuPreCallRecordDestroyPipeline(const VkPipeline pipeline) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600595 for (auto it = gpu_validation_state->shader_map.begin(); it != gpu_validation_state->shader_map.end();) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600596 if (it->second.pipeline == pipeline) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600597 it = gpu_validation_state->shader_map.erase(it);
Karl Schultz7b024b42018-08-30 16:18:18 -0600598 } else {
599 ++it;
600 }
601 }
602}
603
604// Call the SPIR-V Optimizer to run the instrumentation pass on the shader.
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700605bool CoreChecks::GpuInstrumentShader(const VkShaderModuleCreateInfo *pCreateInfo, std::vector<unsigned int> &new_pgm,
606 uint32_t *unique_shader_id) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600607 if (gpu_validation_state->aborted) return false;
Karl Schultz7b024b42018-08-30 16:18:18 -0600608 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600621 optimizer.RegisterPass(
622 CreateInstBindlessCheckPass(gpu_validation_state->desc_set_bind_index, gpu_validation_state->unique_shader_module_id));
Karl Schultz7b024b42018-08-30 16:18:18 -0600623 optimizer.RegisterPass(CreateAggressiveDCEPass());
624 bool pass = optimizer.Run(new_pgm.data(), new_pgm.size(), &new_pgm);
625 if (!pass) {
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700626 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, VK_NULL_HANDLE,
Karl Schultz7b024b42018-08-30 16:18:18 -0600627 "Failure to instrument shader. Proceeding with non-instrumented shader.");
628 }
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600629 *unique_shader_id = gpu_validation_state->unique_shader_module_id++;
Karl Schultz7b024b42018-08-30 16:18:18 -0600630 return pass;
631}
632
Mark Lobodzinski01734072019-02-13 17:39:15 -0700633// Create the instrumented shader data to provide to the driver.
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700634bool CoreChecks::GpuPreCallCreateShaderModule(const VkShaderModuleCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
635 VkShaderModule *pShaderModule, uint32_t *unique_shader_id,
636 VkShaderModuleCreateInfo *instrumented_create_info,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700637 std::vector<unsigned int> *instrumented_pgm) {
Mark Lobodzinski586d10e2019-03-08 18:19:48 -0700638 bool pass = GpuInstrumentShader(pCreateInfo, *instrumented_pgm, unique_shader_id);
Karl Schultz7b024b42018-08-30 16:18:18 -0600639 if (pass) {
Mark Lobodzinski01734072019-02-13 17:39:15 -0700640 instrumented_create_info->pCode = instrumented_pgm->data();
641 instrumented_create_info->codeSize = instrumented_pgm->size() * sizeof(unsigned int);
Karl Schultz7b024b42018-08-30 16:18:18 -0600642 }
Mark Lobodzinski01734072019-02-13 17:39:15 -0700643 return pass;
Karl Schultz7b024b42018-08-30 16:18:18 -0600644}
645
646// Generate the stage-specific part of the message.
647static 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-LunarG6ff87582019-02-08 10:29:07 -0700652 strm << "Stage = Vertex. Vertex Index = " << debug_record[kInstVertOutVertexIndex]
653 << " Instance Index = " << debug_record[kInstVertOutInstanceIndex] << ". ";
Karl Schultz7b024b42018-08-30 16:18:18 -0600654 } 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.
682static 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-LunarGc1d657d2019-02-22 14:55:19 -0700689 vuid_msg = "UNASSIGNED-Descriptor index out of bounds";
Karl Schultz7b024b42018-08-30 16:18:18 -0600690 } break;
691 case 1: {
Karl Schultz7b024b42018-08-30 16:18:18 -0600692 strm << "Descriptor index " << debug_record[kInstBindlessOutDescIndex] << " is uninitialized. ";
Tony-LunarGc1d657d2019-02-22 14:55:19 -0700693 vuid_msg = "UNASSIGNED-Descriptor uninitialized";
Karl Schultz7b024b42018-08-30 16:18:18 -0600694 } 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 Lobodzinskib56bbb92019-02-18 11:49:59 -0700704static std::string LookupDebugUtilsName(const debug_report_data *report_data, const uint64_t object) {
Mark Lobodzinski1d7313a2019-02-07 11:04:42 -0700705 auto object_label = report_data->DebugReportGetUtilsObjectName(object);
706 if (object_label != "") {
707 object_label = "(" + object_label + ")";
Karl Schultz7b024b42018-08-30 16:18:18 -0600708 }
Mark Lobodzinski1d7313a2019-02-07 11:04:42 -0700709 return object_label;
Karl Schultz7b024b42018-08-30 16:18:18 -0600710}
711
712// Generate message from the common portion of the debug report record.
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700713static void GenerateCommonMessage(const debug_report_data *report_data, const GLOBAL_CB_NODE *cb_node, const uint32_t *debug_record,
Tony-LunarGd589c7c2019-01-31 11:23:44 -0700714 const VkShaderModule shader_module_handle, const VkPipeline pipeline_handle,
715 const uint32_t draw_index, std::string &msg) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600716 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 Lobodzinskib56bbb92019-02-18 11:49:59 -0700720 << LookupDebugUtilsName(report_data, HandleToUint64(cb_node->commandBuffer)) << "("
Karl Schultz7b024b42018-08-30 16:18:18 -0600721 << HandleToUint64(cb_node->commandBuffer) << "). ";
722 assert(true);
723 } else {
724 strm << std::hex << std::showbase << "Command buffer "
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700725 << LookupDebugUtilsName(report_data, HandleToUint64(cb_node->commandBuffer)) << "("
Karl Schultz7b024b42018-08-30 16:18:18 -0600726 << HandleToUint64(cb_node->commandBuffer) << "). "
Tony-LunarGd589c7c2019-01-31 11:23:44 -0700727 << "Draw Index " << draw_index << ". "
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700728 << "Pipeline " << LookupDebugUtilsName(report_data, HandleToUint64(pipeline_handle)) << "("
Karl Schultz7b024b42018-08-30 16:18:18 -0600729 << HandleToUint64(pipeline_handle) << "). "
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700730 << "Shader Module " << LookupDebugUtilsName(report_data, HandleToUint64(shader_module_handle)) << "("
Karl Schultz7b024b42018-08-30 16:18:18 -0600731 << 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.
740static 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-LunarG03059b72019-02-19 13:57:41 -0700759
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-LunarG16f9b7e2019-02-19 13:02:03 -0700782static bool GetLineAndFilename(const std::string string, uint32_t *linenumber, std::string &filename) {
Tony-LunarG03059b72019-02-19 13:57:41 -0700783 // # 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
807static bool GetLineAndFilename(const std::string string, uint32_t *linenumber, std::string &filename) {
Tony-LunarG16f9b7e2019-02-19 13:02:03 -0700808 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 Schultz7b024b42018-08-30 16:18:18 -0600819
Tony-LunarG16f9b7e2019-02-19 13:02:03 -0700820 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-LunarG03059b72019-02-19 13:57:41 -0700833#endif // GCC_VERSION
834
Karl Schultz7b024b42018-08-30 16:18:18 -0600835// 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.
837static 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 Schultz7b024b42018-08-30 16:18:18 -0600900 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-LunarG16f9b7e2019-02-19 13:02:03 -0700905 uint32_t parsed_line_number;
906 std::string parsed_filename;
907 bool found_line = GetLineAndFilename(*it, &parsed_line_number, parsed_filename);
Karl Schultz7b024b42018-08-30 16:18:18 -0600908 if (!found_line) continue;
Tony-LunarG16f9b7e2019-02-19 13:02:03 -0700909
910 bool found_filename = parsed_filename.size() > 0;
Karl Schultz7b024b42018-08-30 16:18:18 -0600911 if (found_filename) {
Tony-LunarG16f9b7e2019-02-19 13:02:03 -0700912 current_filename = parsed_filename;
Karl Schultz7b024b42018-08-30 16:18:18 -0600913 }
914 if ((!found_filename) || (current_filename == reported_filename)) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600915 // 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 Lobodzinskie377ac32019-03-07 16:12:46 -0700953void CoreChecks::AnalyzeAndReportError(GLOBAL_CB_NODE *cb_node, VkQueue queue, uint32_t draw_index,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -0700954 uint32_t *const debug_output_buffer) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600955 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 Schultz7b024b42018-08-30 16:18:18 -0600971 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600984 auto it = gpu_validation_state->shader_map.find(debug_record[kInstCommonOutShaderId]);
985 if (it != gpu_validation_state->shader_map.end()) {
Karl Schultz7b024b42018-08-30 16:18:18 -0600986 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 Lobodzinskib56bbb92019-02-18 11:49:59 -0700992 GenerateCommonMessage(report_data, cb_node, debug_record, shader_module_handle, pipeline_handle, draw_index, common_message);
Karl Schultz7b024b42018-08-30 16:18:18 -0600993 GenerateSourceMessages(pgm, debug_record, filename_message, source_message);
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600994 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT, HandleToUint64(queue),
Karl Schultz7b024b42018-08-30 16:18:18 -0600995 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-LunarG5ad17272019-03-05 12:48:24 -07001003// For the given command buffer, map its debug data buffers and read their contents for analysis.
Mark Lobodzinskie377ac32019-03-07 16:12:46 -07001004void CoreChecks::ProcessInstrumentationBuffer(VkQueue queue, GLOBAL_CB_NODE *cb_node) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001005 auto gpu_buffer_list = gpu_validation_state->GetGpuBufferInfo(cb_node->commandBuffer);
1006 if (cb_node && cb_node->hasDrawCmd && gpu_buffer_list.size() > 0) {
Karl Schultz7b024b42018-08-30 16:18:18 -06001007 VkResult result;
1008 char *pData;
Tony-LunarG5ad17272019-03-05 12:48:24 -07001009 uint32_t draw_index = 0;
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001010 for (auto &buffer_info : gpu_buffer_list) {
Tony-LunarGb2501d22019-01-28 09:59:13 -07001011 uint32_t block_offset = buffer_info.mem_block.offset;
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001012 uint32_t block_size = gpu_validation_state->memory_manager->GetBlockSize();
Tony-LunarGb2501d22019-01-28 09:59:13 -07001013 uint32_t offset_to_data = 0;
Mark Lobodzinski5c048802019-03-07 10:47:31 -07001014 const uint32_t map_align = std::max(1U, static_cast<uint32_t>(GetPDProperties()->limits.minMemoryMapAlignment));
Karl Schultz7b024b42018-08-30 16:18:18 -06001015
Tony-LunarGb2501d22019-01-28 09:59:13 -07001016 // 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 Lobodzinskie514d1a2019-03-12 08:47:45 -06001020 result = DispatchMapMemory(this, cb_node->device, buffer_info.mem_block.memory, block_offset, block_size, 0,
1021 (void **)&pData);
Tony-LunarGb2501d22019-01-28 09:59:13 -07001022 // Analyze debug output buffer
1023 if (result == VK_SUCCESS) {
Mark Lobodzinskie377ac32019-03-07 16:12:46 -07001024 AnalyzeAndReportError(cb_node, queue, draw_index, (uint32_t *)(pData + offset_to_data));
Mark Lobodzinskie514d1a2019-03-12 08:47:45 -06001025 DispatchUnmapMemory(this, cb_node->device, buffer_info.mem_block.memory);
Tony-LunarGb2501d22019-01-28 09:59:13 -07001026 }
Tony-LunarGd589c7c2019-01-31 11:23:44 -07001027 draw_index++;
Karl Schultz7b024b42018-08-30 16:18:18 -06001028 }
1029 }
1030}
1031
Karl Schultz58674242019-01-22 15:35:02 -07001032// Submit a memory barrier on graphics queues.
1033// Lazy-create and record the needed command buffer.
Mark Lobodzinskie377ac32019-03-07 16:12:46 -07001034void CoreChecks::SubmitBarrier(VkQueue queue) {
Karl Schultz58674242019-01-22 15:35:02 -07001035 uint32_t queue_family_index = 0;
1036
Mark Lobodzinskie377ac32019-03-07 16:12:46 -07001037 auto it = queueMap.find(queue);
1038 if (it != queueMap.end()) {
Karl Schultz58674242019-01-22 15:35:02 -07001039 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 Lobodzinskib56bbb92019-02-18 11:49:59 -07001044 VkQueueFlags queue_flags = GetPhysicalDeviceState()->queue_family_properties[queue_family_index].queueFlags;
Karl Schultz58674242019-01-22 15:35:02 -07001045 if (!(queue_flags & VK_QUEUE_GRAPHICS_BIT)) {
1046 return;
1047 }
1048
1049 // Lazy-allocate and record the command buffer.
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001050 if (gpu_validation_state->barrier_command_buffer == VK_NULL_HANDLE) {
Karl Schultz58674242019-01-22 15:35:02 -07001051 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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001055 result = DispatchCreateCommandPool(this, device, &pool_create_info, nullptr, &gpu_validation_state->barrier_command_pool);
Karl Schultz58674242019-01-22 15:35:02 -07001056 if (result != VK_SUCCESS) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001057 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device),
Karl Schultz58674242019-01-22 15:35:02 -07001058 "Unable to create command pool for barrier CB.");
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001059 gpu_validation_state->barrier_command_pool = VK_NULL_HANDLE;
Karl Schultz58674242019-01-22 15:35:02 -07001060 return;
1061 }
1062
1063 VkCommandBufferAllocateInfo command_buffer_alloc_info = {};
1064 command_buffer_alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001065 command_buffer_alloc_info.commandPool = gpu_validation_state->barrier_command_pool;
Karl Schultz58674242019-01-22 15:35:02 -07001066 command_buffer_alloc_info.commandBufferCount = 1;
1067 command_buffer_alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001068 result =
1069 DispatchAllocateCommandBuffers(this, device, &command_buffer_alloc_info, &gpu_validation_state->barrier_command_buffer);
Karl Schultz58674242019-01-22 15:35:02 -07001070 if (result != VK_SUCCESS) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001071 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device),
Karl Schultz58674242019-01-22 15:35:02 -07001072 "Unable to create barrier command buffer.");
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001073 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 Schultz58674242019-01-22 15:35:02 -07001076 return;
1077 }
1078
1079 // Hook up command buffer dispatch
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001080 *((const void **)gpu_validation_state->barrier_command_buffer) = *(void **)(device);
Karl Schultz58674242019-01-22 15:35:02 -07001081
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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001085 result = DispatchBeginCommandBuffer(this, gpu_validation_state->barrier_command_buffer, &command_buffer_begin_info);
Karl Schultz58674242019-01-22 15:35:02 -07001086
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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001093 DispatchCmdPipelineBarrier(this, gpu_validation_state->barrier_command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
Mark Lobodzinskie514d1a2019-03-12 08:47:45 -06001094 VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &memory_barrier, 0, nullptr, 0, nullptr);
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001095 DispatchEndCommandBuffer(this, gpu_validation_state->barrier_command_buffer);
Karl Schultz58674242019-01-22 15:35:02 -07001096 }
1097 }
1098
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001099 if (gpu_validation_state->barrier_command_buffer) {
Karl Schultz58674242019-01-22 15:35:02 -07001100 VkSubmitInfo submit_info = {};
1101 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1102 submit_info.commandBufferCount = 1;
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001103 submit_info.pCommandBuffers = &gpu_validation_state->barrier_command_buffer;
Mark Lobodzinskie514d1a2019-03-12 08:47:45 -06001104 DispatchQueueSubmit(this, queue, 1, &submit_info, VK_NULL_HANDLE);
Karl Schultz58674242019-01-22 15:35:02 -07001105 }
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 Lobodzinskie377ac32019-03-07 16:12:46 -07001111void CoreChecks::GpuPostCallQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001112 if (gpu_validation_state->aborted) return;
Karl Schultz58674242019-01-22 15:35:02 -07001113
Mark Lobodzinskie377ac32019-03-07 16:12:46 -07001114 SubmitBarrier(queue);
Karl Schultz58674242019-01-22 15:35:02 -07001115
Mark Lobodzinskie514d1a2019-03-12 08:47:45 -06001116 DispatchQueueWaitIdle(this, queue);
Karl Schultz58674242019-01-22 15:35:02 -07001117
Karl Schultz7b024b42018-08-30 16:18:18 -06001118 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 Lobodzinski1b3a9712019-03-06 15:51:47 -07001121 auto cb_node = GetCBNode(submit->pCommandBuffers[i]);
Mark Lobodzinskie377ac32019-03-07 16:12:46 -07001122 ProcessInstrumentationBuffer(queue, cb_node);
Karl Schultz7b024b42018-08-30 16:18:18 -06001123 for (auto secondaryCmdBuffer : cb_node->linkedCommandBuffers) {
Mark Lobodzinskie377ac32019-03-07 16:12:46 -07001124 ProcessInstrumentationBuffer(queue, secondaryCmdBuffer);
Karl Schultz7b024b42018-08-30 16:18:18 -06001125 }
1126 }
1127 }
1128}
Tony-LunarGb2501d22019-01-28 09:59:13 -07001129
Mark Lobodzinski586d10e2019-03-08 18:19:48 -07001130void CoreChecks::GpuAllocateValidationResources(const VkCommandBuffer cmd_buffer, const VkPipelineBindPoint bind_point) {
Tony-LunarGb2501d22019-01-28 09:59:13 -07001131 VkResult result;
1132
Mark Lobodzinski44da62c2019-03-07 10:50:59 -07001133 if (!(GetEnables()->gpu_validation)) return;
Tony-LunarGb2501d22019-01-28 09:59:13 -07001134
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001135 if (gpu_validation_state->aborted) return;
Tony-LunarGb2501d22019-01-28 09:59:13 -07001136
1137 std::vector<VkDescriptorSet> desc_sets;
1138 VkDescriptorPool desc_pool = VK_NULL_HANDLE;
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001139 result = gpu_validation_state->desc_set_manager->GetDescriptorSets(1, &desc_pool, &desc_sets);
Tony-LunarGb2501d22019-01-28 09:59:13 -07001140 assert(result == VK_SUCCESS);
1141 if (result != VK_SUCCESS) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001142 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device),
Tony-LunarGb2501d22019-01-28 09:59:13 -07001143 "Unable to allocate descriptor sets. Device could become unstable.");
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001144 gpu_validation_state->aborted = true;
Tony-LunarGb2501d22019-01-28 09:59:13 -07001145 return;
1146 }
1147
1148 VkDescriptorBufferInfo desc_buffer_info = {};
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001149 desc_buffer_info.range = gpu_validation_state->memory_manager->GetBlockSize();
Tony-LunarGb2501d22019-01-28 09:59:13 -07001150
Mark Lobodzinski1b3a9712019-03-06 15:51:47 -07001151 auto cb_node = GetCBNode(cmd_buffer);
Tony-LunarGb2501d22019-01-28 09:59:13 -07001152 if (!cb_node) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001153 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), "Unrecognized command buffer");
1154 gpu_validation_state->aborted = true;
Tony-LunarGb2501d22019-01-28 09:59:13 -07001155 return;
1156 }
1157
1158 GpuDeviceMemoryBlock block = {};
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001159 result = gpu_validation_state->memory_manager->GetBlock(&block);
Tony-LunarGb2501d22019-01-28 09:59:13 -07001160 if (result != VK_SUCCESS) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001161 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device),
Tony-LunarGb2501d22019-01-28 09:59:13 -07001162 "Unable to allocate device memory. Device could become unstable.");
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001163 gpu_validation_state->aborted = true;
Tony-LunarGb2501d22019-01-28 09:59:13 -07001164 return;
1165 }
1166
1167 // Record buffer and memory info in CB state tracking
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001168 gpu_validation_state->GetGpuBufferInfo(cmd_buffer).emplace_back(block, desc_sets[0], desc_pool);
Tony-LunarGb2501d22019-01-28 09:59:13 -07001169
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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001180 DispatchUpdateDescriptorSets(this, device, 1, &desc_write, 0, NULL);
Tony-LunarGb2501d22019-01-28 09:59:13 -07001181
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 Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001185 if (pipeline_state && (pipeline_state->pipeline_layout.set_layouts.size() <= gpu_validation_state->desc_set_bind_index)) {
Mark Lobodzinskie514d1a2019-03-12 08:47:45 -06001186 DispatchCmdBindDescriptorSets(this, cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_state->pipeline_layout.layout,
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001187 gpu_validation_state->desc_set_bind_index, 1, desc_sets.data(), 0, nullptr);
Tony-LunarGb2501d22019-01-28 09:59:13 -07001188 }
1189 } else {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -06001190 ReportSetupProblem(VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, HandleToUint64(device), "Unable to find pipeline state");
1191 gpu_validation_state->aborted = true;
Tony-LunarGb2501d22019-01-28 09:59:13 -07001192 return;
1193 }
1194}