blob: 29191aa450113ae78d1fcce393cb5d5bfe10cbd1 [file] [log] [blame]
sfricke-samsungef15e482022-01-26 11:32:49 -08001/* Copyright (c) 2020-2022 The Khronos Group Inc.
2 * Copyright (c) 2020-2022 Valve Corporation
3 * Copyright (c) 2020-2022 LunarG, Inc.
Tony-LunarG1dce2392019-10-23 16:49:29 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: Tony Barbour <tony@lunarg.com>
18 */
19
Jeremy Gebben5160e032022-03-28 14:57:43 -060020#include "gpu_utils.h"
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060021#include "descriptor_sets.h"
Tony-LunarG1dce2392019-10-23 16:49:29 -060022#include "spirv-tools/libspirv.h"
23#include "spirv-tools/optimizer.hpp"
24#include "spirv-tools/instrument.hpp"
Mark Lobodzinski102687e2020-04-28 11:03:28 -060025#include <spirv/unified1/spirv.hpp>
Tony-LunarG1dce2392019-10-23 16:49:29 -060026#include <algorithm>
27#include <regex>
28
29#define VMA_IMPLEMENTATION
30// This define indicates that we will supply Vulkan function pointers at initialization
31#define VMA_STATIC_VULKAN_FUNCTIONS 0
32#include "vk_mem_alloc.h"
33
Tony-LunarG1dce2392019-10-23 16:49:29 -060034// Implementation for Descriptor Set Manager class
Tony-LunarGb5fae462020-03-05 12:43:25 -070035UtilDescriptorSetManager::UtilDescriptorSetManager(VkDevice device, uint32_t numBindingsInSet)
Tony-LunarG1dce2392019-10-23 16:49:29 -060036 : device(device), numBindingsInSet(numBindingsInSet) {}
37
Tony-LunarGb5fae462020-03-05 12:43:25 -070038UtilDescriptorSetManager::~UtilDescriptorSetManager() {
Tony-LunarG1dce2392019-10-23 16:49:29 -060039 for (auto &pool : desc_pool_map_) {
40 DispatchDestroyDescriptorPool(device, pool.first, NULL);
41 }
42 desc_pool_map_.clear();
43}
44
Tony-LunarGb5fae462020-03-05 12:43:25 -070045VkResult UtilDescriptorSetManager::GetDescriptorSet(VkDescriptorPool *desc_pool, VkDescriptorSetLayout ds_layout,
46 VkDescriptorSet *desc_set) {
Tony-LunarG1dce2392019-10-23 16:49:29 -060047 std::vector<VkDescriptorSet> desc_sets;
48 VkResult result = GetDescriptorSets(1, desc_pool, ds_layout, &desc_sets);
Jeremy Gebbenefd97802022-03-28 16:45:05 -060049 assert(result == VK_SUCCESS);
Tony-LunarG1dce2392019-10-23 16:49:29 -060050 if (result == VK_SUCCESS) {
51 *desc_set = desc_sets[0];
52 }
53 return result;
54}
55
Tony-LunarGb5fae462020-03-05 12:43:25 -070056VkResult UtilDescriptorSetManager::GetDescriptorSets(uint32_t count, VkDescriptorPool *pool, VkDescriptorSetLayout ds_layout,
57 std::vector<VkDescriptorSet> *desc_sets) {
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060058 auto guard = Lock();
Tony-LunarG1dce2392019-10-23 16:49:29 -060059 const uint32_t default_pool_size = kItemsPerChunk;
60 VkResult result = VK_SUCCESS;
61 VkDescriptorPool pool_to_use = VK_NULL_HANDLE;
62
Jeremy Gebbenefd97802022-03-28 16:45:05 -060063 assert(count > 0);
Tony-LunarG1dce2392019-10-23 16:49:29 -060064 if (0 == count) {
65 return result;
66 }
67 desc_sets->clear();
68 desc_sets->resize(count);
69
70 for (auto &pool : desc_pool_map_) {
71 if (pool.second.used + count < pool.second.size) {
72 pool_to_use = pool.first;
73 break;
74 }
75 }
76 if (VK_NULL_HANDLE == pool_to_use) {
77 uint32_t pool_count = default_pool_size;
78 if (count > default_pool_size) {
79 pool_count = count;
80 }
81 const VkDescriptorPoolSize size_counts = {
82 VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
83 pool_count * numBindingsInSet,
84 };
Nathaniel Cesariofc6291e2021-04-06 00:22:15 -060085 auto desc_pool_info = LvlInitStruct<VkDescriptorPoolCreateInfo>();
Tony-LunarG1dce2392019-10-23 16:49:29 -060086 desc_pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
87 desc_pool_info.maxSets = pool_count;
88 desc_pool_info.poolSizeCount = 1;
89 desc_pool_info.pPoolSizes = &size_counts;
90 result = DispatchCreateDescriptorPool(device, &desc_pool_info, NULL, &pool_to_use);
91 assert(result == VK_SUCCESS);
92 if (result != VK_SUCCESS) {
93 return result;
94 }
95 desc_pool_map_[pool_to_use].size = desc_pool_info.maxSets;
96 desc_pool_map_[pool_to_use].used = 0;
97 }
98 std::vector<VkDescriptorSetLayout> desc_layouts(count, ds_layout);
99
100 VkDescriptorSetAllocateInfo alloc_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, NULL, pool_to_use, count,
101 desc_layouts.data()};
102
103 result = DispatchAllocateDescriptorSets(device, &alloc_info, desc_sets->data());
104 assert(result == VK_SUCCESS);
105 if (result != VK_SUCCESS) {
106 return result;
107 }
108 *pool = pool_to_use;
109 desc_pool_map_[pool_to_use].used += count;
110 return result;
111}
112
Tony-LunarGb5fae462020-03-05 12:43:25 -0700113void UtilDescriptorSetManager::PutBackDescriptorSet(VkDescriptorPool desc_pool, VkDescriptorSet desc_set) {
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600114 auto guard = Lock();
Tony-LunarG1dce2392019-10-23 16:49:29 -0600115 auto iter = desc_pool_map_.find(desc_pool);
116 if (iter != desc_pool_map_.end()) {
117 VkResult result = DispatchFreeDescriptorSets(device, desc_pool, 1, &desc_set);
118 assert(result == VK_SUCCESS);
119 if (result != VK_SUCCESS) {
120 return;
121 }
122 desc_pool_map_[desc_pool].used--;
123 if (0 == desc_pool_map_[desc_pool].used) {
124 DispatchDestroyDescriptorPool(device, desc_pool, NULL);
125 desc_pool_map_.erase(desc_pool);
126 }
127 }
128 return;
129}
130
131// Trampolines to make VMA call Dispatch for Vulkan calls
132static VKAPI_ATTR void VKAPI_CALL gpuVkGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
133 VkPhysicalDeviceProperties *pProperties) {
134 DispatchGetPhysicalDeviceProperties(physicalDevice, pProperties);
135}
136static VKAPI_ATTR void VKAPI_CALL gpuVkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
137 VkPhysicalDeviceMemoryProperties *pMemoryProperties) {
138 DispatchGetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties);
139}
140static VKAPI_ATTR VkResult VKAPI_CALL gpuVkAllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo,
141 const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMemory) {
142 return DispatchAllocateMemory(device, pAllocateInfo, pAllocator, pMemory);
143}
144static VKAPI_ATTR void VKAPI_CALL gpuVkFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks *pAllocator) {
145 DispatchFreeMemory(device, memory, pAllocator);
146}
147static VKAPI_ATTR VkResult VKAPI_CALL gpuVkMapMemory(VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size,
148 VkMemoryMapFlags flags, void **ppData) {
149 return DispatchMapMemory(device, memory, offset, size, flags, ppData);
150}
151static VKAPI_ATTR void VKAPI_CALL gpuVkUnmapMemory(VkDevice device, VkDeviceMemory memory) { DispatchUnmapMemory(device, memory); }
152static VKAPI_ATTR VkResult VKAPI_CALL gpuVkFlushMappedMemoryRanges(VkDevice device, uint32_t memoryRangeCount,
153 const VkMappedMemoryRange *pMemoryRanges) {
154 return DispatchFlushMappedMemoryRanges(device, memoryRangeCount, pMemoryRanges);
155}
156static VKAPI_ATTR VkResult VKAPI_CALL gpuVkInvalidateMappedMemoryRanges(VkDevice device, uint32_t memoryRangeCount,
157 const VkMappedMemoryRange *pMemoryRanges) {
158 return DispatchInvalidateMappedMemoryRanges(device, memoryRangeCount, pMemoryRanges);
159}
160static VKAPI_ATTR VkResult VKAPI_CALL gpuVkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
161 VkDeviceSize memoryOffset) {
162 return DispatchBindBufferMemory(device, buffer, memory, memoryOffset);
163}
164static VKAPI_ATTR VkResult VKAPI_CALL gpuVkBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory,
165 VkDeviceSize memoryOffset) {
166 return DispatchBindImageMemory(device, image, memory, memoryOffset);
167}
168static VKAPI_ATTR void VKAPI_CALL gpuVkGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer,
169 VkMemoryRequirements *pMemoryRequirements) {
170 DispatchGetBufferMemoryRequirements(device, buffer, pMemoryRequirements);
171}
172static VKAPI_ATTR void VKAPI_CALL gpuVkGetImageMemoryRequirements(VkDevice device, VkImage image,
173 VkMemoryRequirements *pMemoryRequirements) {
174 DispatchGetImageMemoryRequirements(device, image, pMemoryRequirements);
175}
176static VKAPI_ATTR VkResult VKAPI_CALL gpuVkCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo,
177 const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer) {
178 return DispatchCreateBuffer(device, pCreateInfo, pAllocator, pBuffer);
179}
180static VKAPI_ATTR void VKAPI_CALL gpuVkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) {
181 return DispatchDestroyBuffer(device, buffer, pAllocator);
182}
183static VKAPI_ATTR VkResult VKAPI_CALL gpuVkCreateImage(VkDevice device, const VkImageCreateInfo *pCreateInfo,
184 const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
185 return DispatchCreateImage(device, pCreateInfo, pAllocator, pImage);
186}
187static VKAPI_ATTR void VKAPI_CALL gpuVkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
188 DispatchDestroyImage(device, image, pAllocator);
189}
190static VKAPI_ATTR void VKAPI_CALL gpuVkCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
191 uint32_t regionCount, const VkBufferCopy *pRegions) {
192 DispatchCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, regionCount, pRegions);
193}
194
Tony-LunarGb5fae462020-03-05 12:43:25 -0700195VkResult UtilInitializeVma(VkPhysicalDevice physical_device, VkDevice device, VmaAllocator *pAllocator) {
Tony-LunarG1dce2392019-10-23 16:49:29 -0600196 VmaVulkanFunctions functions;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700197 VmaAllocatorCreateInfo allocator_info = {};
198 allocator_info.device = device;
199 allocator_info.physicalDevice = physical_device;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600200
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700201 functions.vkGetPhysicalDeviceProperties = static_cast<PFN_vkGetPhysicalDeviceProperties>(gpuVkGetPhysicalDeviceProperties);
202 functions.vkGetPhysicalDeviceMemoryProperties =
203 static_cast<PFN_vkGetPhysicalDeviceMemoryProperties>(gpuVkGetPhysicalDeviceMemoryProperties);
204 functions.vkAllocateMemory = static_cast<PFN_vkAllocateMemory>(gpuVkAllocateMemory);
205 functions.vkFreeMemory = static_cast<PFN_vkFreeMemory>(gpuVkFreeMemory);
206 functions.vkMapMemory = static_cast<PFN_vkMapMemory>(gpuVkMapMemory);
207 functions.vkUnmapMemory = static_cast<PFN_vkUnmapMemory>(gpuVkUnmapMemory);
208 functions.vkFlushMappedMemoryRanges = static_cast<PFN_vkFlushMappedMemoryRanges>(gpuVkFlushMappedMemoryRanges);
209 functions.vkInvalidateMappedMemoryRanges = static_cast<PFN_vkInvalidateMappedMemoryRanges>(gpuVkInvalidateMappedMemoryRanges);
210 functions.vkBindBufferMemory = static_cast<PFN_vkBindBufferMemory>(gpuVkBindBufferMemory);
211 functions.vkBindImageMemory = static_cast<PFN_vkBindImageMemory>(gpuVkBindImageMemory);
212 functions.vkGetBufferMemoryRequirements = static_cast<PFN_vkGetBufferMemoryRequirements>(gpuVkGetBufferMemoryRequirements);
213 functions.vkGetImageMemoryRequirements = static_cast<PFN_vkGetImageMemoryRequirements>(gpuVkGetImageMemoryRequirements);
214 functions.vkCreateBuffer = static_cast<PFN_vkCreateBuffer>(gpuVkCreateBuffer);
215 functions.vkDestroyBuffer = static_cast<PFN_vkDestroyBuffer>(gpuVkDestroyBuffer);
216 functions.vkCreateImage = static_cast<PFN_vkCreateImage>(gpuVkCreateImage);
217 functions.vkDestroyImage = static_cast<PFN_vkDestroyImage>(gpuVkDestroyImage);
218 functions.vkCmdCopyBuffer = static_cast<PFN_vkCmdCopyBuffer>(gpuVkCmdCopyBuffer);
219 allocator_info.pVulkanFunctions = &functions;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600220
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700221 return vmaCreateAllocator(&allocator_info, pAllocator);
Tony-LunarG1dce2392019-10-23 16:49:29 -0600222}
223
Jeremy Gebben33717862022-03-28 15:53:56 -0600224void GpuAssistedBase::PreCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
225 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, void *modified_ci) {
226 ValidationStateTracker::PreCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, modified_ci);
Tony-LunarG1dce2392019-10-23 16:49:29 -0600227 VkPhysicalDeviceFeatures *features = nullptr;
Jeremy Gebben33717862022-03-28 15:53:56 -0600228 // Use a local variable to query features since this method runs in the instance validation object.
229 // To avoid confusion and race conditions about which physical device's features are stored in the
230 // 'supported_devices' member variable, it will only be set in the device validation objects.
231 // See CreateDevice() below.
232 VkPhysicalDeviceFeatures gpu_supported_features;
233 DispatchGetPhysicalDeviceFeatures(gpu, &gpu_supported_features);
234 auto modified_create_info = static_cast<VkDeviceCreateInfo *>(modified_ci);
Tony-LunarG1dce2392019-10-23 16:49:29 -0600235 if (modified_create_info->pEnabledFeatures) {
236 // If pEnabledFeatures, VkPhysicalDeviceFeatures2 in pNext chain is not allowed
237 features = const_cast<VkPhysicalDeviceFeatures *>(modified_create_info->pEnabledFeatures);
238 } else {
239 VkPhysicalDeviceFeatures2 *features2 = nullptr;
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700240 features2 = const_cast<VkPhysicalDeviceFeatures2 *>(LvlFindInChain<VkPhysicalDeviceFeatures2>(modified_create_info->pNext));
Tony-LunarG1dce2392019-10-23 16:49:29 -0600241 if (features2) features = &features2->features;
242 }
Tony-LunarGf0634eb2021-01-05 15:11:12 -0700243 VkPhysicalDeviceFeatures new_features = {};
244 VkBool32 *desired = reinterpret_cast<VkBool32 *>(&desired_features);
245 VkBool32 *feature_ptr;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600246 if (features) {
Tony-LunarGf0634eb2021-01-05 15:11:12 -0700247 feature_ptr = reinterpret_cast<VkBool32 *>(features);
Tony-LunarG1dce2392019-10-23 16:49:29 -0600248 } else {
Tony-LunarGf0634eb2021-01-05 15:11:12 -0700249 feature_ptr = reinterpret_cast<VkBool32 *>(&new_features);
250 }
251 VkBool32 *supported = reinterpret_cast<VkBool32 *>(&supported_features);
252 for (size_t i = 0; i < sizeof(VkPhysicalDeviceFeatures); i += (sizeof(VkBool32))) {
253 if (*supported && *desired) {
254 *feature_ptr = true;
255 }
256 supported++;
257 desired++;
258 feature_ptr++;
259 }
260 if (!features) {
Tony-LunarG1dce2392019-10-23 16:49:29 -0600261 delete modified_create_info->pEnabledFeatures;
262 modified_create_info->pEnabledFeatures = new VkPhysicalDeviceFeatures(new_features);
263 }
264}
265
Jeremy Gebben33717862022-03-28 15:53:56 -0600266void GpuAssistedBase::CreateDevice(const VkDeviceCreateInfo *pCreateInfo) {
267 ValidationStateTracker::CreateDevice(pCreateInfo);
268 // If api version 1.1 or later, SetDeviceLoaderData will be in the loader
269 auto chain_info = get_chain_info(pCreateInfo, VK_LOADER_DATA_CALLBACK);
270 assert(chain_info->u.pfnSetDeviceLoaderData);
271 vkSetDeviceLoaderData = chain_info->u.pfnSetDeviceLoaderData;
272
273 // Some devices have extremely high limits here, so set a reasonable max because we have to pad
274 // the pipeline layout with dummy descriptor set layouts.
275 adjusted_max_desc_sets = phys_dev_props.limits.maxBoundDescriptorSets;
276 adjusted_max_desc_sets = std::min(33U, adjusted_max_desc_sets);
277
278 // We can't do anything if there is only one.
279 // Device probably not a legit Vulkan device, since there should be at least 4. Protect ourselves.
280 if (adjusted_max_desc_sets == 1) {
281 ReportSetupProblem(device, "Device can bind only a single descriptor set.");
282 aborted = true;
283 return;
284 }
285 desc_set_bind_index = adjusted_max_desc_sets - 1;
286
287 VkResult result1 = UtilInitializeVma(physical_device, device, &vmaAllocator);
288 assert(result1 == VK_SUCCESS);
289 desc_set_manager = layer_data::make_unique<UtilDescriptorSetManager>(device, static_cast<uint32_t>(bindings_.size()));
290
291 const VkDescriptorSetLayoutCreateInfo debug_desc_layout_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, NULL, 0,
292 static_cast<uint32_t>(bindings_.size()), bindings_.data()};
293
294 const VkDescriptorSetLayoutCreateInfo dummy_desc_layout_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, NULL, 0, 0,
295 NULL};
296
297 result1 = DispatchCreateDescriptorSetLayout(device, &debug_desc_layout_info, NULL, &debug_desc_layout);
298
299 // This is a layout used to "pad" a pipeline layout to fill in any gaps to the selected bind index.
300 VkResult result2 = DispatchCreateDescriptorSetLayout(device, &dummy_desc_layout_info, NULL, &dummy_desc_layout);
301
302 assert((result1 == VK_SUCCESS) && (result2 == VK_SUCCESS));
303 if ((result1 != VK_SUCCESS) || (result2 != VK_SUCCESS)) {
304 ReportSetupProblem(device, "Unable to create descriptor set layout.");
305 if (result1 == VK_SUCCESS) {
306 DispatchDestroyDescriptorSetLayout(device, debug_desc_layout, NULL);
307 }
308 if (result2 == VK_SUCCESS) {
309 DispatchDestroyDescriptorSetLayout(device, dummy_desc_layout, NULL);
310 }
311 debug_desc_layout = VK_NULL_HANDLE;
312 dummy_desc_layout = VK_NULL_HANDLE;
313 aborted = true;
314 return;
315 }
316}
317
318void GpuAssistedBase::PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
Jeremy Gebben33717862022-03-28 15:53:56 -0600319 if (debug_desc_layout) {
320 DispatchDestroyDescriptorSetLayout(device, debug_desc_layout, NULL);
321 debug_desc_layout = VK_NULL_HANDLE;
322 }
323 if (dummy_desc_layout) {
324 DispatchDestroyDescriptorSetLayout(device, dummy_desc_layout, NULL);
325 dummy_desc_layout = VK_NULL_HANDLE;
326 }
327 ValidationStateTracker::PreCallRecordDestroyDevice(device, pAllocator);
328 // State Tracker can end up making vma calls through callbacks - don't destroy allocator until ST is done
329 if (vmaAllocator) {
330 vmaDestroyAllocator(vmaAllocator);
331 }
332 desc_set_manager.reset();
333}
334
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600335gpu_utils_state::Queue::Queue(GpuAssistedBase &state, VkQueue q, uint32_t index, VkDeviceQueueCreateFlags flags)
336 : QUEUE_STATE(q, index, flags), state_(state) {}
337
338gpu_utils_state::Queue::~Queue() {
339 if (barrier_command_buffer_) {
340 DispatchFreeCommandBuffers(state_.device, barrier_command_pool_, 1, &barrier_command_buffer_);
341 barrier_command_buffer_ = VK_NULL_HANDLE;
342 }
343 if (barrier_command_pool_) {
344 DispatchDestroyCommandPool(state_.device, barrier_command_pool_, NULL);
345 barrier_command_pool_ = VK_NULL_HANDLE;
346 }
347}
348
349// Submit a memory barrier on graphics queues.
350// Lazy-create and record the needed command buffer.
351void gpu_utils_state::Queue::SubmitBarrier() {
352 if (barrier_command_pool_ == VK_NULL_HANDLE) {
353 VkResult result = VK_SUCCESS;
354
355 auto pool_create_info = LvlInitStruct<VkCommandPoolCreateInfo>();
356 pool_create_info.queueFamilyIndex = queueFamilyIndex;
357 result = DispatchCreateCommandPool(state_.device, &pool_create_info, nullptr, &barrier_command_pool_);
358 if (result != VK_SUCCESS) {
359 state_.ReportSetupProblem(state_.device, "Unable to create command pool for barrier CB.");
360 barrier_command_pool_ = VK_NULL_HANDLE;
361 return;
362 }
363
364 auto buffer_alloc_info = LvlInitStruct<VkCommandBufferAllocateInfo>();
365 buffer_alloc_info.commandPool = barrier_command_pool_;
366 buffer_alloc_info.commandBufferCount = 1;
367 buffer_alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
368 result = DispatchAllocateCommandBuffers(state_.device, &buffer_alloc_info, &barrier_command_buffer_);
369 if (result != VK_SUCCESS) {
370 state_.ReportSetupProblem(state_.device, "Unable to create barrier command buffer.");
371 DispatchDestroyCommandPool(state_.device, barrier_command_pool_, nullptr);
372 barrier_command_pool_ = VK_NULL_HANDLE;
373 barrier_command_buffer_ = VK_NULL_HANDLE;
374 return;
375 }
376
377 // Hook up command buffer dispatch
378 state_.vkSetDeviceLoaderData(state_.device, barrier_command_buffer_);
379
380 // Record a global memory barrier to force availability of device memory operations to the host domain.
381 auto command_buffer_begin_info = LvlInitStruct<VkCommandBufferBeginInfo>();
382 result = DispatchBeginCommandBuffer(barrier_command_buffer_, &command_buffer_begin_info);
383 if (result == VK_SUCCESS) {
384 auto memory_barrier = LvlInitStruct<VkMemoryBarrier>();
385 memory_barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
386 memory_barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT;
387 DispatchCmdPipelineBarrier(barrier_command_buffer_, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0,
388 1, &memory_barrier, 0, nullptr, 0, nullptr);
389 DispatchEndCommandBuffer(barrier_command_buffer_);
390 }
391 }
392 if (barrier_command_buffer_ != VK_NULL_HANDLE) {
393 auto submit_info = LvlInitStruct<VkSubmitInfo>();
394 submit_info.commandBufferCount = 1;
395 submit_info.pCommandBuffers = &barrier_command_buffer_;
396 DispatchQueueSubmit(QUEUE_STATE::Queue(), 1, &submit_info, VK_NULL_HANDLE);
397 }
398}
399
400// Issue a memory barrier to make GPU-written data available to host.
401// Wait for the queue to complete execution.
402// Check the debug buffers for all the command buffers that were submitted.
403void GpuAssistedBase::PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence,
404 VkResult result) {
405 ValidationStateTracker::PostCallRecordQueueSubmit(queue, submitCount, pSubmits, fence, result);
406
407 if (aborted || (result != VK_SUCCESS)) return;
408 bool buffers_present = false;
409 // Don't QueueWaitIdle if there's nothing to process
410 for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
411 const VkSubmitInfo *submit = &pSubmits[submit_idx];
412 for (uint32_t i = 0; i < submit->commandBufferCount; i++) {
413 buffers_present |= CommandBufferNeedsProcessing(submit->pCommandBuffers[i]);
414 }
415 }
416 if (!buffers_present) return;
417
418 SubmitBarrier(queue);
419
420 DispatchQueueWaitIdle(queue);
421
422 for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
423 const VkSubmitInfo *submit = &pSubmits[submit_idx];
424 for (uint32_t i = 0; i < submit->commandBufferCount; i++) {
425 ProcessCommandBuffer(queue, submit->pCommandBuffers[i]);
426 }
427 }
428}
429
430void GpuAssistedBase::RecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence,
431 VkResult result) {
432 if (aborted || (result != VK_SUCCESS)) return;
433 bool buffers_present = false;
434 // Don't QueueWaitIdle if there's nothing to process
435 for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
436 const VkSubmitInfo2 *submit = &pSubmits[submit_idx];
437 for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) {
438 buffers_present |= CommandBufferNeedsProcessing(submit->pCommandBufferInfos[i].commandBuffer);
439 }
440 }
441 if (!buffers_present) return;
442
443 SubmitBarrier(queue);
444
445 DispatchQueueWaitIdle(queue);
446
447 for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
448 const VkSubmitInfo2 *submit = &pSubmits[submit_idx];
449 for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) {
450 ProcessCommandBuffer(queue, submit->pCommandBufferInfos[i].commandBuffer);
451 }
452 }
453}
454
455void GpuAssistedBase::PostCallRecordQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits,
456 VkFence fence, VkResult result) {
457 ValidationStateTracker::PostCallRecordQueueSubmit2KHR(queue, submitCount, pSubmits, fence, result);
458 RecordQueueSubmit2(queue, submitCount, pSubmits, fence, result);
459}
460
461void GpuAssistedBase::PostCallRecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence,
462 VkResult result) {
463 ValidationStateTracker::PostCallRecordQueueSubmit2(queue, submitCount, pSubmits, fence, result);
464 RecordQueueSubmit2(queue, submitCount, pSubmits, fence, result);
465}
466
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600467void GpuAssistedBase::PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
468 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
469 void *cpl_state_data) {
470 if (aborted) {
471 return;
472 }
473 auto cpl_state = static_cast<create_pipeline_layout_api_state *>(cpl_state_data);
474 if (cpl_state->modified_create_info.setLayoutCount >= adjusted_max_desc_sets) {
475 std::ostringstream strm;
476 strm << "Pipeline Layout conflict with validation's descriptor set at slot " << desc_set_bind_index << ". "
477 << "Application has too many descriptor sets in the pipeline layout to continue with gpu validation. "
478 << "Validation is not modifying the pipeline layout. "
479 << "Instrumented shaders are replaced with non-instrumented shaders.";
480 ReportSetupProblem(device, strm.str().c_str());
481 } else {
482 // Modify the pipeline layout by:
483 // 1. Copying the caller's descriptor set desc_layouts
484 // 2. Fill in dummy descriptor layouts up to the max binding
485 // 3. Fill in with the debug descriptor layout at the max binding slot
486 cpl_state->new_layouts.reserve(adjusted_max_desc_sets);
487 cpl_state->new_layouts.insert(cpl_state->new_layouts.end(), &pCreateInfo->pSetLayouts[0],
488 &pCreateInfo->pSetLayouts[pCreateInfo->setLayoutCount]);
489 for (uint32_t i = pCreateInfo->setLayoutCount; i < adjusted_max_desc_sets - 1; ++i) {
490 cpl_state->new_layouts.push_back(dummy_desc_layout);
491 }
492 cpl_state->new_layouts.push_back(debug_desc_layout);
493 cpl_state->modified_create_info.pSetLayouts = cpl_state->new_layouts.data();
494 cpl_state->modified_create_info.setLayoutCount = adjusted_max_desc_sets;
495 }
496 ValidationStateTracker::PreCallRecordCreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout, cpl_state_data);
497}
498
499void GpuAssistedBase::PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
500 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
501 VkResult result) {
502 if (result != VK_SUCCESS) {
503 ReportSetupProblem(device, "Unable to create pipeline layout. Device could become unstable.");
504 aborted = true;
505 }
506 ValidationStateTracker::PostCallRecordCreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout, result);
507}
508
509void GpuAssistedBase::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
510 const VkGraphicsPipelineCreateInfo *pCreateInfos,
511 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
512 void *cgpl_state_data) {
513 if (aborted) return;
514 std::vector<safe_VkGraphicsPipelineCreateInfo> new_pipeline_create_infos;
515 create_graphics_pipeline_api_state *cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state *>(cgpl_state_data);
516 PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, cgpl_state->pipe_state, &new_pipeline_create_infos,
517 VK_PIPELINE_BIND_POINT_GRAPHICS);
518 cgpl_state->printf_create_infos = new_pipeline_create_infos;
519 cgpl_state->pCreateInfos = reinterpret_cast<VkGraphicsPipelineCreateInfo *>(cgpl_state->printf_create_infos.data());
520}
521
522void GpuAssistedBase::PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
523 const VkComputePipelineCreateInfo *pCreateInfos,
524 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
525 void *ccpl_state_data) {
526 if (aborted) return;
527 std::vector<safe_VkComputePipelineCreateInfo> new_pipeline_create_infos;
528 auto *ccpl_state = reinterpret_cast<create_compute_pipeline_api_state *>(ccpl_state_data);
529 PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, ccpl_state->pipe_state, &new_pipeline_create_infos,
530 VK_PIPELINE_BIND_POINT_COMPUTE);
531 ccpl_state->printf_create_infos = new_pipeline_create_infos;
532 ccpl_state->pCreateInfos = reinterpret_cast<VkComputePipelineCreateInfo *>(ccpl_state->printf_create_infos.data());
533}
534
535void GpuAssistedBase::PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
536 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
537 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
538 void *crtpl_state_data) {
539 if (aborted) return;
540 std::vector<safe_VkRayTracingPipelineCreateInfoCommon> new_pipeline_create_infos;
541 auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_api_state *>(crtpl_state_data);
542 PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, crtpl_state->pipe_state, &new_pipeline_create_infos,
543 VK_PIPELINE_BIND_POINT_RAY_TRACING_NV);
544 crtpl_state->printf_create_infos = new_pipeline_create_infos;
545 crtpl_state->pCreateInfos = reinterpret_cast<VkRayTracingPipelineCreateInfoNV *>(crtpl_state->printf_create_infos.data());
546}
547
548void GpuAssistedBase::PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
549 VkPipelineCache pipelineCache, uint32_t count,
550 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
551 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
552 void *crtpl_state_data) {
553 if (aborted) return;
554 std::vector<safe_VkRayTracingPipelineCreateInfoCommon> new_pipeline_create_infos;
555 auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data);
556 PreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, crtpl_state->pipe_state, &new_pipeline_create_infos,
557 VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR);
558 crtpl_state->printf_create_infos = new_pipeline_create_infos;
559 crtpl_state->pCreateInfos = reinterpret_cast<VkRayTracingPipelineCreateInfoKHR *>(crtpl_state->printf_create_infos.data());
560}
561
562template <typename CreateInfos, typename SafeCreateInfos>
563static void UtilCopyCreatePipelineFeedbackData(const uint32_t count, CreateInfos *pCreateInfos, SafeCreateInfos *pSafeCreateInfos) {
564 for (uint32_t i = 0; i < count; i++) {
565 auto src_feedback_struct = LvlFindInChain<VkPipelineCreationFeedbackCreateInfoEXT>(pSafeCreateInfos[i].pNext);
566 if (!src_feedback_struct) return;
567 auto dst_feedback_struct = const_cast<VkPipelineCreationFeedbackCreateInfoEXT *>(
568 LvlFindInChain<VkPipelineCreationFeedbackCreateInfoEXT>(pCreateInfos[i].pNext));
569 *dst_feedback_struct->pPipelineCreationFeedback = *src_feedback_struct->pPipelineCreationFeedback;
570 for (uint32_t j = 0; j < src_feedback_struct->pipelineStageCreationFeedbackCount; j++) {
571 dst_feedback_struct->pPipelineStageCreationFeedbacks[j] = src_feedback_struct->pPipelineStageCreationFeedbacks[j];
572 }
573 }
574}
575
576void GpuAssistedBase::PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
577 const VkGraphicsPipelineCreateInfo *pCreateInfos,
578 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
579 VkResult result, void *cgpl_state_data) {
580 ValidationStateTracker::PostCallRecordCreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator,
581 pPipelines, result, cgpl_state_data);
582 if (aborted) return;
583 create_graphics_pipeline_api_state *cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state *>(cgpl_state_data);
584 UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, cgpl_state->printf_create_infos.data());
585 PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_GRAPHICS);
586}
587
588void GpuAssistedBase::PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
589 const VkComputePipelineCreateInfo *pCreateInfos,
590 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
591 VkResult result, void *ccpl_state_data) {
592 ValidationStateTracker::PostCallRecordCreateComputePipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines,
593 result, ccpl_state_data);
594 if (aborted) return;
595 create_compute_pipeline_api_state *ccpl_state = reinterpret_cast<create_compute_pipeline_api_state *>(ccpl_state_data);
596 UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, ccpl_state->printf_create_infos.data());
597 PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_COMPUTE);
598}
599
600void GpuAssistedBase::PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
601 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
602 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
603 VkResult result, void *crtpl_state_data) {
604 auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data);
605 ValidationStateTracker::PostCallRecordCreateRayTracingPipelinesNV(device, pipelineCache, count, pCreateInfos, pAllocator,
606 pPipelines, result, crtpl_state_data);
607 if (aborted) return;
608 UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, crtpl_state->printf_create_infos.data());
609 PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_RAY_TRACING_NV);
610}
611
612void GpuAssistedBase::PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
613 VkPipelineCache pipelineCache, uint32_t count,
614 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
615 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
616 VkResult result, void *crtpl_state_data) {
617 auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data);
618 ValidationStateTracker::PostCallRecordCreateRayTracingPipelinesKHR(
619 device, deferredOperation, pipelineCache, count, pCreateInfos, pAllocator, pPipelines, result, crtpl_state_data);
620 if (aborted) return;
621 UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, crtpl_state->printf_create_infos.data());
622 PostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR);
623}
624
625// Remove all the shader trackers associated with this destroyed pipeline.
626void GpuAssistedBase::PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) {
627 for (auto it = shader_map.begin(); it != shader_map.end();) {
628 if (it->second.pipeline == pipeline) {
629 it = shader_map.erase(it);
630 } else {
631 ++it;
632 }
633 }
634 ValidationStateTracker::PreCallRecordDestroyPipeline(device, pipeline, pAllocator);
635}
636
637template <typename CreateInfo>
638struct CreatePipelineTraits {};
639template <>
640struct CreatePipelineTraits<VkGraphicsPipelineCreateInfo> {
641 using SafeType = safe_VkGraphicsPipelineCreateInfo;
642 static const SafeType &GetPipelineCI(const PIPELINE_STATE *pipeline_state) {
643 return pipeline_state->GetUnifiedCreateInfo().graphics;
644 }
645 static uint32_t GetStageCount(const VkGraphicsPipelineCreateInfo &createInfo) { return createInfo.stageCount; }
646 static VkShaderModule GetShaderModule(const VkGraphicsPipelineCreateInfo &createInfo, uint32_t stage) {
647 return createInfo.pStages[stage].module;
648 }
649 static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) {
650 createInfo->pStages[stage].module = shader_module;
651 }
652};
653
654template <>
655struct CreatePipelineTraits<VkComputePipelineCreateInfo> {
656 using SafeType = safe_VkComputePipelineCreateInfo;
657 static const SafeType &GetPipelineCI(const PIPELINE_STATE *pipeline_state) {
658 return pipeline_state->GetUnifiedCreateInfo().compute;
659 }
660 static uint32_t GetStageCount(const VkComputePipelineCreateInfo &createInfo) { return 1; }
661 static VkShaderModule GetShaderModule(const VkComputePipelineCreateInfo &createInfo, uint32_t stage) {
662 return createInfo.stage.module;
663 }
664 static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) {
665 assert(stage == 0);
666 createInfo->stage.module = shader_module;
667 }
668};
669
670template <>
671struct CreatePipelineTraits<VkRayTracingPipelineCreateInfoNV> {
672 using SafeType = safe_VkRayTracingPipelineCreateInfoCommon;
673 static const SafeType &GetPipelineCI(const PIPELINE_STATE *pipeline_state) {
674 return pipeline_state->GetUnifiedCreateInfo().raytracing;
675 }
676 static uint32_t GetStageCount(const VkRayTracingPipelineCreateInfoNV &createInfo) { return createInfo.stageCount; }
677 static VkShaderModule GetShaderModule(const VkRayTracingPipelineCreateInfoNV &createInfo, uint32_t stage) {
678 return createInfo.pStages[stage].module;
679 }
680 static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) {
681 createInfo->pStages[stage].module = shader_module;
682 }
683};
684
685template <>
686struct CreatePipelineTraits<VkRayTracingPipelineCreateInfoKHR> {
687 using SafeType = safe_VkRayTracingPipelineCreateInfoCommon;
688 static const SafeType &GetPipelineCI(const PIPELINE_STATE *pipeline_state) {
689 return pipeline_state->GetUnifiedCreateInfo().raytracing;
690 }
691 static uint32_t GetStageCount(const VkRayTracingPipelineCreateInfoKHR &createInfo) { return createInfo.stageCount; }
692 static VkShaderModule GetShaderModule(const VkRayTracingPipelineCreateInfoKHR &createInfo, uint32_t stage) {
693 return createInfo.pStages[stage].module;
694 }
695 static void SetShaderModule(SafeType *createInfo, VkShaderModule shader_module, uint32_t stage) {
696 createInfo->pStages[stage].module = shader_module;
697 }
698};
699
700// Examine the pipelines to see if they use the debug descriptor set binding index.
701// If any do, create new non-instrumented shader modules and use them to replace the instrumented
702// shaders in the pipeline. Return the (possibly) modified create infos to the caller.
703template <typename CreateInfo, typename SafeCreateInfo>
704void GpuAssistedBase::PreCallRecordPipelineCreations(uint32_t count, const CreateInfo *pCreateInfos,
705 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
706 std::vector<std::shared_ptr<PIPELINE_STATE>> &pipe_state,
707 std::vector<SafeCreateInfo> *new_pipeline_create_infos,
708 const VkPipelineBindPoint bind_point) {
709 using Accessor = CreatePipelineTraits<CreateInfo>;
710 if (bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS && bind_point != VK_PIPELINE_BIND_POINT_COMPUTE &&
711 bind_point != VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) {
712 return;
713 }
714
715 // Walk through all the pipelines, make a copy of each and flag each pipeline that contains a shader that uses the debug
716 // descriptor set index.
717 for (uint32_t pipeline = 0; pipeline < count; ++pipeline) {
718 uint32_t stageCount = Accessor::GetStageCount(pCreateInfos[pipeline]);
719 new_pipeline_create_infos->push_back(Accessor::GetPipelineCI(pipe_state[pipeline].get()));
720 const auto &pipe = pipe_state[pipeline];
721
722 if (!pipe->IsGraphicsLibrary()) {
723 bool replace_shaders = false;
724 if (pipe->active_slots.find(desc_set_bind_index) != pipe->active_slots.end()) {
725 replace_shaders = true;
726 }
727 // If the app requests all available sets, the pipeline layout was not modified at pipeline layout creation and the
728 // already instrumented shaders need to be replaced with uninstrumented shaders
729 const auto pipeline_layout = pipe->PipelineLayoutState();
730 if (pipeline_layout->set_layouts.size() >= adjusted_max_desc_sets) {
731 replace_shaders = true;
732 }
733
734 if (replace_shaders) {
735 for (uint32_t stage = 0; stage < stageCount; ++stage) {
736 const auto module_state = Get<SHADER_MODULE_STATE>(Accessor::GetShaderModule(pCreateInfos[pipeline], stage));
737
738 VkShaderModule shader_module;
739 auto create_info = LvlInitStruct<VkShaderModuleCreateInfo>();
740 create_info.pCode = module_state->words.data();
741 create_info.codeSize = module_state->words.size() * sizeof(uint32_t);
742 VkResult result = DispatchCreateShaderModule(device, &create_info, pAllocator, &shader_module);
743 if (result == VK_SUCCESS) {
744 Accessor::SetShaderModule(&(*new_pipeline_create_infos)[pipeline], shader_module, stage);
745 } else {
746 ReportSetupProblem(device,
747 "Unable to replace instrumented shader with non-instrumented one. "
748 "Device could become unstable.");
749 }
750 }
751 }
752 }
753 }
754}
755// For every pipeline:
756// - For every shader in a pipeline:
757// - If the shader had to be replaced in PreCallRecord (because the pipeline is using the debug desc set index):
758// - Destroy it since it has been bound into the pipeline by now. This is our only chance to delete it.
759// - Track the shader in the shader_map
760// - Save the shader binary if it contains debug code
761template <typename CreateInfo>
762void GpuAssistedBase::PostCallRecordPipelineCreations(const uint32_t count, const CreateInfo *pCreateInfos,
763 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
764 const VkPipelineBindPoint bind_point) {
765 using Accessor = CreatePipelineTraits<CreateInfo>;
766 if (bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS && bind_point != VK_PIPELINE_BIND_POINT_COMPUTE &&
767 bind_point != VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) {
768 return;
769 }
770 for (uint32_t pipeline = 0; pipeline < count; ++pipeline) {
771 auto pipeline_state = Get<PIPELINE_STATE>(pPipelines[pipeline]);
772 if (!pipeline_state || pipeline_state->IsGraphicsLibrary()) continue;
773
774 const uint32_t stageCount = static_cast<uint32_t>(pipeline_state->stage_state.size());
775 assert(stageCount > 0);
776
777 for (uint32_t stage = 0; stage < stageCount; ++stage) {
778 if (pipeline_state->active_slots.find(desc_set_bind_index) != pipeline_state->active_slots.end()) {
779 DispatchDestroyShaderModule(device, Accessor::GetShaderModule(pCreateInfos[pipeline], stage), pAllocator);
780 }
781
782 std::shared_ptr<const SHADER_MODULE_STATE> module_state;
783 if (bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) {
784 module_state = Get<SHADER_MODULE_STATE>(pipeline_state->GetUnifiedCreateInfo().graphics.pStages[stage].module);
785 } else if (bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) {
786 assert(stage == 0);
787 module_state = Get<SHADER_MODULE_STATE>(pipeline_state->GetUnifiedCreateInfo().compute.stage.module);
788 } else if (bind_point == VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) {
789 module_state = Get<SHADER_MODULE_STATE>(pipeline_state->GetUnifiedCreateInfo().raytracing.pStages[stage].module);
790 } else {
791 assert(false);
792 }
793
794 std::vector<unsigned int> code;
795 // Save the shader binary
796 // The core_validation ShaderModule tracker saves the binary too, but discards it when the ShaderModule
797 // is destroyed. Applications may destroy ShaderModules after they are placed in a pipeline and before
798 // the pipeline is used, so we have to keep another copy.
799 if (module_state && module_state->has_valid_spirv) code = module_state->words;
800
801 // Be careful to use the originally bound (instrumented) shader here, even if PreCallRecord had to back it
802 // out with a non-instrumented shader. The non-instrumented shader (found in pCreateInfo) was destroyed above.
803 VkShaderModule shader_module = VK_NULL_HANDLE;
804 if (bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) {
805 shader_module = pipeline_state->GetUnifiedCreateInfo().graphics.pStages[stage].module;
806 } else if (bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) {
807 assert(stage == 0);
808 shader_module = pipeline_state->GetUnifiedCreateInfo().compute.stage.module;
809 } else if (bind_point == VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) {
810 shader_module = pipeline_state->GetUnifiedCreateInfo().raytracing.pStages[stage].module;
811 } else {
812 assert(false);
813 }
814 shader_map.emplace(module_state->gpu_validation_shader_id, GpuAssistedShaderTracker{pipeline_state->pipeline(), shader_module,
815 std::move(code)});
816 }
817 }
818}
819
Tony-LunarG1dce2392019-10-23 16:49:29 -0600820// Generate the stage-specific part of the message.
Tony-LunarGb5fae462020-03-05 12:43:25 -0700821void UtilGenerateStageMessage(const uint32_t *debug_record, std::string &msg) {
Tony-LunarG1dce2392019-10-23 16:49:29 -0600822 using namespace spvtools;
823 std::ostringstream strm;
824 switch (debug_record[kInstCommonOutStageIdx]) {
825 case spv::ExecutionModelVertex: {
826 strm << "Stage = Vertex. Vertex Index = " << debug_record[kInstVertOutVertexIndex]
827 << " Instance Index = " << debug_record[kInstVertOutInstanceIndex] << ". ";
828 } break;
829 case spv::ExecutionModelTessellationControl: {
830 strm << "Stage = Tessellation Control. Invocation ID = " << debug_record[kInstTessCtlOutInvocationId]
831 << ", Primitive ID = " << debug_record[kInstTessCtlOutPrimitiveId];
832 } break;
833 case spv::ExecutionModelTessellationEvaluation: {
834 strm << "Stage = Tessellation Eval. Primitive ID = " << debug_record[kInstTessEvalOutPrimitiveId]
835 << ", TessCoord (u, v) = (" << debug_record[kInstTessEvalOutTessCoordU] << ", "
836 << debug_record[kInstTessEvalOutTessCoordV] << "). ";
837 } break;
838 case spv::ExecutionModelGeometry: {
839 strm << "Stage = Geometry. Primitive ID = " << debug_record[kInstGeomOutPrimitiveId]
840 << " Invocation ID = " << debug_record[kInstGeomOutInvocationId] << ". ";
841 } break;
842 case spv::ExecutionModelFragment: {
843 strm << "Stage = Fragment. Fragment coord (x,y) = ("
844 << *reinterpret_cast<const float *>(&debug_record[kInstFragOutFragCoordX]) << ", "
845 << *reinterpret_cast<const float *>(&debug_record[kInstFragOutFragCoordY]) << "). ";
846 } break;
847 case spv::ExecutionModelGLCompute: {
848 strm << "Stage = Compute. Global invocation ID (x, y, z) = (" << debug_record[kInstCompOutGlobalInvocationIdX] << ", "
849 << debug_record[kInstCompOutGlobalInvocationIdY] << ", " << debug_record[kInstCompOutGlobalInvocationIdZ] << " )";
850 } break;
851 case spv::ExecutionModelRayGenerationNV: {
852 strm << "Stage = Ray Generation. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", "
853 << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). ";
854 } break;
855 case spv::ExecutionModelIntersectionNV: {
856 strm << "Stage = Intersection. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", "
857 << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). ";
858 } break;
859 case spv::ExecutionModelAnyHitNV: {
860 strm << "Stage = Any Hit. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", "
861 << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). ";
862 } break;
863 case spv::ExecutionModelClosestHitNV: {
864 strm << "Stage = Closest Hit. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", "
865 << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). ";
866 } break;
867 case spv::ExecutionModelMissNV: {
868 strm << "Stage = Miss. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", "
869 << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). ";
870 } break;
871 case spv::ExecutionModelCallableNV: {
872 strm << "Stage = Callable. Global Launch ID (x,y,z) = (" << debug_record[kInstRayTracingOutLaunchIdX] << ", "
873 << debug_record[kInstRayTracingOutLaunchIdY] << ", " << debug_record[kInstRayTracingOutLaunchIdZ] << "). ";
874 } break;
Tony-LunarGc7ed2082020-06-11 14:00:04 -0600875 case spv::ExecutionModelTaskNV: {
876 strm << "Stage = Task. Global invocation ID (x, y, z) = (" << debug_record[kInstTaskOutGlobalInvocationIdX] << ", "
877 << debug_record[kInstTaskOutGlobalInvocationIdY] << ", " << debug_record[kInstTaskOutGlobalInvocationIdZ] << " )";
878 } break;
879 case spv::ExecutionModelMeshNV: {
880 strm << "Stage = Mesh.Global invocation ID (x, y, z) = (" << debug_record[kInstMeshOutGlobalInvocationIdX] << ", "
881 << debug_record[kInstMeshOutGlobalInvocationIdY] << ", " << debug_record[kInstMeshOutGlobalInvocationIdZ] << " )";
882 } break;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600883 default: {
884 strm << "Internal Error (unexpected stage = " << debug_record[kInstCommonOutStageIdx] << "). ";
885 assert(false);
886 } break;
887 }
888 msg = strm.str();
889}
890
891std::string LookupDebugUtilsName(const debug_report_data *report_data, const uint64_t object) {
892 auto object_label = report_data->DebugReportGetUtilsObjectName(object);
893 if (object_label != "") {
894 object_label = "(" + object_label + ")";
895 }
896 return object_label;
897}
898
899// Generate message from the common portion of the debug report record.
Tony-LunarGb5fae462020-03-05 12:43:25 -0700900void UtilGenerateCommonMessage(const debug_report_data *report_data, const VkCommandBuffer commandBuffer,
901 const uint32_t *debug_record, const VkShaderModule shader_module_handle,
902 const VkPipeline pipeline_handle, const VkPipelineBindPoint pipeline_bind_point,
903 const uint32_t operation_index, std::string &msg) {
Tony-LunarG1dce2392019-10-23 16:49:29 -0600904 using namespace spvtools;
905 std::ostringstream strm;
906 if (shader_module_handle == VK_NULL_HANDLE) {
907 strm << std::hex << std::showbase << "Internal Error: Unable to locate information for shader used in command buffer "
908 << LookupDebugUtilsName(report_data, HandleToUint64(commandBuffer)) << "(" << HandleToUint64(commandBuffer) << "). ";
909 assert(true);
910 } else {
911 strm << std::hex << std::showbase << "Command buffer " << LookupDebugUtilsName(report_data, HandleToUint64(commandBuffer))
912 << "(" << HandleToUint64(commandBuffer) << "). ";
913 if (pipeline_bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) {
914 strm << "Draw ";
915 } else if (pipeline_bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) {
916 strm << "Compute ";
917 } else if (pipeline_bind_point == VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) {
918 strm << "Ray Trace ";
919 } else {
920 assert(false);
921 strm << "Unknown Pipeline Operation ";
922 }
923 strm << "Index " << operation_index << ". "
924 << "Pipeline " << LookupDebugUtilsName(report_data, HandleToUint64(pipeline_handle)) << "("
925 << HandleToUint64(pipeline_handle) << "). "
926 << "Shader Module " << LookupDebugUtilsName(report_data, HandleToUint64(shader_module_handle)) << "("
927 << HandleToUint64(shader_module_handle) << "). ";
928 }
929 strm << std::dec << std::noshowbase;
930 strm << "Shader Instruction Index = " << debug_record[kInstCommonOutInstructionIdx] << ". ";
931 msg = strm.str();
932}
933
934// Read the contents of the SPIR-V OpSource instruction and any following continuation instructions.
935// Split the single string into a vector of strings, one for each line, for easier processing.
sfricke-samsungef15e482022-01-26 11:32:49 -0800936void ReadOpSource(const SHADER_MODULE_STATE &module_state, const uint32_t reported_file_id,
937 std::vector<std::string> &opsource_lines) {
938 for (auto insn : module_state) {
Tony-LunarG1dce2392019-10-23 16:49:29 -0600939 if ((insn.opcode() == spv::OpSource) && (insn.len() >= 5) && (insn.word(3) == reported_file_id)) {
940 std::istringstream in_stream;
941 std::string cur_line;
942 in_stream.str((char *)&insn.word(4));
943 while (std::getline(in_stream, cur_line)) {
944 opsource_lines.push_back(cur_line);
945 }
946 while ((++insn).opcode() == spv::OpSourceContinued) {
947 in_stream.str((char *)&insn.word(1));
948 while (std::getline(in_stream, cur_line)) {
949 opsource_lines.push_back(cur_line);
950 }
951 }
952 break;
953 }
954 }
955}
956
957// The task here is to search the OpSource content to find the #line directive with the
958// line number that is closest to, but still prior to the reported error line number and
959// still within the reported filename.
960// From this known position in the OpSource content we can add the difference between
961// the #line line number and the reported error line number to determine the location
962// in the OpSource content of the reported error line.
963//
964// Considerations:
965// - Look only at #line directives that specify the reported_filename since
966// the reported error line number refers to its location in the reported filename.
967// - If a #line directive does not have a filename, the file is the reported filename, or
968// the filename found in a prior #line directive. (This is C-preprocessor behavior)
969// - It is possible (e.g., inlining) for blocks of code to get shuffled out of their
970// original order and the #line directives are used to keep the numbering correct. This
971// is why we need to examine the entire contents of the source, instead of leaving early
972// when finding a #line line number larger than the reported error line number.
973//
974
975// GCC 4.8 has a problem with std::regex that is fixed in GCC 4.9. Provide fallback code for 4.8
976#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
977
978#if defined(__GNUC__) && GCC_VERSION < 40900
979bool GetLineAndFilename(const std::string string, uint32_t *linenumber, std::string &filename) {
980 // # line <linenumber> "<filename>" or
981 // #line <linenumber> "<filename>"
982 std::vector<std::string> tokens;
983 std::stringstream stream(string);
984 std::string temp;
985 uint32_t line_index = 0;
986
987 while (stream >> temp) tokens.push_back(temp);
988 auto size = tokens.size();
989 if (size > 1) {
990 if (tokens[0] == "#" && tokens[1] == "line") {
991 line_index = 2;
992 } else if (tokens[0] == "#line") {
993 line_index = 1;
994 }
995 }
996 if (0 == line_index) return false;
Mark Young0ec6b062020-11-19 15:32:17 -0700997 *linenumber = static_cast<uint32_t>(std::stoul(tokens[line_index]));
Tony-LunarG1dce2392019-10-23 16:49:29 -0600998 uint32_t filename_index = line_index + 1;
999 // Remove enclosing double quotes around filename
1000 if (size > filename_index) filename = tokens[filename_index].substr(1, tokens[filename_index].size() - 2);
1001 return true;
1002}
1003#else
1004bool GetLineAndFilename(const std::string string, uint32_t *linenumber, std::string &filename) {
1005 static const std::regex line_regex( // matches #line directives
1006 "^" // beginning of line
1007 "\\s*" // optional whitespace
1008 "#" // required text
1009 "\\s*" // optional whitespace
1010 "line" // required text
1011 "\\s+" // required whitespace
1012 "([0-9]+)" // required first capture - line number
1013 "(\\s+)?" // optional second capture - whitespace
1014 "(\".+\")?" // optional third capture - quoted filename with at least one char inside
1015 ".*"); // rest of line (needed when using std::regex_match since the entire line is tested)
1016
1017 std::smatch captures;
1018
1019 bool found_line = std::regex_match(string, captures, line_regex);
1020 if (!found_line) return false;
1021
1022 // filename is optional and considered found only if the whitespace and the filename are captured
1023 if (captures[2].matched && captures[3].matched) {
1024 // Remove enclosing double quotes. The regex guarantees the quotes and at least one char.
1025 filename = captures[3].str().substr(1, captures[3].str().size() - 2);
1026 }
Artem Bolgar82d08362021-06-03 13:11:13 -07001027 *linenumber = (uint32_t)std::stoul(captures[1]);
Tony-LunarG1dce2392019-10-23 16:49:29 -06001028 return true;
1029}
1030#endif // GCC_VERSION
1031
1032// Extract the filename, line number, and column number from the correct OpLine and build a message string from it.
1033// Scan the source (from OpSource) to find the line of source at the reported line number and place it in another message string.
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001034void UtilGenerateSourceMessages(const std::vector<uint32_t> &pgm, const uint32_t *debug_record, bool from_printf,
Tony-LunarGb5fae462020-03-05 12:43:25 -07001035 std::string &filename_msg, std::string &source_msg) {
Tony-LunarG1dce2392019-10-23 16:49:29 -06001036 using namespace spvtools;
1037 std::ostringstream filename_stream;
1038 std::ostringstream source_stream;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001039 SHADER_MODULE_STATE shader(pgm);
Tony-LunarG1dce2392019-10-23 16:49:29 -06001040 // Find the OpLine just before the failing instruction indicated by the debug info.
1041 // SPIR-V can only be iterated in the forward direction due to its opcode/length encoding.
1042 uint32_t instruction_index = 0;
1043 uint32_t reported_file_id = 0;
1044 uint32_t reported_line_number = 0;
1045 uint32_t reported_column_number = 0;
1046 if (shader.words.size() > 0) {
John Zulauf79f06582021-02-27 18:38:39 -07001047 for (const auto &insn : shader) {
Tony-LunarG1dce2392019-10-23 16:49:29 -06001048 if (insn.opcode() == spv::OpLine) {
1049 reported_file_id = insn.word(1);
1050 reported_line_number = insn.word(2);
1051 reported_column_number = insn.word(3);
1052 }
1053 if (instruction_index == debug_record[kInstCommonOutInstructionIdx]) {
1054 break;
1055 }
1056 instruction_index++;
1057 }
1058 }
1059 // Create message with file information obtained from the OpString pointed to by the discovered OpLine.
1060 std::string reported_filename;
1061 if (reported_file_id == 0) {
1062 filename_stream
1063 << "Unable to find SPIR-V OpLine for source information. Build shader with debug info to get source information.";
1064 } else {
1065 bool found_opstring = false;
1066 std::string prefix;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001067 if (from_printf) {
Tony-LunarG1dce2392019-10-23 16:49:29 -06001068 prefix = "Debug shader printf message generated ";
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001069 } else {
Tony-LunarG1dce2392019-10-23 16:49:29 -06001070 prefix = "Shader validation error occurred ";
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001071 }
John Zulauf79f06582021-02-27 18:38:39 -07001072 for (const auto &insn : shader) {
Tony-LunarG1dce2392019-10-23 16:49:29 -06001073 if ((insn.opcode() == spv::OpString) && (insn.len() >= 3) && (insn.word(1) == reported_file_id)) {
1074 found_opstring = true;
1075 reported_filename = (char *)&insn.word(2);
1076 if (reported_filename.empty()) {
1077 filename_stream << prefix << "at line " << reported_line_number;
1078 } else {
1079 filename_stream << prefix << "in file " << reported_filename << " at line " << reported_line_number;
1080 }
1081 if (reported_column_number > 0) {
1082 filename_stream << ", column " << reported_column_number;
1083 }
1084 filename_stream << ".";
1085 break;
1086 }
1087 }
1088 if (!found_opstring) {
Tony-LunarG6d195e12020-10-27 16:54:14 -06001089 filename_stream << "Unable to find SPIR-V OpString for file id " << reported_file_id << " from OpLine instruction."
1090 << std::endl;
1091 filename_stream << "File ID = " << reported_file_id << ", Line Number = " << reported_line_number
1092 << ", Column = " << reported_column_number << std::endl;
Tony-LunarG1dce2392019-10-23 16:49:29 -06001093 }
1094 }
1095 filename_msg = filename_stream.str();
1096
1097 // Create message to display source code line containing error.
1098 if ((reported_file_id != 0)) {
1099 // Read the source code and split it up into separate lines.
1100 std::vector<std::string> opsource_lines;
1101 ReadOpSource(shader, reported_file_id, opsource_lines);
1102 // Find the line in the OpSource content that corresponds to the reported error file and line.
1103 if (!opsource_lines.empty()) {
1104 uint32_t saved_line_number = 0;
1105 std::string current_filename = reported_filename; // current "preprocessor" filename state.
1106 std::vector<std::string>::size_type saved_opsource_offset = 0;
1107 bool found_best_line = false;
1108 for (auto it = opsource_lines.begin(); it != opsource_lines.end(); ++it) {
1109 uint32_t parsed_line_number;
1110 std::string parsed_filename;
1111 bool found_line = GetLineAndFilename(*it, &parsed_line_number, parsed_filename);
1112 if (!found_line) continue;
1113
1114 bool found_filename = parsed_filename.size() > 0;
1115 if (found_filename) {
1116 current_filename = parsed_filename;
1117 }
1118 if ((!found_filename) || (current_filename == reported_filename)) {
1119 // Update the candidate best line directive, if the current one is prior and closer to the reported line
1120 if (reported_line_number >= parsed_line_number) {
1121 if (!found_best_line ||
1122 (reported_line_number - parsed_line_number <= reported_line_number - saved_line_number)) {
1123 saved_line_number = parsed_line_number;
1124 saved_opsource_offset = std::distance(opsource_lines.begin(), it);
1125 found_best_line = true;
1126 }
1127 }
1128 }
1129 }
1130 if (found_best_line) {
1131 assert(reported_line_number >= saved_line_number);
1132 std::vector<std::string>::size_type opsource_index =
1133 (reported_line_number - saved_line_number) + 1 + saved_opsource_offset;
1134 if (opsource_index < opsource_lines.size()) {
1135 source_stream << "\n" << reported_line_number << ": " << opsource_lines[opsource_index].c_str();
1136 } else {
1137 source_stream << "Internal error: calculated source line of " << opsource_index << " for source size of "
1138 << opsource_lines.size() << " lines.";
1139 }
1140 } else {
1141 source_stream << "Unable to find suitable #line directive in SPIR-V OpSource.";
1142 }
1143 } else {
1144 source_stream << "Unable to find SPIR-V OpSource.";
1145 }
1146 }
1147 source_msg = source_stream.str();
1148}