Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2018-2022 The Khronos Group Inc. |
| 2 | * Copyright (c) 2018-2022 Valve Corporation |
| 3 | * Copyright (c) 2018-2022 LunarG, Inc. |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 4 | * |
| 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 | * |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 17 | * Author: Karl Schultz <karl@lunarg.com> |
| 18 | * Author: Tony Barbour <tony@lunarg.com> |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 19 | */ |
| 20 | |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 21 | #include <climits> |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 22 | #include <cmath> |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 23 | #include "gpu_validation.h" |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 24 | #include "spirv-tools/optimizer.hpp" |
| 25 | #include "spirv-tools/instrument.hpp" |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 26 | #include "layer_chassis_dispatch.h" |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 27 | #include "gpu_vuids.h" |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 28 | #include "buffer_state.h" |
| 29 | #include "cmd_buffer_state.h" |
| 30 | #include "render_pass_state.h" |
sjfricke | becf072 | 2022-08-05 16:15:46 +0900 | [diff] [blame^] | 31 | // Generated shaders |
| 32 | #include "gpu_shaders/gpu_shaders_constants.h" |
| 33 | #include "gpu_pre_draw_vert.h" |
| 34 | #include "gpu_as_inspection_comp.h" |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 35 | |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 36 | // Keep in sync with the GLSL shader below. |
| 37 | struct GpuAccelerationStructureBuildValidationBuffer { |
| 38 | uint32_t instances_to_validate; |
| 39 | uint32_t replacement_handle_bits_0; |
| 40 | uint32_t replacement_handle_bits_1; |
| 41 | uint32_t invalid_handle_found; |
| 42 | uint32_t invalid_handle_bits_0; |
| 43 | uint32_t invalid_handle_bits_1; |
| 44 | uint32_t valid_handles_count; |
| 45 | }; |
| 46 | |
Tony-LunarG | 5c38b18 | 2020-06-10 16:15:32 -0600 | [diff] [blame] | 47 | bool GpuAssisted::CheckForDescriptorIndexing(DeviceFeatures enabled_features) const { |
| 48 | bool result = |
| 49 | (IsExtEnabled(device_extensions.vk_ext_descriptor_indexing) && |
| 50 | (enabled_features.core12.descriptorIndexing || enabled_features.core12.shaderInputAttachmentArrayDynamicIndexing || |
| 51 | enabled_features.core12.shaderUniformTexelBufferArrayDynamicIndexing || |
| 52 | enabled_features.core12.shaderStorageTexelBufferArrayDynamicIndexing || |
| 53 | enabled_features.core12.shaderUniformBufferArrayNonUniformIndexing || |
| 54 | enabled_features.core12.shaderSampledImageArrayNonUniformIndexing || |
| 55 | enabled_features.core12.shaderStorageBufferArrayNonUniformIndexing || |
| 56 | enabled_features.core12.shaderStorageImageArrayNonUniformIndexing || |
| 57 | enabled_features.core12.shaderInputAttachmentArrayNonUniformIndexing || |
| 58 | enabled_features.core12.shaderUniformTexelBufferArrayNonUniformIndexing || |
| 59 | enabled_features.core12.shaderStorageTexelBufferArrayNonUniformIndexing || |
| 60 | enabled_features.core12.descriptorBindingUniformBufferUpdateAfterBind || |
| 61 | enabled_features.core12.descriptorBindingSampledImageUpdateAfterBind || |
| 62 | enabled_features.core12.descriptorBindingStorageImageUpdateAfterBind || |
| 63 | enabled_features.core12.descriptorBindingStorageBufferUpdateAfterBind || |
| 64 | enabled_features.core12.descriptorBindingUniformTexelBufferUpdateAfterBind || |
| 65 | enabled_features.core12.descriptorBindingStorageTexelBufferUpdateAfterBind || |
| 66 | enabled_features.core12.descriptorBindingUpdateUnusedWhilePending || |
| 67 | enabled_features.core12.descriptorBindingPartiallyBound || |
| 68 | enabled_features.core12.descriptorBindingVariableDescriptorCount || enabled_features.core12.runtimeDescriptorArray)); |
| 69 | return result; |
| 70 | } |
| 71 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 72 | void GpuAssisted::PreCallRecordCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo, |
| 73 | const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer, void *cb_state_data) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 74 | // Ray tracing acceleration structure instance buffers also need the storage buffer usage as |
| 75 | // acceleration structure build validation will find and replace invalid acceleration structure |
| 76 | // handles inside of a compute shader. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 77 | create_buffer_api_state *cb_state = reinterpret_cast<create_buffer_api_state *>(cb_state_data); |
| 78 | if (cb_state && cb_state->modified_create_info.usage & VK_BUFFER_USAGE_RAY_TRACING_BIT_NV) { |
| 79 | cb_state->modified_create_info.usage |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 80 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 81 | |
| 82 | // Validating DrawIndirectCount countBuffer will require validation shader to bind the count buffer as a storage buffer |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 83 | if (validate_draw_indirect && cb_state && cb_state->modified_create_info.usage & VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT) { |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 84 | cb_state->modified_create_info.usage |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 85 | } |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 86 | ValidationStateTracker::PreCallRecordCreateBuffer(device, pCreateInfo, pAllocator, pBuffer, cb_state_data); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 87 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 88 | // Perform initializations that can be done at Create Device time. |
Jeremy Gebben | 36a3b83 | 2022-03-23 10:54:18 -0600 | [diff] [blame] | 89 | void GpuAssisted::CreateDevice(const VkDeviceCreateInfo *pCreateInfo) { |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 90 | // GpuAssistedBase::CreateDevice will set up bindings |
| 91 | VkDescriptorSetLayoutBinding binding = {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, |
| 92 | VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT | |
| 93 | VK_SHADER_STAGE_MESH_BIT_NV | VK_SHADER_STAGE_TASK_BIT_NV | |
| 94 | kShaderStageAllRayTracing, |
| 95 | NULL}; |
| 96 | bindings_.push_back(binding); |
| 97 | for (auto i = 1; i < 3; i++) { |
| 98 | binding.binding = i; |
| 99 | bindings_.push_back(binding); |
| 100 | } |
| 101 | GpuAssistedBase::CreateDevice(pCreateInfo); |
Mark Lobodzinski | 5dc3dcd | 2019-04-23 14:26:28 -0600 | [diff] [blame] | 102 | |
Jeremy Gebben | 36a3b83 | 2022-03-23 10:54:18 -0600 | [diff] [blame] | 103 | if (enabled_features.core.robustBufferAccess || enabled_features.robustness2_features.robustBufferAccess2) { |
| 104 | buffer_oob_enabled = false; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 105 | } else { |
Tony-LunarG | bcfeccf | 2022-04-19 09:14:35 -0600 | [diff] [blame] | 106 | buffer_oob_enabled = GpuGetOption("khronos_validation.gpuav_buffer_oob", true); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 107 | } |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 108 | |
Tony-LunarG | bcfeccf | 2022-04-19 09:14:35 -0600 | [diff] [blame] | 109 | bool validate_descriptor_indexing = GpuGetOption("khronos_validation.gpuav_descriptor_indexing", true); |
| 110 | validate_draw_indirect = GpuGetOption("khronos_validation.validate_draw_indirect", true); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 111 | |
Jeremy Gebben | 36a3b83 | 2022-03-23 10:54:18 -0600 | [diff] [blame] | 112 | if (phys_dev_props.apiVersion < VK_API_VERSION_1_1) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 113 | ReportSetupProblem(device, "GPU-Assisted validation requires Vulkan 1.1 or later. GPU-Assisted Validation disabled."); |
Jeremy Gebben | 36a3b83 | 2022-03-23 10:54:18 -0600 | [diff] [blame] | 114 | aborted = true; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 115 | return; |
| 116 | } |
Tony-LunarG | 2ab9ede | 2019-05-10 14:34:31 -0600 | [diff] [blame] | 117 | |
Jeremy Gebben | 36a3b83 | 2022-03-23 10:54:18 -0600 | [diff] [blame] | 118 | DispatchGetPhysicalDeviceFeatures(physical_device, &supported_features); |
Tony-LunarG | 04dc83c | 2020-07-07 13:53:02 -0600 | [diff] [blame] | 119 | if (!supported_features.fragmentStoresAndAtomics || !supported_features.vertexPipelineStoresAndAtomics) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 120 | ReportSetupProblem(device, |
Tony-LunarG | 7c668ab | 2019-08-28 16:13:01 -0600 | [diff] [blame] | 121 | "GPU-Assisted validation requires fragmentStoresAndAtomics and vertexPipelineStoresAndAtomics. " |
| 122 | "GPU-Assisted Validation disabled."); |
Jeremy Gebben | 36a3b83 | 2022-03-23 10:54:18 -0600 | [diff] [blame] | 123 | aborted = true; |
Tony-LunarG | 7c668ab | 2019-08-28 16:13:01 -0600 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
sfricke-samsung | 45996a4 | 2021-09-16 13:45:27 -0700 | [diff] [blame] | 127 | if ((IsExtEnabled(device_extensions.vk_ext_buffer_device_address) || |
| 128 | IsExtEnabled(device_extensions.vk_khr_buffer_device_address)) && |
Tony-LunarG | 04dc83c | 2020-07-07 13:53:02 -0600 | [diff] [blame] | 129 | !supported_features.shaderInt64) { |
Mark Lobodzinski | afa7cb8 | 2020-01-29 16:49:36 -0700 | [diff] [blame] | 130 | LogWarning(device, "UNASSIGNED-GPU-Assisted Validation Warning", |
| 131 | "shaderInt64 feature is not available. No buffer device address checking will be attempted"); |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 132 | } |
Jeremy Gebben | 36a3b83 | 2022-03-23 10:54:18 -0600 | [diff] [blame] | 133 | shaderInt64 = supported_features.shaderInt64; |
| 134 | output_buffer_size = sizeof(uint32_t) * (spvtools::kInstMaxOutCnt + 1); |
Tony-LunarG | aef435b | 2021-10-14 14:49:06 -0600 | [diff] [blame] | 135 | if (validate_descriptor_indexing) { |
Jeremy Gebben | 36a3b83 | 2022-03-23 10:54:18 -0600 | [diff] [blame] | 136 | descriptor_indexing = CheckForDescriptorIndexing(enabled_features); |
Tony-LunarG | aef435b | 2021-10-14 14:49:06 -0600 | [diff] [blame] | 137 | } |
Tony-LunarG | 3b1c19f | 2022-05-02 14:11:06 -0600 | [diff] [blame] | 138 | bool use_linear_output_pool = GpuGetOption("khronos_validation.vma_linear_output", true); |
Tony-LunarG | 20d18a7 | 2022-04-19 11:01:47 -0600 | [diff] [blame] | 139 | if (use_linear_output_pool) { |
| 140 | auto output_buffer_create_info = LvlInitStruct<VkBufferCreateInfo>(); |
| 141 | output_buffer_create_info.size = output_buffer_size; |
| 142 | output_buffer_create_info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 143 | VmaAllocationCreateInfo alloc_create_info = {}; |
| 144 | alloc_create_info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 145 | uint32_t mem_type_index; |
| 146 | vmaFindMemoryTypeIndexForBufferInfo(vmaAllocator, &output_buffer_create_info, &alloc_create_info, &mem_type_index); |
| 147 | VmaPoolCreateInfo pool_create_info = {}; |
| 148 | pool_create_info.memoryTypeIndex = mem_type_index; |
| 149 | pool_create_info.blockSize = 0; |
| 150 | pool_create_info.maxBlockCount = 0; |
| 151 | pool_create_info.flags = VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT; |
| 152 | VkResult result = vmaCreatePool(vmaAllocator, &pool_create_info, &output_buffer_pool); |
| 153 | if (result != VK_SUCCESS) { |
| 154 | ReportSetupProblem(device, "Unable to create VMA memory pool"); |
| 155 | } |
| 156 | } |
| 157 | |
Jeremy Gebben | 36a3b83 | 2022-03-23 10:54:18 -0600 | [diff] [blame] | 158 | CreateAccelerationStructureBuildValidationState(); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 159 | } |
| 160 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 161 | void GpuAssistedPreDrawValidationState::Destroy(VkDevice device) { |
| 162 | if (globals_created) { |
| 163 | DispatchDestroyShaderModule(device, shader_module, nullptr); |
| 164 | DispatchDestroyDescriptorSetLayout(device, ds_layout, nullptr); |
| 165 | DispatchDestroyPipelineLayout(device, pipeline_layout, nullptr); |
| 166 | auto to_destroy = renderpass_to_pipeline.snapshot(); |
| 167 | for (auto &entry: to_destroy) { |
| 168 | DispatchDestroyPipeline(device, entry.second, nullptr); |
| 169 | renderpass_to_pipeline.erase(entry.first); |
| 170 | } |
| 171 | globals_created = false; |
| 172 | } |
| 173 | } |
| 174 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 175 | // Clean up device-related resources |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 176 | void GpuAssisted::PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 177 | DestroyAccelerationStructureBuildValidationState(); |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 178 | pre_draw_validation_state.Destroy(device); |
Tony-LunarG | 20d18a7 | 2022-04-19 11:01:47 -0600 | [diff] [blame] | 179 | if (output_buffer_pool) { |
| 180 | vmaDestroyPool(vmaAllocator, output_buffer_pool); |
| 181 | } |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 182 | GpuAssistedBase::PreCallRecordDestroyDevice(device, pAllocator); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 183 | } |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 184 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 185 | void GpuAssisted::CreateAccelerationStructureBuildValidationState() { |
| 186 | if (aborted) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 190 | auto &as_validation_state = acceleration_structure_validation_state; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 191 | if (as_validation_state.initialized) { |
| 192 | return; |
| 193 | } |
| 194 | |
sfricke-samsung | 45996a4 | 2021-09-16 13:45:27 -0700 | [diff] [blame] | 195 | if (!IsExtEnabled(device_extensions.vk_nv_ray_tracing)) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
| 199 | // Outline: |
| 200 | // - Create valid bottom level acceleration structure which acts as replacement |
| 201 | // - Create and load vertex buffer |
| 202 | // - Create and load index buffer |
| 203 | // - Create, allocate memory for, and bind memory for acceleration structure |
| 204 | // - Query acceleration structure handle |
| 205 | // - Create command pool and command buffer |
| 206 | // - Record build acceleration structure command |
| 207 | // - Submit command buffer and wait for completion |
| 208 | // - Cleanup |
| 209 | // - Create compute pipeline for validating instance buffers |
| 210 | // - Create descriptor set layout |
| 211 | // - Create pipeline layout |
| 212 | // - Create pipeline |
| 213 | // - Cleanup |
| 214 | |
| 215 | VkResult result = VK_SUCCESS; |
| 216 | |
| 217 | VkBuffer vbo = VK_NULL_HANDLE; |
| 218 | VmaAllocation vbo_allocation = VK_NULL_HANDLE; |
| 219 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 220 | auto vbo_ci = LvlInitStruct<VkBufferCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 221 | vbo_ci.size = sizeof(float) * 9; |
| 222 | vbo_ci.usage = VK_BUFFER_USAGE_RAY_TRACING_BIT_NV; |
| 223 | |
| 224 | VmaAllocationCreateInfo vbo_ai = {}; |
| 225 | vbo_ai.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; |
| 226 | vbo_ai.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 227 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 228 | result = vmaCreateBuffer(vmaAllocator, &vbo_ci, &vbo_ai, &vbo, &vbo_allocation, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 229 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 230 | ReportSetupProblem(device, "Failed to create vertex buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | if (result == VK_SUCCESS) { |
| 235 | uint8_t *mapped_vbo_buffer = nullptr; |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 236 | result = vmaMapMemory(vmaAllocator, vbo_allocation, reinterpret_cast<void **>(&mapped_vbo_buffer)); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 237 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 238 | ReportSetupProblem(device, "Failed to map vertex buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 239 | } else { |
| 240 | const std::vector<float> vertices = {1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f}; |
| 241 | std::memcpy(mapped_vbo_buffer, (uint8_t *)vertices.data(), sizeof(float) * vertices.size()); |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 242 | vmaUnmapMemory(vmaAllocator, vbo_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | VkBuffer ibo = VK_NULL_HANDLE; |
| 247 | VmaAllocation ibo_allocation = VK_NULL_HANDLE; |
| 248 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 249 | auto ibo_ci = LvlInitStruct<VkBufferCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 250 | ibo_ci.size = sizeof(uint32_t) * 3; |
| 251 | ibo_ci.usage = VK_BUFFER_USAGE_RAY_TRACING_BIT_NV; |
| 252 | |
| 253 | VmaAllocationCreateInfo ibo_ai = {}; |
| 254 | ibo_ai.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; |
| 255 | ibo_ai.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 256 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 257 | result = vmaCreateBuffer(vmaAllocator, &ibo_ci, &ibo_ai, &ibo, &ibo_allocation, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 258 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 259 | ReportSetupProblem(device, "Failed to create index buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | if (result == VK_SUCCESS) { |
| 264 | uint8_t *mapped_ibo_buffer = nullptr; |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 265 | result = vmaMapMemory(vmaAllocator, ibo_allocation, reinterpret_cast<void **>(&mapped_ibo_buffer)); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 266 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 267 | ReportSetupProblem(device, "Failed to map index buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 268 | } else { |
| 269 | const std::vector<uint32_t> indicies = {0, 1, 2}; |
| 270 | std::memcpy(mapped_ibo_buffer, (uint8_t *)indicies.data(), sizeof(uint32_t) * indicies.size()); |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 271 | vmaUnmapMemory(vmaAllocator, ibo_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 275 | auto geometry = LvlInitStruct<VkGeometryNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 276 | geometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_NV; |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 277 | geometry.geometry.triangles = LvlInitStruct<VkGeometryTrianglesNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 278 | geometry.geometry.triangles.vertexData = vbo; |
| 279 | geometry.geometry.triangles.vertexOffset = 0; |
| 280 | geometry.geometry.triangles.vertexCount = 3; |
| 281 | geometry.geometry.triangles.vertexStride = 12; |
| 282 | geometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT; |
| 283 | geometry.geometry.triangles.indexData = ibo; |
| 284 | geometry.geometry.triangles.indexOffset = 0; |
| 285 | geometry.geometry.triangles.indexCount = 3; |
| 286 | geometry.geometry.triangles.indexType = VK_INDEX_TYPE_UINT32; |
| 287 | geometry.geometry.triangles.transformData = VK_NULL_HANDLE; |
| 288 | geometry.geometry.triangles.transformOffset = 0; |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 289 | geometry.geometry.aabbs = LvlInitStruct<VkGeometryAABBNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 290 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 291 | auto as_ci = LvlInitStruct<VkAccelerationStructureCreateInfoNV>(); |
| 292 | as_ci.info = LvlInitStruct<VkAccelerationStructureInfoNV>(); |
Tony-LunarG | 80fecea | 2022-06-28 10:05:21 -0600 | [diff] [blame] | 293 | as_ci.info.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_NV; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 294 | as_ci.info.instanceCount = 0; |
| 295 | as_ci.info.geometryCount = 1; |
| 296 | as_ci.info.pGeometries = &geometry; |
| 297 | if (result == VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 298 | result = DispatchCreateAccelerationStructureNV(device, &as_ci, nullptr, &as_validation_state.replacement_as); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 299 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 300 | ReportSetupProblem(device, "Failed to create acceleration structure for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | VkMemoryRequirements2 as_mem_requirements = {}; |
| 305 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 306 | auto as_mem_requirements_info = LvlInitStruct<VkAccelerationStructureMemoryRequirementsInfoNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 307 | as_mem_requirements_info.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_OBJECT_NV; |
| 308 | as_mem_requirements_info.accelerationStructure = as_validation_state.replacement_as; |
| 309 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 310 | DispatchGetAccelerationStructureMemoryRequirementsNV(device, &as_mem_requirements_info, &as_mem_requirements); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | VmaAllocationInfo as_memory_ai = {}; |
| 314 | if (result == VK_SUCCESS) { |
| 315 | VmaAllocationCreateInfo as_memory_aci = {}; |
| 316 | as_memory_aci.usage = VMA_MEMORY_USAGE_GPU_ONLY; |
| 317 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 318 | result = vmaAllocateMemory(vmaAllocator, &as_mem_requirements.memoryRequirements, &as_memory_aci, |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 319 | &as_validation_state.replacement_as_allocation, &as_memory_ai); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 320 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 321 | ReportSetupProblem(device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 322 | "Failed to alloc acceleration structure memory for acceleration structure build validation."); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 327 | auto as_bind_info = LvlInitStruct<VkBindAccelerationStructureMemoryInfoNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 328 | as_bind_info.accelerationStructure = as_validation_state.replacement_as; |
| 329 | as_bind_info.memory = as_memory_ai.deviceMemory; |
| 330 | as_bind_info.memoryOffset = as_memory_ai.offset; |
| 331 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 332 | result = DispatchBindAccelerationStructureMemoryNV(device, 1, &as_bind_info); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 333 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 334 | ReportSetupProblem(device, "Failed to bind acceleration structure memory for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | if (result == VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 339 | result = DispatchGetAccelerationStructureHandleNV(device, as_validation_state.replacement_as, sizeof(uint64_t), |
| 340 | &as_validation_state.replacement_as_handle); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 341 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 342 | ReportSetupProblem(device, "Failed to get acceleration structure handle for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | VkMemoryRequirements2 scratch_mem_requirements = {}; |
| 347 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 348 | auto scratch_mem_requirements_info = LvlInitStruct<VkAccelerationStructureMemoryRequirementsInfoNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 349 | scratch_mem_requirements_info.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_BUILD_SCRATCH_NV; |
| 350 | scratch_mem_requirements_info.accelerationStructure = as_validation_state.replacement_as; |
| 351 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 352 | DispatchGetAccelerationStructureMemoryRequirementsNV(device, &scratch_mem_requirements_info, &scratch_mem_requirements); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | VkBuffer scratch = VK_NULL_HANDLE; |
Tony-LunarG | 1890028 | 2020-05-20 12:34:33 -0600 | [diff] [blame] | 356 | VmaAllocation scratch_allocation = {}; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 357 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 358 | auto scratch_ci = LvlInitStruct<VkBufferCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 359 | scratch_ci.size = scratch_mem_requirements.memoryRequirements.size; |
| 360 | scratch_ci.usage = VK_BUFFER_USAGE_RAY_TRACING_BIT_NV; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 361 | VmaAllocationCreateInfo scratch_aci = {}; |
| 362 | scratch_aci.usage = VMA_MEMORY_USAGE_GPU_ONLY; |
| 363 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 364 | result = vmaCreateBuffer(vmaAllocator, &scratch_ci, &scratch_aci, &scratch, &scratch_allocation, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 365 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 366 | ReportSetupProblem(device, "Failed to create scratch buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | VkCommandPool command_pool = VK_NULL_HANDLE; |
| 371 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 372 | auto command_pool_ci = LvlInitStruct<VkCommandPoolCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 373 | command_pool_ci.queueFamilyIndex = 0; |
| 374 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 375 | result = DispatchCreateCommandPool(device, &command_pool_ci, nullptr, &command_pool); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 376 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 377 | ReportSetupProblem(device, "Failed to create command pool for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | VkCommandBuffer command_buffer = VK_NULL_HANDLE; |
| 382 | |
| 383 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 384 | auto command_buffer_ai = LvlInitStruct<VkCommandBufferAllocateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 385 | command_buffer_ai.commandPool = command_pool; |
| 386 | command_buffer_ai.commandBufferCount = 1; |
| 387 | command_buffer_ai.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 388 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 389 | result = DispatchAllocateCommandBuffers(device, &command_buffer_ai, &command_buffer); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 390 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 391 | ReportSetupProblem(device, "Failed to create command buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // Hook up command buffer dispatch |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 395 | vkSetDeviceLoaderData(device, command_buffer); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 399 | auto command_buffer_bi = LvlInitStruct<VkCommandBufferBeginInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 400 | |
| 401 | result = DispatchBeginCommandBuffer(command_buffer, &command_buffer_bi); |
| 402 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 403 | ReportSetupProblem(device, "Failed to begin command buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | if (result == VK_SUCCESS) { |
| 408 | DispatchCmdBuildAccelerationStructureNV(command_buffer, &as_ci.info, VK_NULL_HANDLE, 0, VK_FALSE, |
| 409 | as_validation_state.replacement_as, VK_NULL_HANDLE, scratch, 0); |
| 410 | DispatchEndCommandBuffer(command_buffer); |
| 411 | } |
| 412 | |
| 413 | VkQueue queue = VK_NULL_HANDLE; |
| 414 | if (result == VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 415 | DispatchGetDeviceQueue(device, 0, 0, &queue); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 416 | |
| 417 | // Hook up queue dispatch |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 418 | vkSetDeviceLoaderData(device, queue); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 419 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 420 | auto submit_info = LvlInitStruct<VkSubmitInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 421 | submit_info.commandBufferCount = 1; |
| 422 | submit_info.pCommandBuffers = &command_buffer; |
| 423 | result = DispatchQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE); |
| 424 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 425 | ReportSetupProblem(device, "Failed to submit command buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | if (result == VK_SUCCESS) { |
| 430 | result = DispatchQueueWaitIdle(queue); |
| 431 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 432 | ReportSetupProblem(device, "Failed to wait for queue idle for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
| 436 | if (vbo != VK_NULL_HANDLE) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 437 | vmaDestroyBuffer(vmaAllocator, vbo, vbo_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 438 | } |
| 439 | if (ibo != VK_NULL_HANDLE) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 440 | vmaDestroyBuffer(vmaAllocator, ibo, ibo_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 441 | } |
| 442 | if (scratch != VK_NULL_HANDLE) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 443 | vmaDestroyBuffer(vmaAllocator, scratch, scratch_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 444 | } |
| 445 | if (command_pool != VK_NULL_HANDLE) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 446 | DispatchDestroyCommandPool(device, command_pool, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 449 | if (debug_desc_layout == VK_NULL_HANDLE) { |
| 450 | ReportSetupProblem(device, "Failed to find descriptor set layout for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 451 | result = VK_INCOMPLETE; |
| 452 | } |
| 453 | |
| 454 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 455 | auto pipeline_layout_ci = LvlInitStruct<VkPipelineLayoutCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 456 | pipeline_layout_ci.setLayoutCount = 1; |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 457 | pipeline_layout_ci.pSetLayouts = &debug_desc_layout; |
| 458 | result = DispatchCreatePipelineLayout(device, &pipeline_layout_ci, 0, &as_validation_state.pipeline_layout); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 459 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 460 | ReportSetupProblem(device, "Failed to create pipeline layout for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | VkShaderModule shader_module = VK_NULL_HANDLE; |
| 465 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 466 | auto shader_module_ci = LvlInitStruct<VkShaderModuleCreateInfo>(); |
sjfricke | becf072 | 2022-08-05 16:15:46 +0900 | [diff] [blame^] | 467 | shader_module_ci.codeSize = sizeof(gpu_as_inspection_comp); |
| 468 | shader_module_ci.pCode = gpu_as_inspection_comp; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 469 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 470 | result = DispatchCreateShaderModule(device, &shader_module_ci, nullptr, &shader_module); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 471 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 472 | ReportSetupProblem(device, "Failed to create compute shader module for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 477 | auto pipeline_stage_ci = LvlInitStruct<VkPipelineShaderStageCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 478 | pipeline_stage_ci.stage = VK_SHADER_STAGE_COMPUTE_BIT; |
| 479 | pipeline_stage_ci.module = shader_module; |
| 480 | pipeline_stage_ci.pName = "main"; |
| 481 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 482 | auto pipeline_ci = LvlInitStruct<VkComputePipelineCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 483 | pipeline_ci.stage = pipeline_stage_ci; |
| 484 | pipeline_ci.layout = as_validation_state.pipeline_layout; |
| 485 | |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 486 | result = DispatchCreateComputePipelines(device, VK_NULL_HANDLE, 1, &pipeline_ci, nullptr, &as_validation_state.pipeline); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 487 | if (result != VK_SUCCESS) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 488 | ReportSetupProblem(device, "Failed to create compute pipeline for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | if (shader_module != VK_NULL_HANDLE) { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 493 | DispatchDestroyShaderModule(device, shader_module, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if (result == VK_SUCCESS) { |
| 497 | as_validation_state.initialized = true; |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 498 | LogInfo(device, "UNASSIGNED-GPU-Assisted Validation.", "Acceleration Structure Building GPU Validation Enabled."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 499 | } else { |
Jeremy Gebben | 2178201 | 2022-03-15 16:23:27 -0600 | [diff] [blame] | 500 | aborted = true; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 504 | void GpuAssisted::DestroyAccelerationStructureBuildValidationState() { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 505 | auto &as_validation_state = acceleration_structure_validation_state; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 506 | if (as_validation_state.pipeline != VK_NULL_HANDLE) { |
| 507 | DispatchDestroyPipeline(device, as_validation_state.pipeline, nullptr); |
| 508 | } |
| 509 | if (as_validation_state.pipeline_layout != VK_NULL_HANDLE) { |
| 510 | DispatchDestroyPipelineLayout(device, as_validation_state.pipeline_layout, nullptr); |
| 511 | } |
| 512 | if (as_validation_state.replacement_as != VK_NULL_HANDLE) { |
| 513 | DispatchDestroyAccelerationStructureNV(device, as_validation_state.replacement_as, nullptr); |
| 514 | } |
| 515 | if (as_validation_state.replacement_as_allocation != VK_NULL_HANDLE) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 516 | vmaFreeMemory(vmaAllocator, as_validation_state.replacement_as_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 520 | struct GPUAV_RESTORABLE_PIPELINE_STATE { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 521 | VkPipelineBindPoint pipeline_bind_point = VK_PIPELINE_BIND_POINT_MAX_ENUM; |
| 522 | VkPipeline pipeline = VK_NULL_HANDLE; |
| 523 | VkPipelineLayout pipeline_layout = VK_NULL_HANDLE; |
| 524 | std::vector<VkDescriptorSet> descriptor_sets; |
| 525 | std::vector<std::vector<uint32_t>> dynamic_offsets; |
| 526 | uint32_t push_descriptor_set_index = 0; |
| 527 | std::vector<safe_VkWriteDescriptorSet> push_descriptor_set_writes; |
| 528 | std::vector<uint8_t> push_constants_data; |
| 529 | PushConstantRangesId push_constants_ranges; |
| 530 | |
| 531 | void Create(CMD_BUFFER_STATE *cb_state, VkPipelineBindPoint bind_point) { |
| 532 | pipeline_bind_point = bind_point; |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 533 | const auto lv_bind_point = ConvertToLvlBindPoint(bind_point); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 534 | |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 535 | LAST_BOUND_STATE &last_bound = cb_state->lastBound[lv_bind_point]; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 536 | if (last_bound.pipeline_state) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 537 | pipeline = last_bound.pipeline_state->pipeline(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 538 | pipeline_layout = last_bound.pipeline_layout; |
| 539 | descriptor_sets.reserve(last_bound.per_set.size()); |
| 540 | for (std::size_t i = 0; i < last_bound.per_set.size(); i++) { |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 541 | const auto &bound_descriptor_set = last_bound.per_set[i].bound_descriptor_set; |
ziga-lunarg | e0b552b | 2021-09-05 21:39:57 +0200 | [diff] [blame] | 542 | if (bound_descriptor_set) { |
| 543 | descriptor_sets.push_back(bound_descriptor_set->GetSet()); |
| 544 | if (bound_descriptor_set->IsPushDescriptor()) { |
| 545 | push_descriptor_set_index = static_cast<uint32_t>(i); |
| 546 | } |
| 547 | dynamic_offsets.push_back(last_bound.per_set[i].dynamicOffsets); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 548 | } |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | if (last_bound.push_descriptor_set) { |
| 552 | push_descriptor_set_writes = last_bound.push_descriptor_set->GetWrites(); |
| 553 | } |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 554 | const auto &pipeline_layout = last_bound.pipeline_state->PipelineLayoutState(); |
| 555 | if (pipeline_layout->push_constant_ranges == cb_state->push_constant_data_ranges) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 556 | push_constants_data = cb_state->push_constant_data; |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 557 | push_constants_ranges = pipeline_layout->push_constant_ranges; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | void Restore(VkCommandBuffer command_buffer) const { |
| 563 | if (pipeline != VK_NULL_HANDLE) { |
| 564 | DispatchCmdBindPipeline(command_buffer, pipeline_bind_point, pipeline); |
| 565 | if (!descriptor_sets.empty()) { |
| 566 | for (std::size_t i = 0; i < descriptor_sets.size(); i++) { |
| 567 | VkDescriptorSet descriptor_set = descriptor_sets[i]; |
| 568 | if (descriptor_set != VK_NULL_HANDLE) { |
| 569 | DispatchCmdBindDescriptorSets(command_buffer, pipeline_bind_point, pipeline_layout, |
| 570 | static_cast<uint32_t>(i), 1, &descriptor_set, |
| 571 | static_cast<uint32_t>(dynamic_offsets[i].size()), dynamic_offsets[i].data()); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | if (!push_descriptor_set_writes.empty()) { |
| 576 | DispatchCmdPushDescriptorSetKHR(command_buffer, pipeline_bind_point, pipeline_layout, push_descriptor_set_index, |
| 577 | static_cast<uint32_t>(push_descriptor_set_writes.size()), |
| 578 | reinterpret_cast<const VkWriteDescriptorSet *>(push_descriptor_set_writes.data())); |
| 579 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 580 | if (!push_constants_data.empty()) { |
| 581 | for (const auto &push_constant_range : *push_constants_ranges) { |
| 582 | if (push_constant_range.size == 0) continue; |
| 583 | DispatchCmdPushConstants(command_buffer, pipeline_layout, push_constant_range.stageFlags, |
| 584 | push_constant_range.offset, push_constant_range.size, push_constants_data.data()); |
| 585 | } |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | } |
| 589 | }; |
| 590 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 591 | void GpuAssisted::PreCallRecordCmdBuildAccelerationStructureNV(VkCommandBuffer commandBuffer, |
| 592 | const VkAccelerationStructureInfoNV *pInfo, VkBuffer instanceData, |
| 593 | VkDeviceSize instanceOffset, VkBool32 update, |
| 594 | VkAccelerationStructureNV dst, VkAccelerationStructureNV src, |
| 595 | VkBuffer scratch, VkDeviceSize scratchOffset) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 596 | ValidationStateTracker::PreCallRecordCmdBuildAccelerationStructureNV(commandBuffer, pInfo, instanceData, instanceOffset, update, |
| 597 | dst, src, scratch, scratchOffset); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 598 | if (pInfo == nullptr || pInfo->type != VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_NV) { |
| 599 | return; |
| 600 | } |
| 601 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 602 | auto &as_validation_state = acceleration_structure_validation_state; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 603 | if (!as_validation_state.initialized) { |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | // Empty acceleration structure is valid according to the spec. |
| 608 | if (pInfo->instanceCount == 0 || instanceData == VK_NULL_HANDLE) { |
| 609 | return; |
| 610 | } |
| 611 | |
Jeremy Gebben | 04697b0 | 2022-03-23 16:18:12 -0600 | [diff] [blame] | 612 | auto cb_state = GetWrite<gpuav_state::CommandBuffer>(commandBuffer); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 613 | assert(cb_state != nullptr); |
| 614 | |
| 615 | std::vector<uint64_t> current_valid_handles; |
Jeremy Gebben | b7bfbd0 | 2021-11-01 15:17:50 -0600 | [diff] [blame] | 616 | ForEach<ACCELERATION_STRUCTURE_STATE>([¤t_valid_handles](const ACCELERATION_STRUCTURE_STATE &as_state) { |
Jeff Bolz | 95176d0 | 2020-04-01 00:36:16 -0500 | [diff] [blame] | 617 | if (as_state.built && as_state.create_infoNV.info.type == VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_NV) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 618 | current_valid_handles.push_back(as_state.opaque_handle); |
| 619 | } |
Jeremy Gebben | b7bfbd0 | 2021-11-01 15:17:50 -0600 | [diff] [blame] | 620 | }); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 621 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 622 | GpuAssistedAccelerationStructureBuildValidationBufferInfo as_validation_buffer_info = {}; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 623 | as_validation_buffer_info.acceleration_structure = dst; |
| 624 | |
| 625 | const VkDeviceSize validation_buffer_size = |
| 626 | // One uint for number of instances to validate |
| 627 | 4 + |
| 628 | // Two uint for the replacement acceleration structure handle |
| 629 | 8 + |
| 630 | // One uint for number of invalid handles found |
| 631 | 4 + |
| 632 | // Two uint for the first invalid handle found |
| 633 | 8 + |
| 634 | // One uint for the number of current valid handles |
| 635 | 4 + |
| 636 | // Two uint for each current valid handle |
| 637 | (8 * current_valid_handles.size()); |
| 638 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 639 | auto validation_buffer_create_info = LvlInitStruct<VkBufferCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 640 | validation_buffer_create_info.size = validation_buffer_size; |
| 641 | validation_buffer_create_info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 642 | |
| 643 | VmaAllocationCreateInfo validation_buffer_alloc_info = {}; |
| 644 | validation_buffer_alloc_info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; |
| 645 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 646 | VkResult result = vmaCreateBuffer(vmaAllocator, &validation_buffer_create_info, &validation_buffer_alloc_info, |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 647 | &as_validation_buffer_info.buffer, &as_validation_buffer_info.buffer_allocation, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 648 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 649 | ReportSetupProblem(device, "Unable to allocate device memory. Device could become unstable."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 650 | aborted = true; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | |
| 654 | GpuAccelerationStructureBuildValidationBuffer *mapped_validation_buffer = nullptr; |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 655 | result = vmaMapMemory(vmaAllocator, as_validation_buffer_info.buffer_allocation, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 656 | reinterpret_cast<void **>(&mapped_validation_buffer)); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 657 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 658 | ReportSetupProblem(device, "Unable to allocate device memory for acceleration structure build val buffer."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 659 | aborted = true; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 660 | return; |
| 661 | } |
| 662 | |
| 663 | mapped_validation_buffer->instances_to_validate = pInfo->instanceCount; |
| 664 | mapped_validation_buffer->replacement_handle_bits_0 = |
| 665 | reinterpret_cast<const uint32_t *>(&as_validation_state.replacement_as_handle)[0]; |
| 666 | mapped_validation_buffer->replacement_handle_bits_1 = |
| 667 | reinterpret_cast<const uint32_t *>(&as_validation_state.replacement_as_handle)[1]; |
| 668 | mapped_validation_buffer->invalid_handle_found = 0; |
| 669 | mapped_validation_buffer->invalid_handle_bits_0 = 0; |
| 670 | mapped_validation_buffer->invalid_handle_bits_1 = 0; |
| 671 | mapped_validation_buffer->valid_handles_count = static_cast<uint32_t>(current_valid_handles.size()); |
| 672 | |
| 673 | uint32_t *mapped_valid_handles = reinterpret_cast<uint32_t *>(&mapped_validation_buffer[1]); |
| 674 | for (std::size_t i = 0; i < current_valid_handles.size(); i++) { |
| 675 | const uint64_t current_valid_handle = current_valid_handles[i]; |
| 676 | |
| 677 | *mapped_valid_handles = reinterpret_cast<const uint32_t *>(¤t_valid_handle)[0]; |
| 678 | ++mapped_valid_handles; |
| 679 | *mapped_valid_handles = reinterpret_cast<const uint32_t *>(¤t_valid_handle)[1]; |
| 680 | ++mapped_valid_handles; |
| 681 | } |
| 682 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 683 | vmaUnmapMemory(vmaAllocator, as_validation_buffer_info.buffer_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 684 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 685 | static constexpr const VkDeviceSize k_instance_size = 64; |
| 686 | const VkDeviceSize instance_buffer_size = k_instance_size * pInfo->instanceCount; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 687 | |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 688 | result = desc_set_manager->GetDescriptorSet(&as_validation_buffer_info.descriptor_pool, debug_desc_layout, |
| 689 | &as_validation_buffer_info.descriptor_set); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 690 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 691 | ReportSetupProblem(device, "Unable to get descriptor set for acceleration structure build."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 692 | aborted = true; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 693 | return; |
| 694 | } |
| 695 | |
| 696 | VkDescriptorBufferInfo descriptor_buffer_infos[2] = {}; |
| 697 | descriptor_buffer_infos[0].buffer = instanceData; |
| 698 | descriptor_buffer_infos[0].offset = instanceOffset; |
| 699 | descriptor_buffer_infos[0].range = instance_buffer_size; |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 700 | descriptor_buffer_infos[1].buffer = as_validation_buffer_info.buffer; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 701 | descriptor_buffer_infos[1].offset = 0; |
| 702 | descriptor_buffer_infos[1].range = validation_buffer_size; |
| 703 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 704 | VkWriteDescriptorSet descriptor_set_writes[2] = { |
| 705 | LvlInitStruct<VkWriteDescriptorSet>(), |
| 706 | LvlInitStruct<VkWriteDescriptorSet>(), |
| 707 | }; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 708 | descriptor_set_writes[0].dstSet = as_validation_buffer_info.descriptor_set; |
| 709 | descriptor_set_writes[0].dstBinding = 0; |
| 710 | descriptor_set_writes[0].descriptorCount = 1; |
| 711 | descriptor_set_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 712 | descriptor_set_writes[0].pBufferInfo = &descriptor_buffer_infos[0]; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 713 | descriptor_set_writes[1].dstSet = as_validation_buffer_info.descriptor_set; |
| 714 | descriptor_set_writes[1].dstBinding = 1; |
| 715 | descriptor_set_writes[1].descriptorCount = 1; |
| 716 | descriptor_set_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 717 | descriptor_set_writes[1].pBufferInfo = &descriptor_buffer_infos[1]; |
| 718 | |
| 719 | DispatchUpdateDescriptorSets(device, 2, descriptor_set_writes, 0, nullptr); |
| 720 | |
| 721 | // Issue a memory barrier to make sure anything writing to the instance buffer has finished. |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 722 | auto memory_barrier = LvlInitStruct<VkMemoryBarrier>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 723 | memory_barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT; |
| 724 | memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 725 | DispatchCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1, |
| 726 | &memory_barrier, 0, nullptr, 0, nullptr); |
| 727 | |
| 728 | // Save a copy of the compute pipeline state that needs to be restored. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 729 | GPUAV_RESTORABLE_PIPELINE_STATE restorable_state; |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 730 | restorable_state.Create(cb_state.get(), VK_PIPELINE_BIND_POINT_COMPUTE); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 731 | |
| 732 | // Switch to and launch the validation compute shader to find, replace, and report invalid acceleration structure handles. |
| 733 | DispatchCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, as_validation_state.pipeline); |
| 734 | DispatchCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, as_validation_state.pipeline_layout, 0, 1, |
| 735 | &as_validation_buffer_info.descriptor_set, 0, nullptr); |
| 736 | DispatchCmdDispatch(commandBuffer, 1, 1, 1); |
| 737 | |
| 738 | // Issue a buffer memory barrier to make sure that any invalid bottom level acceleration structure handles |
| 739 | // have been replaced by the validation compute shader before any builds take place. |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 740 | auto instance_buffer_barrier = LvlInitStruct<VkBufferMemoryBarrier>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 741 | instance_buffer_barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; |
| 742 | instance_buffer_barrier.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_NV; |
| 743 | instance_buffer_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 744 | instance_buffer_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 745 | instance_buffer_barrier.buffer = instanceData; |
| 746 | instance_buffer_barrier.offset = instanceOffset; |
| 747 | instance_buffer_barrier.size = instance_buffer_size; |
| 748 | DispatchCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, |
| 749 | VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_NV, 0, 0, nullptr, 1, &instance_buffer_barrier, 0, |
| 750 | nullptr); |
| 751 | |
| 752 | // Restore the previous compute pipeline state. |
| 753 | restorable_state.Restore(commandBuffer); |
| 754 | |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 755 | cb_state->as_validation_buffers.emplace_back(std::move(as_validation_buffer_info)); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 758 | void gpuav_state::CommandBuffer::ProcessAccelerationStructure(VkQueue queue) { |
sjfricke | 52defd4 | 2022-08-08 16:37:46 +0900 | [diff] [blame] | 759 | if (!has_build_as_cmd) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 760 | return; |
| 761 | } |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 762 | auto *device_state = static_cast<GpuAssisted *>(dev_data); |
| 763 | for (const auto &as_validation_buffer_info : as_validation_buffers) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 764 | GpuAccelerationStructureBuildValidationBuffer *mapped_validation_buffer = nullptr; |
| 765 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 766 | VkResult result = vmaMapMemory(device_state->vmaAllocator, as_validation_buffer_info.buffer_allocation, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 767 | reinterpret_cast<void **>(&mapped_validation_buffer)); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 768 | if (result == VK_SUCCESS) { |
| 769 | if (mapped_validation_buffer->invalid_handle_found > 0) { |
| 770 | uint64_t invalid_handle = 0; |
| 771 | reinterpret_cast<uint32_t *>(&invalid_handle)[0] = mapped_validation_buffer->invalid_handle_bits_0; |
| 772 | reinterpret_cast<uint32_t *>(&invalid_handle)[1] = mapped_validation_buffer->invalid_handle_bits_1; |
| 773 | |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 774 | device_state->LogError( |
| 775 | as_validation_buffer_info.acceleration_structure, "UNASSIGNED-AccelerationStructure", |
| 776 | "Attempted to build top level acceleration structure using invalid bottom level acceleration structure " |
| 777 | "handle (%" PRIu64 ")", |
| 778 | invalid_handle); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 779 | } |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 780 | vmaUnmapMemory(device_state->vmaAllocator, as_validation_buffer_info.buffer_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 785 | void GpuAssisted::PostCallRecordBindAccelerationStructureMemoryNV(VkDevice device, uint32_t bindInfoCount, |
| 786 | const VkBindAccelerationStructureMemoryInfoNV *pBindInfos, |
| 787 | VkResult result) { |
| 788 | if (VK_SUCCESS != result) return; |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 789 | ValidationStateTracker::PostCallRecordBindAccelerationStructureMemoryNV(device, bindInfoCount, pBindInfos, result); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 790 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
| 791 | const VkBindAccelerationStructureMemoryInfoNV &info = pBindInfos[i]; |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 792 | auto as_state = Get<ACCELERATION_STRUCTURE_STATE>(info.accelerationStructure); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 793 | if (as_state) { |
| 794 | DispatchGetAccelerationStructureHandleNV(device, info.accelerationStructure, 8, &as_state->opaque_handle); |
| 795 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 796 | } |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 797 | } |
Mark Lobodzinski | ff7d800 | 2019-02-13 13:01:26 -0700 | [diff] [blame] | 798 | |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 799 | // Free the device memory and descriptor set(s) associated with a command buffer. |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 800 | void GpuAssisted::DestroyBuffer(GpuAssistedBufferInfo &buffer_info) { |
| 801 | vmaDestroyBuffer(vmaAllocator, buffer_info.output_mem_block.buffer, buffer_info.output_mem_block.allocation); |
| 802 | if (buffer_info.di_input_mem_block.buffer) { |
| 803 | vmaDestroyBuffer(vmaAllocator, buffer_info.di_input_mem_block.buffer, buffer_info.di_input_mem_block.allocation); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 804 | } |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 805 | if (buffer_info.bda_input_mem_block.buffer) { |
| 806 | vmaDestroyBuffer(vmaAllocator, buffer_info.bda_input_mem_block.buffer, buffer_info.bda_input_mem_block.allocation); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 807 | } |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 808 | if (buffer_info.desc_set != VK_NULL_HANDLE) { |
| 809 | desc_set_manager->PutBackDescriptorSet(buffer_info.desc_pool, buffer_info.desc_set); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 810 | } |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 811 | if (buffer_info.pre_draw_resources.desc_set != VK_NULL_HANDLE) { |
| 812 | desc_set_manager->PutBackDescriptorSet(buffer_info.pre_draw_resources.desc_pool, buffer_info.pre_draw_resources.desc_set); |
| 813 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 814 | } |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 815 | |
| 816 | void GpuAssisted::DestroyBuffer(GpuAssistedAccelerationStructureBuildValidationBufferInfo &as_validation_buffer_info) { |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 817 | vmaDestroyBuffer(vmaAllocator, as_validation_buffer_info.buffer, as_validation_buffer_info.buffer_allocation); |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 818 | |
| 819 | if (as_validation_buffer_info.descriptor_set != VK_NULL_HANDLE) { |
| 820 | desc_set_manager->PutBackDescriptorSet(as_validation_buffer_info.descriptor_pool, as_validation_buffer_info.descriptor_set); |
| 821 | } |
| 822 | } |
| 823 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 824 | void GpuAssisted::PostCallRecordGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice, |
| 825 | VkPhysicalDeviceProperties *pPhysicalDeviceProperties) { |
| 826 | // There is an implicit layer that can cause this call to return 0 for maxBoundDescriptorSets - Ignore such calls |
Mark Lobodzinski | 90eea5b | 2020-05-15 12:54:00 -0600 | [diff] [blame] | 827 | if (enabled[gpu_validation_reserve_binding_slot] && pPhysicalDeviceProperties->limits.maxBoundDescriptorSets > 0) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 828 | if (pPhysicalDeviceProperties->limits.maxBoundDescriptorSets > 1) { |
| 829 | pPhysicalDeviceProperties->limits.maxBoundDescriptorSets -= 1; |
| 830 | } else { |
Mark Lobodzinski | afa7cb8 | 2020-01-29 16:49:36 -0700 | [diff] [blame] | 831 | LogWarning(physicalDevice, "UNASSIGNED-GPU-Assisted Validation Setup Error.", |
| 832 | "Unable to reserve descriptor binding slot on a device with only one slot."); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 833 | } |
| 834 | } |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 835 | ValidationStateTracker::PostCallRecordGetPhysicalDeviceProperties(physicalDevice, pPhysicalDeviceProperties); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | void GpuAssisted::PostCallRecordGetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice, |
| 839 | VkPhysicalDeviceProperties2 *pPhysicalDeviceProperties2) { |
| 840 | // There is an implicit layer that can cause this call to return 0 for maxBoundDescriptorSets - Ignore such calls |
Mark Lobodzinski | 90eea5b | 2020-05-15 12:54:00 -0600 | [diff] [blame] | 841 | if (enabled[gpu_validation_reserve_binding_slot] && pPhysicalDeviceProperties2->properties.limits.maxBoundDescriptorSets > 0) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 842 | if (pPhysicalDeviceProperties2->properties.limits.maxBoundDescriptorSets > 1) { |
| 843 | pPhysicalDeviceProperties2->properties.limits.maxBoundDescriptorSets -= 1; |
| 844 | } else { |
Mark Lobodzinski | afa7cb8 | 2020-01-29 16:49:36 -0700 | [diff] [blame] | 845 | LogWarning(physicalDevice, "UNASSIGNED-GPU-Assisted Validation Setup Error.", |
| 846 | "Unable to reserve descriptor binding slot on a device with only one slot."); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 847 | } |
| 848 | } |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 849 | ValidationStateTracker::PostCallRecordGetPhysicalDeviceProperties2(physicalDevice, pPhysicalDeviceProperties2); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 850 | } |
| 851 | |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 852 | void GpuAssisted::PreCallRecordDestroyRenderPass(VkDevice device, VkRenderPass renderPass, |
| 853 | const VkAllocationCallbacks *pAllocator) { |
Jeremy Gebben | bba3921 | 2022-03-29 16:39:06 -0600 | [diff] [blame] | 854 | auto pipeline = pre_draw_validation_state.renderpass_to_pipeline.pop(renderPass); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 855 | if (pipeline != pre_draw_validation_state.renderpass_to_pipeline.end()) { |
| 856 | DispatchDestroyPipeline(device, pipeline->second, nullptr); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 857 | } |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 858 | ValidationStateTracker::PreCallRecordDestroyRenderPass(device, renderPass, pAllocator); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 859 | } |
| 860 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 861 | // Call the SPIR-V Optimizer to run the instrumentation pass on the shader. |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 862 | bool GpuAssisted::InstrumentShader(const VkShaderModuleCreateInfo *pCreateInfo, std::vector<uint32_t> &new_pgm, |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 863 | uint32_t *unique_shader_id) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 864 | if (aborted) return false; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 865 | if (pCreateInfo->pCode[0] != spv::MagicNumber) return false; |
| 866 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 867 | const spvtools::MessageConsumer gpu_console_message_consumer = |
Tony-LunarG | 7964170 | 2020-07-13 15:43:05 -0600 | [diff] [blame] | 868 | [this](spv_message_level_t level, const char *, const spv_position_t &position, const char *message) -> void { |
| 869 | switch (level) { |
| 870 | case SPV_MSG_FATAL: |
| 871 | case SPV_MSG_INTERNAL_ERROR: |
| 872 | case SPV_MSG_ERROR: |
| 873 | this->LogError(this->device, "UNASSIGNED-GPU-Assisted", "Error during shader instrumentation: line %zu: %s", |
| 874 | position.index, message); |
| 875 | break; |
| 876 | default: |
| 877 | break; |
| 878 | } |
| 879 | }; |
| 880 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 881 | // Load original shader SPIR-V |
| 882 | uint32_t num_words = static_cast<uint32_t>(pCreateInfo->codeSize / 4); |
| 883 | new_pgm.clear(); |
| 884 | new_pgm.reserve(num_words); |
| 885 | new_pgm.insert(new_pgm.end(), &pCreateInfo->pCode[0], &pCreateInfo->pCode[num_words]); |
| 886 | |
| 887 | // Call the optimizer to instrument the shader. |
| 888 | // Use the unique_shader_module_id as a shader ID so we can look up its handle later in the shader_map. |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 889 | // If descriptor indexing is enabled, enable length checks and updated descriptor checks |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 890 | using namespace spvtools; |
sfricke-samsung | 45996a4 | 2021-09-16 13:45:27 -0700 | [diff] [blame] | 891 | spv_target_env target_env = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4)); |
Tony-LunarG | f29f77f | 2020-08-26 15:48:00 -0600 | [diff] [blame] | 892 | spvtools::ValidatorOptions val_options; |
| 893 | AdjustValidatorOptions(device_extensions, enabled_features, val_options); |
| 894 | spvtools::OptimizerOptions opt_options; |
| 895 | opt_options.set_run_validator(true); |
| 896 | opt_options.set_validator_options(val_options); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 897 | Optimizer optimizer(target_env); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 898 | optimizer.SetMessageConsumer(gpu_console_message_consumer); |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 899 | optimizer.RegisterPass(CreateInstBindlessCheckPass(desc_set_bind_index, unique_shader_module_id, descriptor_indexing, |
Tony-LunarG | e8632e4 | 2020-11-18 17:03:12 -0700 | [diff] [blame] | 900 | descriptor_indexing, buffer_oob_enabled, buffer_oob_enabled)); |
Tony-LunarG | 57400d4 | 2021-10-14 11:18:43 -0600 | [diff] [blame] | 901 | // Call CreateAggressiveDCEPass with preserve_interface == true |
| 902 | optimizer.RegisterPass(CreateAggressiveDCEPass(true)); |
sfricke-samsung | 45996a4 | 2021-09-16 13:45:27 -0700 | [diff] [blame] | 903 | if ((IsExtEnabled(device_extensions.vk_ext_buffer_device_address) || |
| 904 | IsExtEnabled(device_extensions.vk_khr_buffer_device_address)) && |
| 905 | shaderInt64 && enabled_features.core12.bufferDeviceAddress) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 906 | optimizer.RegisterPass(CreateInstBuffAddrCheckPass(desc_set_bind_index, unique_shader_module_id)); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 907 | } |
Tony-LunarG | f29f77f | 2020-08-26 15:48:00 -0600 | [diff] [blame] | 908 | bool pass = optimizer.Run(new_pgm.data(), new_pgm.size(), &new_pgm, opt_options); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 909 | if (!pass) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 910 | ReportSetupProblem(device, "Failure to instrument shader. Proceeding with non-instrumented shader."); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 911 | } |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 912 | *unique_shader_id = unique_shader_module_id++; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 913 | return pass; |
| 914 | } |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 915 | // Create the instrumented shader data to provide to the driver. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 916 | void GpuAssisted::PreCallRecordCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo, |
| 917 | const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule, |
| 918 | void *csm_state_data) { |
| 919 | create_shader_module_api_state *csm_state = reinterpret_cast<create_shader_module_api_state *>(csm_state_data); |
| 920 | bool pass = InstrumentShader(pCreateInfo, csm_state->instrumented_pgm, &csm_state->unique_shader_id); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 921 | if (pass) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 922 | csm_state->instrumented_create_info.pCode = csm_state->instrumented_pgm.data(); |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 923 | csm_state->instrumented_create_info.codeSize = csm_state->instrumented_pgm.size() * sizeof(uint32_t); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 924 | } |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 925 | ValidationStateTracker::PreCallRecordCreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule, csm_state_data); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 926 | } |
Tony-LunarG | 20678ff | 2021-05-07 14:56:26 -0600 | [diff] [blame] | 927 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 928 | // Generate the part of the message describing the violation. |
Tony-LunarG | 1a7c9f9 | 2021-04-29 16:04:42 -0600 | [diff] [blame] | 929 | bool GenerateValidationMessage(const uint32_t *debug_record, std::string &msg, std::string &vuid_msg, GpuAssistedBufferInfo buf_info, GpuAssisted *gpu_assisted) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 930 | using namespace spvtools; |
| 931 | std::ostringstream strm; |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 932 | bool return_code = true; |
sjfricke | 125f74d | 2022-08-08 18:27:25 +0900 | [diff] [blame] | 933 | static_assert( |
| 934 | spvtools::kInstErrorMax == _kInstErrorMax, |
| 935 | "If this asserts then SPIRV-Tools was updated with a new instrument.hpp and kInstErrorMax was updated. This needs to be " |
| 936 | "changed in GPU-AV so that the GLSL gpu_shaders can read the constants."); |
| 937 | static_assert(spvtools::kInstValidationOutError == _kInstValidationOutError, |
| 938 | "If this asserts then SPIRV-Tools was updated with a new instrument.hpp and kInstValidationOutError was updated. " |
| 939 | "This needs to be changed in GPU-AV so that the GLSL gpu_shaders can read the constants."); |
Tony-LunarG | ab47cac | 2019-12-20 15:28:01 -0700 | [diff] [blame] | 940 | switch (debug_record[kInstValidationOutError]) { |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 941 | case kInstErrorBindlessBounds: { |
Tony-LunarG | ab47cac | 2019-12-20 15:28:01 -0700 | [diff] [blame] | 942 | strm << "Index of " << debug_record[kInstBindlessBoundsOutDescIndex] << " used to index descriptor array of length " |
| 943 | << debug_record[kInstBindlessBoundsOutDescBound] << ". "; |
Tony-LunarG | c1d657d | 2019-02-22 14:55:19 -0700 | [diff] [blame] | 944 | vuid_msg = "UNASSIGNED-Descriptor index out of bounds"; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 945 | } break; |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 946 | case kInstErrorBindlessUninit: { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 947 | strm << "Descriptor index " << debug_record[kInstBindlessUninitOutDescIndex] << " is uninitialized."; |
Tony-LunarG | c1d657d | 2019-02-22 14:55:19 -0700 | [diff] [blame] | 948 | vuid_msg = "UNASSIGNED-Descriptor uninitialized"; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 949 | } break; |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 950 | case kInstErrorBuffAddrUnallocRef: { |
Tony-LunarG | ab47cac | 2019-12-20 15:28:01 -0700 | [diff] [blame] | 951 | uint64_t *ptr = (uint64_t *)&debug_record[kInstBuffAddrUnallocOutDescPtrLo]; |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 952 | strm << "Device address 0x" << std::hex << *ptr << " access out of bounds. "; |
| 953 | vuid_msg = "UNASSIGNED-Device address out of bounds"; |
| 954 | } break; |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 955 | case kInstErrorBuffOOBUniform: |
| 956 | case kInstErrorBuffOOBStorage: { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 957 | auto size = debug_record[kInstBindlessBuffOOBOutBuffSize]; |
| 958 | if (size == 0) { |
| 959 | strm << "Descriptor index " << debug_record[kInstBindlessBuffOOBOutDescIndex] << " is uninitialized."; |
| 960 | vuid_msg = "UNASSIGNED-Descriptor uninitialized"; |
| 961 | } else { |
| 962 | strm << "Descriptor index " << debug_record[kInstBindlessBuffOOBOutDescIndex] |
| 963 | << " access out of bounds. Descriptor size is " << debug_record[kInstBindlessBuffOOBOutBuffSize] |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 964 | << " and highest byte accessed was " << debug_record[kInstBindlessBuffOOBOutBuffOff]; |
Tony-LunarG | f0d4e2b | 2021-04-14 11:52:28 -0600 | [diff] [blame] | 965 | const GpuVuid vuid = GetGpuVuid(buf_info.cmd_type); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 966 | if (debug_record[kInstValidationOutError] == kInstErrorBuffOOBUniform) |
| 967 | vuid_msg = vuid.uniform_access_oob; |
| 968 | else |
| 969 | vuid_msg = vuid.storage_access_oob; |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 970 | } |
| 971 | } break; |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 972 | case kInstErrorBuffOOBUniformTexel: |
| 973 | case kInstErrorBuffOOBStorageTexel: { |
| 974 | auto size = debug_record[kInstBindlessBuffOOBOutBuffSize]; |
| 975 | if (size == 0) { |
| 976 | strm << "Descriptor index " << debug_record[kInstBindlessBuffOOBOutDescIndex] << " is uninitialized."; |
| 977 | vuid_msg = "UNASSIGNED-Descriptor uninitialized"; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 978 | } else { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 979 | strm << "Descriptor index " << debug_record[kInstBindlessBuffOOBOutDescIndex] |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 980 | << " access out of bounds. Descriptor size is " << debug_record[kInstBindlessBuffOOBOutBuffSize] |
| 981 | << " texels and highest texel accessed was " << debug_record[kInstBindlessBuffOOBOutBuffOff]; |
Tony-LunarG | f0d4e2b | 2021-04-14 11:52:28 -0600 | [diff] [blame] | 982 | const GpuVuid vuid = GetGpuVuid(buf_info.cmd_type); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 983 | if (debug_record[kInstValidationOutError] == kInstErrorBuffOOBUniformTexel) |
| 984 | vuid_msg = vuid.uniform_access_oob; |
| 985 | else |
| 986 | vuid_msg = vuid.storage_access_oob; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 987 | } |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 988 | } break; |
sjfricke | 125f74d | 2022-08-08 18:27:25 +0900 | [diff] [blame] | 989 | case _kInstErrorPreDrawValidate: { |
Tim Van Patten | 38bdcdd | 2021-05-14 16:41:00 -0600 | [diff] [blame] | 990 | // Buffer size must be >= (stride * (drawCount - 1) + offset + sizeof(VkDrawIndexedIndirectCommand)) |
sjfricke | 125f74d | 2022-08-08 18:27:25 +0900 | [diff] [blame] | 991 | if (debug_record[_kPreDrawValidateSubError] == pre_draw_count_exceeds_bufsize_error) { |
| 992 | uint32_t count = debug_record[_kPreDrawValidateSubError + 1]; |
Tony-LunarG | f0d4e2b | 2021-04-14 11:52:28 -0600 | [diff] [blame] | 993 | uint32_t stride = buf_info.pre_draw_resources.stride; |
| 994 | uint32_t offset = static_cast<uint32_t>(buf_info.pre_draw_resources.offset); |
| 995 | uint32_t draw_size = (stride * (count - 1) + offset + sizeof(VkDrawIndexedIndirectCommand)); |
| 996 | const GpuVuid vuid = GetGpuVuid(buf_info.cmd_type); |
| 997 | strm << "Indirect draw count of " << count << " would exceed buffer size " << buf_info.pre_draw_resources.buf_size |
| 998 | << " of buffer " << buf_info.pre_draw_resources.buffer << " stride = " << stride << " offset = " << offset |
| 999 | << " (stride * (drawCount - 1) + offset + sizeof(VkDrawIndexedIndirectCommand)) = " << draw_size; |
Tony-LunarG | 64aeaf7 | 2021-04-14 11:13:35 -0600 | [diff] [blame] | 1000 | if (count == 1) { |
| 1001 | vuid_msg = vuid.count_exceeds_bufsize_1; |
| 1002 | } else { |
| 1003 | vuid_msg = vuid.count_exceeds_bufsize; |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1004 | } |
sjfricke | 125f74d | 2022-08-08 18:27:25 +0900 | [diff] [blame] | 1005 | } else if (debug_record[_kPreDrawValidateSubError] == pre_draw_count_exceeds_limit_error) { |
| 1006 | uint32_t count = debug_record[_kPreDrawValidateSubError + 1]; |
Tony-LunarG | 1a7c9f9 | 2021-04-29 16:04:42 -0600 | [diff] [blame] | 1007 | const GpuVuid vuid = GetGpuVuid(buf_info.cmd_type); |
| 1008 | strm << "Indirect draw count of " << count << " would exceed maxDrawIndirectCount limit of " |
| 1009 | << gpu_assisted->phys_dev_props.limits.maxDrawIndirectCount; |
| 1010 | vuid_msg = vuid.count_exceeds_device_limit; |
sjfricke | 125f74d | 2022-08-08 18:27:25 +0900 | [diff] [blame] | 1011 | } else if (debug_record[_kPreDrawValidateSubError] == pre_draw_first_instance_error) { |
| 1012 | uint32_t index = debug_record[_kPreDrawValidateSubError + 1]; |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1013 | const GpuVuid vuid = GetGpuVuid(buf_info.cmd_type); |
| 1014 | strm << "The drawIndirectFirstInstance feature is not enabled, but the firstInstance member of the " |
| 1015 | "VkDrawIndirectCommand structure at index " |
| 1016 | << index << " is not zero"; |
| 1017 | vuid_msg = vuid.first_instance_not_zero; |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1018 | } |
| 1019 | return_code = false; |
| 1020 | } break; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1021 | default: { |
Tony-LunarG | ab47cac | 2019-12-20 15:28:01 -0700 | [diff] [blame] | 1022 | strm << "Internal Error (unexpected error type = " << debug_record[kInstValidationOutError] << "). "; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1023 | vuid_msg = "UNASSIGNED-Internal Error"; |
| 1024 | assert(false); |
| 1025 | } break; |
| 1026 | } |
| 1027 | msg = strm.str(); |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1028 | return return_code; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1029 | } |
| 1030 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1031 | // Pull together all the information from the debug record to build the error message strings, |
| 1032 | // and then assemble them into a single message string. |
| 1033 | // Retrieve the shader program referenced by the unique shader ID provided in the debug record. |
| 1034 | // We had to keep a copy of the shader program with the same lifecycle as the pipeline to make |
| 1035 | // sure it is available when the pipeline is submitted. (The ShaderModule tracking object also |
| 1036 | // keeps a copy, but it can be destroyed after the pipeline is created and before it is submitted.) |
| 1037 | // |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1038 | void GpuAssisted::AnalyzeAndGenerateMessages(VkCommandBuffer command_buffer, VkQueue queue, GpuAssistedBufferInfo &buffer_info, |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1039 | uint32_t operation_index, uint32_t *const debug_output_buffer) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1040 | using namespace spvtools; |
| 1041 | const uint32_t total_words = debug_output_buffer[0]; |
| 1042 | // A zero here means that the shader instrumentation didn't write anything. |
| 1043 | // If you have nothing to say, don't say it here. |
| 1044 | if (0 == total_words) { |
| 1045 | return; |
| 1046 | } |
| 1047 | // The first word in the debug output buffer is the number of words that would have |
| 1048 | // been written by the shader instrumentation, if there was enough room in the buffer we provided. |
| 1049 | // The number of words actually written by the shaders is determined by the size of the buffer |
| 1050 | // we provide via the descriptor. So, we process only the number of words that can fit in the |
| 1051 | // buffer. |
| 1052 | // Each "report" written by the shader instrumentation is considered a "record". This function |
| 1053 | // is hard-coded to process only one record because it expects the buffer to be large enough to |
| 1054 | // hold only one record. If there is a desire to process more than one record, this function needs |
| 1055 | // to be modified to loop over records and the buffer size increased. |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1056 | std::string validation_message; |
| 1057 | std::string stage_message; |
| 1058 | std::string common_message; |
| 1059 | std::string filename_message; |
| 1060 | std::string source_message; |
| 1061 | std::string vuid_msg; |
| 1062 | VkShaderModule shader_module_handle = VK_NULL_HANDLE; |
| 1063 | VkPipeline pipeline_handle = VK_NULL_HANDLE; |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 1064 | std::vector<uint32_t> pgm; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1065 | // The first record starts at this offset after the total_words. |
| 1066 | const uint32_t *debug_record = &debug_output_buffer[kDebugOutputDataOffset]; |
| 1067 | // Lookup the VkShaderModule handle and SPIR-V code used to create the shader, using the unique shader ID value returned |
| 1068 | // by the instrumented shader. |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1069 | auto it = shader_map.find(debug_record[kInstCommonOutShaderId]); |
| 1070 | if (it != shader_map.end()) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1071 | shader_module_handle = it->second.shader_module; |
| 1072 | pipeline_handle = it->second.pipeline; |
| 1073 | pgm = it->second.pgm; |
| 1074 | } |
Tony-LunarG | 1a7c9f9 | 2021-04-29 16:04:42 -0600 | [diff] [blame] | 1075 | bool gen_full_message = GenerateValidationMessage(debug_record, validation_message, vuid_msg, buffer_info, this); |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1076 | if (gen_full_message) { |
| 1077 | UtilGenerateStageMessage(debug_record, stage_message); |
| 1078 | UtilGenerateCommonMessage(report_data, command_buffer, debug_record, shader_module_handle, pipeline_handle, |
| 1079 | buffer_info.pipeline_bind_point, operation_index, common_message); |
| 1080 | UtilGenerateSourceMessages(pgm, debug_record, false, filename_message, source_message); |
| 1081 | LogError(queue, vuid_msg.c_str(), "%s %s %s %s%s", validation_message.c_str(), common_message.c_str(), stage_message.c_str(), |
| 1082 | filename_message.c_str(), source_message.c_str()); |
| 1083 | } |
| 1084 | else { |
| 1085 | LogError(queue, vuid_msg.c_str(), "%s", validation_message.c_str()); |
| 1086 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1087 | // The debug record at word kInstCommonOutSize is the number of words in the record |
| 1088 | // written by the shader. Clear the entire record plus the total_words word at the start. |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1089 | const uint32_t words_to_clear = 1 + std::min(debug_record[kInstCommonOutSize], static_cast<uint32_t>(kInstMaxOutCnt)); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1090 | memset(debug_output_buffer, 0, sizeof(uint32_t) * words_to_clear); |
| 1091 | } |
| 1092 | |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 1093 | // For the given command buffer, map its debug data buffers and read their contents for analysis. |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 1094 | void gpuav_state::CommandBuffer::Process(VkQueue queue) { |
| 1095 | auto *device_state = static_cast<GpuAssisted *>(dev_data); |
sjfricke | 52defd4 | 2022-08-08 16:37:46 +0900 | [diff] [blame] | 1096 | if (has_draw_cmd || has_trace_rays_cmd || has_dispatch_cmd) { |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 1097 | auto &gpu_buffer_list = gpuav_buffer_list; |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 1098 | uint32_t draw_index = 0; |
| 1099 | uint32_t compute_index = 0; |
| 1100 | uint32_t ray_trace_index = 0; |
| 1101 | |
| 1102 | for (auto &buffer_info : gpu_buffer_list) { |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 1103 | char *data; |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 1104 | |
| 1105 | uint32_t operation_index = 0; |
| 1106 | if (buffer_info.pipeline_bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) { |
| 1107 | operation_index = draw_index; |
sjfricke | 9a20980 | 2022-08-03 17:57:40 +0900 | [diff] [blame] | 1108 | draw_index++; |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 1109 | } else if (buffer_info.pipeline_bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) { |
| 1110 | operation_index = compute_index; |
sjfricke | 9a20980 | 2022-08-03 17:57:40 +0900 | [diff] [blame] | 1111 | compute_index++; |
sjfricke | 62366d3 | 2022-08-01 21:04:10 +0900 | [diff] [blame] | 1112 | } else if (buffer_info.pipeline_bind_point == VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR) { |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 1113 | operation_index = ray_trace_index; |
sjfricke | 9a20980 | 2022-08-03 17:57:40 +0900 | [diff] [blame] | 1114 | ray_trace_index++; |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 1115 | } else { |
| 1116 | assert(false); |
| 1117 | } |
| 1118 | |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 1119 | VkResult result = vmaMapMemory(device_state->vmaAllocator, buffer_info.output_mem_block.allocation, (void **)&data); |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 1120 | if (result == VK_SUCCESS) { |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 1121 | device_state->AnalyzeAndGenerateMessages(commandBuffer(), queue, buffer_info, operation_index, (uint32_t *)data); |
| 1122 | vmaUnmapMemory(device_state->vmaAllocator, buffer_info.output_mem_block.allocation); |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 1123 | } |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 1124 | } |
| 1125 | } |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 1126 | ProcessAccelerationStructure(queue); |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 1127 | } |
| 1128 | |
Jeremy Gebben | f4816f7 | 2022-07-15 08:56:06 -0600 | [diff] [blame] | 1129 | void GpuAssisted::SetBindingState(uint32_t *data, uint32_t index, const cvdescriptorset::DescriptorBinding *binding) { |
| 1130 | switch (binding->descriptor_class) { |
| 1131 | case cvdescriptorset::DescriptorClass::GeneralBuffer: { |
| 1132 | auto buffer_binding = static_cast<const cvdescriptorset::BufferBinding *>(binding); |
| 1133 | for (uint32_t di = 0; di < buffer_binding->count; di++) { |
| 1134 | const auto &desc = buffer_binding->descriptors[di]; |
| 1135 | if (!buffer_binding->updated[di]) { |
| 1136 | data[index++] = 0; |
| 1137 | continue; |
| 1138 | } |
| 1139 | auto buffer = desc.GetBuffer(); |
| 1140 | if (buffer == VK_NULL_HANDLE) { |
| 1141 | data[index++] = UINT_MAX; |
| 1142 | } else { |
| 1143 | auto buffer_state = desc.GetBufferState(); |
| 1144 | data[index++] = static_cast<uint32_t>(buffer_state->createInfo.size); |
| 1145 | } |
| 1146 | } |
| 1147 | break; |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1148 | } |
Jeremy Gebben | f4816f7 | 2022-07-15 08:56:06 -0600 | [diff] [blame] | 1149 | case cvdescriptorset::DescriptorClass::TexelBuffer: { |
| 1150 | auto texel_binding = static_cast<const cvdescriptorset::TexelBinding *>(binding); |
| 1151 | for (uint32_t di = 0; di < texel_binding->count; di++) { |
| 1152 | const auto &desc = texel_binding->descriptors[di]; |
| 1153 | if (!texel_binding->updated[di]) { |
| 1154 | data[index++] = 0; |
| 1155 | continue; |
| 1156 | } |
| 1157 | auto buffer_view = desc.GetBufferView(); |
| 1158 | if (buffer_view == VK_NULL_HANDLE) { |
| 1159 | data[index++] = UINT_MAX; |
| 1160 | } else { |
| 1161 | auto buffer_view_state = desc.GetBufferViewState(); |
| 1162 | data[index++] = static_cast<uint32_t>(buffer_view_state->buffer_state->createInfo.size); |
| 1163 | } |
| 1164 | } |
| 1165 | break; |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1166 | } |
Jeremy Gebben | f4816f7 | 2022-07-15 08:56:06 -0600 | [diff] [blame] | 1167 | case cvdescriptorset::DescriptorClass::Mutable: { |
| 1168 | auto mutable_binding = static_cast<const cvdescriptorset::MutableBinding *>(binding); |
| 1169 | for (uint32_t di = 0; di < mutable_binding->count; di++) { |
| 1170 | const auto &desc = mutable_binding->descriptors[di]; |
| 1171 | if (!mutable_binding->updated[di]) { |
| 1172 | data[index++] = 0; |
| 1173 | continue; |
| 1174 | } |
Jeremy Gebben | c08f650 | 2022-07-15 09:55:06 -0600 | [diff] [blame] | 1175 | switch (desc.ActiveType()) { |
| 1176 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 1177 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 1178 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 1179 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 1180 | data[index++] = static_cast<uint32_t>(desc.GetBufferSize()); |
| 1181 | break; |
| 1182 | default: |
Jeremy Gebben | f4816f7 | 2022-07-15 08:56:06 -0600 | [diff] [blame] | 1183 | data[index++] = 1; |
Jeremy Gebben | c08f650 | 2022-07-15 09:55:06 -0600 | [diff] [blame] | 1184 | break; |
Jeremy Gebben | f4816f7 | 2022-07-15 08:56:06 -0600 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | break; |
ziga | 7a255fb | 2021-11-20 21:17:07 +0100 | [diff] [blame] | 1188 | } |
Jeremy Gebben | f4816f7 | 2022-07-15 08:56:06 -0600 | [diff] [blame] | 1189 | default: { |
| 1190 | for (uint32_t i = 0; i < binding->count; i++, index++) { |
| 1191 | data[index] = static_cast<uint32_t>(binding->updated[i]); |
| 1192 | } |
| 1193 | break; |
| 1194 | } |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1198 | // For the given command buffer, map its debug data buffers and update the status of any update after bind descriptors |
Jeremy Gebben | 135550d | 2022-03-21 07:15:07 -0600 | [diff] [blame] | 1199 | void GpuAssisted::UpdateInstrumentationBuffer(gpuav_state::CommandBuffer *cb_node) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1200 | uint32_t *data; |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 1201 | for (auto &buffer_info : cb_node->gpuav_buffer_list) { |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1202 | if (buffer_info.di_input_mem_block.update_at_submit.size() > 0) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1203 | VkResult result = |
| 1204 | vmaMapMemory(vmaAllocator, buffer_info.di_input_mem_block.allocation, reinterpret_cast<void **>(&data)); |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1205 | if (result == VK_SUCCESS) { |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1206 | for (const auto &update : buffer_info.di_input_mem_block.update_at_submit) { |
Jeremy Gebben | f4816f7 | 2022-07-15 08:56:06 -0600 | [diff] [blame] | 1207 | SetBindingState(data, update.first, update.second); |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1208 | } |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1209 | vmaUnmapMemory(vmaAllocator, buffer_info.di_input_mem_block.allocation); |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1215 | void GpuAssisted::PreRecordCommandBuffer(VkCommandBuffer command_buffer) { |
Jeremy Gebben | 04697b0 | 2022-03-23 16:18:12 -0600 | [diff] [blame] | 1216 | auto cb_node = GetWrite<gpuav_state::CommandBuffer>(command_buffer); |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 1217 | UpdateInstrumentationBuffer(cb_node.get()); |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1218 | for (auto *secondary_cmd_buffer : cb_node->linkedCommandBuffers) { |
Jeremy Gebben | 04697b0 | 2022-03-23 16:18:12 -0600 | [diff] [blame] | 1219 | auto guard = secondary_cmd_buffer->WriteLock(); |
Jeremy Gebben | 135550d | 2022-03-21 07:15:07 -0600 | [diff] [blame] | 1220 | UpdateInstrumentationBuffer(static_cast<gpuav_state::CommandBuffer *>(secondary_cmd_buffer)); |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1224 | void GpuAssisted::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1225 | ValidationStateTracker::PreCallRecordQueueSubmit(queue, submitCount, pSubmits, fence); |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1226 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1227 | const VkSubmitInfo *submit = &pSubmits[submit_idx]; |
| 1228 | for (uint32_t i = 0; i < submit->commandBufferCount; i++) { |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1229 | PreRecordCommandBuffer(submit->pCommandBuffers[i]); |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | } |
Tony-LunarG | 26fe284 | 2021-11-16 14:07:59 -0700 | [diff] [blame] | 1233 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1234 | void GpuAssisted::PreCallRecordQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, |
| 1235 | VkFence fence) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1236 | ValidationStateTracker::PreCallRecordQueueSubmit2KHR(queue, submitCount, pSubmits, fence); |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1237 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1238 | const VkSubmitInfo2KHR *submit = &pSubmits[submit_idx]; |
| 1239 | for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) { |
| 1240 | PreRecordCommandBuffer(submit->pCommandBufferInfos[i].commandBuffer); |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
Tony-LunarG | 26fe284 | 2021-11-16 14:07:59 -0700 | [diff] [blame] | 1245 | void GpuAssisted::PreCallRecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence) { |
| 1246 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1247 | const VkSubmitInfo2 *submit = &pSubmits[submit_idx]; |
| 1248 | for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) { |
| 1249 | PreRecordCommandBuffer(submit->pCommandBufferInfos[i].commandBuffer); |
| 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1254 | void GpuAssisted::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, |
| 1255 | uint32_t firstVertex, uint32_t firstInstance) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1256 | ValidationStateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1257 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAW); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1258 | } |
| 1259 | |
Tony-LunarG | 745150c | 2021-07-02 15:07:31 -0600 | [diff] [blame] | 1260 | void GpuAssisted::PreCallRecordCmdDrawMultiEXT(VkCommandBuffer commandBuffer, uint32_t drawCount, |
| 1261 | const VkMultiDrawInfoEXT *pVertexInfo, uint32_t instanceCount, |
| 1262 | uint32_t firstInstance, uint32_t stride) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1263 | ValidationStateTracker::PreCallRecordCmdDrawMultiEXT(commandBuffer, drawCount, pVertexInfo, instanceCount, firstInstance, |
| 1264 | stride); |
Tony-LunarG | 745150c | 2021-07-02 15:07:31 -0600 | [diff] [blame] | 1265 | for (uint32_t i = 0; i < drawCount; i++) { |
| 1266 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWMULTIEXT); |
| 1267 | } |
| 1268 | } |
| 1269 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1270 | void GpuAssisted::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, |
| 1271 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1272 | ValidationStateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, |
| 1273 | firstInstance); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1274 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDEXED); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1275 | } |
| 1276 | |
Tony-LunarG | 745150c | 2021-07-02 15:07:31 -0600 | [diff] [blame] | 1277 | void GpuAssisted::PreCallRecordCmdDrawMultiIndexedEXT(VkCommandBuffer commandBuffer, uint32_t drawCount, |
| 1278 | const VkMultiDrawIndexedInfoEXT *pIndexInfo, uint32_t instanceCount, |
| 1279 | uint32_t firstInstance, uint32_t stride, const int32_t *pVertexOffset) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1280 | ValidationStateTracker::PreCallRecordCmdDrawMultiIndexedEXT(commandBuffer, drawCount, pIndexInfo, instanceCount, firstInstance, |
| 1281 | stride, pVertexOffset); |
Tony-LunarG | 745150c | 2021-07-02 15:07:31 -0600 | [diff] [blame] | 1282 | for (uint32_t i = 0; i < drawCount; i++) { |
| 1283 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWMULTIINDEXEDEXT); |
| 1284 | } |
| 1285 | } |
| 1286 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1287 | void GpuAssisted::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count, |
| 1288 | uint32_t stride) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1289 | ValidationStateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, count, stride); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1290 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, count, stride, 0, 0}; |
| 1291 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDIRECT, &cdi_state); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | void GpuAssisted::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1295 | uint32_t count, uint32_t stride) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1296 | ValidationStateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, count, stride); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1297 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, count, stride, 0, 0}; |
| 1298 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDEXEDINDIRECT, &cdi_state); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1299 | } |
| 1300 | |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1301 | void GpuAssisted::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1302 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 1303 | uint32_t stride) { |
| 1304 | ValidationStateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, |
| 1305 | maxDrawCount, stride); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1306 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, 0, stride, countBuffer, countBufferOffset}; |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 1307 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDIRECTCOUNTKHR, &cdi_state); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | void GpuAssisted::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1311 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1312 | |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1313 | uint32_t stride) { |
| 1314 | ValidationStateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, |
| 1315 | maxDrawCount, stride); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1316 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, 0, stride, countBuffer, countBufferOffset}; |
| 1317 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDIRECTCOUNT, &cdi_state); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1318 | } |
| 1319 | |
Tony-LunarG | 54176fb | 2020-12-02 10:47:22 -0700 | [diff] [blame] | 1320 | void GpuAssisted::PreCallRecordCmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer, uint32_t instanceCount, |
| 1321 | uint32_t firstInstance, VkBuffer counterBuffer, |
| 1322 | VkDeviceSize counterBufferOffset, uint32_t counterOffset, |
| 1323 | uint32_t vertexStride) { |
| 1324 | ValidationStateTracker::PreCallRecordCmdDrawIndirectByteCountEXT(commandBuffer, instanceCount, firstInstance, counterBuffer, |
| 1325 | counterBufferOffset, counterOffset, vertexStride); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1326 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDIRECTBYTECOUNTEXT); |
Tony-LunarG | 54176fb | 2020-12-02 10:47:22 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1329 | void GpuAssisted::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1330 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 1331 | uint32_t maxDrawCount, uint32_t stride) { |
| 1332 | ValidationStateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, |
| 1333 | countBufferOffset, maxDrawCount, stride); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1334 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, 0, stride, countBuffer, countBufferOffset}; |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 1335 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDEXEDINDIRECTCOUNTKHR, &cdi_state); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | void GpuAssisted::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1339 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 1340 | uint32_t maxDrawCount, uint32_t stride) { |
| 1341 | ValidationStateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, |
| 1342 | maxDrawCount, stride); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1343 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, 0, stride, countBuffer, countBufferOffset}; |
| 1344 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDEXEDINDIRECTCOUNT, &cdi_state); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | void GpuAssisted::PreCallRecordCmdDrawMeshTasksNV(VkCommandBuffer commandBuffer, uint32_t taskCount, uint32_t firstTask) { |
| 1348 | ValidationStateTracker::PreCallRecordCmdDrawMeshTasksNV(commandBuffer, taskCount, firstTask); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1349 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWMESHTASKSNV); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | void GpuAssisted::PreCallRecordCmdDrawMeshTasksIndirectNV(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1353 | uint32_t drawCount, uint32_t stride) { |
| 1354 | ValidationStateTracker::PreCallRecordCmdDrawMeshTasksIndirectNV(commandBuffer, buffer, offset, drawCount, stride); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1355 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWMESHTASKSINDIRECTNV); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | void GpuAssisted::PreCallRecordCmdDrawMeshTasksIndirectCountNV(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1359 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 1360 | uint32_t maxDrawCount, uint32_t stride) { |
| 1361 | ValidationStateTracker::PreCallRecordCmdDrawMeshTasksIndirectCountNV(commandBuffer, buffer, offset, countBuffer, |
| 1362 | countBufferOffset, maxDrawCount, stride); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1363 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWMESHTASKSINDIRECTCOUNTNV); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1364 | } |
| 1365 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1366 | void GpuAssisted::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1367 | ValidationStateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1368 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, CMD_DISPATCH); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | void GpuAssisted::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1372 | ValidationStateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1373 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, CMD_DISPATCHINDIRECT); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1374 | } |
| 1375 | |
Tony-LunarG | d13f9b5 | 2020-09-08 15:45:45 -0600 | [diff] [blame] | 1376 | void GpuAssisted::PreCallRecordCmdDispatchBase(VkCommandBuffer commandBuffer, uint32_t baseGroupX, uint32_t baseGroupY, |
| 1377 | uint32_t baseGroupZ, uint32_t groupCountX, uint32_t groupCountY, |
| 1378 | uint32_t groupCountZ) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1379 | ValidationStateTracker::PreCallRecordCmdDispatchBase(commandBuffer, baseGroupX, baseGroupY, baseGroupZ, groupCountX, |
| 1380 | groupCountY, groupCountZ); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1381 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, CMD_DISPATCHBASE); |
Tony-LunarG | d13f9b5 | 2020-09-08 15:45:45 -0600 | [diff] [blame] | 1382 | } |
| 1383 | |
Tony-LunarG | 52c8c60 | 2020-09-10 16:29:56 -0600 | [diff] [blame] | 1384 | void GpuAssisted::PreCallRecordCmdDispatchBaseKHR(VkCommandBuffer commandBuffer, uint32_t baseGroupX, uint32_t baseGroupY, |
| 1385 | uint32_t baseGroupZ, uint32_t groupCountX, uint32_t groupCountY, |
| 1386 | uint32_t groupCountZ) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1387 | ValidationStateTracker::PreCallRecordCmdDispatchBaseKHR(commandBuffer, baseGroupX, baseGroupY, baseGroupZ, groupCountX, |
| 1388 | groupCountY, groupCountZ); |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 1389 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, CMD_DISPATCHBASEKHR); |
Tony-LunarG | 52c8c60 | 2020-09-10 16:29:56 -0600 | [diff] [blame] | 1390 | } |
| 1391 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1392 | void GpuAssisted::PreCallRecordCmdTraceRaysNV(VkCommandBuffer commandBuffer, VkBuffer raygenShaderBindingTableBuffer, |
| 1393 | VkDeviceSize raygenShaderBindingOffset, VkBuffer missShaderBindingTableBuffer, |
| 1394 | VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride, |
| 1395 | VkBuffer hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset, |
| 1396 | VkDeviceSize hitShaderBindingStride, VkBuffer callableShaderBindingTableBuffer, |
| 1397 | VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride, |
| 1398 | uint32_t width, uint32_t height, uint32_t depth) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1399 | ValidationStateTracker::PreCallRecordCmdTraceRaysNV( |
| 1400 | commandBuffer, raygenShaderBindingTableBuffer, raygenShaderBindingOffset, missShaderBindingTableBuffer, |
| 1401 | missShaderBindingOffset, missShaderBindingStride, hitShaderBindingTableBuffer, hitShaderBindingOffset, |
| 1402 | hitShaderBindingStride, callableShaderBindingTableBuffer, callableShaderBindingOffset, callableShaderBindingStride, width, |
| 1403 | height, depth); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1404 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_NV, CMD_TRACERAYSNV); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1405 | } |
| 1406 | |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1407 | void GpuAssisted::PreCallRecordCmdTraceRaysKHR(VkCommandBuffer commandBuffer, |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1408 | const VkStridedDeviceAddressRegionKHR *pRaygenShaderBindingTable, |
| 1409 | const VkStridedDeviceAddressRegionKHR *pMissShaderBindingTable, |
| 1410 | const VkStridedDeviceAddressRegionKHR *pHitShaderBindingTable, |
| 1411 | const VkStridedDeviceAddressRegionKHR *pCallableShaderBindingTable, uint32_t width, |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1412 | uint32_t height, uint32_t depth) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1413 | ValidationStateTracker::PreCallRecordCmdTraceRaysKHR(commandBuffer, pRaygenShaderBindingTable, pMissShaderBindingTable, |
| 1414 | pHitShaderBindingTable, pCallableShaderBindingTable, width, height, depth); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1415 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, CMD_TRACERAYSKHR); |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1416 | } |
| 1417 | |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1418 | |
| 1419 | void GpuAssisted::PreCallRecordCmdTraceRaysIndirectKHR(VkCommandBuffer commandBuffer, |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1420 | const VkStridedDeviceAddressRegionKHR *pRaygenShaderBindingTable, |
| 1421 | const VkStridedDeviceAddressRegionKHR *pMissShaderBindingTable, |
| 1422 | const VkStridedDeviceAddressRegionKHR *pHitShaderBindingTable, |
| 1423 | const VkStridedDeviceAddressRegionKHR *pCallableShaderBindingTable, |
Shannon McPherson | 54e1f89 | 2020-11-27 11:04:19 -0700 | [diff] [blame] | 1424 | VkDeviceAddress indirectDeviceAddress) { |
Tony-LunarG | 9dc7d4a | 2021-11-04 13:25:59 -0600 | [diff] [blame] | 1425 | ValidationStateTracker::PreCallRecordCmdTraceRaysIndirectKHR(commandBuffer, pRaygenShaderBindingTable, pMissShaderBindingTable, |
| 1426 | pHitShaderBindingTable, pCallableShaderBindingTable, |
| 1427 | indirectDeviceAddress); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1428 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, CMD_TRACERAYSINDIRECTKHR); |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1429 | } |
| 1430 | |
sfricke-samsung | f91881c | 2022-03-31 01:12:00 -0500 | [diff] [blame] | 1431 | void GpuAssisted::PreCallRecordCmdTraceRaysIndirect2KHR(VkCommandBuffer commandBuffer, VkDeviceAddress indirectDeviceAddress) { |
| 1432 | ValidationStateTracker::PreCallRecordCmdTraceRaysIndirect2KHR(commandBuffer, indirectDeviceAddress); |
| 1433 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, CMD_TRACERAYSINDIRECT2KHR); |
| 1434 | } |
| 1435 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1436 | // This function will add the returned VkPipeline handle to another object incharge of destroying it. Caller does NOT have to |
| 1437 | // destroy it |
Jeremy Gebben | bba3921 | 2022-03-29 16:39:06 -0600 | [diff] [blame] | 1438 | VkPipeline GpuAssisted::GetValidationPipeline(VkRenderPass render_pass) { |
| 1439 | VkPipeline pipeline = VK_NULL_HANDLE; |
| 1440 | //NOTE: for dynamic rendering, render_pass will be VK_NULL_HANDLE but we'll use that as a map |
| 1441 | //key anyways; |
| 1442 | auto pipeentry = pre_draw_validation_state.renderpass_to_pipeline.find(render_pass); |
| 1443 | if (pipeentry != pre_draw_validation_state.renderpass_to_pipeline.end()) { |
| 1444 | pipeline = pipeentry->second; |
| 1445 | } |
| 1446 | if (pipeline != VK_NULL_HANDLE) { |
| 1447 | return pipeline; |
| 1448 | } |
| 1449 | auto pipeline_stage_ci = LvlInitStruct<VkPipelineShaderStageCreateInfo>(); |
| 1450 | pipeline_stage_ci.stage = VK_SHADER_STAGE_VERTEX_BIT; |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1451 | pipeline_stage_ci.module = pre_draw_validation_state.shader_module; |
Jeremy Gebben | bba3921 | 2022-03-29 16:39:06 -0600 | [diff] [blame] | 1452 | pipeline_stage_ci.pName = "main"; |
| 1453 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1454 | auto pipeline_ci = LvlInitStruct<VkGraphicsPipelineCreateInfo>(); |
| 1455 | auto vertex_input_state = LvlInitStruct<VkPipelineVertexInputStateCreateInfo>(); |
| 1456 | auto input_assembly_state = LvlInitStruct<VkPipelineInputAssemblyStateCreateInfo>(); |
| 1457 | input_assembly_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
| 1458 | auto rasterization_state = LvlInitStruct<VkPipelineRasterizationStateCreateInfo>(); |
| 1459 | rasterization_state.rasterizerDiscardEnable = VK_TRUE; |
| 1460 | auto color_blend_state = LvlInitStruct<VkPipelineColorBlendStateCreateInfo>(); |
Jeremy Gebben | bba3921 | 2022-03-29 16:39:06 -0600 | [diff] [blame] | 1461 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1462 | pipeline_ci.pVertexInputState = &vertex_input_state; |
| 1463 | pipeline_ci.pInputAssemblyState = &input_assembly_state; |
| 1464 | pipeline_ci.pRasterizationState = &rasterization_state; |
| 1465 | pipeline_ci.pColorBlendState = &color_blend_state; |
| 1466 | pipeline_ci.renderPass = render_pass; |
| 1467 | pipeline_ci.layout = pre_draw_validation_state.pipeline_layout; |
| 1468 | pipeline_ci.stageCount = 1; |
| 1469 | pipeline_ci.pStages = &pipeline_stage_ci; |
Jeremy Gebben | bba3921 | 2022-03-29 16:39:06 -0600 | [diff] [blame] | 1470 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1471 | VkResult result = DispatchCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipeline_ci, nullptr, &pipeline); |
Jeremy Gebben | bba3921 | 2022-03-29 16:39:06 -0600 | [diff] [blame] | 1472 | if (result != VK_SUCCESS) { |
| 1473 | ReportSetupProblem(device, "Unable to create graphics pipeline. Aborting GPU-AV"); |
| 1474 | aborted = true; |
| 1475 | return VK_NULL_HANDLE; |
| 1476 | } |
| 1477 | |
| 1478 | pre_draw_validation_state.renderpass_to_pipeline.insert(render_pass, pipeline); |
| 1479 | return pipeline; |
| 1480 | } |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1481 | |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1482 | void GpuAssisted::AllocatePreDrawValidationResources(GpuAssistedDeviceMemoryBlock output_block, |
| 1483 | GpuAssistedPreDrawResources &resources, const LAST_BOUND_STATE &state, |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1484 | VkPipeline *pPipeline, const GpuAssistedCmdDrawIndirectState *cdi_state) { |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1485 | VkResult result; |
| 1486 | if (!pre_draw_validation_state.globals_created) { |
| 1487 | auto shader_module_ci = LvlInitStruct<VkShaderModuleCreateInfo>(); |
sjfricke | becf072 | 2022-08-05 16:15:46 +0900 | [diff] [blame^] | 1488 | shader_module_ci.codeSize = sizeof(gpu_pre_draw_vert); |
| 1489 | shader_module_ci.pCode = gpu_pre_draw_vert; |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1490 | result = DispatchCreateShaderModule(device, &shader_module_ci, nullptr, &pre_draw_validation_state.shader_module); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1491 | if (result != VK_SUCCESS) { |
| 1492 | ReportSetupProblem(device, "Unable to create shader module. Aborting GPU-AV"); |
| 1493 | aborted = true; |
| 1494 | return; |
| 1495 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1496 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1497 | std::vector<VkDescriptorSetLayoutBinding> bindings = { |
| 1498 | {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT, nullptr}, // output buffer |
| 1499 | {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT, nullptr}, // count/draws buffer |
| 1500 | }; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1501 | |
sjfricke | e9b3937 | 2022-05-22 13:02:17 +0900 | [diff] [blame] | 1502 | VkDescriptorSetLayoutCreateInfo ds_layout_ci = LvlInitStruct<VkDescriptorSetLayoutCreateInfo>(); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1503 | ds_layout_ci.bindingCount = static_cast<uint32_t>(bindings.size()); |
| 1504 | ds_layout_ci.pBindings = bindings.data(); |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1505 | result = DispatchCreateDescriptorSetLayout(device, &ds_layout_ci, nullptr, &pre_draw_validation_state.ds_layout); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1506 | if (result != VK_SUCCESS) { |
| 1507 | ReportSetupProblem(device, "Unable to create descriptor set layout. Aborting GPU-AV"); |
| 1508 | aborted = true; |
| 1509 | return; |
| 1510 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1511 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1512 | VkPushConstantRange push_constant_range = {}; |
| 1513 | push_constant_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; |
| 1514 | push_constant_range.offset = 0; |
| 1515 | push_constant_range.size = resources.push_constant_words * sizeof(uint32_t); |
| 1516 | VkPipelineLayoutCreateInfo pipeline_layout_ci = LvlInitStruct<VkPipelineLayoutCreateInfo>(); |
| 1517 | pipeline_layout_ci.pushConstantRangeCount = 1; |
| 1518 | pipeline_layout_ci.pPushConstantRanges = &push_constant_range; |
| 1519 | pipeline_layout_ci.setLayoutCount = 1; |
| 1520 | pipeline_layout_ci.pSetLayouts = &pre_draw_validation_state.ds_layout; |
| 1521 | result = DispatchCreatePipelineLayout(device, &pipeline_layout_ci, nullptr, &pre_draw_validation_state.pipeline_layout); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1522 | if (result != VK_SUCCESS) { |
| 1523 | ReportSetupProblem(device, "Unable to create pipeline layout. Aborting GPU-AV"); |
| 1524 | aborted = true; |
| 1525 | return; |
| 1526 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1527 | |
| 1528 | pre_draw_validation_state.globals_created = true; |
| 1529 | } |
Tony-LunarG | 463bae3 | 2022-02-25 09:31:17 -0700 | [diff] [blame] | 1530 | |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 1531 | VkRenderPass render_pass = state.pipeline_state->RenderPassState()->renderPass(); |
Jeremy Gebben | bba3921 | 2022-03-29 16:39:06 -0600 | [diff] [blame] | 1532 | *pPipeline = GetValidationPipeline(render_pass); |
| 1533 | if (*pPipeline == VK_NULL_HANDLE) { |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1534 | ReportSetupProblem(device, "Could not find or create a pipeline. Aborting GPU-AV"); |
| 1535 | aborted = true; |
Jeremy Gebben | bba3921 | 2022-03-29 16:39:06 -0600 | [diff] [blame] | 1536 | return; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1537 | } |
| 1538 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1539 | result = desc_set_manager->GetDescriptorSet(&resources.desc_pool, pre_draw_validation_state.ds_layout, &resources.desc_set); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1540 | if (result != VK_SUCCESS) { |
| 1541 | ReportSetupProblem(device, "Unable to allocate descriptor set. Aborting GPU-AV"); |
| 1542 | aborted = true; |
| 1543 | return; |
| 1544 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1545 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1546 | const uint32_t buffer_count = 2; |
| 1547 | VkDescriptorBufferInfo buffer_infos[buffer_count] = {}; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1548 | // Error output buffer |
| 1549 | buffer_infos[0].buffer = output_block.buffer; |
| 1550 | buffer_infos[0].offset = 0; |
| 1551 | buffer_infos[0].range = VK_WHOLE_SIZE; |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1552 | if (cdi_state->count_buffer) { |
| 1553 | // Count buffer |
| 1554 | buffer_infos[1].buffer = cdi_state->count_buffer; |
| 1555 | } else { |
| 1556 | // Draw Buffer |
| 1557 | buffer_infos[1].buffer = cdi_state->buffer; |
| 1558 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1559 | buffer_infos[1].offset = 0; |
| 1560 | buffer_infos[1].range = VK_WHOLE_SIZE; |
| 1561 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1562 | VkWriteDescriptorSet desc_writes[buffer_count] = {}; |
| 1563 | for (uint32_t i = 0; i < buffer_count; i++) { |
sjfricke | e9b3937 | 2022-05-22 13:02:17 +0900 | [diff] [blame] | 1564 | desc_writes[i] = LvlInitStruct<VkWriteDescriptorSet>(); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1565 | desc_writes[i].dstBinding = i; |
| 1566 | desc_writes[i].descriptorCount = 1; |
| 1567 | desc_writes[i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 1568 | desc_writes[i].pBufferInfo = &buffer_infos[i]; |
| 1569 | desc_writes[i].dstSet = resources.desc_set; |
| 1570 | } |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1571 | DispatchUpdateDescriptorSets(device, buffer_count, desc_writes, 0, NULL); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1572 | } |
| 1573 | |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1574 | void GpuAssisted::AllocateValidationResources(const VkCommandBuffer cmd_buffer, const VkPipelineBindPoint bind_point, |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1575 | CMD_TYPE cmd_type, const GpuAssistedCmdDrawIndirectState *cdi_state) { |
Jason Macnak | 67407e7 | 2019-07-11 11:05:09 -0700 | [diff] [blame] | 1576 | if (bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS && bind_point != VK_PIPELINE_BIND_POINT_COMPUTE && |
sjfricke | 62366d3 | 2022-08-01 21:04:10 +0900 | [diff] [blame] | 1577 | bind_point != VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR) { |
andreyg | ca287f2 | 2019-04-10 00:15:33 +0300 | [diff] [blame] | 1578 | return; |
| 1579 | } |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1580 | VkResult result; |
| 1581 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1582 | if (aborted) return; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1583 | |
Jeremy Gebben | 04697b0 | 2022-03-23 16:18:12 -0600 | [diff] [blame] | 1584 | auto cb_node = GetWrite<gpuav_state::CommandBuffer>(cmd_buffer); |
Nathaniel Cesario | bcb7968 | 2022-03-31 21:13:52 -0600 | [diff] [blame] | 1585 | if (!cb_node) { |
| 1586 | ReportSetupProblem(device, "Unrecognized command buffer"); |
| 1587 | aborted = true; |
| 1588 | return; |
| 1589 | } |
| 1590 | const auto lv_bind_point = ConvertToLvlBindPoint(bind_point); |
| 1591 | auto const &state = cb_node->lastBound[lv_bind_point]; |
| 1592 | const auto *pipeline_state = state.pipeline_state; |
| 1593 | |
| 1594 | // TODO (ncesario) remove once VK_EXT_graphics_pipeline_library support is added for GPU-AV |
| 1595 | if (pipeline_state->IsGraphicsLibrary()) { |
| 1596 | ReportSetupProblem(device, "GPU-AV does not currently support VK_EXT_graphics_pipeline_library"); |
| 1597 | aborted = true; |
| 1598 | return; |
| 1599 | } |
| 1600 | |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1601 | std::vector<VkDescriptorSet> desc_sets; |
| 1602 | VkDescriptorPool desc_pool = VK_NULL_HANDLE; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1603 | result = desc_set_manager->GetDescriptorSets(1, &desc_pool, debug_desc_layout, &desc_sets); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1604 | assert(result == VK_SUCCESS); |
| 1605 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 1606 | ReportSetupProblem(device, "Unable to allocate descriptor sets. Device could become unstable."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1607 | aborted = true; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1608 | return; |
| 1609 | } |
| 1610 | |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1611 | VkDescriptorBufferInfo output_desc_buffer_info = {}; |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1612 | output_desc_buffer_info.range = output_buffer_size; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1613 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1614 | // Allocate memory for the output block that the gpu will use to return any error information |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1615 | GpuAssistedDeviceMemoryBlock output_block = {}; |
sjfricke | e9b3937 | 2022-05-22 13:02:17 +0900 | [diff] [blame] | 1616 | VkBufferCreateInfo buffer_info = LvlInitStruct<VkBufferCreateInfo>(); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1617 | buffer_info.size = output_buffer_size; |
| 1618 | buffer_info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 1619 | VmaAllocationCreateInfo alloc_info = {}; |
Tony-LunarG | ef49796 | 2022-04-19 08:48:52 -0600 | [diff] [blame] | 1620 | alloc_info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
Tony-LunarG | 20d18a7 | 2022-04-19 11:01:47 -0600 | [diff] [blame] | 1621 | alloc_info.pool = output_buffer_pool; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1622 | result = vmaCreateBuffer(vmaAllocator, &buffer_info, &alloc_info, &output_block.buffer, &output_block.allocation, nullptr); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1623 | if (result != VK_SUCCESS) { |
Tony-LunarG | a2aa78b | 2022-04-19 08:41:38 -0600 | [diff] [blame] | 1624 | ReportSetupProblem(device, "Unable to allocate device memory. Device could become unstable.", true); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1625 | aborted = true; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1626 | return; |
| 1627 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1628 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1629 | // Clear the output block to zeros so that only error information from the gpu will be present |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1630 | uint32_t *data_ptr; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1631 | result = vmaMapMemory(vmaAllocator, output_block.allocation, reinterpret_cast<void **>(&data_ptr)); |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 1632 | if (result == VK_SUCCESS) { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1633 | memset(data_ptr, 0, output_buffer_size); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1634 | vmaUnmapMemory(vmaAllocator, output_block.allocation); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1635 | } |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1636 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1637 | GpuAssistedDeviceMemoryBlock di_input_block = {}, bda_input_block = {}; |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1638 | VkDescriptorBufferInfo di_input_desc_buffer_info = {}; |
| 1639 | VkDescriptorBufferInfo bda_input_desc_buffer_info = {}; |
| 1640 | VkWriteDescriptorSet desc_writes[3] = {}; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1641 | GpuAssistedPreDrawResources pre_draw_resources = {}; |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1642 | uint32_t desc_count = 1; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1643 | uint32_t number_of_sets = static_cast<uint32_t>(state.per_set.size()); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1644 | |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 1645 | if (validate_draw_indirect && ((cmd_type == CMD_DRAWINDIRECTCOUNT || cmd_type == CMD_DRAWINDIRECTCOUNTKHR || |
| 1646 | cmd_type == CMD_DRAWINDEXEDINDIRECTCOUNT || cmd_type == CMD_DRAWINDEXEDINDIRECTCOUNTKHR) || |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1647 | ((cmd_type == CMD_DRAWINDIRECT || cmd_type == CMD_DRAWINDEXEDINDIRECT) && |
| 1648 | !(enabled_features.core.drawIndirectFirstInstance)))) { |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1649 | // Insert a draw that can examine some device memory right before the draw we're validating (Pre Draw Validation) |
Tony-LunarG | 20678ff | 2021-05-07 14:56:26 -0600 | [diff] [blame] | 1650 | // |
| 1651 | // NOTE that this validation does not attempt to abort invalid api calls as most other validation does. A crash |
| 1652 | // or DEVICE_LOST resulting from the invalid call will prevent preceeding validation errors from being reported. |
| 1653 | |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1654 | assert(bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1655 | assert(cdi_state != NULL); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1656 | VkPipeline validation_pipeline; |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1657 | AllocatePreDrawValidationResources(output_block, pre_draw_resources, state, &validation_pipeline, cdi_state); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1658 | if (aborted) return; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1659 | |
| 1660 | // Save current graphics pipeline state |
| 1661 | GPUAV_RESTORABLE_PIPELINE_STATE restorable_state; |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 1662 | restorable_state.Create(cb_node.get(), VK_PIPELINE_BIND_POINT_GRAPHICS); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1663 | |
Tony-LunarG | f0d4e2b | 2021-04-14 11:52:28 -0600 | [diff] [blame] | 1664 | // Save parameters for error message |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1665 | pre_draw_resources.buffer = cdi_state->buffer; |
| 1666 | pre_draw_resources.offset = cdi_state->offset; |
| 1667 | pre_draw_resources.stride = cdi_state->stride; |
| 1668 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1669 | uint32_t push_constants[pre_draw_resources.push_constant_words] = {}; |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 1670 | if (cmd_type == CMD_DRAWINDIRECTCOUNT || cmd_type == CMD_DRAWINDIRECTCOUNTKHR || cmd_type == CMD_DRAWINDEXEDINDIRECTCOUNT || |
| 1671 | cmd_type == CMD_DRAWINDEXEDINDIRECTCOUNTKHR) { |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1672 | // Validate count buffer |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1673 | if (cdi_state->count_buffer_offset > std::numeric_limits<uint32_t>::max()) { |
| 1674 | ReportSetupProblem(device, |
| 1675 | "Count buffer offset is larger than can be contained in an unsigned int. Aborting GPU-AV"); |
| 1676 | aborted = true; |
| 1677 | return; |
| 1678 | } |
| 1679 | |
| 1680 | // Buffer size must be >= (stride * (drawCount - 1) + offset + sizeof(VkDrawIndirectCommand)) |
| 1681 | uint32_t struct_size; |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 1682 | if (cmd_type == CMD_DRAWINDIRECTCOUNT || cmd_type == CMD_DRAWINDIRECTCOUNTKHR) { |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1683 | struct_size = sizeof(VkDrawIndirectCommand); |
| 1684 | } else { |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 1685 | assert(cmd_type == CMD_DRAWINDEXEDINDIRECTCOUNT || cmd_type == CMD_DRAWINDEXEDINDIRECTCOUNTKHR); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1686 | struct_size = sizeof(VkDrawIndexedIndirectCommand); |
| 1687 | } |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 1688 | auto buffer_state = Get<BUFFER_STATE>(cdi_state->buffer); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1689 | uint32_t max_count; |
| 1690 | uint64_t bufsize = buffer_state->createInfo.size; |
| 1691 | uint64_t first_command_bytes = struct_size + cdi_state->offset; |
| 1692 | if (first_command_bytes > bufsize) { |
| 1693 | max_count = 0; |
| 1694 | } else { |
| 1695 | max_count = 1 + static_cast<uint32_t>(std::floor(((bufsize - first_command_bytes) / cdi_state->stride))); |
| 1696 | } |
| 1697 | pre_draw_resources.buf_size = buffer_state->createInfo.size; |
| 1698 | |
| 1699 | assert(phys_dev_props.limits.maxDrawIndirectCount > 0); |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1700 | push_constants[0] = phys_dev_props.limits.maxDrawIndirectCount; |
| 1701 | push_constants[1] = max_count; |
| 1702 | push_constants[2] = static_cast<uint32_t>((cdi_state->count_buffer_offset / sizeof(uint32_t))); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1703 | } else { |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1704 | // Validate buffer for firstInstance check instead of count buffer check |
| 1705 | push_constants[0] = 0; |
| 1706 | push_constants[1] = cdi_state->draw_count; |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1707 | if (cmd_type == CMD_DRAWINDIRECT) { |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1708 | push_constants[2] = static_cast<uint32_t>( |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1709 | ((cdi_state->offset + offsetof(struct VkDrawIndirectCommand, firstInstance)) / sizeof(uint32_t))); |
| 1710 | } else { |
| 1711 | assert(cmd_type == CMD_DRAWINDEXEDINDIRECT); |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1712 | push_constants[2] = static_cast<uint32_t>( |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1713 | ((cdi_state->offset + offsetof(struct VkDrawIndexedIndirectCommand, firstInstance)) / sizeof(uint32_t))); |
| 1714 | } |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1715 | push_constants[3] = (cdi_state->stride / sizeof(uint32_t)); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1716 | } |
Tony-LunarG | f0d4e2b | 2021-04-14 11:52:28 -0600 | [diff] [blame] | 1717 | |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1718 | // Insert diagnostic draw |
| 1719 | DispatchCmdBindPipeline(cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, validation_pipeline); |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame] | 1720 | DispatchCmdPushConstants(cmd_buffer, pre_draw_validation_state.pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT, 0, |
| 1721 | sizeof(push_constants), push_constants); |
| 1722 | DispatchCmdBindDescriptorSets(cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pre_draw_validation_state.pipeline_layout, 0, 1, |
| 1723 | &pre_draw_resources.desc_set, 0, nullptr); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1724 | DispatchCmdDraw(cmd_buffer, 3, 1, 0, 0); |
| 1725 | |
| 1726 | // Restore the previous graphics pipeline state. |
| 1727 | restorable_state.Restore(cmd_buffer); |
| 1728 | } |
| 1729 | |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1730 | bool has_buffers = false; |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1731 | // Figure out how much memory we need for the input block based on how many sets and bindings there are |
| 1732 | // and how big each of the bindings is |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1733 | if (number_of_sets > 0 && (descriptor_indexing || buffer_oob_enabled)) { |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1734 | uint32_t descriptor_count = 0; // Number of descriptors, including all array elements |
| 1735 | uint32_t binding_count = 0; // Number of bindings based on the max binding number used |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1736 | for (const auto &s : state.per_set) { |
Jeff Bolz | b1fc073 | 2019-08-11 20:16:49 -0500 | [diff] [blame] | 1737 | auto desc = s.bound_descriptor_set; |
Tony-LunarG | d9224b1 | 2019-09-11 11:43:04 -0600 | [diff] [blame] | 1738 | if (desc && (desc->GetBindingCount() > 0)) { |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1739 | binding_count += desc->GetLayout()->GetMaxBinding() + 1; |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1740 | for (const auto &binding : *desc) { |
Tony-LunarG | 7564b38 | 2019-08-21 10:11:35 -0600 | [diff] [blame] | 1741 | // Shader instrumentation is tracking inline uniform blocks as scalers. Don't try to validate inline uniform |
| 1742 | // blocks |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1743 | if (binding->type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) { |
Tony-LunarG | 7564b38 | 2019-08-21 10:11:35 -0600 | [diff] [blame] | 1744 | descriptor_count++; |
Mark Lobodzinski | afa7cb8 | 2020-01-29 16:49:36 -0700 | [diff] [blame] | 1745 | LogWarning(device, "UNASSIGNED-GPU-Assisted Validation Warning", |
| 1746 | "VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT descriptors will not be validated by GPU assisted " |
| 1747 | "validation"); |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1748 | } else { |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1749 | descriptor_count += binding->count; |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1750 | } |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1751 | if (!has_buffers && (binding->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER || |
| 1752 | binding->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC || |
| 1753 | binding->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || |
| 1754 | binding->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC || |
| 1755 | binding->type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER || |
| 1756 | binding->type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)) { |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1757 | has_buffers = true; |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 1758 | } |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1759 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1760 | } |
| 1761 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1762 | |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1763 | if (descriptor_indexing || has_buffers) { |
| 1764 | // Note that the size of the input buffer is dependent on the maximum binding number, which |
| 1765 | // can be very large. This is because for (set = s, binding = b, index = i), the validation |
| 1766 | // code is going to dereference Input[ i + Input[ b + Input[ s + Input[ Input[0] ] ] ] ] to |
| 1767 | // see if descriptors have been written. In gpu_validation.md, we note this and advise |
| 1768 | // using densely packed bindings as a best practice when using gpu-av with descriptor indexing |
| 1769 | uint32_t words_needed; |
| 1770 | if (descriptor_indexing) { |
| 1771 | words_needed = 1 + (number_of_sets * 2) + (binding_count * 2) + descriptor_count; |
| 1772 | } else { |
| 1773 | words_needed = 1 + number_of_sets + binding_count + descriptor_count; |
| 1774 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1775 | buffer_info.size = words_needed * 4; |
Tony-LunarG | 20d18a7 | 2022-04-19 11:01:47 -0600 | [diff] [blame] | 1776 | alloc_info.pool = VK_NULL_HANDLE; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1777 | result = vmaCreateBuffer(vmaAllocator, &buffer_info, &alloc_info, &di_input_block.buffer, &di_input_block.allocation, |
| 1778 | nullptr); |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1779 | if (result != VK_SUCCESS) { |
Tony-LunarG | a2aa78b | 2022-04-19 08:41:38 -0600 | [diff] [blame] | 1780 | ReportSetupProblem(device, "Unable to allocate device memory. Device could become unstable.", true); |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1781 | aborted = true; |
| 1782 | return; |
| 1783 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1784 | |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1785 | // Populate input buffer first with the sizes of every descriptor in every set, then with whether |
| 1786 | // each element of each descriptor has been written or not. See gpu_validation.md for a more thourough |
| 1787 | // outline of the input buffer format |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1788 | result = vmaMapMemory(vmaAllocator, di_input_block.allocation, reinterpret_cast<void **>(&data_ptr)); |
| 1789 | memset(data_ptr, 0, static_cast<size_t>(buffer_info.size)); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1790 | |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1791 | // Descriptor indexing needs the number of descriptors at each binding. |
| 1792 | if (descriptor_indexing) { |
| 1793 | // Pointer to a sets array that points into the sizes array |
| 1794 | uint32_t *sets_to_sizes = data_ptr + 1; |
| 1795 | // Pointer to the sizes array that contains the array size of the descriptor at each binding |
| 1796 | uint32_t *sizes = sets_to_sizes + number_of_sets; |
| 1797 | // Pointer to another sets array that points into the bindings array that points into the written array |
| 1798 | uint32_t *sets_to_bindings = sizes + binding_count; |
| 1799 | // Pointer to the bindings array that points at the start of the writes in the writes array for each binding |
| 1800 | uint32_t *bindings_to_written = sets_to_bindings + number_of_sets; |
| 1801 | // Index of the next entry in the written array to be updated |
| 1802 | uint32_t written_index = 1 + (number_of_sets * 2) + (binding_count * 2); |
| 1803 | uint32_t bind_counter = number_of_sets + 1; |
| 1804 | // Index of the start of the sets_to_bindings array |
| 1805 | data_ptr[0] = number_of_sets + binding_count + 1; |
| 1806 | |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1807 | for (const auto &s : state.per_set) { |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1808 | auto desc = s.bound_descriptor_set; |
| 1809 | if (desc && (desc->GetBindingCount() > 0)) { |
| 1810 | auto layout = desc->GetLayout(); |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1811 | // For each set, fill in index of its bindings sizes in the sizes array |
| 1812 | *sets_to_sizes++ = bind_counter; |
| 1813 | // For each set, fill in the index of its bindings in the bindings_to_written array |
| 1814 | *sets_to_bindings++ = bind_counter + number_of_sets + binding_count; |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1815 | for (auto &binding : *desc) { |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1816 | // For each binding, fill in its size in the sizes array |
| 1817 | // Shader instrumentation is tracking inline uniform blocks as scalers. Don't try to validate inline |
| 1818 | // uniform blocks |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1819 | if (VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT == binding->type) { |
| 1820 | sizes[binding->binding] = 1; |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1821 | } else { |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1822 | sizes[binding->binding] = binding->count; |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1823 | } |
| 1824 | // Fill in the starting index for this binding in the written array in the bindings_to_written array |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1825 | bindings_to_written[binding->binding] = written_index; |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1826 | |
| 1827 | // Shader instrumentation is tracking inline uniform blocks as scalers. Don't try to validate inline |
| 1828 | // uniform blocks |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1829 | if (VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT == binding->type) { |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1830 | data_ptr[written_index++] = UINT_MAX; |
| 1831 | continue; |
| 1832 | } |
| 1833 | |
Jeremy Gebben | f4816f7 | 2022-07-15 08:56:06 -0600 | [diff] [blame] | 1834 | if ((binding->binding_flags & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT) != 0) { |
| 1835 | di_input_block.update_at_submit[written_index] = binding.get(); |
| 1836 | } else { |
| 1837 | SetBindingState(data_ptr, written_index, binding.get()); |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1838 | } |
Jeremy Gebben | f4816f7 | 2022-07-15 08:56:06 -0600 | [diff] [blame] | 1839 | written_index += binding->count; |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1840 | } |
| 1841 | auto last = desc->GetLayout()->GetMaxBinding(); |
| 1842 | bindings_to_written += last + 1; |
| 1843 | bind_counter += last + 1; |
| 1844 | sizes += last + 1; |
| 1845 | } else { |
| 1846 | *sets_to_sizes++ = 0; |
| 1847 | *sets_to_bindings++ = 0; |
| 1848 | } |
| 1849 | } |
| 1850 | } else { |
| 1851 | // If no descriptor indexing, we don't need number of descriptors at each binding, so |
| 1852 | // no sets_to_sizes or sizes arrays, just sets_to_bindings, bindings_to_written and written_index |
| 1853 | |
| 1854 | // Pointer to sets array that points into the bindings array that points into the written array |
| 1855 | uint32_t *sets_to_bindings = data_ptr + 1; |
| 1856 | // Pointer to the bindings array that points at the start of the writes in the writes array for each binding |
| 1857 | uint32_t *bindings_to_written = sets_to_bindings + number_of_sets; |
| 1858 | // Index of the next entry in the written array to be updated |
| 1859 | uint32_t written_index = 1 + number_of_sets + binding_count; |
| 1860 | uint32_t bind_counter = number_of_sets + 1; |
| 1861 | data_ptr[0] = 1; |
| 1862 | |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1863 | for (const auto &s : state.per_set) { |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1864 | auto desc = s.bound_descriptor_set; |
| 1865 | if (desc && (desc->GetBindingCount() > 0)) { |
| 1866 | auto layout = desc->GetLayout(); |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1867 | *sets_to_bindings++ = bind_counter; |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1868 | for (auto &binding : *desc) { |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1869 | // Fill in the starting index for this binding in the written array in the bindings_to_written array |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1870 | bindings_to_written[binding->binding] = written_index; |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1871 | |
| 1872 | // Shader instrumentation is tracking inline uniform blocks as scalers. Don't try to validate inline |
| 1873 | // uniform blocks |
Jeremy Gebben | 1b9fdb8 | 2022-06-15 15:31:32 -0600 | [diff] [blame] | 1874 | if (VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT == binding->type) { |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1875 | data_ptr[written_index++] = UINT_MAX; |
| 1876 | continue; |
| 1877 | } |
| 1878 | |
Jeremy Gebben | f4816f7 | 2022-07-15 08:56:06 -0600 | [diff] [blame] | 1879 | // note that VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT is part of descriptor indexing |
| 1880 | SetBindingState(data_ptr, written_index, binding.get()); |
| 1881 | written_index += binding->count; |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1882 | } |
| 1883 | auto last = desc->GetLayout()->GetMaxBinding(); |
| 1884 | bindings_to_written += last + 1; |
| 1885 | bind_counter += last + 1; |
| 1886 | } else { |
| 1887 | *sets_to_bindings++ = 0; |
| 1888 | } |
| 1889 | } |
| 1890 | } |
| 1891 | vmaUnmapMemory(vmaAllocator, di_input_block.allocation); |
| 1892 | |
| 1893 | di_input_desc_buffer_info.range = (words_needed * 4); |
| 1894 | di_input_desc_buffer_info.buffer = di_input_block.buffer; |
| 1895 | di_input_desc_buffer_info.offset = 0; |
| 1896 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 1897 | desc_writes[1] = LvlInitStruct<VkWriteDescriptorSet>(); |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 1898 | desc_writes[1].dstBinding = 1; |
| 1899 | desc_writes[1].descriptorCount = 1; |
| 1900 | desc_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 1901 | desc_writes[1].pBufferInfo = &di_input_desc_buffer_info; |
| 1902 | desc_writes[1].dstSet = desc_sets[0]; |
| 1903 | |
| 1904 | desc_count = 2; |
| 1905 | } |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 1906 | } |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1907 | |
sfricke-samsung | 45996a4 | 2021-09-16 13:45:27 -0700 | [diff] [blame] | 1908 | if ((IsExtEnabled(device_extensions.vk_ext_buffer_device_address) || |
| 1909 | IsExtEnabled(device_extensions.vk_khr_buffer_device_address)) && |
Jeremy Gebben | b0171bb | 2022-03-18 13:46:13 -0600 | [diff] [blame] | 1910 | shaderInt64 && enabled_features.core12.bufferDeviceAddress) { |
| 1911 | auto address_ranges = GetBufferAddressRanges(); |
| 1912 | if (address_ranges.size() > 0) { |
| 1913 | // Example BDA input buffer assuming 2 buffers using BDA: |
| 1914 | // Word 0 | Index of start of buffer sizes (in this case 5) |
| 1915 | // Word 1 | 0x0000000000000000 |
| 1916 | // Word 2 | Device Address of first buffer (Addresses sorted in ascending order) |
| 1917 | // Word 3 | Device Address of second buffer |
| 1918 | // Word 4 | 0xffffffffffffffff |
| 1919 | // Word 5 | 0 (size of pretend buffer at word 1) |
| 1920 | // Word 6 | Size in bytes of first buffer |
| 1921 | // Word 7 | Size in bytes of second buffer |
| 1922 | // Word 8 | 0 (size of pretend buffer in word 4) |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1923 | |
Jeremy Gebben | b0171bb | 2022-03-18 13:46:13 -0600 | [diff] [blame] | 1924 | uint32_t num_buffers = static_cast<uint32_t>(address_ranges.size()); |
| 1925 | uint32_t words_needed = (num_buffers + 3) + (num_buffers + 2); |
Jeremy Gebben | b0171bb | 2022-03-18 13:46:13 -0600 | [diff] [blame] | 1926 | buffer_info.size = words_needed * 8; // 64 bit words |
Tony-LunarG | 20d18a7 | 2022-04-19 11:01:47 -0600 | [diff] [blame] | 1927 | alloc_info.pool = VK_NULL_HANDLE; |
Jeremy Gebben | b0171bb | 2022-03-18 13:46:13 -0600 | [diff] [blame] | 1928 | result = vmaCreateBuffer(vmaAllocator, &buffer_info, &alloc_info, &bda_input_block.buffer, &bda_input_block.allocation, |
| 1929 | nullptr); |
| 1930 | if (result != VK_SUCCESS) { |
Tony-LunarG | a2aa78b | 2022-04-19 08:41:38 -0600 | [diff] [blame] | 1931 | ReportSetupProblem(device, "Unable to allocate device memory. Device could become unstable.", true); |
Jeremy Gebben | b0171bb | 2022-03-18 13:46:13 -0600 | [diff] [blame] | 1932 | aborted = true; |
| 1933 | return; |
| 1934 | } |
| 1935 | uint64_t *bda_data; |
| 1936 | result = vmaMapMemory(vmaAllocator, bda_input_block.allocation, reinterpret_cast<void **>(&bda_data)); |
| 1937 | uint32_t address_index = 1; |
| 1938 | uint32_t size_index = 3 + num_buffers; |
| 1939 | memset(bda_data, 0, static_cast<size_t>(buffer_info.size)); |
| 1940 | bda_data[0] = size_index; // Start of buffer sizes |
| 1941 | bda_data[address_index++] = 0; // NULL address |
| 1942 | bda_data[size_index++] = 0; |
| 1943 | |
| 1944 | for (const auto &range : address_ranges) { |
| 1945 | bda_data[address_index++] = range.begin; |
| 1946 | bda_data[size_index++] = range.end - range.begin; |
| 1947 | } |
| 1948 | bda_data[address_index] = UINTPTR_MAX; |
| 1949 | bda_data[size_index] = 0; |
| 1950 | vmaUnmapMemory(vmaAllocator, bda_input_block.allocation); |
| 1951 | |
| 1952 | bda_input_desc_buffer_info.range = (words_needed * 8); |
| 1953 | bda_input_desc_buffer_info.buffer = bda_input_block.buffer; |
| 1954 | bda_input_desc_buffer_info.offset = 0; |
| 1955 | |
| 1956 | desc_writes[desc_count] = LvlInitStruct<VkWriteDescriptorSet>(); |
| 1957 | desc_writes[desc_count].dstBinding = 2; |
| 1958 | desc_writes[desc_count].descriptorCount = 1; |
| 1959 | desc_writes[desc_count].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 1960 | desc_writes[desc_count].pBufferInfo = &bda_input_desc_buffer_info; |
| 1961 | desc_writes[desc_count].dstSet = desc_sets[0]; |
| 1962 | desc_count++; |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1963 | } |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1964 | } |
| 1965 | |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1966 | // Write the descriptor |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1967 | output_desc_buffer_info.buffer = output_block.buffer; |
| 1968 | output_desc_buffer_info.offset = 0; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1969 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 1970 | desc_writes[0] = LvlInitStruct<VkWriteDescriptorSet>(); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1971 | desc_writes[0].descriptorCount = 1; |
| 1972 | desc_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 1973 | desc_writes[0].pBufferInfo = &output_desc_buffer_info; |
| 1974 | desc_writes[0].dstSet = desc_sets[0]; |
| 1975 | DispatchUpdateDescriptorSets(device, desc_count, desc_writes, 0, NULL); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1976 | |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 1977 | if (pipeline_state) { |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 1978 | const auto &pipeline_layout = pipeline_state->PipelineLayoutState(); |
| 1979 | if ((pipeline_layout->set_layouts.size() <= desc_set_bind_index) && !pipeline_layout->Destroyed()) { |
| 1980 | DispatchCmdBindDescriptorSets(cmd_buffer, bind_point, pipeline_layout->layout(), desc_set_bind_index, 1, |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1981 | desc_sets.data(), 0, nullptr); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1982 | } |
Nathaniel Cesario | 3fd4f76 | 2022-02-16 16:07:06 -0700 | [diff] [blame] | 1983 | if (pipeline_layout->Destroyed()) { |
Tony-LunarG | 9de6e5f | 2020-06-22 13:02:48 -0600 | [diff] [blame] | 1984 | ReportSetupProblem(device, "Pipeline layout has been destroyed, aborting GPU-AV"); |
| 1985 | aborted = true; |
| 1986 | } else { |
| 1987 | // Record buffer and memory info in CB state tracking |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 1988 | cb_node->gpuav_buffer_list.emplace_back(output_block, di_input_block, bda_input_block, pre_draw_resources, desc_sets[0], |
| 1989 | desc_pool, bind_point, cmd_type); |
Tony-LunarG | 9de6e5f | 2020-06-22 13:02:48 -0600 | [diff] [blame] | 1990 | } |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1991 | } else { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 1992 | ReportSetupProblem(device, "Unable to find pipeline state"); |
Tony-LunarG | 9de6e5f | 2020-06-22 13:02:48 -0600 | [diff] [blame] | 1993 | aborted = true; |
| 1994 | } |
| 1995 | if (aborted) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1996 | vmaDestroyBuffer(vmaAllocator, di_input_block.buffer, di_input_block.allocation); |
| 1997 | vmaDestroyBuffer(vmaAllocator, bda_input_block.buffer, bda_input_block.allocation); |
| 1998 | vmaDestroyBuffer(vmaAllocator, output_block.buffer, output_block.allocation); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1999 | return; |
| 2000 | } |
| 2001 | } |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 2002 | |
| 2003 | std::shared_ptr<CMD_BUFFER_STATE> GpuAssisted::CreateCmdBufferState(VkCommandBuffer cb, |
| 2004 | const VkCommandBufferAllocateInfo *pCreateInfo, |
Jeremy Gebben | cd7fa28 | 2021-10-27 10:25:32 -0600 | [diff] [blame] | 2005 | const COMMAND_POOL_STATE *pool) { |
Jeremy Gebben | 135550d | 2022-03-21 07:15:07 -0600 | [diff] [blame] | 2006 | return std::static_pointer_cast<CMD_BUFFER_STATE>(std::make_shared<gpuav_state::CommandBuffer>(this, cb, pCreateInfo, pool)); |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 2007 | } |
| 2008 | |
Jeremy Gebben | 135550d | 2022-03-21 07:15:07 -0600 | [diff] [blame] | 2009 | gpuav_state::CommandBuffer::CommandBuffer(GpuAssisted *ga, VkCommandBuffer cb, const VkCommandBufferAllocateInfo *pCreateInfo, |
| 2010 | const COMMAND_POOL_STATE *pool) |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 2011 | : gpu_utils_state::CommandBuffer(ga, cb, pCreateInfo, pool) {} |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 2012 | |
Jeremy Gebben | 135550d | 2022-03-21 07:15:07 -0600 | [diff] [blame] | 2013 | void gpuav_state::CommandBuffer::Reset() { |
Jeremy Gebben | f6bb4bb | 2021-08-11 15:41:09 -0600 | [diff] [blame] | 2014 | CMD_BUFFER_STATE::Reset(); |
| 2015 | auto gpuav = static_cast<GpuAssisted *>(dev_data); |
| 2016 | // Free the device memory and descriptor set(s) associated with a command buffer. |
| 2017 | if (gpuav->aborted) { |
| 2018 | return; |
| 2019 | } |
| 2020 | for (auto &buffer_info : gpuav_buffer_list) { |
| 2021 | gpuav->DestroyBuffer(buffer_info); |
| 2022 | } |
| 2023 | gpuav_buffer_list.clear(); |
| 2024 | |
| 2025 | for (auto &as_validation_buffer_info : as_validation_buffers) { |
| 2026 | gpuav->DestroyBuffer(as_validation_buffer_info); |
| 2027 | } |
| 2028 | as_validation_buffers.clear(); |
| 2029 | } |