Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2018-2021 The Khronos Group Inc. |
| 2 | * Copyright (c) 2018-2021 Valve Corporation |
| 3 | * Copyright (c) 2018-2021 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" |
Tony-LunarG | 20678ff | 2021-05-07 14:56:26 -0600 | [diff] [blame] | 28 | #include "gpu_pre_draw_constants.h" |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 29 | #include "sync_utils.h" |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 30 | #include "buffer_state.h" |
| 31 | #include "cmd_buffer_state.h" |
| 32 | #include "render_pass_state.h" |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 33 | |
Jason Macnak | 67407e7 | 2019-07-11 11:05:09 -0700 | [diff] [blame] | 34 | static const VkShaderStageFlags kShaderStageAllRayTracing = |
| 35 | VK_SHADER_STAGE_ANY_HIT_BIT_NV | VK_SHADER_STAGE_CALLABLE_BIT_NV | VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV | |
| 36 | VK_SHADER_STAGE_INTERSECTION_BIT_NV | VK_SHADER_STAGE_MISS_BIT_NV | VK_SHADER_STAGE_RAYGEN_BIT_NV; |
| 37 | |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 38 | // Keep in sync with the GLSL shader below. |
| 39 | struct GpuAccelerationStructureBuildValidationBuffer { |
| 40 | uint32_t instances_to_validate; |
| 41 | uint32_t replacement_handle_bits_0; |
| 42 | uint32_t replacement_handle_bits_1; |
| 43 | uint32_t invalid_handle_found; |
| 44 | uint32_t invalid_handle_bits_0; |
| 45 | uint32_t invalid_handle_bits_1; |
| 46 | uint32_t valid_handles_count; |
| 47 | }; |
| 48 | |
| 49 | // This is the GLSL source for the compute shader that is used during ray tracing acceleration structure |
| 50 | // building validation which inspects instance buffers for top level acceleration structure builds and |
| 51 | // reports and replaces invalid bottom level acceleration structure handles with good bottom level |
| 52 | // acceleration structure handle so that applications can continue without undefined behavior long enough |
| 53 | // to report errors. |
| 54 | // |
| 55 | // #version 450 |
| 56 | // layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; |
| 57 | // struct VkGeometryInstanceNV { |
| 58 | // uint unused[14]; |
| 59 | // uint handle_bits_0; |
| 60 | // uint handle_bits_1; |
| 61 | // }; |
| 62 | // layout(set=0, binding=0, std430) buffer InstanceBuffer { |
| 63 | // VkGeometryInstanceNV instances[]; |
| 64 | // }; |
| 65 | // layout(set=0, binding=1, std430) buffer ValidationBuffer { |
| 66 | // uint instances_to_validate; |
| 67 | // uint replacement_handle_bits_0; |
| 68 | // uint replacement_handle_bits_1; |
| 69 | // uint invalid_handle_found; |
| 70 | // uint invalid_handle_bits_0; |
| 71 | // uint invalid_handle_bits_1; |
| 72 | // uint valid_handles_count; |
| 73 | // uint valid_handles[]; |
| 74 | // }; |
| 75 | // void main() { |
| 76 | // for (uint instance_index = 0; instance_index < instances_to_validate; instance_index++) { |
| 77 | // uint instance_handle_bits_0 = instances[instance_index].handle_bits_0; |
| 78 | // uint instance_handle_bits_1 = instances[instance_index].handle_bits_1; |
| 79 | // bool valid = false; |
| 80 | // for (uint valid_handle_index = 0; valid_handle_index < valid_handles_count; valid_handle_index++) { |
| 81 | // if (instance_handle_bits_0 == valid_handles[2*valid_handle_index+0] && |
| 82 | // instance_handle_bits_1 == valid_handles[2*valid_handle_index+1]) { |
| 83 | // valid = true; |
| 84 | // break; |
| 85 | // } |
| 86 | // } |
| 87 | // if (!valid) { |
| 88 | // invalid_handle_found += 1; |
| 89 | // invalid_handle_bits_0 = instance_handle_bits_0; |
| 90 | // invalid_handle_bits_1 = instance_handle_bits_1; |
| 91 | // instances[instance_index].handle_bits_0 = replacement_handle_bits_0; |
| 92 | // instances[instance_index].handle_bits_1 = replacement_handle_bits_1; |
| 93 | // } |
| 94 | // } |
| 95 | // } |
| 96 | // |
| 97 | // To regenerate the spirv below: |
| 98 | // 1. Save the above GLSL source to a file called validation_shader.comp. |
| 99 | // 2. Run in terminal |
| 100 | // |
| 101 | // glslangValidator.exe -x -V validation_shader.comp -o validation_shader.comp.spv |
| 102 | // |
| 103 | // 4. Copy-paste the contents of validation_shader.comp.spv here (clang-format will fix up the alignment). |
| 104 | static const uint32_t kComputeShaderSpirv[] = { |
| 105 | 0x07230203, 0x00010000, 0x00080007, 0x0000006d, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, |
| 106 | 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0005000f, 0x00000005, 0x00000004, 0x6e69616d, |
| 107 | 0x00000000, 0x00060010, 0x00000004, 0x00000011, 0x00000001, 0x00000001, 0x00000001, 0x00030003, 0x00000002, 0x000001c2, |
| 108 | 0x00040005, 0x00000004, 0x6e69616d, 0x00000000, 0x00060005, 0x00000008, 0x74736e69, 0x65636e61, 0x646e695f, 0x00007865, |
| 109 | 0x00070005, 0x00000011, 0x696c6156, 0x69746164, 0x75426e6f, 0x72656666, 0x00000000, 0x00090006, 0x00000011, 0x00000000, |
| 110 | 0x74736e69, 0x65636e61, 0x6f745f73, 0x6c61765f, 0x74616469, 0x00000065, 0x000a0006, 0x00000011, 0x00000001, 0x6c706572, |
| 111 | 0x6d656361, 0x5f746e65, 0x646e6168, 0x625f656c, 0x5f737469, 0x00000030, 0x000a0006, 0x00000011, 0x00000002, 0x6c706572, |
| 112 | 0x6d656361, 0x5f746e65, 0x646e6168, 0x625f656c, 0x5f737469, 0x00000031, 0x00090006, 0x00000011, 0x00000003, 0x61766e69, |
| 113 | 0x5f64696c, 0x646e6168, 0x665f656c, 0x646e756f, 0x00000000, 0x00090006, 0x00000011, 0x00000004, 0x61766e69, 0x5f64696c, |
| 114 | 0x646e6168, 0x625f656c, 0x5f737469, 0x00000030, 0x00090006, 0x00000011, 0x00000005, 0x61766e69, 0x5f64696c, 0x646e6168, |
| 115 | 0x625f656c, 0x5f737469, 0x00000031, 0x00080006, 0x00000011, 0x00000006, 0x696c6176, 0x61685f64, 0x656c646e, 0x6f635f73, |
| 116 | 0x00746e75, 0x00070006, 0x00000011, 0x00000007, 0x696c6176, 0x61685f64, 0x656c646e, 0x00000073, 0x00030005, 0x00000013, |
| 117 | 0x00000000, 0x00080005, 0x0000001b, 0x74736e69, 0x65636e61, 0x6e61685f, 0x5f656c64, 0x73746962, 0x0000305f, 0x00080005, |
| 118 | 0x0000001e, 0x65476b56, 0x74656d6f, 0x6e497972, 0x6e617473, 0x564e6563, 0x00000000, 0x00050006, 0x0000001e, 0x00000000, |
| 119 | 0x73756e75, 0x00006465, 0x00070006, 0x0000001e, 0x00000001, 0x646e6168, 0x625f656c, 0x5f737469, 0x00000030, 0x00070006, |
| 120 | 0x0000001e, 0x00000002, 0x646e6168, 0x625f656c, 0x5f737469, 0x00000031, 0x00060005, 0x00000020, 0x74736e49, 0x65636e61, |
| 121 | 0x66667542, 0x00007265, 0x00060006, 0x00000020, 0x00000000, 0x74736e69, 0x65636e61, 0x00000073, 0x00030005, 0x00000022, |
| 122 | 0x00000000, 0x00080005, 0x00000027, 0x74736e69, 0x65636e61, 0x6e61685f, 0x5f656c64, 0x73746962, 0x0000315f, 0x00040005, |
| 123 | 0x0000002d, 0x696c6176, 0x00000064, 0x00070005, 0x0000002f, 0x696c6176, 0x61685f64, 0x656c646e, 0x646e695f, 0x00007865, |
| 124 | 0x00040047, 0x00000010, 0x00000006, 0x00000004, 0x00050048, 0x00000011, 0x00000000, 0x00000023, 0x00000000, 0x00050048, |
| 125 | 0x00000011, 0x00000001, 0x00000023, 0x00000004, 0x00050048, 0x00000011, 0x00000002, 0x00000023, 0x00000008, 0x00050048, |
| 126 | 0x00000011, 0x00000003, 0x00000023, 0x0000000c, 0x00050048, 0x00000011, 0x00000004, 0x00000023, 0x00000010, 0x00050048, |
| 127 | 0x00000011, 0x00000005, 0x00000023, 0x00000014, 0x00050048, 0x00000011, 0x00000006, 0x00000023, 0x00000018, 0x00050048, |
| 128 | 0x00000011, 0x00000007, 0x00000023, 0x0000001c, 0x00030047, 0x00000011, 0x00000003, 0x00040047, 0x00000013, 0x00000022, |
| 129 | 0x00000000, 0x00040047, 0x00000013, 0x00000021, 0x00000001, 0x00040047, 0x0000001d, 0x00000006, 0x00000004, 0x00050048, |
| 130 | 0x0000001e, 0x00000000, 0x00000023, 0x00000000, 0x00050048, 0x0000001e, 0x00000001, 0x00000023, 0x00000038, 0x00050048, |
| 131 | 0x0000001e, 0x00000002, 0x00000023, 0x0000003c, 0x00040047, 0x0000001f, 0x00000006, 0x00000040, 0x00050048, 0x00000020, |
| 132 | 0x00000000, 0x00000023, 0x00000000, 0x00030047, 0x00000020, 0x00000003, 0x00040047, 0x00000022, 0x00000022, 0x00000000, |
| 133 | 0x00040047, 0x00000022, 0x00000021, 0x00000000, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00040015, |
| 134 | 0x00000006, 0x00000020, 0x00000000, 0x00040020, 0x00000007, 0x00000007, 0x00000006, 0x0004002b, 0x00000006, 0x00000009, |
| 135 | 0x00000000, 0x0003001d, 0x00000010, 0x00000006, 0x000a001e, 0x00000011, 0x00000006, 0x00000006, 0x00000006, 0x00000006, |
| 136 | 0x00000006, 0x00000006, 0x00000006, 0x00000010, 0x00040020, 0x00000012, 0x00000002, 0x00000011, 0x0004003b, 0x00000012, |
| 137 | 0x00000013, 0x00000002, 0x00040015, 0x00000014, 0x00000020, 0x00000001, 0x0004002b, 0x00000014, 0x00000015, 0x00000000, |
| 138 | 0x00040020, 0x00000016, 0x00000002, 0x00000006, 0x00020014, 0x00000019, 0x0004002b, 0x00000006, 0x0000001c, 0x0000000e, |
| 139 | 0x0004001c, 0x0000001d, 0x00000006, 0x0000001c, 0x0005001e, 0x0000001e, 0x0000001d, 0x00000006, 0x00000006, 0x0003001d, |
| 140 | 0x0000001f, 0x0000001e, 0x0003001e, 0x00000020, 0x0000001f, 0x00040020, 0x00000021, 0x00000002, 0x00000020, 0x0004003b, |
| 141 | 0x00000021, 0x00000022, 0x00000002, 0x0004002b, 0x00000014, 0x00000024, 0x00000001, 0x0004002b, 0x00000014, 0x00000029, |
| 142 | 0x00000002, 0x00040020, 0x0000002c, 0x00000007, 0x00000019, 0x0003002a, 0x00000019, 0x0000002e, 0x0004002b, 0x00000014, |
| 143 | 0x00000036, 0x00000006, 0x0004002b, 0x00000014, 0x0000003b, 0x00000007, 0x0004002b, 0x00000006, 0x0000003c, 0x00000002, |
| 144 | 0x0004002b, 0x00000006, 0x00000048, 0x00000001, 0x00030029, 0x00000019, 0x00000050, 0x0004002b, 0x00000014, 0x00000058, |
| 145 | 0x00000003, 0x0004002b, 0x00000014, 0x0000005d, 0x00000004, 0x0004002b, 0x00000014, 0x00000060, 0x00000005, 0x00050036, |
| 146 | 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x0004003b, 0x00000007, 0x00000008, 0x00000007, |
| 147 | 0x0004003b, 0x00000007, 0x0000001b, 0x00000007, 0x0004003b, 0x00000007, 0x00000027, 0x00000007, 0x0004003b, 0x0000002c, |
| 148 | 0x0000002d, 0x00000007, 0x0004003b, 0x00000007, 0x0000002f, 0x00000007, 0x0003003e, 0x00000008, 0x00000009, 0x000200f9, |
| 149 | 0x0000000a, 0x000200f8, 0x0000000a, 0x000400f6, 0x0000000c, 0x0000000d, 0x00000000, 0x000200f9, 0x0000000e, 0x000200f8, |
| 150 | 0x0000000e, 0x0004003d, 0x00000006, 0x0000000f, 0x00000008, 0x00050041, 0x00000016, 0x00000017, 0x00000013, 0x00000015, |
| 151 | 0x0004003d, 0x00000006, 0x00000018, 0x00000017, 0x000500b0, 0x00000019, 0x0000001a, 0x0000000f, 0x00000018, 0x000400fa, |
| 152 | 0x0000001a, 0x0000000b, 0x0000000c, 0x000200f8, 0x0000000b, 0x0004003d, 0x00000006, 0x00000023, 0x00000008, 0x00070041, |
| 153 | 0x00000016, 0x00000025, 0x00000022, 0x00000015, 0x00000023, 0x00000024, 0x0004003d, 0x00000006, 0x00000026, 0x00000025, |
| 154 | 0x0003003e, 0x0000001b, 0x00000026, 0x0004003d, 0x00000006, 0x00000028, 0x00000008, 0x00070041, 0x00000016, 0x0000002a, |
| 155 | 0x00000022, 0x00000015, 0x00000028, 0x00000029, 0x0004003d, 0x00000006, 0x0000002b, 0x0000002a, 0x0003003e, 0x00000027, |
| 156 | 0x0000002b, 0x0003003e, 0x0000002d, 0x0000002e, 0x0003003e, 0x0000002f, 0x00000009, 0x000200f9, 0x00000030, 0x000200f8, |
| 157 | 0x00000030, 0x000400f6, 0x00000032, 0x00000033, 0x00000000, 0x000200f9, 0x00000034, 0x000200f8, 0x00000034, 0x0004003d, |
| 158 | 0x00000006, 0x00000035, 0x0000002f, 0x00050041, 0x00000016, 0x00000037, 0x00000013, 0x00000036, 0x0004003d, 0x00000006, |
| 159 | 0x00000038, 0x00000037, 0x000500b0, 0x00000019, 0x00000039, 0x00000035, 0x00000038, 0x000400fa, 0x00000039, 0x00000031, |
| 160 | 0x00000032, 0x000200f8, 0x00000031, 0x0004003d, 0x00000006, 0x0000003a, 0x0000001b, 0x0004003d, 0x00000006, 0x0000003d, |
| 161 | 0x0000002f, 0x00050084, 0x00000006, 0x0000003e, 0x0000003c, 0x0000003d, 0x00050080, 0x00000006, 0x0000003f, 0x0000003e, |
| 162 | 0x00000009, 0x00060041, 0x00000016, 0x00000040, 0x00000013, 0x0000003b, 0x0000003f, 0x0004003d, 0x00000006, 0x00000041, |
| 163 | 0x00000040, 0x000500aa, 0x00000019, 0x00000042, 0x0000003a, 0x00000041, 0x000300f7, 0x00000044, 0x00000000, 0x000400fa, |
| 164 | 0x00000042, 0x00000043, 0x00000044, 0x000200f8, 0x00000043, 0x0004003d, 0x00000006, 0x00000045, 0x00000027, 0x0004003d, |
| 165 | 0x00000006, 0x00000046, 0x0000002f, 0x00050084, 0x00000006, 0x00000047, 0x0000003c, 0x00000046, 0x00050080, 0x00000006, |
| 166 | 0x00000049, 0x00000047, 0x00000048, 0x00060041, 0x00000016, 0x0000004a, 0x00000013, 0x0000003b, 0x00000049, 0x0004003d, |
| 167 | 0x00000006, 0x0000004b, 0x0000004a, 0x000500aa, 0x00000019, 0x0000004c, 0x00000045, 0x0000004b, 0x000200f9, 0x00000044, |
| 168 | 0x000200f8, 0x00000044, 0x000700f5, 0x00000019, 0x0000004d, 0x00000042, 0x00000031, 0x0000004c, 0x00000043, 0x000300f7, |
| 169 | 0x0000004f, 0x00000000, 0x000400fa, 0x0000004d, 0x0000004e, 0x0000004f, 0x000200f8, 0x0000004e, 0x0003003e, 0x0000002d, |
| 170 | 0x00000050, 0x000200f9, 0x00000032, 0x000200f8, 0x0000004f, 0x000200f9, 0x00000033, 0x000200f8, 0x00000033, 0x0004003d, |
| 171 | 0x00000006, 0x00000052, 0x0000002f, 0x00050080, 0x00000006, 0x00000053, 0x00000052, 0x00000024, 0x0003003e, 0x0000002f, |
| 172 | 0x00000053, 0x000200f9, 0x00000030, 0x000200f8, 0x00000032, 0x0004003d, 0x00000019, 0x00000054, 0x0000002d, 0x000400a8, |
| 173 | 0x00000019, 0x00000055, 0x00000054, 0x000300f7, 0x00000057, 0x00000000, 0x000400fa, 0x00000055, 0x00000056, 0x00000057, |
| 174 | 0x000200f8, 0x00000056, 0x00050041, 0x00000016, 0x00000059, 0x00000013, 0x00000058, 0x0004003d, 0x00000006, 0x0000005a, |
| 175 | 0x00000059, 0x00050080, 0x00000006, 0x0000005b, 0x0000005a, 0x00000048, 0x00050041, 0x00000016, 0x0000005c, 0x00000013, |
| 176 | 0x00000058, 0x0003003e, 0x0000005c, 0x0000005b, 0x0004003d, 0x00000006, 0x0000005e, 0x0000001b, 0x00050041, 0x00000016, |
| 177 | 0x0000005f, 0x00000013, 0x0000005d, 0x0003003e, 0x0000005f, 0x0000005e, 0x0004003d, 0x00000006, 0x00000061, 0x00000027, |
| 178 | 0x00050041, 0x00000016, 0x00000062, 0x00000013, 0x00000060, 0x0003003e, 0x00000062, 0x00000061, 0x0004003d, 0x00000006, |
| 179 | 0x00000063, 0x00000008, 0x00050041, 0x00000016, 0x00000064, 0x00000013, 0x00000024, 0x0004003d, 0x00000006, 0x00000065, |
| 180 | 0x00000064, 0x00070041, 0x00000016, 0x00000066, 0x00000022, 0x00000015, 0x00000063, 0x00000024, 0x0003003e, 0x00000066, |
| 181 | 0x00000065, 0x0004003d, 0x00000006, 0x00000067, 0x00000008, 0x00050041, 0x00000016, 0x00000068, 0x00000013, 0x00000029, |
| 182 | 0x0004003d, 0x00000006, 0x00000069, 0x00000068, 0x00070041, 0x00000016, 0x0000006a, 0x00000022, 0x00000015, 0x00000067, |
| 183 | 0x00000029, 0x0003003e, 0x0000006a, 0x00000069, 0x000200f9, 0x00000057, 0x000200f8, 0x00000057, 0x000200f9, 0x0000000d, |
| 184 | 0x000200f8, 0x0000000d, 0x0004003d, 0x00000006, 0x0000006b, 0x00000008, 0x00050080, 0x00000006, 0x0000006c, 0x0000006b, |
| 185 | 0x00000024, 0x0003003e, 0x00000008, 0x0000006c, 0x000200f9, 0x0000000a, 0x000200f8, 0x0000000c, 0x000100fd, 0x00010038}; |
| 186 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 187 | // Convenience function for reporting problems with setting up GPU Validation. |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 188 | template <typename T> |
| 189 | void GpuAssisted::ReportSetupProblem(T object, const char *const specific_message) const { |
| 190 | LogError(object, "UNASSIGNED-GPU-Assisted Validation Error. ", "Detail: (%s)", specific_message); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 191 | } |
| 192 | |
Tony-LunarG | 5c38b18 | 2020-06-10 16:15:32 -0600 | [diff] [blame] | 193 | bool GpuAssisted::CheckForDescriptorIndexing(DeviceFeatures enabled_features) const { |
| 194 | bool result = |
| 195 | (IsExtEnabled(device_extensions.vk_ext_descriptor_indexing) && |
| 196 | (enabled_features.core12.descriptorIndexing || enabled_features.core12.shaderInputAttachmentArrayDynamicIndexing || |
| 197 | enabled_features.core12.shaderUniformTexelBufferArrayDynamicIndexing || |
| 198 | enabled_features.core12.shaderStorageTexelBufferArrayDynamicIndexing || |
| 199 | enabled_features.core12.shaderUniformBufferArrayNonUniformIndexing || |
| 200 | enabled_features.core12.shaderSampledImageArrayNonUniformIndexing || |
| 201 | enabled_features.core12.shaderStorageBufferArrayNonUniformIndexing || |
| 202 | enabled_features.core12.shaderStorageImageArrayNonUniformIndexing || |
| 203 | enabled_features.core12.shaderInputAttachmentArrayNonUniformIndexing || |
| 204 | enabled_features.core12.shaderUniformTexelBufferArrayNonUniformIndexing || |
| 205 | enabled_features.core12.shaderStorageTexelBufferArrayNonUniformIndexing || |
| 206 | enabled_features.core12.descriptorBindingUniformBufferUpdateAfterBind || |
| 207 | enabled_features.core12.descriptorBindingSampledImageUpdateAfterBind || |
| 208 | enabled_features.core12.descriptorBindingStorageImageUpdateAfterBind || |
| 209 | enabled_features.core12.descriptorBindingStorageBufferUpdateAfterBind || |
| 210 | enabled_features.core12.descriptorBindingUniformTexelBufferUpdateAfterBind || |
| 211 | enabled_features.core12.descriptorBindingStorageTexelBufferUpdateAfterBind || |
| 212 | enabled_features.core12.descriptorBindingUpdateUnusedWhilePending || |
| 213 | enabled_features.core12.descriptorBindingPartiallyBound || |
| 214 | enabled_features.core12.descriptorBindingVariableDescriptorCount || enabled_features.core12.runtimeDescriptorArray)); |
| 215 | return result; |
| 216 | } |
| 217 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 218 | void GpuAssisted::PreCallRecordCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo, |
| 219 | const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer, void *cb_state_data) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 220 | // Ray tracing acceleration structure instance buffers also need the storage buffer usage as |
| 221 | // acceleration structure build validation will find and replace invalid acceleration structure |
| 222 | // handles inside of a compute shader. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 223 | create_buffer_api_state *cb_state = reinterpret_cast<create_buffer_api_state *>(cb_state_data); |
| 224 | if (cb_state && cb_state->modified_create_info.usage & VK_BUFFER_USAGE_RAY_TRACING_BIT_NV) { |
| 225 | cb_state->modified_create_info.usage |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 226 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 227 | |
| 228 | // 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] | 229 | 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] | 230 | cb_state->modified_create_info.usage |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 231 | } |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 234 | // Turn on necessary device features. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 235 | void GpuAssisted::PreCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *create_info, |
| 236 | const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, |
Mark Lobodzinski | b588cd4 | 2020-09-30 11:03:34 -0600 | [diff] [blame] | 237 | void *modified_create_info) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 238 | DispatchGetPhysicalDeviceFeatures(gpu, &supported_features); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 239 | VkPhysicalDeviceFeatures features = {}; |
| 240 | features.vertexPipelineStoresAndAtomics = true; |
| 241 | features.fragmentStoresAndAtomics = true; |
| 242 | features.shaderInt64 = true; |
Mark Lobodzinski | b588cd4 | 2020-09-30 11:03:34 -0600 | [diff] [blame] | 243 | UtilPreCallRecordCreateDevice(gpu, reinterpret_cast<safe_VkDeviceCreateInfo *>(modified_create_info), supported_features, |
| 244 | features); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 245 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 246 | // Perform initializations that can be done at Create Device time. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 247 | void GpuAssisted::PostCallRecordCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo, |
| 248 | const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) { |
| 249 | // The state tracker sets up the device state |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 250 | ValidationStateTracker::PostCallRecordCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice, result); |
Mark Lobodzinski | 5dc3dcd | 2019-04-23 14:26:28 -0600 | [diff] [blame] | 251 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 252 | ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map); |
| 253 | ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, this->container_type); |
| 254 | GpuAssisted *device_gpu_assisted = static_cast<GpuAssisted *>(validation_data); |
Tony-LunarG | 65f9c49 | 2019-01-17 14:24:42 -0700 | [diff] [blame] | 255 | |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 256 | if (device_gpu_assisted->enabled_features.core.robustBufferAccess || |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 257 | device_gpu_assisted->enabled_features.robustness2_features.robustBufferAccess2) { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 258 | device_gpu_assisted->buffer_oob_enabled = false; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 259 | } else { |
Tony-LunarG | c99dbef | 2021-06-14 15:11:50 -0600 | [diff] [blame] | 260 | std::string bufferoob_string = getLayerOption("khronos_validation.gpuav_buffer_oob"); |
| 261 | transform(bufferoob_string.begin(), bufferoob_string.end(), bufferoob_string.begin(), ::tolower); |
| 262 | device_gpu_assisted->buffer_oob_enabled = bufferoob_string.length() ? !bufferoob_string.compare("true") : true; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 263 | } |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 264 | |
Tony-LunarG | c99dbef | 2021-06-14 15:11:50 -0600 | [diff] [blame] | 265 | std::string draw_indirect_string = getLayerOption("khronos_validation.validate_draw_indirect"); |
| 266 | transform(draw_indirect_string.begin(), draw_indirect_string.end(), draw_indirect_string.begin(), ::tolower); |
| 267 | device_gpu_assisted->validate_draw_indirect = draw_indirect_string.length() ? !draw_indirect_string.compare("true") : true; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 268 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 269 | if (device_gpu_assisted->phys_dev_props.apiVersion < VK_API_VERSION_1_1) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 270 | ReportSetupProblem(device, "GPU-Assisted validation requires Vulkan 1.1 or later. GPU-Assisted Validation disabled."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 271 | device_gpu_assisted->aborted = true; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 272 | return; |
| 273 | } |
Tony-LunarG | 2ab9ede | 2019-05-10 14:34:31 -0600 | [diff] [blame] | 274 | |
Tony-LunarG | 04dc83c | 2020-07-07 13:53:02 -0600 | [diff] [blame] | 275 | if (!supported_features.fragmentStoresAndAtomics || !supported_features.vertexPipelineStoresAndAtomics) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 276 | ReportSetupProblem(device, |
Tony-LunarG | 7c668ab | 2019-08-28 16:13:01 -0600 | [diff] [blame] | 277 | "GPU-Assisted validation requires fragmentStoresAndAtomics and vertexPipelineStoresAndAtomics. " |
| 278 | "GPU-Assisted Validation disabled."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 279 | device_gpu_assisted->aborted = true; |
Tony-LunarG | 7c668ab | 2019-08-28 16:13:01 -0600 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
Tony-LunarG | 7e0842f | 2019-12-10 09:26:34 -0700 | [diff] [blame] | 283 | if ((device_extensions.vk_ext_buffer_device_address || device_extensions.vk_khr_buffer_device_address) && |
Tony-LunarG | 04dc83c | 2020-07-07 13:53:02 -0600 | [diff] [blame] | 284 | !supported_features.shaderInt64) { |
Mark Lobodzinski | afa7cb8 | 2020-01-29 16:49:36 -0700 | [diff] [blame] | 285 | LogWarning(device, "UNASSIGNED-GPU-Assisted Validation Warning", |
| 286 | "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] | 287 | } |
Tony-LunarG | 04dc83c | 2020-07-07 13:53:02 -0600 | [diff] [blame] | 288 | device_gpu_assisted->shaderInt64 = supported_features.shaderInt64; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 289 | device_gpu_assisted->physicalDevice = physicalDevice; |
| 290 | device_gpu_assisted->device = *pDevice; |
Tony-LunarG | bb145f3 | 2020-04-27 13:41:29 -0600 | [diff] [blame] | 291 | device_gpu_assisted->output_buffer_size = sizeof(uint32_t) * (spvtools::kInstMaxOutCnt + 1); |
Tony-LunarG | 5c38b18 | 2020-06-10 16:15:32 -0600 | [diff] [blame] | 292 | device_gpu_assisted->descriptor_indexing = CheckForDescriptorIndexing(device_gpu_assisted->enabled_features); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 293 | std::vector<VkDescriptorSetLayoutBinding> bindings; |
| 294 | VkDescriptorSetLayoutBinding binding = {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, |
Tony-LunarG | c7ed208 | 2020-06-11 14:00:04 -0600 | [diff] [blame] | 295 | VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT | |
| 296 | VK_SHADER_STAGE_MESH_BIT_NV | VK_SHADER_STAGE_TASK_BIT_NV | |
| 297 | kShaderStageAllRayTracing, |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 298 | NULL}; |
| 299 | bindings.push_back(binding); |
| 300 | for (auto i = 1; i < 3; i++) { |
| 301 | binding.binding = i; |
| 302 | bindings.push_back(binding); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 303 | } |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 304 | UtilPostCallRecordCreateDevice(pCreateInfo, bindings, device_gpu_assisted, device_gpu_assisted->phys_dev_props); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 305 | CreateAccelerationStructureBuildValidationState(device_gpu_assisted); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 306 | } |
| 307 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 308 | void GpuAssisted::PostCallRecordGetBufferDeviceAddress(VkDevice device, const VkBufferDeviceAddressInfo *pInfo, |
Tony-LunarG | 588c705 | 2020-04-23 10:47:21 -0600 | [diff] [blame] | 309 | VkDeviceAddress address) { |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 310 | BUFFER_STATE *buffer_state = GetBufferState(pInfo->buffer); |
| 311 | // Validate against the size requested when the buffer was created |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 312 | if (buffer_state) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 313 | buffer_map[address] = buffer_state->createInfo.size; |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 314 | } |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 315 | } |
| 316 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 317 | void GpuAssisted::PostCallRecordGetBufferDeviceAddressEXT(VkDevice device, const VkBufferDeviceAddressInfo *pInfo, |
Tony-LunarG | 588c705 | 2020-04-23 10:47:21 -0600 | [diff] [blame] | 318 | VkDeviceAddress address) { |
| 319 | PostCallRecordGetBufferDeviceAddress(device, pInfo, address); |
| 320 | } |
| 321 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 322 | void GpuAssisted::PostCallRecordGetBufferDeviceAddressKHR(VkDevice device, const VkBufferDeviceAddressInfo *pInfo, |
Tony-LunarG | 7e0842f | 2019-12-10 09:26:34 -0700 | [diff] [blame] | 323 | VkDeviceAddress address) { |
Tony-LunarG | 588c705 | 2020-04-23 10:47:21 -0600 | [diff] [blame] | 324 | PostCallRecordGetBufferDeviceAddress(device, pInfo, address); |
Tony-LunarG | 7e0842f | 2019-12-10 09:26:34 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 327 | void GpuAssisted::PreCallRecordDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) { |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 328 | BUFFER_STATE *buffer_state = GetBufferState(buffer); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 329 | if (buffer_state) buffer_map.erase(buffer_state->deviceAddress); |
Tony-LunarG | 2966c73 | 2020-05-21 10:33:53 -0600 | [diff] [blame] | 330 | ValidationStateTracker::PreCallRecordDestroyBuffer(device, buffer, pAllocator); |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 331 | } |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 332 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 333 | // Clean up device-related resources |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 334 | void GpuAssisted::PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 335 | DestroyAccelerationStructureBuildValidationState(); |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 336 | UtilPreCallRecordDestroyDevice(this); |
Tony-LunarG | 2966c73 | 2020-05-21 10:33:53 -0600 | [diff] [blame] | 337 | ValidationStateTracker::PreCallRecordDestroyDevice(device, pAllocator); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 338 | if (pre_draw_validation_state.globals_created) { |
| 339 | DispatchDestroyShaderModule(device, pre_draw_validation_state.validation_shader_module, nullptr); |
| 340 | DispatchDestroyDescriptorSetLayout(device, pre_draw_validation_state.validation_ds_layout, nullptr); |
| 341 | DispatchDestroyPipelineLayout(device, pre_draw_validation_state.validation_pipeline_layout, nullptr); |
| 342 | for (auto it = pre_draw_validation_state.renderpass_to_pipeline.begin(); |
| 343 | it != pre_draw_validation_state.renderpass_to_pipeline.end(); ++it) { |
| 344 | DispatchDestroyPipeline(device, it->second, nullptr); |
| 345 | } |
| 346 | pre_draw_validation_state.renderpass_to_pipeline.clear(); |
| 347 | pre_draw_validation_state.globals_created = false; |
| 348 | } |
Tony-LunarG | 0a863bc | 2020-09-16 09:50:04 -0600 | [diff] [blame] | 349 | // State Tracker can end up making vma calls through callbacks - don't destroy allocator until ST is done |
| 350 | if (vmaAllocator) { |
| 351 | vmaDestroyAllocator(vmaAllocator); |
| 352 | } |
| 353 | desc_set_manager.reset(); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 354 | } |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 355 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 356 | void GpuAssisted::CreateAccelerationStructureBuildValidationState(GpuAssisted *device_gpuav) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 357 | if (device_gpuav->aborted) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 361 | auto &as_validation_state = device_gpuav->acceleration_structure_validation_state; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 362 | if (as_validation_state.initialized) { |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | if (!device_extensions.vk_nv_ray_tracing) { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | // Outline: |
| 371 | // - Create valid bottom level acceleration structure which acts as replacement |
| 372 | // - Create and load vertex buffer |
| 373 | // - Create and load index buffer |
| 374 | // - Create, allocate memory for, and bind memory for acceleration structure |
| 375 | // - Query acceleration structure handle |
| 376 | // - Create command pool and command buffer |
| 377 | // - Record build acceleration structure command |
| 378 | // - Submit command buffer and wait for completion |
| 379 | // - Cleanup |
| 380 | // - Create compute pipeline for validating instance buffers |
| 381 | // - Create descriptor set layout |
| 382 | // - Create pipeline layout |
| 383 | // - Create pipeline |
| 384 | // - Cleanup |
| 385 | |
| 386 | VkResult result = VK_SUCCESS; |
| 387 | |
| 388 | VkBuffer vbo = VK_NULL_HANDLE; |
| 389 | VmaAllocation vbo_allocation = VK_NULL_HANDLE; |
| 390 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 391 | auto vbo_ci = LvlInitStruct<VkBufferCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 392 | vbo_ci.size = sizeof(float) * 9; |
| 393 | vbo_ci.usage = VK_BUFFER_USAGE_RAY_TRACING_BIT_NV; |
| 394 | |
| 395 | VmaAllocationCreateInfo vbo_ai = {}; |
| 396 | vbo_ai.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; |
| 397 | vbo_ai.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 398 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 399 | result = vmaCreateBuffer(device_gpuav->vmaAllocator, &vbo_ci, &vbo_ai, &vbo, &vbo_allocation, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 400 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 401 | ReportSetupProblem(device, "Failed to create vertex buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | if (result == VK_SUCCESS) { |
| 406 | uint8_t *mapped_vbo_buffer = nullptr; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 407 | result = vmaMapMemory(device_gpuav->vmaAllocator, vbo_allocation, reinterpret_cast<void **>(&mapped_vbo_buffer)); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 408 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 409 | ReportSetupProblem(device, "Failed to map vertex buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 410 | } else { |
| 411 | const std::vector<float> vertices = {1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f}; |
| 412 | std::memcpy(mapped_vbo_buffer, (uint8_t *)vertices.data(), sizeof(float) * vertices.size()); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 413 | vmaUnmapMemory(device_gpuav->vmaAllocator, vbo_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | VkBuffer ibo = VK_NULL_HANDLE; |
| 418 | VmaAllocation ibo_allocation = VK_NULL_HANDLE; |
| 419 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 420 | auto ibo_ci = LvlInitStruct<VkBufferCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 421 | ibo_ci.size = sizeof(uint32_t) * 3; |
| 422 | ibo_ci.usage = VK_BUFFER_USAGE_RAY_TRACING_BIT_NV; |
| 423 | |
| 424 | VmaAllocationCreateInfo ibo_ai = {}; |
| 425 | ibo_ai.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; |
| 426 | ibo_ai.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 427 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 428 | result = vmaCreateBuffer(device_gpuav->vmaAllocator, &ibo_ci, &ibo_ai, &ibo, &ibo_allocation, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 429 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 430 | ReportSetupProblem(device, "Failed to create index buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | if (result == VK_SUCCESS) { |
| 435 | uint8_t *mapped_ibo_buffer = nullptr; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 436 | result = vmaMapMemory(device_gpuav->vmaAllocator, ibo_allocation, reinterpret_cast<void **>(&mapped_ibo_buffer)); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 437 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 438 | ReportSetupProblem(device, "Failed to map index buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 439 | } else { |
| 440 | const std::vector<uint32_t> indicies = {0, 1, 2}; |
| 441 | std::memcpy(mapped_ibo_buffer, (uint8_t *)indicies.data(), sizeof(uint32_t) * indicies.size()); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 442 | vmaUnmapMemory(device_gpuav->vmaAllocator, ibo_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 446 | auto geometry = LvlInitStruct<VkGeometryNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 447 | geometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_NV; |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 448 | geometry.geometry.triangles = LvlInitStruct<VkGeometryTrianglesNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 449 | geometry.geometry.triangles.vertexData = vbo; |
| 450 | geometry.geometry.triangles.vertexOffset = 0; |
| 451 | geometry.geometry.triangles.vertexCount = 3; |
| 452 | geometry.geometry.triangles.vertexStride = 12; |
| 453 | geometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT; |
| 454 | geometry.geometry.triangles.indexData = ibo; |
| 455 | geometry.geometry.triangles.indexOffset = 0; |
| 456 | geometry.geometry.triangles.indexCount = 3; |
| 457 | geometry.geometry.triangles.indexType = VK_INDEX_TYPE_UINT32; |
| 458 | geometry.geometry.triangles.transformData = VK_NULL_HANDLE; |
| 459 | geometry.geometry.triangles.transformOffset = 0; |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 460 | geometry.geometry.aabbs = LvlInitStruct<VkGeometryAABBNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 461 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 462 | auto as_ci = LvlInitStruct<VkAccelerationStructureCreateInfoNV>(); |
| 463 | as_ci.info = LvlInitStruct<VkAccelerationStructureInfoNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 464 | as_ci.info.instanceCount = 0; |
| 465 | as_ci.info.geometryCount = 1; |
| 466 | as_ci.info.pGeometries = &geometry; |
| 467 | if (result == VK_SUCCESS) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 468 | result = DispatchCreateAccelerationStructureNV(device_gpuav->device, &as_ci, nullptr, &as_validation_state.replacement_as); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 469 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 470 | ReportSetupProblem(device_gpuav->device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 471 | "Failed to create acceleration structure for acceleration structure build validation."); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | VkMemoryRequirements2 as_mem_requirements = {}; |
| 476 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 477 | auto as_mem_requirements_info = LvlInitStruct<VkAccelerationStructureMemoryRequirementsInfoNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 478 | as_mem_requirements_info.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_OBJECT_NV; |
| 479 | as_mem_requirements_info.accelerationStructure = as_validation_state.replacement_as; |
| 480 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 481 | DispatchGetAccelerationStructureMemoryRequirementsNV(device_gpuav->device, &as_mem_requirements_info, &as_mem_requirements); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | VmaAllocationInfo as_memory_ai = {}; |
| 485 | if (result == VK_SUCCESS) { |
| 486 | VmaAllocationCreateInfo as_memory_aci = {}; |
| 487 | as_memory_aci.usage = VMA_MEMORY_USAGE_GPU_ONLY; |
| 488 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 489 | result = vmaAllocateMemory(device_gpuav->vmaAllocator, &as_mem_requirements.memoryRequirements, &as_memory_aci, |
| 490 | &as_validation_state.replacement_as_allocation, &as_memory_ai); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 491 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 492 | ReportSetupProblem(device_gpuav->device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 493 | "Failed to alloc acceleration structure memory for acceleration structure build validation."); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 498 | auto as_bind_info = LvlInitStruct<VkBindAccelerationStructureMemoryInfoNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 499 | as_bind_info.accelerationStructure = as_validation_state.replacement_as; |
| 500 | as_bind_info.memory = as_memory_ai.deviceMemory; |
| 501 | as_bind_info.memoryOffset = as_memory_ai.offset; |
| 502 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 503 | result = DispatchBindAccelerationStructureMemoryNV(device_gpuav->device, 1, &as_bind_info); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 504 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 505 | ReportSetupProblem(device_gpuav->device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 506 | "Failed to bind acceleration structure memory for acceleration structure build validation."); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if (result == VK_SUCCESS) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 511 | result = DispatchGetAccelerationStructureHandleNV(device_gpuav->device, as_validation_state.replacement_as, |
| 512 | sizeof(uint64_t), &as_validation_state.replacement_as_handle); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 513 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 514 | ReportSetupProblem(device_gpuav->device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 515 | "Failed to get acceleration structure handle for acceleration structure build validation."); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | VkMemoryRequirements2 scratch_mem_requirements = {}; |
| 520 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 521 | auto scratch_mem_requirements_info = LvlInitStruct<VkAccelerationStructureMemoryRequirementsInfoNV>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 522 | scratch_mem_requirements_info.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_BUILD_SCRATCH_NV; |
| 523 | scratch_mem_requirements_info.accelerationStructure = as_validation_state.replacement_as; |
| 524 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 525 | DispatchGetAccelerationStructureMemoryRequirementsNV(device_gpuav->device, &scratch_mem_requirements_info, |
| 526 | &scratch_mem_requirements); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | VkBuffer scratch = VK_NULL_HANDLE; |
Tony-LunarG | 1890028 | 2020-05-20 12:34:33 -0600 | [diff] [blame] | 530 | VmaAllocation scratch_allocation = {}; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 531 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 532 | auto scratch_ci = LvlInitStruct<VkBufferCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 533 | scratch_ci.size = scratch_mem_requirements.memoryRequirements.size; |
| 534 | scratch_ci.usage = VK_BUFFER_USAGE_RAY_TRACING_BIT_NV; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 535 | VmaAllocationCreateInfo scratch_aci = {}; |
| 536 | scratch_aci.usage = VMA_MEMORY_USAGE_GPU_ONLY; |
| 537 | |
Tony-LunarG | 1890028 | 2020-05-20 12:34:33 -0600 | [diff] [blame] | 538 | result = vmaCreateBuffer(device_gpuav->vmaAllocator, &scratch_ci, &scratch_aci, &scratch, &scratch_allocation, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 539 | if (result != VK_SUCCESS) { |
Tony-LunarG | 1890028 | 2020-05-20 12:34:33 -0600 | [diff] [blame] | 540 | ReportSetupProblem(device_gpuav->device, |
| 541 | "Failed to create scratch buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | VkCommandPool command_pool = VK_NULL_HANDLE; |
| 546 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 547 | auto command_pool_ci = LvlInitStruct<VkCommandPoolCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 548 | command_pool_ci.queueFamilyIndex = 0; |
| 549 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 550 | result = DispatchCreateCommandPool(device_gpuav->device, &command_pool_ci, nullptr, &command_pool); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 551 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 552 | ReportSetupProblem(device_gpuav->device, "Failed to create command pool for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | VkCommandBuffer command_buffer = VK_NULL_HANDLE; |
| 557 | |
| 558 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 559 | auto command_buffer_ai = LvlInitStruct<VkCommandBufferAllocateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 560 | command_buffer_ai.commandPool = command_pool; |
| 561 | command_buffer_ai.commandBufferCount = 1; |
| 562 | command_buffer_ai.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 563 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 564 | result = DispatchAllocateCommandBuffers(device_gpuav->device, &command_buffer_ai, &command_buffer); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 565 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 566 | ReportSetupProblem(device_gpuav->device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 567 | "Failed to create command buffer for acceleration structure build validation."); |
| 568 | } |
| 569 | |
| 570 | // Hook up command buffer dispatch |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 571 | device_gpuav->vkSetDeviceLoaderData(device_gpuav->device, command_buffer); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 575 | auto command_buffer_bi = LvlInitStruct<VkCommandBufferBeginInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 576 | |
| 577 | result = DispatchBeginCommandBuffer(command_buffer, &command_buffer_bi); |
| 578 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 579 | ReportSetupProblem(device_gpuav->device, "Failed to begin command buffer for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
| 583 | if (result == VK_SUCCESS) { |
| 584 | DispatchCmdBuildAccelerationStructureNV(command_buffer, &as_ci.info, VK_NULL_HANDLE, 0, VK_FALSE, |
| 585 | as_validation_state.replacement_as, VK_NULL_HANDLE, scratch, 0); |
| 586 | DispatchEndCommandBuffer(command_buffer); |
| 587 | } |
| 588 | |
| 589 | VkQueue queue = VK_NULL_HANDLE; |
| 590 | if (result == VK_SUCCESS) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 591 | DispatchGetDeviceQueue(device_gpuav->device, 0, 0, &queue); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 592 | |
| 593 | // Hook up queue dispatch |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 594 | device_gpuav->vkSetDeviceLoaderData(device_gpuav->device, queue); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 595 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 596 | auto submit_info = LvlInitStruct<VkSubmitInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 597 | submit_info.commandBufferCount = 1; |
| 598 | submit_info.pCommandBuffers = &command_buffer; |
| 599 | result = DispatchQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE); |
| 600 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 601 | ReportSetupProblem(device_gpuav->device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 602 | "Failed to submit command buffer for acceleration structure build validation."); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (result == VK_SUCCESS) { |
| 607 | result = DispatchQueueWaitIdle(queue); |
| 608 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 609 | ReportSetupProblem(device_gpuav->device, "Failed to wait for queue idle for acceleration structure build validation."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
| 613 | if (vbo != VK_NULL_HANDLE) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 614 | vmaDestroyBuffer(device_gpuav->vmaAllocator, vbo, vbo_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 615 | } |
| 616 | if (ibo != VK_NULL_HANDLE) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 617 | vmaDestroyBuffer(device_gpuav->vmaAllocator, ibo, ibo_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 618 | } |
| 619 | if (scratch != VK_NULL_HANDLE) { |
Tony-LunarG | 1890028 | 2020-05-20 12:34:33 -0600 | [diff] [blame] | 620 | vmaDestroyBuffer(device_gpuav->vmaAllocator, scratch, scratch_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 621 | } |
| 622 | if (command_pool != VK_NULL_HANDLE) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 623 | DispatchDestroyCommandPool(device_gpuav->device, command_pool, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 626 | if (device_gpuav->debug_desc_layout == VK_NULL_HANDLE) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 627 | ReportSetupProblem(device_gpuav->device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 628 | "Failed to find descriptor set layout for acceleration structure build validation."); |
| 629 | result = VK_INCOMPLETE; |
| 630 | } |
| 631 | |
| 632 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 633 | auto pipeline_layout_ci = LvlInitStruct<VkPipelineLayoutCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 634 | pipeline_layout_ci.setLayoutCount = 1; |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 635 | pipeline_layout_ci.pSetLayouts = &device_gpuav->debug_desc_layout; |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 636 | result = DispatchCreatePipelineLayout(device_gpuav->device, &pipeline_layout_ci, 0, &as_validation_state.pipeline_layout); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 637 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 638 | ReportSetupProblem(device_gpuav->device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 639 | "Failed to create pipeline layout for acceleration structure build validation."); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | VkShaderModule shader_module = VK_NULL_HANDLE; |
| 644 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 645 | auto shader_module_ci = LvlInitStruct<VkShaderModuleCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 646 | shader_module_ci.codeSize = sizeof(kComputeShaderSpirv); |
| 647 | shader_module_ci.pCode = (uint32_t *)kComputeShaderSpirv; |
| 648 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 649 | result = DispatchCreateShaderModule(device_gpuav->device, &shader_module_ci, nullptr, &shader_module); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 650 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 651 | ReportSetupProblem(device_gpuav->device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 652 | "Failed to create compute shader module for acceleration structure build validation."); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | if (result == VK_SUCCESS) { |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 657 | auto pipeline_stage_ci = LvlInitStruct<VkPipelineShaderStageCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 658 | pipeline_stage_ci.stage = VK_SHADER_STAGE_COMPUTE_BIT; |
| 659 | pipeline_stage_ci.module = shader_module; |
| 660 | pipeline_stage_ci.pName = "main"; |
| 661 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 662 | auto pipeline_ci = LvlInitStruct<VkComputePipelineCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 663 | pipeline_ci.stage = pipeline_stage_ci; |
| 664 | pipeline_ci.layout = as_validation_state.pipeline_layout; |
| 665 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 666 | result = DispatchCreateComputePipelines(device_gpuav->device, VK_NULL_HANDLE, 1, &pipeline_ci, nullptr, |
| 667 | &as_validation_state.pipeline); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 668 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 669 | ReportSetupProblem(device_gpuav->device, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 670 | "Failed to create compute pipeline for acceleration structure build validation."); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (shader_module != VK_NULL_HANDLE) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 675 | DispatchDestroyShaderModule(device_gpuav->device, shader_module, nullptr); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if (result == VK_SUCCESS) { |
| 679 | as_validation_state.initialized = true; |
Mark Lobodzinski | afa7cb8 | 2020-01-29 16:49:36 -0700 | [diff] [blame] | 680 | LogInfo(device_gpuav->device, "UNASSIGNED-GPU-Assisted Validation.", |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 681 | "Acceleration Structure Building GPU Validation Enabled."); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 682 | } else { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 683 | device_gpuav->aborted = true; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 687 | void GpuAssisted::DestroyAccelerationStructureBuildValidationState() { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 688 | auto &as_validation_state = acceleration_structure_validation_state; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 689 | if (as_validation_state.pipeline != VK_NULL_HANDLE) { |
| 690 | DispatchDestroyPipeline(device, as_validation_state.pipeline, nullptr); |
| 691 | } |
| 692 | if (as_validation_state.pipeline_layout != VK_NULL_HANDLE) { |
| 693 | DispatchDestroyPipelineLayout(device, as_validation_state.pipeline_layout, nullptr); |
| 694 | } |
| 695 | if (as_validation_state.replacement_as != VK_NULL_HANDLE) { |
| 696 | DispatchDestroyAccelerationStructureNV(device, as_validation_state.replacement_as, nullptr); |
| 697 | } |
| 698 | if (as_validation_state.replacement_as_allocation != VK_NULL_HANDLE) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 699 | vmaFreeMemory(vmaAllocator, as_validation_state.replacement_as_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 703 | struct GPUAV_RESTORABLE_PIPELINE_STATE { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 704 | VkPipelineBindPoint pipeline_bind_point = VK_PIPELINE_BIND_POINT_MAX_ENUM; |
| 705 | VkPipeline pipeline = VK_NULL_HANDLE; |
| 706 | VkPipelineLayout pipeline_layout = VK_NULL_HANDLE; |
| 707 | std::vector<VkDescriptorSet> descriptor_sets; |
| 708 | std::vector<std::vector<uint32_t>> dynamic_offsets; |
| 709 | uint32_t push_descriptor_set_index = 0; |
| 710 | std::vector<safe_VkWriteDescriptorSet> push_descriptor_set_writes; |
| 711 | std::vector<uint8_t> push_constants_data; |
| 712 | PushConstantRangesId push_constants_ranges; |
| 713 | |
| 714 | void Create(CMD_BUFFER_STATE *cb_state, VkPipelineBindPoint bind_point) { |
| 715 | pipeline_bind_point = bind_point; |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 716 | const auto lv_bind_point = ConvertToLvlBindPoint(bind_point); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 717 | |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 718 | LAST_BOUND_STATE &last_bound = cb_state->lastBound[lv_bind_point]; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 719 | if (last_bound.pipeline_state) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 720 | pipeline = last_bound.pipeline_state->pipeline(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 721 | pipeline_layout = last_bound.pipeline_layout; |
| 722 | descriptor_sets.reserve(last_bound.per_set.size()); |
| 723 | for (std::size_t i = 0; i < last_bound.per_set.size(); i++) { |
| 724 | const auto *bound_descriptor_set = last_bound.per_set[i].bound_descriptor_set; |
| 725 | |
| 726 | descriptor_sets.push_back(bound_descriptor_set->GetSet()); |
| 727 | if (bound_descriptor_set->IsPushDescriptor()) { |
| 728 | push_descriptor_set_index = static_cast<uint32_t>(i); |
| 729 | } |
| 730 | dynamic_offsets.push_back(last_bound.per_set[i].dynamicOffsets); |
| 731 | } |
| 732 | |
| 733 | if (last_bound.push_descriptor_set) { |
| 734 | push_descriptor_set_writes = last_bound.push_descriptor_set->GetWrites(); |
| 735 | } |
Jeff Bolz | e7fc67b | 2019-10-04 12:29:31 -0500 | [diff] [blame] | 736 | if (last_bound.pipeline_state->pipeline_layout->push_constant_ranges == cb_state->push_constant_data_ranges) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 737 | push_constants_data = cb_state->push_constant_data; |
Jeff Bolz | e7fc67b | 2019-10-04 12:29:31 -0500 | [diff] [blame] | 738 | push_constants_ranges = last_bound.pipeline_state->pipeline_layout->push_constant_ranges; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | void Restore(VkCommandBuffer command_buffer) const { |
| 744 | if (pipeline != VK_NULL_HANDLE) { |
| 745 | DispatchCmdBindPipeline(command_buffer, pipeline_bind_point, pipeline); |
| 746 | if (!descriptor_sets.empty()) { |
| 747 | for (std::size_t i = 0; i < descriptor_sets.size(); i++) { |
| 748 | VkDescriptorSet descriptor_set = descriptor_sets[i]; |
| 749 | if (descriptor_set != VK_NULL_HANDLE) { |
| 750 | DispatchCmdBindDescriptorSets(command_buffer, pipeline_bind_point, pipeline_layout, |
| 751 | static_cast<uint32_t>(i), 1, &descriptor_set, |
| 752 | static_cast<uint32_t>(dynamic_offsets[i].size()), dynamic_offsets[i].data()); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | if (!push_descriptor_set_writes.empty()) { |
| 757 | DispatchCmdPushDescriptorSetKHR(command_buffer, pipeline_bind_point, pipeline_layout, push_descriptor_set_index, |
| 758 | static_cast<uint32_t>(push_descriptor_set_writes.size()), |
| 759 | reinterpret_cast<const VkWriteDescriptorSet *>(push_descriptor_set_writes.data())); |
| 760 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 761 | if (!push_constants_data.empty()) { |
| 762 | for (const auto &push_constant_range : *push_constants_ranges) { |
| 763 | if (push_constant_range.size == 0) continue; |
| 764 | DispatchCmdPushConstants(command_buffer, pipeline_layout, push_constant_range.stageFlags, |
| 765 | push_constant_range.offset, push_constant_range.size, push_constants_data.data()); |
| 766 | } |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | } |
| 770 | }; |
| 771 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 772 | void GpuAssisted::PreCallRecordCmdBuildAccelerationStructureNV(VkCommandBuffer commandBuffer, |
| 773 | const VkAccelerationStructureInfoNV *pInfo, VkBuffer instanceData, |
| 774 | VkDeviceSize instanceOffset, VkBool32 update, |
| 775 | VkAccelerationStructureNV dst, VkAccelerationStructureNV src, |
| 776 | VkBuffer scratch, VkDeviceSize scratchOffset) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 777 | if (pInfo == nullptr || pInfo->type != VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_NV) { |
| 778 | return; |
| 779 | } |
| 780 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 781 | auto &as_validation_state = acceleration_structure_validation_state; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 782 | if (!as_validation_state.initialized) { |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | // Empty acceleration structure is valid according to the spec. |
| 787 | if (pInfo->instanceCount == 0 || instanceData == VK_NULL_HANDLE) { |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer); |
| 792 | assert(cb_state != nullptr); |
| 793 | |
| 794 | std::vector<uint64_t> current_valid_handles; |
| 795 | for (const auto &as_state_kv : accelerationStructureMap) { |
| 796 | const ACCELERATION_STRUCTURE_STATE &as_state = *as_state_kv.second; |
Jeff Bolz | 95176d0 | 2020-04-01 00:36:16 -0500 | [diff] [blame] | 797 | 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] | 798 | current_valid_handles.push_back(as_state.opaque_handle); |
| 799 | } |
| 800 | } |
| 801 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 802 | GpuAssistedAccelerationStructureBuildValidationBufferInfo as_validation_buffer_info = {}; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 803 | as_validation_buffer_info.acceleration_structure = dst; |
| 804 | |
| 805 | const VkDeviceSize validation_buffer_size = |
| 806 | // One uint for number of instances to validate |
| 807 | 4 + |
| 808 | // Two uint for the replacement acceleration structure handle |
| 809 | 8 + |
| 810 | // One uint for number of invalid handles found |
| 811 | 4 + |
| 812 | // Two uint for the first invalid handle found |
| 813 | 8 + |
| 814 | // One uint for the number of current valid handles |
| 815 | 4 + |
| 816 | // Two uint for each current valid handle |
| 817 | (8 * current_valid_handles.size()); |
| 818 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 819 | auto validation_buffer_create_info = LvlInitStruct<VkBufferCreateInfo>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 820 | validation_buffer_create_info.size = validation_buffer_size; |
| 821 | validation_buffer_create_info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 822 | |
| 823 | VmaAllocationCreateInfo validation_buffer_alloc_info = {}; |
| 824 | validation_buffer_alloc_info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; |
| 825 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 826 | VkResult result = vmaCreateBuffer(vmaAllocator, &validation_buffer_create_info, &validation_buffer_alloc_info, |
| 827 | &as_validation_buffer_info.validation_buffer, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 828 | &as_validation_buffer_info.validation_buffer_allocation, nullptr); |
| 829 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 830 | ReportSetupProblem(device, "Unable to allocate device memory. Device could become unstable."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 831 | aborted = true; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | |
| 835 | GpuAccelerationStructureBuildValidationBuffer *mapped_validation_buffer = nullptr; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 836 | result = vmaMapMemory(vmaAllocator, as_validation_buffer_info.validation_buffer_allocation, |
| 837 | reinterpret_cast<void **>(&mapped_validation_buffer)); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 838 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 839 | 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] | 840 | aborted = true; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 841 | return; |
| 842 | } |
| 843 | |
| 844 | mapped_validation_buffer->instances_to_validate = pInfo->instanceCount; |
| 845 | mapped_validation_buffer->replacement_handle_bits_0 = |
| 846 | reinterpret_cast<const uint32_t *>(&as_validation_state.replacement_as_handle)[0]; |
| 847 | mapped_validation_buffer->replacement_handle_bits_1 = |
| 848 | reinterpret_cast<const uint32_t *>(&as_validation_state.replacement_as_handle)[1]; |
| 849 | mapped_validation_buffer->invalid_handle_found = 0; |
| 850 | mapped_validation_buffer->invalid_handle_bits_0 = 0; |
| 851 | mapped_validation_buffer->invalid_handle_bits_1 = 0; |
| 852 | mapped_validation_buffer->valid_handles_count = static_cast<uint32_t>(current_valid_handles.size()); |
| 853 | |
| 854 | uint32_t *mapped_valid_handles = reinterpret_cast<uint32_t *>(&mapped_validation_buffer[1]); |
| 855 | for (std::size_t i = 0; i < current_valid_handles.size(); i++) { |
| 856 | const uint64_t current_valid_handle = current_valid_handles[i]; |
| 857 | |
| 858 | *mapped_valid_handles = reinterpret_cast<const uint32_t *>(¤t_valid_handle)[0]; |
| 859 | ++mapped_valid_handles; |
| 860 | *mapped_valid_handles = reinterpret_cast<const uint32_t *>(¤t_valid_handle)[1]; |
| 861 | ++mapped_valid_handles; |
| 862 | } |
| 863 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 864 | vmaUnmapMemory(vmaAllocator, as_validation_buffer_info.validation_buffer_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 865 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 866 | static constexpr const VkDeviceSize k_instance_size = 64; |
| 867 | const VkDeviceSize instance_buffer_size = k_instance_size * pInfo->instanceCount; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 868 | |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 869 | result = desc_set_manager->GetDescriptorSet(&as_validation_buffer_info.descriptor_pool, debug_desc_layout, |
| 870 | &as_validation_buffer_info.descriptor_set); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 871 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 872 | ReportSetupProblem(device, "Unable to get descriptor set for acceleration structure build."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 873 | aborted = true; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 874 | return; |
| 875 | } |
| 876 | |
| 877 | VkDescriptorBufferInfo descriptor_buffer_infos[2] = {}; |
| 878 | descriptor_buffer_infos[0].buffer = instanceData; |
| 879 | descriptor_buffer_infos[0].offset = instanceOffset; |
| 880 | descriptor_buffer_infos[0].range = instance_buffer_size; |
| 881 | descriptor_buffer_infos[1].buffer = as_validation_buffer_info.validation_buffer; |
| 882 | descriptor_buffer_infos[1].offset = 0; |
| 883 | descriptor_buffer_infos[1].range = validation_buffer_size; |
| 884 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 885 | VkWriteDescriptorSet descriptor_set_writes[2] = { |
| 886 | LvlInitStruct<VkWriteDescriptorSet>(), |
| 887 | LvlInitStruct<VkWriteDescriptorSet>(), |
| 888 | }; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 889 | descriptor_set_writes[0].dstSet = as_validation_buffer_info.descriptor_set; |
| 890 | descriptor_set_writes[0].dstBinding = 0; |
| 891 | descriptor_set_writes[0].descriptorCount = 1; |
| 892 | descriptor_set_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 893 | descriptor_set_writes[0].pBufferInfo = &descriptor_buffer_infos[0]; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 894 | descriptor_set_writes[1].dstSet = as_validation_buffer_info.descriptor_set; |
| 895 | descriptor_set_writes[1].dstBinding = 1; |
| 896 | descriptor_set_writes[1].descriptorCount = 1; |
| 897 | descriptor_set_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 898 | descriptor_set_writes[1].pBufferInfo = &descriptor_buffer_infos[1]; |
| 899 | |
| 900 | DispatchUpdateDescriptorSets(device, 2, descriptor_set_writes, 0, nullptr); |
| 901 | |
| 902 | // 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] | 903 | auto memory_barrier = LvlInitStruct<VkMemoryBarrier>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 904 | memory_barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT; |
| 905 | memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 906 | DispatchCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1, |
| 907 | &memory_barrier, 0, nullptr, 0, nullptr); |
| 908 | |
| 909 | // 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] | 910 | GPUAV_RESTORABLE_PIPELINE_STATE restorable_state; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 911 | restorable_state.Create(cb_state, VK_PIPELINE_BIND_POINT_COMPUTE); |
| 912 | |
| 913 | // Switch to and launch the validation compute shader to find, replace, and report invalid acceleration structure handles. |
| 914 | DispatchCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, as_validation_state.pipeline); |
| 915 | DispatchCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, as_validation_state.pipeline_layout, 0, 1, |
| 916 | &as_validation_buffer_info.descriptor_set, 0, nullptr); |
| 917 | DispatchCmdDispatch(commandBuffer, 1, 1, 1); |
| 918 | |
| 919 | // Issue a buffer memory barrier to make sure that any invalid bottom level acceleration structure handles |
| 920 | // 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] | 921 | auto instance_buffer_barrier = LvlInitStruct<VkBufferMemoryBarrier>(); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 922 | instance_buffer_barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; |
| 923 | instance_buffer_barrier.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_NV; |
| 924 | instance_buffer_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 925 | instance_buffer_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 926 | instance_buffer_barrier.buffer = instanceData; |
| 927 | instance_buffer_barrier.offset = instanceOffset; |
| 928 | instance_buffer_barrier.size = instance_buffer_size; |
| 929 | DispatchCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, |
| 930 | VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_NV, 0, 0, nullptr, 1, &instance_buffer_barrier, 0, |
| 931 | nullptr); |
| 932 | |
| 933 | // Restore the previous compute pipeline state. |
| 934 | restorable_state.Restore(commandBuffer); |
| 935 | |
| 936 | as_validation_state.validation_buffers[commandBuffer].push_back(std::move(as_validation_buffer_info)); |
| 937 | } |
| 938 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 939 | void GpuAssisted::ProcessAccelerationStructureBuildValidationBuffer(VkQueue queue, CMD_BUFFER_STATE *cb_node) { |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 940 | if (cb_node == nullptr || !cb_node->hasBuildAccelerationStructureCmd) { |
| 941 | return; |
| 942 | } |
| 943 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 944 | auto &as_validation_info = acceleration_structure_validation_state; |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 945 | auto &as_validation_buffer_infos = as_validation_info.validation_buffers[cb_node->commandBuffer()]; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 946 | for (const auto &as_validation_buffer_info : as_validation_buffer_infos) { |
| 947 | GpuAccelerationStructureBuildValidationBuffer *mapped_validation_buffer = nullptr; |
| 948 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 949 | VkResult result = vmaMapMemory(vmaAllocator, as_validation_buffer_info.validation_buffer_allocation, |
| 950 | reinterpret_cast<void **>(&mapped_validation_buffer)); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 951 | if (result == VK_SUCCESS) { |
| 952 | if (mapped_validation_buffer->invalid_handle_found > 0) { |
| 953 | uint64_t invalid_handle = 0; |
| 954 | reinterpret_cast<uint32_t *>(&invalid_handle)[0] = mapped_validation_buffer->invalid_handle_bits_0; |
| 955 | reinterpret_cast<uint32_t *>(&invalid_handle)[1] = mapped_validation_buffer->invalid_handle_bits_1; |
| 956 | |
Mark Lobodzinski | afa7cb8 | 2020-01-29 16:49:36 -0700 | [diff] [blame] | 957 | LogError(as_validation_buffer_info.acceleration_structure, "UNASSIGNED-AccelerationStructure", |
| 958 | "Attempted to build top level acceleration structure using invalid bottom level acceleration structure " |
| 959 | "handle (%" PRIu64 ")", |
| 960 | invalid_handle); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 961 | } |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 962 | vmaUnmapMemory(vmaAllocator, as_validation_buffer_info.validation_buffer_allocation); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 967 | void GpuAssisted::PostCallRecordBindAccelerationStructureMemoryNV(VkDevice device, uint32_t bindInfoCount, |
| 968 | const VkBindAccelerationStructureMemoryInfoNV *pBindInfos, |
| 969 | VkResult result) { |
| 970 | if (VK_SUCCESS != result) return; |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 971 | ValidationStateTracker::PostCallRecordBindAccelerationStructureMemoryNV(device, bindInfoCount, pBindInfos, result); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 972 | for (uint32_t i = 0; i < bindInfoCount; i++) { |
| 973 | const VkBindAccelerationStructureMemoryInfoNV &info = pBindInfos[i]; |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 974 | ACCELERATION_STRUCTURE_STATE *as_state = GetAccelerationStructureStateNV(info.accelerationStructure); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 975 | if (as_state) { |
| 976 | DispatchGetAccelerationStructureHandleNV(device, info.accelerationStructure, 8, &as_state->opaque_handle); |
| 977 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 978 | } |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 979 | } |
Mark Lobodzinski | ff7d800 | 2019-02-13 13:01:26 -0700 | [diff] [blame] | 980 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 981 | // Modify the pipeline layout to include our debug descriptor set and any needed padding with the dummy descriptor set. |
| 982 | void GpuAssisted::PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo, |
| 983 | const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout, |
| 984 | void *cpl_state_data) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 985 | if (aborted) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 986 | return; |
| 987 | } |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 988 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 989 | create_pipeline_layout_api_state *cpl_state = reinterpret_cast<create_pipeline_layout_api_state *>(cpl_state_data); |
| 990 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 991 | if (cpl_state->modified_create_info.setLayoutCount >= adjusted_max_desc_sets) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 992 | std::ostringstream strm; |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 993 | strm << "Pipeline Layout conflict with validation's descriptor set at slot " << desc_set_bind_index << ". " |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 994 | << "Application has too many descriptor sets in the pipeline layout to continue with gpu validation. " |
| 995 | << "Validation is not modifying the pipeline layout. " |
| 996 | << "Instrumented shaders are replaced with non-instrumented shaders."; |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 997 | ReportSetupProblem(device, strm.str().c_str()); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 998 | } else { |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 999 | UtilPreCallRecordCreatePipelineLayout(cpl_state, this, pCreateInfo); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1000 | } |
Mark Lobodzinski | ff7d800 | 2019-02-13 13:01:26 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1003 | void GpuAssisted::PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo, |
| 1004 | const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout, |
| 1005 | VkResult result) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1006 | ValidationStateTracker::PostCallRecordCreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout, result); |
| 1007 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1008 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 1009 | ReportSetupProblem(device, "Unable to create pipeline layout. Device could become unstable."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1010 | aborted = true; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1011 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1012 | } |
| 1013 | |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1014 | // Free the device memory and descriptor set(s) associated with a command buffer. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1015 | void GpuAssisted::ResetCommandBuffer(VkCommandBuffer commandBuffer) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1016 | if (aborted) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1017 | return; |
| 1018 | } |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1019 | auto gpuav_buffer_list = GetBufferInfo(commandBuffer); |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1020 | for (const auto &buffer_info : gpuav_buffer_list) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1021 | vmaDestroyBuffer(vmaAllocator, buffer_info.output_mem_block.buffer, buffer_info.output_mem_block.allocation); |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1022 | if (buffer_info.di_input_mem_block.buffer) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1023 | vmaDestroyBuffer(vmaAllocator, buffer_info.di_input_mem_block.buffer, buffer_info.di_input_mem_block.allocation); |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1024 | } |
| 1025 | if (buffer_info.bda_input_mem_block.buffer) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1026 | 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] | 1027 | } |
Tony-LunarG | dcbc2c3 | 2019-05-06 10:17:44 -0600 | [diff] [blame] | 1028 | if (buffer_info.desc_set != VK_NULL_HANDLE) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1029 | desc_set_manager->PutBackDescriptorSet(buffer_info.desc_pool, buffer_info.desc_set); |
Tony-LunarG | dcbc2c3 | 2019-05-06 10:17:44 -0600 | [diff] [blame] | 1030 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1031 | if (buffer_info.pre_draw_resources.desc_set != VK_NULL_HANDLE) { |
| 1032 | desc_set_manager->PutBackDescriptorSet(buffer_info.pre_draw_resources.desc_pool, |
| 1033 | buffer_info.pre_draw_resources.desc_set); |
| 1034 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1035 | } |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1036 | command_buffer_map.erase(commandBuffer); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 1037 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1038 | auto &as_validation_info = acceleration_structure_validation_state; |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 1039 | auto &as_validation_buffer_infos = as_validation_info.validation_buffers[commandBuffer]; |
| 1040 | for (auto &as_validation_buffer_info : as_validation_buffer_infos) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1041 | vmaDestroyBuffer(vmaAllocator, as_validation_buffer_info.validation_buffer, |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 1042 | as_validation_buffer_info.validation_buffer_allocation); |
| 1043 | |
| 1044 | if (as_validation_buffer_info.descriptor_set != VK_NULL_HANDLE) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1045 | desc_set_manager->PutBackDescriptorSet(as_validation_buffer_info.descriptor_pool, |
| 1046 | as_validation_buffer_info.descriptor_set); |
Jason Macnak | 83cfd58 | 2019-07-31 10:14:24 -0700 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | as_validation_info.validation_buffers.erase(commandBuffer); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1050 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1051 | // Just gives a warning about a possible deadlock. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1052 | bool GpuAssisted::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents, |
| 1053 | VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, |
| 1054 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, |
| 1055 | uint32_t bufferMemoryBarrierCount, |
| 1056 | const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 1057 | const VkImageMemoryBarrier *pImageMemoryBarriers) const { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1058 | if (srcStageMask & VK_PIPELINE_STAGE_HOST_BIT) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 1059 | ReportSetupProblem(commandBuffer, |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1060 | "CmdWaitEvents recorded with VK_PIPELINE_STAGE_HOST_BIT set. " |
| 1061 | "GPU_Assisted validation waits on queue completion. " |
| 1062 | "This wait could block the host's signaling of this event, resulting in deadlock."); |
| 1063 | } |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1064 | return false; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1065 | } |
| 1066 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1067 | bool GpuAssisted::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents, |
| 1068 | const VkDependencyInfoKHR *pDependencyInfos) const { |
| 1069 | VkPipelineStageFlags2KHR srcStageMask = 0; |
| 1070 | |
| 1071 | for (uint32_t i = 0; i < eventCount; i++) { |
| 1072 | auto stage_masks = sync_utils::GetGlobalStageMasks(pDependencyInfos[i]); |
| 1073 | srcStageMask = stage_masks.src; |
| 1074 | } |
| 1075 | |
| 1076 | if (srcStageMask & VK_PIPELINE_STAGE_HOST_BIT) { |
| 1077 | ReportSetupProblem(commandBuffer, |
| 1078 | "CmdWaitEvents2KHR recorded with VK_PIPELINE_STAGE_HOST_BIT set. " |
| 1079 | "GPU_Assisted validation waits on queue completion. " |
| 1080 | "This wait could block the host's signaling of this event, resulting in deadlock."); |
| 1081 | } |
| 1082 | return false; |
| 1083 | } |
| 1084 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1085 | void GpuAssisted::PostCallRecordGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice, |
| 1086 | VkPhysicalDeviceProperties *pPhysicalDeviceProperties) { |
| 1087 | // 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] | 1088 | if (enabled[gpu_validation_reserve_binding_slot] && pPhysicalDeviceProperties->limits.maxBoundDescriptorSets > 0) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1089 | if (pPhysicalDeviceProperties->limits.maxBoundDescriptorSets > 1) { |
| 1090 | pPhysicalDeviceProperties->limits.maxBoundDescriptorSets -= 1; |
| 1091 | } else { |
Mark Lobodzinski | afa7cb8 | 2020-01-29 16:49:36 -0700 | [diff] [blame] | 1092 | LogWarning(physicalDevice, "UNASSIGNED-GPU-Assisted Validation Setup Error.", |
| 1093 | "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] | 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | void GpuAssisted::PostCallRecordGetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice, |
| 1099 | VkPhysicalDeviceProperties2 *pPhysicalDeviceProperties2) { |
| 1100 | // 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] | 1101 | if (enabled[gpu_validation_reserve_binding_slot] && pPhysicalDeviceProperties2->properties.limits.maxBoundDescriptorSets > 0) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1102 | if (pPhysicalDeviceProperties2->properties.limits.maxBoundDescriptorSets > 1) { |
| 1103 | pPhysicalDeviceProperties2->properties.limits.maxBoundDescriptorSets -= 1; |
| 1104 | } else { |
Mark Lobodzinski | afa7cb8 | 2020-01-29 16:49:36 -0700 | [diff] [blame] | 1105 | LogWarning(physicalDevice, "UNASSIGNED-GPU-Assisted Validation Setup Error.", |
| 1106 | "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] | 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | void GpuAssisted::PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 1112 | const VkGraphicsPipelineCreateInfo *pCreateInfos, |
| 1113 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 1114 | void *cgpl_state_data) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1115 | std::vector<safe_VkGraphicsPipelineCreateInfo> new_pipeline_create_infos; |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1116 | create_graphics_pipeline_api_state *cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state *>(cgpl_state_data); |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 1117 | UtilPreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, cgpl_state->pipe_state, |
| 1118 | &new_pipeline_create_infos, VK_PIPELINE_BIND_POINT_GRAPHICS, this); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1119 | cgpl_state->gpu_create_infos = new_pipeline_create_infos; |
| 1120 | cgpl_state->pCreateInfos = reinterpret_cast<VkGraphicsPipelineCreateInfo *>(cgpl_state->gpu_create_infos.data()); |
Tony-LunarG | eb25bf5 | 2019-04-26 10:46:41 -0600 | [diff] [blame] | 1121 | } |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1122 | |
| 1123 | void GpuAssisted::PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 1124 | const VkComputePipelineCreateInfo *pCreateInfos, |
| 1125 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 1126 | void *ccpl_state_data) { |
Tony-LunarG | eb25bf5 | 2019-04-26 10:46:41 -0600 | [diff] [blame] | 1127 | std::vector<safe_VkComputePipelineCreateInfo> new_pipeline_create_infos; |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1128 | auto *ccpl_state = reinterpret_cast<create_compute_pipeline_api_state *>(ccpl_state_data); |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 1129 | UtilPreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, ccpl_state->pipe_state, |
| 1130 | &new_pipeline_create_infos, VK_PIPELINE_BIND_POINT_COMPUTE, this); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1131 | ccpl_state->gpu_create_infos = new_pipeline_create_infos; |
| 1132 | ccpl_state->pCreateInfos = reinterpret_cast<VkComputePipelineCreateInfo *>(ccpl_state->gpu_create_infos.data()); |
Jason Macnak | 67407e7 | 2019-07-11 11:05:09 -0700 | [diff] [blame] | 1133 | } |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1134 | |
| 1135 | void GpuAssisted::PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 1136 | const VkRayTracingPipelineCreateInfoNV *pCreateInfos, |
| 1137 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 1138 | void *crtpl_state_data) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1139 | std::vector<safe_VkRayTracingPipelineCreateInfoCommon> new_pipeline_create_infos; |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1140 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_api_state *>(crtpl_state_data); |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 1141 | UtilPreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, crtpl_state->pipe_state, |
| 1142 | &new_pipeline_create_infos, VK_PIPELINE_BIND_POINT_RAY_TRACING_NV, this); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1143 | crtpl_state->gpu_create_infos = new_pipeline_create_infos; |
| 1144 | crtpl_state->pCreateInfos = reinterpret_cast<VkRayTracingPipelineCreateInfoNV *>(crtpl_state->gpu_create_infos.data()); |
Tony-LunarG | eb25bf5 | 2019-04-26 10:46:41 -0600 | [diff] [blame] | 1145 | } |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1146 | |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1147 | void GpuAssisted::PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation, |
| 1148 | VkPipelineCache pipelineCache, uint32_t count, |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1149 | const VkRayTracingPipelineCreateInfoKHR *pCreateInfos, |
| 1150 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 1151 | void *crtpl_state_data) { |
| 1152 | std::vector<safe_VkRayTracingPipelineCreateInfoCommon> new_pipeline_create_infos; |
| 1153 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data); |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 1154 | UtilPreCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, crtpl_state->pipe_state, |
| 1155 | &new_pipeline_create_infos, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, this); |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1156 | crtpl_state->gpu_create_infos = new_pipeline_create_infos; |
| 1157 | crtpl_state->pCreateInfos = reinterpret_cast<VkRayTracingPipelineCreateInfoKHR *>(crtpl_state->gpu_create_infos.data()); |
| 1158 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1159 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1160 | void GpuAssisted::PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 1161 | const VkGraphicsPipelineCreateInfo *pCreateInfos, |
| 1162 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 1163 | VkResult result, void *cgpl_state_data) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1164 | ValidationStateTracker::PostCallRecordCreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator, |
| 1165 | pPipelines, result, cgpl_state_data); |
Tony-LunarG | c876c6e | 2020-09-09 15:19:43 -0600 | [diff] [blame] | 1166 | create_graphics_pipeline_api_state *cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state *>(cgpl_state_data); |
| 1167 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, cgpl_state->gpu_create_infos.data()); |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 1168 | UtilPostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_GRAPHICS, this); |
Tony-LunarG | eb25bf5 | 2019-04-26 10:46:41 -0600 | [diff] [blame] | 1169 | } |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1170 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1171 | void GpuAssisted::PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 1172 | const VkComputePipelineCreateInfo *pCreateInfos, |
| 1173 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 1174 | VkResult result, void *ccpl_state_data) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1175 | ValidationStateTracker::PostCallRecordCreateComputePipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines, |
| 1176 | result, ccpl_state_data); |
Tony-LunarG | c876c6e | 2020-09-09 15:19:43 -0600 | [diff] [blame] | 1177 | create_compute_pipeline_api_state *ccpl_state = reinterpret_cast<create_compute_pipeline_api_state *>(ccpl_state_data); |
| 1178 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, ccpl_state->gpu_create_infos.data()); |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 1179 | UtilPostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_COMPUTE, this); |
Tony-LunarG | eb25bf5 | 2019-04-26 10:46:41 -0600 | [diff] [blame] | 1180 | } |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1181 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1182 | void GpuAssisted::PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 1183 | const VkRayTracingPipelineCreateInfoNV *pCreateInfos, |
| 1184 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 1185 | VkResult result, void *crtpl_state_data) { |
Tony-LunarG | c876c6e | 2020-09-09 15:19:43 -0600 | [diff] [blame] | 1186 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1187 | ValidationStateTracker::PostCallRecordCreateRayTracingPipelinesNV(device, pipelineCache, count, pCreateInfos, pAllocator, |
| 1188 | pPipelines, result, crtpl_state_data); |
Tony-LunarG | c876c6e | 2020-09-09 15:19:43 -0600 | [diff] [blame] | 1189 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, crtpl_state->gpu_create_infos.data()); |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 1190 | UtilPostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_RAY_TRACING_NV, this); |
Jason Macnak | 67407e7 | 2019-07-11 11:05:09 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1193 | void GpuAssisted::PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation, |
| 1194 | VkPipelineCache pipelineCache, uint32_t count, |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1195 | const VkRayTracingPipelineCreateInfoKHR *pCreateInfos, |
| 1196 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 1197 | VkResult result, void *crtpl_state_data) { |
Tony-LunarG | c876c6e | 2020-09-09 15:19:43 -0600 | [diff] [blame] | 1198 | auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data); |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1199 | ValidationStateTracker::PostCallRecordCreateRayTracingPipelinesKHR( |
| 1200 | device, deferredOperation, pipelineCache, count, pCreateInfos, pAllocator, pPipelines, result, crtpl_state_data); |
Tony-LunarG | c876c6e | 2020-09-09 15:19:43 -0600 | [diff] [blame] | 1201 | UtilCopyCreatePipelineFeedbackData(count, pCreateInfos, crtpl_state->gpu_create_infos.data()); |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 1202 | UtilPostCallRecordPipelineCreations(count, pCreateInfos, pAllocator, pPipelines, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, this); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | // Remove all the shader trackers associated with this destroyed pipeline. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1206 | void GpuAssisted::PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1207 | for (auto it = shader_map.begin(); it != shader_map.end();) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1208 | if (it->second.pipeline == pipeline) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1209 | it = shader_map.erase(it); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1210 | } else { |
| 1211 | ++it; |
| 1212 | } |
| 1213 | } |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1214 | ValidationStateTracker::PreCallRecordDestroyPipeline(device, pipeline, pAllocator); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1215 | } |
| 1216 | |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1217 | void GpuAssisted::PreCallRecordDestroyRenderPass(VkDevice device, VkRenderPass renderPass, |
| 1218 | const VkAllocationCallbacks *pAllocator) { |
| 1219 | auto pipeline = pre_draw_validation_state.renderpass_to_pipeline.find(renderPass); |
| 1220 | if (pipeline != pre_draw_validation_state.renderpass_to_pipeline.end()) { |
| 1221 | DispatchDestroyPipeline(device, pipeline->second, nullptr); |
| 1222 | pre_draw_validation_state.renderpass_to_pipeline.erase(pipeline); |
| 1223 | } |
| 1224 | } |
| 1225 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1226 | // Call the SPIR-V Optimizer to run the instrumentation pass on the shader. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1227 | bool GpuAssisted::InstrumentShader(const VkShaderModuleCreateInfo *pCreateInfo, std::vector<unsigned int> &new_pgm, |
| 1228 | uint32_t *unique_shader_id) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1229 | if (aborted) return false; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1230 | if (pCreateInfo->pCode[0] != spv::MagicNumber) return false; |
| 1231 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1232 | const spvtools::MessageConsumer gpu_console_message_consumer = |
Tony-LunarG | 7964170 | 2020-07-13 15:43:05 -0600 | [diff] [blame] | 1233 | [this](spv_message_level_t level, const char *, const spv_position_t &position, const char *message) -> void { |
| 1234 | switch (level) { |
| 1235 | case SPV_MSG_FATAL: |
| 1236 | case SPV_MSG_INTERNAL_ERROR: |
| 1237 | case SPV_MSG_ERROR: |
| 1238 | this->LogError(this->device, "UNASSIGNED-GPU-Assisted", "Error during shader instrumentation: line %zu: %s", |
| 1239 | position.index, message); |
| 1240 | break; |
| 1241 | default: |
| 1242 | break; |
| 1243 | } |
| 1244 | }; |
| 1245 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1246 | // Load original shader SPIR-V |
| 1247 | uint32_t num_words = static_cast<uint32_t>(pCreateInfo->codeSize / 4); |
| 1248 | new_pgm.clear(); |
| 1249 | new_pgm.reserve(num_words); |
| 1250 | new_pgm.insert(new_pgm.end(), &pCreateInfo->pCode[0], &pCreateInfo->pCode[num_words]); |
| 1251 | |
| 1252 | // Call the optimizer to instrument the shader. |
| 1253 | // 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] | 1254 | // If descriptor indexing is enabled, enable length checks and updated descriptor checks |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1255 | using namespace spvtools; |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 1256 | spv_target_env target_env = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled)); |
Tony-LunarG | f29f77f | 2020-08-26 15:48:00 -0600 | [diff] [blame] | 1257 | spvtools::ValidatorOptions val_options; |
| 1258 | AdjustValidatorOptions(device_extensions, enabled_features, val_options); |
| 1259 | spvtools::OptimizerOptions opt_options; |
| 1260 | opt_options.set_run_validator(true); |
| 1261 | opt_options.set_validator_options(val_options); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1262 | Optimizer optimizer(target_env); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1263 | optimizer.SetMessageConsumer(gpu_console_message_consumer); |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1264 | 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] | 1265 | descriptor_indexing, buffer_oob_enabled, buffer_oob_enabled)); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1266 | optimizer.RegisterPass(CreateAggressiveDCEPass()); |
Tony-LunarG | 5c38b18 | 2020-06-10 16:15:32 -0600 | [diff] [blame] | 1267 | if ((device_extensions.vk_ext_buffer_device_address || device_extensions.vk_khr_buffer_device_address) && shaderInt64 && |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1268 | enabled_features.core12.bufferDeviceAddress) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1269 | optimizer.RegisterPass(CreateInstBuffAddrCheckPass(desc_set_bind_index, unique_shader_module_id)); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1270 | } |
Tony-LunarG | f29f77f | 2020-08-26 15:48:00 -0600 | [diff] [blame] | 1271 | 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] | 1272 | if (!pass) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 1273 | ReportSetupProblem(device, "Failure to instrument shader. Proceeding with non-instrumented shader."); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1274 | } |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1275 | *unique_shader_id = unique_shader_module_id++; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1276 | return pass; |
| 1277 | } |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 1278 | // Create the instrumented shader data to provide to the driver. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1279 | void GpuAssisted::PreCallRecordCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo, |
| 1280 | const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule, |
| 1281 | void *csm_state_data) { |
| 1282 | create_shader_module_api_state *csm_state = reinterpret_cast<create_shader_module_api_state *>(csm_state_data); |
| 1283 | 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] | 1284 | if (pass) { |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1285 | csm_state->instrumented_create_info.pCode = csm_state->instrumented_pgm.data(); |
| 1286 | csm_state->instrumented_create_info.codeSize = csm_state->instrumented_pgm.size() * sizeof(unsigned int); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1287 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1288 | } |
Tony-LunarG | 20678ff | 2021-05-07 14:56:26 -0600 | [diff] [blame] | 1289 | |
Tony-LunarG | 7345a06 | 2021-06-24 13:16:38 -0600 | [diff] [blame^] | 1290 | static const int kInstErrorPreDrawValidate = spvtools::kInstErrorMax + 1; |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1291 | static const int kPreDrawValidateSubError = spvtools::kInstValidationOutError + 1; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1292 | // Generate the part of the message describing the violation. |
Tony-LunarG | 1a7c9f9 | 2021-04-29 16:04:42 -0600 | [diff] [blame] | 1293 | 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] | 1294 | using namespace spvtools; |
| 1295 | std::ostringstream strm; |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1296 | bool return_code = true; |
Tony-LunarG | 9d7a3bc | 2021-04-26 15:55:18 -0600 | [diff] [blame] | 1297 | assert(kInstErrorPreDrawValidate == _kInstErrorPreDrawValidate); |
| 1298 | assert(kInstValidationOutError == _kInstValidationOutError); |
Tony-LunarG | ab47cac | 2019-12-20 15:28:01 -0700 | [diff] [blame] | 1299 | switch (debug_record[kInstValidationOutError]) { |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1300 | case kInstErrorBindlessBounds: { |
Tony-LunarG | ab47cac | 2019-12-20 15:28:01 -0700 | [diff] [blame] | 1301 | strm << "Index of " << debug_record[kInstBindlessBoundsOutDescIndex] << " used to index descriptor array of length " |
| 1302 | << debug_record[kInstBindlessBoundsOutDescBound] << ". "; |
Tony-LunarG | c1d657d | 2019-02-22 14:55:19 -0700 | [diff] [blame] | 1303 | vuid_msg = "UNASSIGNED-Descriptor index out of bounds"; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1304 | } break; |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1305 | case kInstErrorBindlessUninit: { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1306 | strm << "Descriptor index " << debug_record[kInstBindlessUninitOutDescIndex] << " is uninitialized."; |
Tony-LunarG | c1d657d | 2019-02-22 14:55:19 -0700 | [diff] [blame] | 1307 | vuid_msg = "UNASSIGNED-Descriptor uninitialized"; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1308 | } break; |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1309 | case kInstErrorBuffAddrUnallocRef: { |
Tony-LunarG | ab47cac | 2019-12-20 15:28:01 -0700 | [diff] [blame] | 1310 | uint64_t *ptr = (uint64_t *)&debug_record[kInstBuffAddrUnallocOutDescPtrLo]; |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1311 | strm << "Device address 0x" << std::hex << *ptr << " access out of bounds. "; |
| 1312 | vuid_msg = "UNASSIGNED-Device address out of bounds"; |
| 1313 | } break; |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1314 | case kInstErrorBuffOOBUniform: |
| 1315 | case kInstErrorBuffOOBStorage: { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1316 | auto size = debug_record[kInstBindlessBuffOOBOutBuffSize]; |
| 1317 | if (size == 0) { |
| 1318 | strm << "Descriptor index " << debug_record[kInstBindlessBuffOOBOutDescIndex] << " is uninitialized."; |
| 1319 | vuid_msg = "UNASSIGNED-Descriptor uninitialized"; |
| 1320 | } else { |
| 1321 | strm << "Descriptor index " << debug_record[kInstBindlessBuffOOBOutDescIndex] |
| 1322 | << " access out of bounds. Descriptor size is " << debug_record[kInstBindlessBuffOOBOutBuffSize] |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1323 | << " and highest byte accessed was " << debug_record[kInstBindlessBuffOOBOutBuffOff]; |
Tony-LunarG | f0d4e2b | 2021-04-14 11:52:28 -0600 | [diff] [blame] | 1324 | const GpuVuid vuid = GetGpuVuid(buf_info.cmd_type); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1325 | if (debug_record[kInstValidationOutError] == kInstErrorBuffOOBUniform) |
| 1326 | vuid_msg = vuid.uniform_access_oob; |
| 1327 | else |
| 1328 | vuid_msg = vuid.storage_access_oob; |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1329 | } |
| 1330 | } break; |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1331 | case kInstErrorBuffOOBUniformTexel: |
| 1332 | case kInstErrorBuffOOBStorageTexel: { |
| 1333 | auto size = debug_record[kInstBindlessBuffOOBOutBuffSize]; |
| 1334 | if (size == 0) { |
| 1335 | strm << "Descriptor index " << debug_record[kInstBindlessBuffOOBOutDescIndex] << " is uninitialized."; |
| 1336 | vuid_msg = "UNASSIGNED-Descriptor uninitialized"; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1337 | } else { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1338 | strm << "Descriptor index " << debug_record[kInstBindlessBuffOOBOutDescIndex] |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1339 | << " access out of bounds. Descriptor size is " << debug_record[kInstBindlessBuffOOBOutBuffSize] |
| 1340 | << " texels and highest texel accessed was " << debug_record[kInstBindlessBuffOOBOutBuffOff]; |
Tony-LunarG | f0d4e2b | 2021-04-14 11:52:28 -0600 | [diff] [blame] | 1341 | const GpuVuid vuid = GetGpuVuid(buf_info.cmd_type); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1342 | if (debug_record[kInstValidationOutError] == kInstErrorBuffOOBUniformTexel) |
| 1343 | vuid_msg = vuid.uniform_access_oob; |
| 1344 | else |
| 1345 | vuid_msg = vuid.storage_access_oob; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1346 | } |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1347 | } break; |
| 1348 | case kInstErrorPreDrawValidate: { |
Tim Van Patten | 38bdcdd | 2021-05-14 16:41:00 -0600 | [diff] [blame] | 1349 | // Buffer size must be >= (stride * (drawCount - 1) + offset + sizeof(VkDrawIndexedIndirectCommand)) |
Tony-LunarG | 9d7a3bc | 2021-04-26 15:55:18 -0600 | [diff] [blame] | 1350 | if (debug_record[kPreDrawValidateSubError] == pre_draw_count_exceeds_bufsize_error) { |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1351 | uint32_t count = debug_record[kPreDrawValidateSubError + 1]; |
Tony-LunarG | f0d4e2b | 2021-04-14 11:52:28 -0600 | [diff] [blame] | 1352 | uint32_t stride = buf_info.pre_draw_resources.stride; |
| 1353 | uint32_t offset = static_cast<uint32_t>(buf_info.pre_draw_resources.offset); |
| 1354 | uint32_t draw_size = (stride * (count - 1) + offset + sizeof(VkDrawIndexedIndirectCommand)); |
| 1355 | const GpuVuid vuid = GetGpuVuid(buf_info.cmd_type); |
| 1356 | strm << "Indirect draw count of " << count << " would exceed buffer size " << buf_info.pre_draw_resources.buf_size |
| 1357 | << " of buffer " << buf_info.pre_draw_resources.buffer << " stride = " << stride << " offset = " << offset |
| 1358 | << " (stride * (drawCount - 1) + offset + sizeof(VkDrawIndexedIndirectCommand)) = " << draw_size; |
Tony-LunarG | 64aeaf7 | 2021-04-14 11:13:35 -0600 | [diff] [blame] | 1359 | if (count == 1) { |
| 1360 | vuid_msg = vuid.count_exceeds_bufsize_1; |
| 1361 | } else { |
| 1362 | vuid_msg = vuid.count_exceeds_bufsize; |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1363 | } |
Tony-LunarG | 1a7c9f9 | 2021-04-29 16:04:42 -0600 | [diff] [blame] | 1364 | } else if (debug_record[kPreDrawValidateSubError] == pre_draw_count_exceeds_limit_error) { |
| 1365 | uint32_t count = debug_record[kPreDrawValidateSubError + 1]; |
| 1366 | const GpuVuid vuid = GetGpuVuid(buf_info.cmd_type); |
| 1367 | strm << "Indirect draw count of " << count << " would exceed maxDrawIndirectCount limit of " |
| 1368 | << gpu_assisted->phys_dev_props.limits.maxDrawIndirectCount; |
| 1369 | vuid_msg = vuid.count_exceeds_device_limit; |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1370 | } else if (debug_record[kPreDrawValidateSubError] == pre_draw_first_instance_error) { |
| 1371 | uint32_t index = debug_record[kPreDrawValidateSubError + 1]; |
| 1372 | const GpuVuid vuid = GetGpuVuid(buf_info.cmd_type); |
| 1373 | strm << "The drawIndirectFirstInstance feature is not enabled, but the firstInstance member of the " |
| 1374 | "VkDrawIndirectCommand structure at index " |
| 1375 | << index << " is not zero"; |
| 1376 | vuid_msg = vuid.first_instance_not_zero; |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1377 | } |
| 1378 | return_code = false; |
| 1379 | } break; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1380 | default: { |
Tony-LunarG | ab47cac | 2019-12-20 15:28:01 -0700 | [diff] [blame] | 1381 | strm << "Internal Error (unexpected error type = " << debug_record[kInstValidationOutError] << "). "; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1382 | vuid_msg = "UNASSIGNED-Internal Error"; |
| 1383 | assert(false); |
| 1384 | } break; |
| 1385 | } |
| 1386 | msg = strm.str(); |
Tony-LunarG | 63f82e0 | 2021-04-12 16:13:48 -0600 | [diff] [blame] | 1387 | return return_code; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1388 | } |
| 1389 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1390 | // Pull together all the information from the debug record to build the error message strings, |
| 1391 | // and then assemble them into a single message string. |
| 1392 | // Retrieve the shader program referenced by the unique shader ID provided in the debug record. |
| 1393 | // We had to keep a copy of the shader program with the same lifecycle as the pipeline to make |
| 1394 | // sure it is available when the pipeline is submitted. (The ShaderModule tracking object also |
| 1395 | // keeps a copy, but it can be destroyed after the pipeline is created and before it is submitted.) |
| 1396 | // |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1397 | void GpuAssisted::AnalyzeAndGenerateMessages(VkCommandBuffer command_buffer, VkQueue queue, GpuAssistedBufferInfo &buffer_info, |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1398 | uint32_t operation_index, uint32_t *const debug_output_buffer) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1399 | using namespace spvtools; |
| 1400 | const uint32_t total_words = debug_output_buffer[0]; |
| 1401 | // A zero here means that the shader instrumentation didn't write anything. |
| 1402 | // If you have nothing to say, don't say it here. |
| 1403 | if (0 == total_words) { |
| 1404 | return; |
| 1405 | } |
| 1406 | // The first word in the debug output buffer is the number of words that would have |
| 1407 | // been written by the shader instrumentation, if there was enough room in the buffer we provided. |
| 1408 | // The number of words actually written by the shaders is determined by the size of the buffer |
| 1409 | // we provide via the descriptor. So, we process only the number of words that can fit in the |
| 1410 | // buffer. |
| 1411 | // Each "report" written by the shader instrumentation is considered a "record". This function |
| 1412 | // is hard-coded to process only one record because it expects the buffer to be large enough to |
| 1413 | // hold only one record. If there is a desire to process more than one record, this function needs |
| 1414 | // to be modified to loop over records and the buffer size increased. |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1415 | std::string validation_message; |
| 1416 | std::string stage_message; |
| 1417 | std::string common_message; |
| 1418 | std::string filename_message; |
| 1419 | std::string source_message; |
| 1420 | std::string vuid_msg; |
| 1421 | VkShaderModule shader_module_handle = VK_NULL_HANDLE; |
| 1422 | VkPipeline pipeline_handle = VK_NULL_HANDLE; |
| 1423 | std::vector<unsigned int> pgm; |
| 1424 | // The first record starts at this offset after the total_words. |
| 1425 | const uint32_t *debug_record = &debug_output_buffer[kDebugOutputDataOffset]; |
| 1426 | // Lookup the VkShaderModule handle and SPIR-V code used to create the shader, using the unique shader ID value returned |
| 1427 | // by the instrumented shader. |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1428 | auto it = shader_map.find(debug_record[kInstCommonOutShaderId]); |
| 1429 | if (it != shader_map.end()) { |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1430 | shader_module_handle = it->second.shader_module; |
| 1431 | pipeline_handle = it->second.pipeline; |
| 1432 | pgm = it->second.pgm; |
| 1433 | } |
Tony-LunarG | 1a7c9f9 | 2021-04-29 16:04:42 -0600 | [diff] [blame] | 1434 | 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] | 1435 | if (gen_full_message) { |
| 1436 | UtilGenerateStageMessage(debug_record, stage_message); |
| 1437 | UtilGenerateCommonMessage(report_data, command_buffer, debug_record, shader_module_handle, pipeline_handle, |
| 1438 | buffer_info.pipeline_bind_point, operation_index, common_message); |
| 1439 | UtilGenerateSourceMessages(pgm, debug_record, false, filename_message, source_message); |
| 1440 | LogError(queue, vuid_msg.c_str(), "%s %s %s %s%s", validation_message.c_str(), common_message.c_str(), stage_message.c_str(), |
| 1441 | filename_message.c_str(), source_message.c_str()); |
| 1442 | } |
| 1443 | else { |
| 1444 | LogError(queue, vuid_msg.c_str(), "%s", validation_message.c_str()); |
| 1445 | } |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1446 | // The debug record at word kInstCommonOutSize is the number of words in the record |
| 1447 | // 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] | 1448 | 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] | 1449 | memset(debug_output_buffer, 0, sizeof(uint32_t) * words_to_clear); |
| 1450 | } |
| 1451 | |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1452 | void GpuAssisted::SetDescriptorInitialized(uint32_t *pData, uint32_t index, const cvdescriptorset::Descriptor *descriptor) { |
| 1453 | if (descriptor->GetClass() == cvdescriptorset::DescriptorClass::GeneralBuffer) { |
| 1454 | auto buffer = static_cast<const cvdescriptorset::BufferDescriptor *>(descriptor)->GetBuffer(); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1455 | if (buffer == VK_NULL_HANDLE) { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1456 | pData[index] = UINT_MAX; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1457 | } else { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1458 | auto buffer_state = static_cast<const cvdescriptorset::BufferDescriptor *>(descriptor)->GetBufferState(); |
| 1459 | pData[index] = static_cast<uint32_t>(buffer_state->createInfo.size); |
| 1460 | } |
| 1461 | } else if (descriptor->GetClass() == cvdescriptorset::DescriptorClass::TexelBuffer) { |
| 1462 | auto buffer_view = static_cast<const cvdescriptorset::TexelDescriptor *>(descriptor)->GetBufferView(); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1463 | if (buffer_view == VK_NULL_HANDLE) { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1464 | pData[index] = UINT_MAX; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1465 | } else { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1466 | auto buffer_view_state = static_cast<const cvdescriptorset::TexelDescriptor *>(descriptor)->GetBufferViewState(); |
| 1467 | pData[index] = static_cast<uint32_t>(buffer_view_state->buffer_state->createInfo.size); |
| 1468 | } |
| 1469 | } else { |
| 1470 | pData[index] = 1; |
| 1471 | } |
| 1472 | } |
| 1473 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1474 | // For the given command buffer, map its debug data buffers and update the status of any update after bind descriptors |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1475 | void GpuAssisted::UpdateInstrumentationBuffer(CMD_BUFFER_STATE *cb_node) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1476 | auto gpu_buffer_list = GetBufferInfo(cb_node->commandBuffer()); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1477 | uint32_t *data; |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1478 | for (auto &buffer_info : gpu_buffer_list) { |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1479 | if (buffer_info.di_input_mem_block.update_at_submit.size() > 0) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1480 | VkResult result = |
| 1481 | 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] | 1482 | if (result == VK_SUCCESS) { |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1483 | for (const auto &update : buffer_info.di_input_mem_block.update_at_submit) { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1484 | if (update.second->updated) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1485 | SetDescriptorInitialized(data, update.first, update.second); |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1486 | } |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1487 | } |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1488 | vmaUnmapMemory(vmaAllocator, buffer_info.di_input_mem_block.allocation); |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1494 | void GpuAssisted::PreRecordCommandBuffer(VkCommandBuffer command_buffer) { |
| 1495 | auto cb_node = GetCBState(command_buffer); |
| 1496 | UpdateInstrumentationBuffer(cb_node); |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1497 | for (auto *secondary_cmd_buffer : cb_node->linkedCommandBuffers) { |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1498 | UpdateInstrumentationBuffer(secondary_cmd_buffer); |
| 1499 | } |
| 1500 | } |
| 1501 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1502 | void GpuAssisted::PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence) { |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1503 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1504 | const VkSubmitInfo *submit = &pSubmits[submit_idx]; |
| 1505 | for (uint32_t i = 0; i < submit->commandBufferCount; i++) { |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1506 | PreRecordCommandBuffer(submit->pCommandBuffers[i]); |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1507 | } |
| 1508 | } |
| 1509 | } |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1510 | void GpuAssisted::PreCallRecordQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, |
| 1511 | VkFence fence) { |
| 1512 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1513 | const VkSubmitInfo2KHR *submit = &pSubmits[submit_idx]; |
| 1514 | for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) { |
| 1515 | PreRecordCommandBuffer(submit->pCommandBufferInfos[i].commandBuffer); |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | bool GpuAssisted::CommandBufferNeedsProcessing(VkCommandBuffer command_buffer) { |
| 1521 | bool buffers_present = false; |
| 1522 | auto cb_node = GetCBState(command_buffer); |
| 1523 | |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1524 | if (GetBufferInfo(cb_node->commandBuffer()).size() || cb_node->hasBuildAccelerationStructureCmd) { |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1525 | buffers_present = true; |
| 1526 | } |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1527 | for (const auto *secondary_cmd_buffer : cb_node->linkedCommandBuffers) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1528 | if (GetBufferInfo(secondary_cmd_buffer->commandBuffer()).size() || cb_node->hasBuildAccelerationStructureCmd) { |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1529 | buffers_present = true; |
| 1530 | } |
| 1531 | } |
| 1532 | return buffers_present; |
| 1533 | } |
| 1534 | |
| 1535 | void GpuAssisted::ProcessCommandBuffer(VkQueue queue, VkCommandBuffer command_buffer) { |
| 1536 | auto cb_node = GetCBState(command_buffer); |
| 1537 | |
| 1538 | UtilProcessInstrumentationBuffer(queue, cb_node, this); |
| 1539 | ProcessAccelerationStructureBuildValidationBuffer(queue, cb_node); |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 1540 | for (auto *secondary_cmd_buffer : cb_node->linkedCommandBuffers) { |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1541 | UtilProcessInstrumentationBuffer(queue, secondary_cmd_buffer, this); |
| 1542 | ProcessAccelerationStructureBuildValidationBuffer(queue, cb_node); |
| 1543 | } |
| 1544 | } |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1545 | |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1546 | // Issue a memory barrier to make GPU-written data available to host. |
| 1547 | // Wait for the queue to complete execution. |
| 1548 | // Check the debug buffers for all the command buffers that were submitted. |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1549 | void GpuAssisted::PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence, |
| 1550 | VkResult result) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1551 | ValidationStateTracker::PostCallRecordQueueSubmit(queue, submitCount, pSubmits, fence, result); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1552 | |
Mark Lobodzinski | 09379db | 2020-05-07 08:22:01 -0600 | [diff] [blame] | 1553 | if (aborted || (result != VK_SUCCESS)) return; |
Tony-LunarG | 3cc795e | 2019-08-26 12:13:50 -0600 | [diff] [blame] | 1554 | bool buffers_present = false; |
| 1555 | // Don't QueueWaitIdle if there's nothing to process |
| 1556 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1557 | const VkSubmitInfo *submit = &pSubmits[submit_idx]; |
| 1558 | for (uint32_t i = 0; i < submit->commandBufferCount; i++) { |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1559 | buffers_present |= CommandBufferNeedsProcessing(submit->pCommandBuffers[i]); |
Tony-LunarG | 3cc795e | 2019-08-26 12:13:50 -0600 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | if (!buffers_present) return; |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1563 | |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 1564 | UtilSubmitBarrier(queue, this); |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1565 | |
Tony-LunarG | 152a88b | 2019-03-20 15:42:24 -0600 | [diff] [blame] | 1566 | DispatchQueueWaitIdle(queue); |
Karl Schultz | 5867424 | 2019-01-22 15:35:02 -0700 | [diff] [blame] | 1567 | |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1568 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1569 | const VkSubmitInfo *submit = &pSubmits[submit_idx]; |
| 1570 | for (uint32_t i = 0; i < submit->commandBufferCount; i++) { |
Jeremy Gebben | a3705f4 | 2021-01-19 16:47:43 -0700 | [diff] [blame] | 1571 | ProcessCommandBuffer(queue, submit->pCommandBuffers[i]); |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | void GpuAssisted::PostCallRecordQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, |
| 1577 | VkFence fence, VkResult result) { |
| 1578 | ValidationStateTracker::PostCallRecordQueueSubmit2KHR(queue, submitCount, pSubmits, fence, result); |
| 1579 | |
| 1580 | if (aborted || (result != VK_SUCCESS)) return; |
| 1581 | bool buffers_present = false; |
| 1582 | // Don't QueueWaitIdle if there's nothing to process |
| 1583 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1584 | const VkSubmitInfo2KHR *submit = &pSubmits[submit_idx]; |
| 1585 | for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) { |
| 1586 | buffers_present |= CommandBufferNeedsProcessing(submit->pCommandBufferInfos[i].commandBuffer); |
| 1587 | } |
| 1588 | } |
| 1589 | if (!buffers_present) return; |
| 1590 | |
| 1591 | UtilSubmitBarrier(queue, this); |
| 1592 | |
| 1593 | DispatchQueueWaitIdle(queue); |
| 1594 | |
| 1595 | for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) { |
| 1596 | const VkSubmitInfo2KHR *submit = &pSubmits[submit_idx]; |
| 1597 | for (uint32_t i = 0; i < submit->commandBufferInfoCount; i++) { |
| 1598 | ProcessCommandBuffer(queue, submit->pCommandBufferInfos[i].commandBuffer); |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | } |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1602 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1603 | void GpuAssisted::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, |
| 1604 | uint32_t firstVertex, uint32_t firstInstance) { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1605 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAW); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | void GpuAssisted::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount, |
| 1609 | uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1610 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDEXED); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | void GpuAssisted::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count, |
| 1614 | uint32_t stride) { |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1615 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, count, stride, 0, 0}; |
| 1616 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDIRECT, &cdi_state); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | void GpuAssisted::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1620 | uint32_t count, uint32_t stride) { |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1621 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, count, stride, 0, 0}; |
| 1622 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDEXEDINDIRECT, &cdi_state); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1623 | } |
| 1624 | |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1625 | void GpuAssisted::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1626 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
| 1627 | uint32_t stride) { |
| 1628 | ValidationStateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, |
| 1629 | maxDrawCount, stride); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1630 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, 0, stride, countBuffer, countBufferOffset}; |
| 1631 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDIRECTCOUNT, &cdi_state); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | void GpuAssisted::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1635 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount, |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1636 | |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1637 | uint32_t stride) { |
| 1638 | ValidationStateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, |
| 1639 | maxDrawCount, stride); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1640 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, 0, stride, countBuffer, countBufferOffset}; |
| 1641 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDIRECTCOUNT, &cdi_state); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1642 | } |
| 1643 | |
Tony-LunarG | 54176fb | 2020-12-02 10:47:22 -0700 | [diff] [blame] | 1644 | void GpuAssisted::PreCallRecordCmdDrawIndirectByteCountEXT(VkCommandBuffer commandBuffer, uint32_t instanceCount, |
| 1645 | uint32_t firstInstance, VkBuffer counterBuffer, |
| 1646 | VkDeviceSize counterBufferOffset, uint32_t counterOffset, |
| 1647 | uint32_t vertexStride) { |
| 1648 | ValidationStateTracker::PreCallRecordCmdDrawIndirectByteCountEXT(commandBuffer, instanceCount, firstInstance, counterBuffer, |
| 1649 | counterBufferOffset, counterOffset, vertexStride); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1650 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDIRECTBYTECOUNTEXT); |
Tony-LunarG | 54176fb | 2020-12-02 10:47:22 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1653 | void GpuAssisted::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1654 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 1655 | uint32_t maxDrawCount, uint32_t stride) { |
| 1656 | ValidationStateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, |
| 1657 | countBufferOffset, maxDrawCount, stride); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1658 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, 0, stride, countBuffer, countBufferOffset}; |
| 1659 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDEXEDINDIRECTCOUNT, &cdi_state); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | void GpuAssisted::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1663 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 1664 | uint32_t maxDrawCount, uint32_t stride) { |
| 1665 | ValidationStateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, |
| 1666 | maxDrawCount, stride); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1667 | GpuAssistedCmdDrawIndirectState cdi_state = {buffer, offset, 0, stride, countBuffer, countBufferOffset}; |
| 1668 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWINDEXEDINDIRECTCOUNT, &cdi_state); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | void GpuAssisted::PreCallRecordCmdDrawMeshTasksNV(VkCommandBuffer commandBuffer, uint32_t taskCount, uint32_t firstTask) { |
| 1672 | ValidationStateTracker::PreCallRecordCmdDrawMeshTasksNV(commandBuffer, taskCount, firstTask); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1673 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWMESHTASKSNV); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | void GpuAssisted::PreCallRecordCmdDrawMeshTasksIndirectNV(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1677 | uint32_t drawCount, uint32_t stride) { |
| 1678 | ValidationStateTracker::PreCallRecordCmdDrawMeshTasksIndirectNV(commandBuffer, buffer, offset, drawCount, stride); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1679 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWMESHTASKSINDIRECTNV); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | void GpuAssisted::PreCallRecordCmdDrawMeshTasksIndirectCountNV(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, |
| 1683 | VkBuffer countBuffer, VkDeviceSize countBufferOffset, |
| 1684 | uint32_t maxDrawCount, uint32_t stride) { |
| 1685 | ValidationStateTracker::PreCallRecordCmdDrawMeshTasksIndirectCountNV(commandBuffer, buffer, offset, countBuffer, |
| 1686 | countBufferOffset, maxDrawCount, stride); |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1687 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, CMD_DRAWMESHTASKSINDIRECTCOUNTNV); |
Tony-LunarG | 2fb8ff0 | 2020-06-11 12:45:07 -0600 | [diff] [blame] | 1688 | } |
| 1689 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1690 | void GpuAssisted::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1691 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, CMD_DISPATCH); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | void GpuAssisted::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1695 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, CMD_DISPATCHINDIRECT); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1696 | } |
| 1697 | |
Tony-LunarG | d13f9b5 | 2020-09-08 15:45:45 -0600 | [diff] [blame] | 1698 | void GpuAssisted::PreCallRecordCmdDispatchBase(VkCommandBuffer commandBuffer, uint32_t baseGroupX, uint32_t baseGroupY, |
| 1699 | uint32_t baseGroupZ, uint32_t groupCountX, uint32_t groupCountY, |
| 1700 | uint32_t groupCountZ) { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1701 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, CMD_DISPATCHBASE); |
Tony-LunarG | d13f9b5 | 2020-09-08 15:45:45 -0600 | [diff] [blame] | 1702 | } |
| 1703 | |
Tony-LunarG | 52c8c60 | 2020-09-10 16:29:56 -0600 | [diff] [blame] | 1704 | void GpuAssisted::PreCallRecordCmdDispatchBaseKHR(VkCommandBuffer commandBuffer, uint32_t baseGroupX, uint32_t baseGroupY, |
| 1705 | uint32_t baseGroupZ, uint32_t groupCountX, uint32_t groupCountY, |
| 1706 | uint32_t groupCountZ) { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1707 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, CMD_DISPATCHBASE); |
Tony-LunarG | 52c8c60 | 2020-09-10 16:29:56 -0600 | [diff] [blame] | 1708 | } |
| 1709 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1710 | void GpuAssisted::PreCallRecordCmdTraceRaysNV(VkCommandBuffer commandBuffer, VkBuffer raygenShaderBindingTableBuffer, |
| 1711 | VkDeviceSize raygenShaderBindingOffset, VkBuffer missShaderBindingTableBuffer, |
| 1712 | VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride, |
| 1713 | VkBuffer hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset, |
| 1714 | VkDeviceSize hitShaderBindingStride, VkBuffer callableShaderBindingTableBuffer, |
| 1715 | VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride, |
| 1716 | uint32_t width, uint32_t height, uint32_t depth) { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1717 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_NV, CMD_TRACERAYSNV); |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | void GpuAssisted::PostCallRecordCmdTraceRaysNV(VkCommandBuffer commandBuffer, VkBuffer raygenShaderBindingTableBuffer, |
| 1721 | VkDeviceSize raygenShaderBindingOffset, VkBuffer missShaderBindingTableBuffer, |
| 1722 | VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride, |
| 1723 | VkBuffer hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset, |
| 1724 | VkDeviceSize hitShaderBindingStride, VkBuffer callableShaderBindingTableBuffer, |
| 1725 | VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride, |
| 1726 | uint32_t width, uint32_t height, uint32_t depth) { |
| 1727 | CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer); |
| 1728 | cb_state->hasTraceRaysCmd = true; |
| 1729 | } |
| 1730 | |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1731 | void GpuAssisted::PreCallRecordCmdTraceRaysKHR(VkCommandBuffer commandBuffer, |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1732 | const VkStridedDeviceAddressRegionKHR *pRaygenShaderBindingTable, |
| 1733 | const VkStridedDeviceAddressRegionKHR *pMissShaderBindingTable, |
| 1734 | const VkStridedDeviceAddressRegionKHR *pHitShaderBindingTable, |
| 1735 | const VkStridedDeviceAddressRegionKHR *pCallableShaderBindingTable, uint32_t width, |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1736 | uint32_t height, uint32_t depth) { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1737 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, CMD_TRACERAYSKHR); |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | void GpuAssisted::PostCallRecordCmdTraceRaysKHR(VkCommandBuffer commandBuffer, |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1741 | const VkStridedDeviceAddressRegionKHR *pRaygenShaderBindingTable, |
| 1742 | const VkStridedDeviceAddressRegionKHR *pMissShaderBindingTable, |
| 1743 | const VkStridedDeviceAddressRegionKHR *pHitShaderBindingTable, |
| 1744 | const VkStridedDeviceAddressRegionKHR *pCallableShaderBindingTable, uint32_t width, |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1745 | uint32_t height, uint32_t depth) { |
| 1746 | CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer); |
| 1747 | cb_state->hasTraceRaysCmd = true; |
| 1748 | } |
| 1749 | |
| 1750 | void GpuAssisted::PreCallRecordCmdTraceRaysIndirectKHR(VkCommandBuffer commandBuffer, |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1751 | const VkStridedDeviceAddressRegionKHR *pRaygenShaderBindingTable, |
| 1752 | const VkStridedDeviceAddressRegionKHR *pMissShaderBindingTable, |
| 1753 | const VkStridedDeviceAddressRegionKHR *pHitShaderBindingTable, |
| 1754 | const VkStridedDeviceAddressRegionKHR *pCallableShaderBindingTable, |
Shannon McPherson | 54e1f89 | 2020-11-27 11:04:19 -0700 | [diff] [blame] | 1755 | VkDeviceAddress indirectDeviceAddress) { |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1756 | AllocateValidationResources(commandBuffer, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, CMD_TRACERAYSINDIRECTKHR); |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | void GpuAssisted::PostCallRecordCmdTraceRaysIndirectKHR(VkCommandBuffer commandBuffer, |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1760 | const VkStridedDeviceAddressRegionKHR *pRaygenShaderBindingTable, |
| 1761 | const VkStridedDeviceAddressRegionKHR *pMissShaderBindingTable, |
| 1762 | const VkStridedDeviceAddressRegionKHR *pHitShaderBindingTable, |
| 1763 | const VkStridedDeviceAddressRegionKHR *pCallableShaderBindingTable, |
Shannon McPherson | 54e1f89 | 2020-11-27 11:04:19 -0700 | [diff] [blame] | 1764 | VkDeviceAddress indirectDeviceAddress) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 1765 | CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer); |
| 1766 | cb_state->hasTraceRaysCmd = true; |
| 1767 | } |
| 1768 | |
Tony-LunarG | 9d7a3bc | 2021-04-26 15:55:18 -0600 | [diff] [blame] | 1769 | // To generate the pre draw validation shader, run the following from the repository base level |
Tony-LunarG | 20678ff | 2021-05-07 14:56:26 -0600 | [diff] [blame] | 1770 | // python ./scripts/generate_spirv.py --outfilename ./layers/generated/gpu_pre_draw_shader.h ./layers/gpu_pre_draw_shader.vert |
| 1771 | // ./External/glslang/build/install/bin/glslangValidator.exe |
Tony-LunarG | 9d7a3bc | 2021-04-26 15:55:18 -0600 | [diff] [blame] | 1772 | #include "gpu_pre_draw_shader.h" |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1773 | void GpuAssisted::AllocatePreDrawValidationResources(GpuAssistedDeviceMemoryBlock output_block, |
| 1774 | GpuAssistedPreDrawResources &resources, const LAST_BOUND_STATE &state, |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1775 | VkPipeline *pPipeline, const GpuAssistedCmdDrawIndirectState *cdi_state) { |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1776 | VkResult result; |
| 1777 | if (!pre_draw_validation_state.globals_created) { |
| 1778 | auto shader_module_ci = LvlInitStruct<VkShaderModuleCreateInfo>(); |
Tony-LunarG | 9d7a3bc | 2021-04-26 15:55:18 -0600 | [diff] [blame] | 1779 | shader_module_ci.codeSize = sizeof(gpu_pre_draw_shader_vert); |
| 1780 | shader_module_ci.pCode = gpu_pre_draw_shader_vert; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1781 | result = |
| 1782 | DispatchCreateShaderModule(device, &shader_module_ci, nullptr, &pre_draw_validation_state.validation_shader_module); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1783 | if (result != VK_SUCCESS) { |
| 1784 | ReportSetupProblem(device, "Unable to create shader module. Aborting GPU-AV"); |
| 1785 | aborted = true; |
| 1786 | return; |
| 1787 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1788 | |
| 1789 | std::vector<VkDescriptorSetLayoutBinding> bindings; |
| 1790 | VkDescriptorSetLayoutBinding binding = {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT, NULL}; |
| 1791 | // 0 - output buffer, 1 - count buffer |
| 1792 | bindings.push_back(binding); |
| 1793 | binding.binding = 1; |
| 1794 | bindings.push_back(binding); |
| 1795 | |
| 1796 | VkDescriptorSetLayoutCreateInfo ds_layout_ci = {}; |
| 1797 | ds_layout_ci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
| 1798 | ds_layout_ci.bindingCount = static_cast<uint32_t>(bindings.size()); |
| 1799 | ds_layout_ci.pBindings = bindings.data(); |
| 1800 | result = DispatchCreateDescriptorSetLayout(device, &ds_layout_ci, nullptr, &pre_draw_validation_state.validation_ds_layout); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1801 | if (result != VK_SUCCESS) { |
| 1802 | ReportSetupProblem(device, "Unable to create descriptor set layout. Aborting GPU-AV"); |
| 1803 | aborted = true; |
| 1804 | return; |
| 1805 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1806 | |
| 1807 | const uint32_t push_constant_range_count = 1; |
| 1808 | VkPushConstantRange push_constant_ranges[push_constant_range_count] = {}; |
| 1809 | push_constant_ranges[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; |
| 1810 | push_constant_ranges[0].offset = 0; |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1811 | push_constant_ranges[0].size = 4 * sizeof(uint32_t); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1812 | VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo[1] = {}; |
| 1813 | pipelineLayoutCreateInfo[0].sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; |
| 1814 | pipelineLayoutCreateInfo[0].pNext = NULL; |
| 1815 | pipelineLayoutCreateInfo[0].pushConstantRangeCount = push_constant_range_count; |
| 1816 | pipelineLayoutCreateInfo[0].pPushConstantRanges = push_constant_ranges; |
| 1817 | pipelineLayoutCreateInfo[0].setLayoutCount = 1; |
| 1818 | pipelineLayoutCreateInfo[0].pSetLayouts = &pre_draw_validation_state.validation_ds_layout; |
| 1819 | result = DispatchCreatePipelineLayout(device, pipelineLayoutCreateInfo, NULL, |
| 1820 | &pre_draw_validation_state.validation_pipeline_layout); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1821 | if (result != VK_SUCCESS) { |
| 1822 | ReportSetupProblem(device, "Unable to create pipeline layout. Aborting GPU-AV"); |
| 1823 | aborted = true; |
| 1824 | return; |
| 1825 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1826 | |
| 1827 | pre_draw_validation_state.globals_created = true; |
| 1828 | } |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1829 | VkRenderPass render_pass = state.pipeline_state->rp_state->renderPass(); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1830 | assert(render_pass != VK_NULL_HANDLE); |
| 1831 | auto pipeline = pre_draw_validation_state.renderpass_to_pipeline.find(render_pass); |
| 1832 | if (pipeline == pre_draw_validation_state.renderpass_to_pipeline.end()) { |
| 1833 | auto pipeline_stage_ci = LvlInitStruct<VkPipelineShaderStageCreateInfo>(); |
| 1834 | pipeline_stage_ci.stage = VK_SHADER_STAGE_VERTEX_BIT; |
| 1835 | pipeline_stage_ci.module = pre_draw_validation_state.validation_shader_module; |
| 1836 | pipeline_stage_ci.pName = "main"; |
| 1837 | |
| 1838 | auto graphicsPipelineCreateInfo = LvlInitStruct<VkGraphicsPipelineCreateInfo>(); |
| 1839 | auto vertexInputState = LvlInitStruct<VkPipelineVertexInputStateCreateInfo>(); |
| 1840 | auto inputAssemblyState = LvlInitStruct<VkPipelineInputAssemblyStateCreateInfo>(); |
| 1841 | inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
| 1842 | auto rasterizationState = LvlInitStruct<VkPipelineRasterizationStateCreateInfo>(); |
| 1843 | rasterizationState.rasterizerDiscardEnable = VK_TRUE; |
| 1844 | auto colorBlendState = LvlInitStruct<VkPipelineColorBlendStateCreateInfo>(); |
| 1845 | |
| 1846 | graphicsPipelineCreateInfo.pVertexInputState = &vertexInputState; |
| 1847 | graphicsPipelineCreateInfo.pInputAssemblyState = &inputAssemblyState; |
| 1848 | graphicsPipelineCreateInfo.pRasterizationState = &rasterizationState; |
| 1849 | graphicsPipelineCreateInfo.pColorBlendState = &colorBlendState; |
| 1850 | graphicsPipelineCreateInfo.renderPass = render_pass; |
| 1851 | graphicsPipelineCreateInfo.layout = pre_draw_validation_state.validation_pipeline_layout; |
| 1852 | graphicsPipelineCreateInfo.stageCount = 1; |
| 1853 | graphicsPipelineCreateInfo.pStages = &pipeline_stage_ci; |
| 1854 | |
| 1855 | VkPipeline new_pipeline = VK_NULL_HANDLE; |
| 1856 | result = DispatchCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, nullptr, &new_pipeline); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1857 | if (result != VK_SUCCESS) { |
| 1858 | ReportSetupProblem(device, "Unable to create graphics pipeline. Aborting GPU-AV"); |
| 1859 | aborted = true; |
| 1860 | return; |
| 1861 | } |
| 1862 | |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1863 | *pPipeline = new_pipeline; |
| 1864 | pre_draw_validation_state.renderpass_to_pipeline[render_pass] = new_pipeline; |
| 1865 | } else { |
| 1866 | *pPipeline = pipeline->second; |
| 1867 | } |
| 1868 | |
| 1869 | result = desc_set_manager->GetDescriptorSet(&resources.desc_pool, pre_draw_validation_state.validation_ds_layout, |
| 1870 | &resources.desc_set); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1871 | if (result != VK_SUCCESS) { |
| 1872 | ReportSetupProblem(device, "Unable to allocate descriptor set. Aborting GPU-AV"); |
| 1873 | aborted = true; |
| 1874 | return; |
| 1875 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1876 | |
| 1877 | VkDescriptorBufferInfo buffer_infos[3] = {}; |
| 1878 | // Error output buffer |
| 1879 | buffer_infos[0].buffer = output_block.buffer; |
| 1880 | buffer_infos[0].offset = 0; |
| 1881 | buffer_infos[0].range = VK_WHOLE_SIZE; |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1882 | if (cdi_state->count_buffer) { |
| 1883 | // Count buffer |
| 1884 | buffer_infos[1].buffer = cdi_state->count_buffer; |
| 1885 | } else { |
| 1886 | // Draw Buffer |
| 1887 | buffer_infos[1].buffer = cdi_state->buffer; |
| 1888 | } |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1889 | buffer_infos[1].offset = 0; |
| 1890 | buffer_infos[1].range = VK_WHOLE_SIZE; |
| 1891 | |
| 1892 | VkWriteDescriptorSet desc_writes[2] = {}; |
| 1893 | for (auto i = 0; i < 2; i++) { |
| 1894 | desc_writes[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1895 | desc_writes[i].dstBinding = i; |
| 1896 | desc_writes[i].descriptorCount = 1; |
| 1897 | desc_writes[i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 1898 | desc_writes[i].pBufferInfo = &buffer_infos[i]; |
| 1899 | desc_writes[i].dstSet = resources.desc_set; |
| 1900 | } |
| 1901 | DispatchUpdateDescriptorSets(device, 2, desc_writes, 0, NULL); |
| 1902 | } |
| 1903 | |
Tony-LunarG | 7de10e8 | 2020-11-24 11:31:55 -0700 | [diff] [blame] | 1904 | void GpuAssisted::AllocateValidationResources(const VkCommandBuffer cmd_buffer, const VkPipelineBindPoint bind_point, |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1905 | CMD_TYPE cmd_type, const GpuAssistedCmdDrawIndirectState *cdi_state) { |
Jason Macnak | 67407e7 | 2019-07-11 11:05:09 -0700 | [diff] [blame] | 1906 | if (bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS && bind_point != VK_PIPELINE_BIND_POINT_COMPUTE && |
| 1907 | bind_point != VK_PIPELINE_BIND_POINT_RAY_TRACING_NV) { |
andreyg | ca287f2 | 2019-04-10 00:15:33 +0300 | [diff] [blame] | 1908 | return; |
| 1909 | } |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1910 | VkResult result; |
| 1911 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1912 | if (aborted) return; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1913 | |
| 1914 | std::vector<VkDescriptorSet> desc_sets; |
| 1915 | VkDescriptorPool desc_pool = VK_NULL_HANDLE; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 1916 | 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] | 1917 | assert(result == VK_SUCCESS); |
| 1918 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 1919 | ReportSetupProblem(device, "Unable to allocate descriptor sets. Device could become unstable."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1920 | aborted = true; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1921 | return; |
| 1922 | } |
| 1923 | |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1924 | VkDescriptorBufferInfo output_desc_buffer_info = {}; |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1925 | output_desc_buffer_info.range = output_buffer_size; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1926 | |
Mark Lobodzinski | cefe42f | 2019-04-25 12:16:27 -0600 | [diff] [blame] | 1927 | auto cb_node = GetCBState(cmd_buffer); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1928 | if (!cb_node) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 1929 | ReportSetupProblem(device, "Unrecognized command buffer"); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1930 | aborted = true; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1931 | return; |
| 1932 | } |
| 1933 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1934 | // 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] | 1935 | GpuAssistedDeviceMemoryBlock output_block = {}; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1936 | VkBufferCreateInfo buffer_info = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; |
| 1937 | buffer_info.size = output_buffer_size; |
| 1938 | buffer_info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 1939 | VmaAllocationCreateInfo alloc_info = {}; |
| 1940 | alloc_info.usage = VMA_MEMORY_USAGE_GPU_TO_CPU; |
| 1941 | 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] | 1942 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 1943 | ReportSetupProblem(device, "Unable to allocate device memory. Device could become unstable."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1944 | aborted = true; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 1945 | return; |
| 1946 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1947 | |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1948 | // 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] | 1949 | uint32_t *data_ptr; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1950 | result = vmaMapMemory(vmaAllocator, output_block.allocation, reinterpret_cast<void **>(&data_ptr)); |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 1951 | if (result == VK_SUCCESS) { |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 1952 | memset(data_ptr, 0, output_buffer_size); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 1953 | vmaUnmapMemory(vmaAllocator, output_block.allocation); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1954 | } |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 1955 | |
Tony-LunarG | 2ba1cb3 | 2019-09-25 15:16:11 -0600 | [diff] [blame] | 1956 | GpuAssistedDeviceMemoryBlock di_input_block = {}, bda_input_block = {}; |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 1957 | VkDescriptorBufferInfo di_input_desc_buffer_info = {}; |
| 1958 | VkDescriptorBufferInfo bda_input_desc_buffer_info = {}; |
| 1959 | VkWriteDescriptorSet desc_writes[3] = {}; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1960 | GpuAssistedPreDrawResources pre_draw_resources = {}; |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 1961 | uint32_t desc_count = 1; |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 1962 | const auto lv_bind_point = ConvertToLvlBindPoint(bind_point); |
| 1963 | auto const &state = cb_node->lastBound[lv_bind_point]; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1964 | 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] | 1965 | |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1966 | if (validate_draw_indirect && ((cmd_type == CMD_DRAWINDIRECTCOUNT || cmd_type == CMD_DRAWINDEXEDINDIRECTCOUNT) || |
| 1967 | ((cmd_type == CMD_DRAWINDIRECT || cmd_type == CMD_DRAWINDEXEDINDIRECT) && |
| 1968 | !(enabled_features.core.drawIndirectFirstInstance)))) { |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1969 | // 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] | 1970 | // |
| 1971 | // NOTE that this validation does not attempt to abort invalid api calls as most other validation does. A crash |
| 1972 | // or DEVICE_LOST resulting from the invalid call will prevent preceeding validation errors from being reported. |
| 1973 | |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1974 | assert(bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS); |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1975 | assert(cdi_state != NULL); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1976 | VkPipeline validation_pipeline; |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1977 | AllocatePreDrawValidationResources(output_block, pre_draw_resources, state, &validation_pipeline, cdi_state); |
Tony-LunarG | 2c6d57f | 2021-04-13 10:07:15 -0600 | [diff] [blame] | 1978 | if (aborted) return; |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 1979 | |
| 1980 | // Save current graphics pipeline state |
| 1981 | GPUAV_RESTORABLE_PIPELINE_STATE restorable_state; |
| 1982 | restorable_state.Create(cb_node, VK_PIPELINE_BIND_POINT_GRAPHICS); |
| 1983 | |
Tony-LunarG | f0d4e2b | 2021-04-14 11:52:28 -0600 | [diff] [blame] | 1984 | // Save parameters for error message |
Tony-LunarG | 3723a3a | 2021-05-04 14:52:39 -0600 | [diff] [blame] | 1985 | pre_draw_resources.buffer = cdi_state->buffer; |
| 1986 | pre_draw_resources.offset = cdi_state->offset; |
| 1987 | pre_draw_resources.stride = cdi_state->stride; |
| 1988 | |
| 1989 | uint32_t pushConstants[4] = {}; |
| 1990 | if (cmd_type == CMD_DRAWINDIRECTCOUNT || cmd_type == CMD_DRAWINDEXEDINDIRECTCOUNT) { |
| 1991 | if (cdi_state->count_buffer_offset > std::numeric_limits<uint32_t>::max()) { |
| 1992 | ReportSetupProblem(device, |
| 1993 | "Count buffer offset is larger than can be contained in an unsigned int. Aborting GPU-AV"); |
| 1994 | aborted = true; |
| 1995 | return; |
| 1996 | } |
| 1997 | |
| 1998 | // Buffer size must be >= (stride * (drawCount - 1) + offset + sizeof(VkDrawIndirectCommand)) |
| 1999 | uint32_t struct_size; |
| 2000 | if (cmd_type == CMD_DRAWINDIRECTCOUNT) { |
| 2001 | struct_size = sizeof(VkDrawIndirectCommand); |
| 2002 | } else { |
| 2003 | assert(cmd_type == CMD_DRAWINDEXEDINDIRECTCOUNT); |
| 2004 | struct_size = sizeof(VkDrawIndexedIndirectCommand); |
| 2005 | } |
| 2006 | BUFFER_STATE *buffer_state = GetBufferState(cdi_state->buffer); |
| 2007 | uint32_t max_count; |
| 2008 | uint64_t bufsize = buffer_state->createInfo.size; |
| 2009 | uint64_t first_command_bytes = struct_size + cdi_state->offset; |
| 2010 | if (first_command_bytes > bufsize) { |
| 2011 | max_count = 0; |
| 2012 | } else { |
| 2013 | max_count = 1 + static_cast<uint32_t>(std::floor(((bufsize - first_command_bytes) / cdi_state->stride))); |
| 2014 | } |
| 2015 | pre_draw_resources.buf_size = buffer_state->createInfo.size; |
| 2016 | |
| 2017 | assert(phys_dev_props.limits.maxDrawIndirectCount > 0); |
| 2018 | pushConstants[0] = phys_dev_props.limits.maxDrawIndirectCount; |
| 2019 | pushConstants[1] = max_count; |
| 2020 | pushConstants[2] = static_cast<uint32_t>((cdi_state->count_buffer_offset / sizeof(uint32_t))); |
| 2021 | } else { |
| 2022 | pushConstants[0] = 0; // firstInstance check instead of count buffer check |
| 2023 | pushConstants[1] = cdi_state->drawCount; |
| 2024 | if (cmd_type == CMD_DRAWINDIRECT) { |
| 2025 | pushConstants[2] = static_cast<uint32_t>( |
| 2026 | ((cdi_state->offset + offsetof(struct VkDrawIndirectCommand, firstInstance)) / sizeof(uint32_t))); |
| 2027 | } else { |
| 2028 | assert(cmd_type == CMD_DRAWINDEXEDINDIRECT); |
| 2029 | pushConstants[2] = static_cast<uint32_t>( |
| 2030 | ((cdi_state->offset + offsetof(struct VkDrawIndexedIndirectCommand, firstInstance)) / sizeof(uint32_t))); |
| 2031 | } |
| 2032 | pushConstants[3] = (cdi_state->stride / sizeof(uint32_t)); |
| 2033 | } |
Tony-LunarG | f0d4e2b | 2021-04-14 11:52:28 -0600 | [diff] [blame] | 2034 | |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 2035 | // Insert diagnostic draw |
| 2036 | DispatchCmdBindPipeline(cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, validation_pipeline); |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 2037 | DispatchCmdPushConstants(cmd_buffer, pre_draw_validation_state.validation_pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT, 0, |
| 2038 | sizeof(pushConstants), pushConstants); |
| 2039 | DispatchCmdBindDescriptorSets(cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 2040 | pre_draw_validation_state.validation_pipeline_layout, 0, 1, &pre_draw_resources.desc_set, 0, |
| 2041 | nullptr); |
| 2042 | DispatchCmdDraw(cmd_buffer, 3, 1, 0, 0); |
| 2043 | |
| 2044 | // Restore the previous graphics pipeline state. |
| 2045 | restorable_state.Restore(cmd_buffer); |
| 2046 | } |
| 2047 | |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2048 | bool has_buffers = false; |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 2049 | // Figure out how much memory we need for the input block based on how many sets and bindings there are |
| 2050 | // and how big each of the bindings is |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 2051 | if (number_of_sets > 0 && (descriptor_indexing || buffer_oob_enabled)) { |
Tony-LunarG | 81efe39 | 2019-03-07 15:43:27 -0700 | [diff] [blame] | 2052 | uint32_t descriptor_count = 0; // Number of descriptors, including all array elements |
| 2053 | 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] | 2054 | for (const auto &s : state.per_set) { |
Jeff Bolz | b1fc073 | 2019-08-11 20:16:49 -0500 | [diff] [blame] | 2055 | auto desc = s.bound_descriptor_set; |
Tony-LunarG | d9224b1 | 2019-09-11 11:43:04 -0600 | [diff] [blame] | 2056 | if (desc && (desc->GetBindingCount() > 0)) { |
| 2057 | auto bindings = desc->GetLayout()->GetSortedBindingSet(); |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 2058 | binding_count += desc->GetLayout()->GetMaxBinding() + 1; |
| 2059 | for (auto binding : bindings) { |
Tony-LunarG | 7564b38 | 2019-08-21 10:11:35 -0600 | [diff] [blame] | 2060 | // Shader instrumentation is tracking inline uniform blocks as scalers. Don't try to validate inline uniform |
| 2061 | // blocks |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2062 | auto descriptor_type = desc->GetLayout()->GetTypeFromBinding(binding); |
| 2063 | if (descriptor_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) { |
Tony-LunarG | 7564b38 | 2019-08-21 10:11:35 -0600 | [diff] [blame] | 2064 | descriptor_count++; |
Mark Lobodzinski | afa7cb8 | 2020-01-29 16:49:36 -0700 | [diff] [blame] | 2065 | LogWarning(device, "UNASSIGNED-GPU-Assisted Validation Warning", |
| 2066 | "VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT descriptors will not be validated by GPU assisted " |
| 2067 | "validation"); |
Tony-LunarG | 7564b38 | 2019-08-21 10:11:35 -0600 | [diff] [blame] | 2068 | } else if (binding == desc->GetLayout()->GetMaxBinding() && desc->IsVariableDescriptorCount(binding)) { |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 2069 | descriptor_count += desc->GetVariableDescriptorCount(); |
| 2070 | } else { |
| 2071 | descriptor_count += desc->GetDescriptorCountFromBinding(binding); |
| 2072 | } |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2073 | if (!has_buffers && (descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER || |
| 2074 | descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC || |
| 2075 | descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || |
Tony-LunarG | e8632e4 | 2020-11-18 17:03:12 -0700 | [diff] [blame] | 2076 | descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC || |
| 2077 | descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER || |
| 2078 | descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)) { |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2079 | has_buffers = true; |
Tony-LunarG | a77cade | 2019-03-06 10:49:22 -0700 | [diff] [blame] | 2080 | } |
Tony-LunarG | c28e28a | 2020-08-14 10:37:48 -0600 | [diff] [blame] | 2081 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 2082 | } |
| 2083 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 2084 | |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2085 | if (descriptor_indexing || has_buffers) { |
| 2086 | // Note that the size of the input buffer is dependent on the maximum binding number, which |
| 2087 | // can be very large. This is because for (set = s, binding = b, index = i), the validation |
| 2088 | // code is going to dereference Input[ i + Input[ b + Input[ s + Input[ Input[0] ] ] ] ] to |
| 2089 | // see if descriptors have been written. In gpu_validation.md, we note this and advise |
| 2090 | // using densely packed bindings as a best practice when using gpu-av with descriptor indexing |
| 2091 | uint32_t words_needed; |
| 2092 | if (descriptor_indexing) { |
| 2093 | words_needed = 1 + (number_of_sets * 2) + (binding_count * 2) + descriptor_count; |
| 2094 | } else { |
| 2095 | words_needed = 1 + number_of_sets + binding_count + descriptor_count; |
| 2096 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2097 | alloc_info.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; |
| 2098 | buffer_info.size = words_needed * 4; |
| 2099 | result = vmaCreateBuffer(vmaAllocator, &buffer_info, &alloc_info, &di_input_block.buffer, &di_input_block.allocation, |
| 2100 | nullptr); |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2101 | if (result != VK_SUCCESS) { |
| 2102 | ReportSetupProblem(device, "Unable to allocate device memory. Device could become unstable."); |
| 2103 | aborted = true; |
| 2104 | return; |
| 2105 | } |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 2106 | |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2107 | // Populate input buffer first with the sizes of every descriptor in every set, then with whether |
| 2108 | // each element of each descriptor has been written or not. See gpu_validation.md for a more thourough |
| 2109 | // outline of the input buffer format |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2110 | result = vmaMapMemory(vmaAllocator, di_input_block.allocation, reinterpret_cast<void **>(&data_ptr)); |
| 2111 | memset(data_ptr, 0, static_cast<size_t>(buffer_info.size)); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 2112 | |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2113 | // Descriptor indexing needs the number of descriptors at each binding. |
| 2114 | if (descriptor_indexing) { |
| 2115 | // Pointer to a sets array that points into the sizes array |
| 2116 | uint32_t *sets_to_sizes = data_ptr + 1; |
| 2117 | // Pointer to the sizes array that contains the array size of the descriptor at each binding |
| 2118 | uint32_t *sizes = sets_to_sizes + number_of_sets; |
| 2119 | // Pointer to another sets array that points into the bindings array that points into the written array |
| 2120 | uint32_t *sets_to_bindings = sizes + binding_count; |
| 2121 | // Pointer to the bindings array that points at the start of the writes in the writes array for each binding |
| 2122 | uint32_t *bindings_to_written = sets_to_bindings + number_of_sets; |
| 2123 | // Index of the next entry in the written array to be updated |
| 2124 | uint32_t written_index = 1 + (number_of_sets * 2) + (binding_count * 2); |
| 2125 | uint32_t bind_counter = number_of_sets + 1; |
| 2126 | // Index of the start of the sets_to_bindings array |
| 2127 | data_ptr[0] = number_of_sets + binding_count + 1; |
| 2128 | |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 2129 | for (const auto &s : state.per_set) { |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2130 | auto desc = s.bound_descriptor_set; |
| 2131 | if (desc && (desc->GetBindingCount() > 0)) { |
| 2132 | auto layout = desc->GetLayout(); |
| 2133 | auto bindings = layout->GetSortedBindingSet(); |
| 2134 | // For each set, fill in index of its bindings sizes in the sizes array |
| 2135 | *sets_to_sizes++ = bind_counter; |
| 2136 | // For each set, fill in the index of its bindings in the bindings_to_written array |
| 2137 | *sets_to_bindings++ = bind_counter + number_of_sets + binding_count; |
| 2138 | for (auto binding : bindings) { |
| 2139 | // For each binding, fill in its size in the sizes array |
| 2140 | // Shader instrumentation is tracking inline uniform blocks as scalers. Don't try to validate inline |
| 2141 | // uniform blocks |
| 2142 | if (VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT == desc->GetLayout()->GetTypeFromBinding(binding)) { |
| 2143 | sizes[binding] = 1; |
| 2144 | } else if (binding == layout->GetMaxBinding() && desc->IsVariableDescriptorCount(binding)) { |
| 2145 | sizes[binding] = desc->GetVariableDescriptorCount(); |
| 2146 | } else { |
| 2147 | sizes[binding] = desc->GetDescriptorCountFromBinding(binding); |
| 2148 | } |
| 2149 | // Fill in the starting index for this binding in the written array in the bindings_to_written array |
| 2150 | bindings_to_written[binding] = written_index; |
| 2151 | |
| 2152 | // Shader instrumentation is tracking inline uniform blocks as scalers. Don't try to validate inline |
| 2153 | // uniform blocks |
| 2154 | if (VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT == desc->GetLayout()->GetTypeFromBinding(binding)) { |
| 2155 | data_ptr[written_index++] = UINT_MAX; |
| 2156 | continue; |
| 2157 | } |
| 2158 | |
| 2159 | auto index_range = desc->GetGlobalIndexRangeFromBinding(binding, true); |
| 2160 | // For each array element in the binding, update the written array with whether it has been written |
| 2161 | for (uint32_t i = index_range.start; i < index_range.end; ++i) { |
| 2162 | auto *descriptor = desc->GetDescriptorFromGlobalIndex(i); |
| 2163 | if (descriptor->updated) { |
| 2164 | SetDescriptorInitialized(data_ptr, written_index, descriptor); |
| 2165 | } else if (desc->IsUpdateAfterBind(binding)) { |
| 2166 | // If it hasn't been written now and it's update after bind, put it in a list to check at |
| 2167 | // QueueSubmit |
| 2168 | di_input_block.update_at_submit[written_index] = descriptor; |
| 2169 | } |
| 2170 | written_index++; |
| 2171 | } |
| 2172 | } |
| 2173 | auto last = desc->GetLayout()->GetMaxBinding(); |
| 2174 | bindings_to_written += last + 1; |
| 2175 | bind_counter += last + 1; |
| 2176 | sizes += last + 1; |
| 2177 | } else { |
| 2178 | *sets_to_sizes++ = 0; |
| 2179 | *sets_to_bindings++ = 0; |
| 2180 | } |
| 2181 | } |
| 2182 | } else { |
| 2183 | // If no descriptor indexing, we don't need number of descriptors at each binding, so |
| 2184 | // no sets_to_sizes or sizes arrays, just sets_to_bindings, bindings_to_written and written_index |
| 2185 | |
| 2186 | // Pointer to sets array that points into the bindings array that points into the written array |
| 2187 | uint32_t *sets_to_bindings = data_ptr + 1; |
| 2188 | // Pointer to the bindings array that points at the start of the writes in the writes array for each binding |
| 2189 | uint32_t *bindings_to_written = sets_to_bindings + number_of_sets; |
| 2190 | // Index of the next entry in the written array to be updated |
| 2191 | uint32_t written_index = 1 + number_of_sets + binding_count; |
| 2192 | uint32_t bind_counter = number_of_sets + 1; |
| 2193 | data_ptr[0] = 1; |
| 2194 | |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 2195 | for (const auto &s : state.per_set) { |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2196 | auto desc = s.bound_descriptor_set; |
| 2197 | if (desc && (desc->GetBindingCount() > 0)) { |
| 2198 | auto layout = desc->GetLayout(); |
| 2199 | auto bindings = layout->GetSortedBindingSet(); |
| 2200 | *sets_to_bindings++ = bind_counter; |
| 2201 | for (auto binding : bindings) { |
| 2202 | // Fill in the starting index for this binding in the written array in the bindings_to_written array |
| 2203 | bindings_to_written[binding] = written_index; |
| 2204 | |
| 2205 | // Shader instrumentation is tracking inline uniform blocks as scalers. Don't try to validate inline |
| 2206 | // uniform blocks |
| 2207 | if (VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT == desc->GetLayout()->GetTypeFromBinding(binding)) { |
| 2208 | data_ptr[written_index++] = UINT_MAX; |
| 2209 | continue; |
| 2210 | } |
| 2211 | |
| 2212 | auto index_range = desc->GetGlobalIndexRangeFromBinding(binding, true); |
| 2213 | |
| 2214 | // For each array element in the binding, update the written array with whether it has been written |
| 2215 | for (uint32_t i = index_range.start; i < index_range.end; ++i) { |
| 2216 | auto *descriptor = desc->GetDescriptorFromGlobalIndex(i); |
| 2217 | if (descriptor->updated) { |
| 2218 | SetDescriptorInitialized(data_ptr, written_index, descriptor); |
| 2219 | } else if (desc->IsUpdateAfterBind(binding)) { |
| 2220 | // If it hasn't been written now and it's update after bind, put it in a list to check at |
| 2221 | // QueueSubmit |
| 2222 | di_input_block.update_at_submit[written_index] = descriptor; |
| 2223 | } |
| 2224 | written_index++; |
| 2225 | } |
| 2226 | } |
| 2227 | auto last = desc->GetLayout()->GetMaxBinding(); |
| 2228 | bindings_to_written += last + 1; |
| 2229 | bind_counter += last + 1; |
| 2230 | } else { |
| 2231 | *sets_to_bindings++ = 0; |
| 2232 | } |
| 2233 | } |
| 2234 | } |
| 2235 | vmaUnmapMemory(vmaAllocator, di_input_block.allocation); |
| 2236 | |
| 2237 | di_input_desc_buffer_info.range = (words_needed * 4); |
| 2238 | di_input_desc_buffer_info.buffer = di_input_block.buffer; |
| 2239 | di_input_desc_buffer_info.offset = 0; |
| 2240 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 2241 | desc_writes[1] = LvlInitStruct<VkWriteDescriptorSet>(); |
Tony-LunarG | e29097a | 2020-12-03 10:59:19 -0700 | [diff] [blame] | 2242 | desc_writes[1].dstBinding = 1; |
| 2243 | desc_writes[1].descriptorCount = 1; |
| 2244 | desc_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 2245 | desc_writes[1].pBufferInfo = &di_input_desc_buffer_info; |
| 2246 | desc_writes[1].dstSet = desc_sets[0]; |
| 2247 | |
| 2248 | desc_count = 2; |
| 2249 | } |
Tony-LunarG | 0e56472 | 2019-03-19 16:09:14 -0600 | [diff] [blame] | 2250 | } |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 2251 | |
Tony-LunarG | c111b24 | 2020-06-30 14:43:45 -0600 | [diff] [blame] | 2252 | if ((device_extensions.vk_ext_buffer_device_address || device_extensions.vk_khr_buffer_device_address) && buffer_map.size() && |
Tony-LunarG | 5c38b18 | 2020-06-10 16:15:32 -0600 | [diff] [blame] | 2253 | shaderInt64 && enabled_features.core12.bufferDeviceAddress) { |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 2254 | // Example BDA input buffer assuming 2 buffers using BDA: |
| 2255 | // Word 0 | Index of start of buffer sizes (in this case 5) |
| 2256 | // Word 1 | 0x0000000000000000 |
| 2257 | // Word 2 | Device Address of first buffer (Addresses sorted in ascending order) |
| 2258 | // Word 3 | Device Address of second buffer |
| 2259 | // Word 4 | 0xffffffffffffffff |
| 2260 | // Word 5 | 0 (size of pretend buffer at word 1) |
| 2261 | // Word 6 | Size in bytes of first buffer |
| 2262 | // Word 7 | Size in bytes of second buffer |
| 2263 | // Word 8 | 0 (size of pretend buffer in word 4) |
| 2264 | |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 2265 | uint32_t num_buffers = static_cast<uint32_t>(buffer_map.size()); |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 2266 | uint32_t words_needed = (num_buffers + 3) + (num_buffers + 2); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2267 | alloc_info.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; |
| 2268 | buffer_info.size = words_needed * 8; // 64 bit words |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 2269 | result = |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2270 | vmaCreateBuffer(vmaAllocator, &buffer_info, &alloc_info, &bda_input_block.buffer, &bda_input_block.allocation, nullptr); |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 2271 | if (result != VK_SUCCESS) { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 2272 | ReportSetupProblem(device, "Unable to allocate device memory. Device could become unstable."); |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 2273 | aborted = true; |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 2274 | return; |
| 2275 | } |
| 2276 | uint64_t *bda_data; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2277 | result = vmaMapMemory(vmaAllocator, bda_input_block.allocation, reinterpret_cast<void **>(&bda_data)); |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 2278 | uint32_t address_index = 1; |
| 2279 | uint32_t size_index = 3 + num_buffers; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2280 | memset(bda_data, 0, static_cast<size_t>(buffer_info.size)); |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 2281 | bda_data[0] = size_index; // Start of buffer sizes |
| 2282 | bda_data[address_index++] = 0; // NULL address |
| 2283 | bda_data[size_index++] = 0; |
| 2284 | |
John Zulauf | 79f0658 | 2021-02-27 18:38:39 -0700 | [diff] [blame] | 2285 | for (const auto &value : buffer_map) { |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 2286 | bda_data[address_index++] = value.first; |
| 2287 | bda_data[size_index++] = value.second; |
| 2288 | } |
| 2289 | bda_data[address_index] = UINTPTR_MAX; |
| 2290 | bda_data[size_index] = 0; |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 2291 | vmaUnmapMemory(vmaAllocator, bda_input_block.allocation); |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 2292 | |
| 2293 | bda_input_desc_buffer_info.range = (words_needed * 8); |
| 2294 | bda_input_desc_buffer_info.buffer = bda_input_block.buffer; |
| 2295 | bda_input_desc_buffer_info.offset = 0; |
| 2296 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 2297 | desc_writes[desc_count] = LvlInitStruct<VkWriteDescriptorSet>(); |
Tony-LunarG | 8eb5a00 | 2019-07-25 16:49:00 -0600 | [diff] [blame] | 2298 | desc_writes[desc_count].dstBinding = 2; |
| 2299 | desc_writes[desc_count].descriptorCount = 1; |
| 2300 | desc_writes[desc_count].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 2301 | desc_writes[desc_count].pBufferInfo = &bda_input_desc_buffer_info; |
| 2302 | desc_writes[desc_count].dstSet = desc_sets[0]; |
| 2303 | desc_count++; |
| 2304 | } |
| 2305 | |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 2306 | // Write the descriptor |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 2307 | output_desc_buffer_info.buffer = output_block.buffer; |
| 2308 | output_desc_buffer_info.offset = 0; |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 2309 | |
Nathaniel Cesario | fc6291e | 2021-04-06 00:22:15 -0600 | [diff] [blame] | 2310 | desc_writes[0] = LvlInitStruct<VkWriteDescriptorSet>(); |
Tony-LunarG | 1b2e0c3 | 2019-02-07 17:13:27 -0700 | [diff] [blame] | 2311 | desc_writes[0].descriptorCount = 1; |
| 2312 | desc_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 2313 | desc_writes[0].pBufferInfo = &output_desc_buffer_info; |
| 2314 | desc_writes[0].dstSet = desc_sets[0]; |
| 2315 | DispatchUpdateDescriptorSets(device, desc_count, desc_writes, 0, NULL); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 2316 | |
locke-lunarg | b8d7a7a | 2020-10-25 16:01:52 -0600 | [diff] [blame] | 2317 | const auto *pipeline_state = state.pipeline_state; |
| 2318 | if (pipeline_state) { |
| 2319 | if ((pipeline_state->pipeline_layout->set_layouts.size() <= desc_set_bind_index) && |
Jeremy Gebben | 9efe1cf | 2021-05-15 20:05:09 -0600 | [diff] [blame] | 2320 | !pipeline_state->pipeline_layout->Destroyed()) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2321 | DispatchCmdBindDescriptorSets(cmd_buffer, bind_point, pipeline_state->pipeline_layout->layout(), desc_set_bind_index, 1, |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 2322 | desc_sets.data(), 0, nullptr); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 2323 | } |
Jeremy Gebben | 9efe1cf | 2021-05-15 20:05:09 -0600 | [diff] [blame] | 2324 | if (pipeline_state->pipeline_layout->Destroyed()) { |
Tony-LunarG | 9de6e5f | 2020-06-22 13:02:48 -0600 | [diff] [blame] | 2325 | ReportSetupProblem(device, "Pipeline layout has been destroyed, aborting GPU-AV"); |
| 2326 | aborted = true; |
| 2327 | } else { |
| 2328 | // Record buffer and memory info in CB state tracking |
| 2329 | GetBufferInfo(cmd_buffer) |
Tony-LunarG | a3ec16c | 2021-04-06 12:19:57 -0600 | [diff] [blame] | 2330 | .emplace_back(output_block, di_input_block, bda_input_block, pre_draw_resources, desc_sets[0], desc_pool, |
| 2331 | bind_point, cmd_type); |
Tony-LunarG | 9de6e5f | 2020-06-22 13:02:48 -0600 | [diff] [blame] | 2332 | } |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 2333 | } else { |
Mark Lobodzinski | a8151b0 | 2020-02-27 13:38:08 -0700 | [diff] [blame] | 2334 | ReportSetupProblem(device, "Unable to find pipeline state"); |
Tony-LunarG | 9de6e5f | 2020-06-22 13:02:48 -0600 | [diff] [blame] | 2335 | aborted = true; |
| 2336 | } |
| 2337 | if (aborted) { |
Tony-LunarG | 99b880b | 2019-09-26 11:19:52 -0600 | [diff] [blame] | 2338 | vmaDestroyBuffer(vmaAllocator, di_input_block.buffer, di_input_block.allocation); |
| 2339 | vmaDestroyBuffer(vmaAllocator, bda_input_block.buffer, bda_input_block.allocation); |
| 2340 | vmaDestroyBuffer(vmaAllocator, output_block.buffer, output_block.allocation); |
Tony-LunarG | b2501d2 | 2019-01-28 09:59:13 -0700 | [diff] [blame] | 2341 | return; |
| 2342 | } |
| 2343 | } |