blob: 7b1c488f027308b647b04050923ca5d5e5b60280 [file] [log] [blame]
Mark Lobodzinskia8151b02020-02-27 13:38:08 -07001/* Copyright (c) 2018-2020 The Khronos Group Inc.
2 * Copyright (c) 2018-2020 Valve Corporation
3 * Copyright (c) 2018-2020 LunarG, Inc.
Karl Schultz7b024b42018-08-30 16:18:18 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060017 * Author: Karl Schultz <karl@lunarg.com>
18 * Author: Tony Barbour <tony@lunarg.com>
Karl Schultz7b024b42018-08-30 16:18:18 -060019 */
20
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060021#pragma once
22
23#include "chassis.h"
24#include "state_tracker.h"
Tony-LunarG0e564722019-03-19 16:09:14 -060025#include "vk_mem_alloc.h"
Tony-LunarGb5fae462020-03-05 12:43:25 -070026#include "gpu_utils.h"
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060027class GpuAssisted;
Tony-LunarG0e564722019-03-19 16:09:14 -060028
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060029struct GpuAssistedDeviceMemoryBlock {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -060030 VkBuffer buffer;
Tony-LunarG0e564722019-03-19 16:09:14 -060031 VmaAllocation allocation;
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060032 std::unordered_map<uint32_t, const cvdescriptorset::Descriptor*> update_at_submit;
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -060033};
34
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060035struct GpuAssistedBufferInfo {
36 GpuAssistedDeviceMemoryBlock output_mem_block;
37 GpuAssistedDeviceMemoryBlock di_input_mem_block; // Descriptor Indexing input
38 GpuAssistedDeviceMemoryBlock bda_input_mem_block; // Buffer Device Address input
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -060039 VkDescriptorSet desc_set;
40 VkDescriptorPool desc_pool;
Jason Macnak67407e72019-07-11 11:05:09 -070041 VkPipelineBindPoint pipeline_bind_point;
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060042 GpuAssistedBufferInfo(GpuAssistedDeviceMemoryBlock output_mem_block, GpuAssistedDeviceMemoryBlock di_input_mem_block,
43 GpuAssistedDeviceMemoryBlock bda_input_mem_block, VkDescriptorSet desc_set, VkDescriptorPool desc_pool,
44 VkPipelineBindPoint pipeline_bind_point)
Jason Macnak67407e72019-07-11 11:05:09 -070045 : output_mem_block(output_mem_block),
Tony-LunarG8eb5a002019-07-25 16:49:00 -060046 di_input_mem_block(di_input_mem_block),
47 bda_input_mem_block(bda_input_mem_block),
Jason Macnak67407e72019-07-11 11:05:09 -070048 desc_set(desc_set),
49 desc_pool(desc_pool),
50 pipeline_bind_point(pipeline_bind_point){};
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -060051};
52
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060053struct GpuAssistedShaderTracker {
54 VkPipeline pipeline;
55 VkShaderModule shader_module;
56 std::vector<unsigned int> pgm;
57};
58
59struct GpuAssistedAccelerationStructureBuildValidationBufferInfo {
Jason Macnak83cfd582019-07-31 10:14:24 -070060 // The acceleration structure that is being built.
61 VkAccelerationStructureNV acceleration_structure = VK_NULL_HANDLE;
62
63 // The descriptor pool and descriptor set being used to validate a given build.
64 VkDescriptorPool descriptor_pool = VK_NULL_HANDLE;
65 VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
66
67 // The storage buffer used by the validating compute shader whichcontains info about
68 // the valid handles and which is written to communicate found invalid handles.
69 VkBuffer validation_buffer = VK_NULL_HANDLE;
70 VmaAllocation validation_buffer_allocation = VK_NULL_HANDLE;
71};
72
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060073struct GpuAssistedAccelerationStructureBuildValidationState {
Jason Macnak83cfd582019-07-31 10:14:24 -070074 bool initialized = false;
75
76 VkPipeline pipeline = VK_NULL_HANDLE;
77 VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
78
79 VkAccelerationStructureNV replacement_as = VK_NULL_HANDLE;
80 VmaAllocation replacement_as_allocation = VK_NULL_HANDLE;
81 uint64_t replacement_as_handle = 0;
82
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060083 std::unordered_map<VkCommandBuffer, std::vector<GpuAssistedAccelerationStructureBuildValidationBufferInfo>> validation_buffers;
Jason Macnak83cfd582019-07-31 10:14:24 -070084};
85
Tony-LunarG99b880b2019-09-26 11:19:52 -060086class GpuAssisted : public ValidationStateTracker {
Tony-LunarG1dce2392019-10-23 16:49:29 -060087 VkPhysicalDeviceFeatures supported_features;
Tony-LunarG8eb5a002019-07-25 16:49:00 -060088 VkBool32 shaderInt64;
Tony-LunarG99b880b2019-09-26 11:19:52 -060089 uint32_t unique_shader_module_id = 0;
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060090 std::unordered_map<VkCommandBuffer, std::vector<GpuAssistedBufferInfo>> command_buffer_map; // gpu_buffer_list;
Tony-LunarG0e564722019-03-19 16:09:14 -060091 uint32_t output_buffer_size;
Tony-LunarG8eb5a002019-07-25 16:49:00 -060092 std::map<VkDeviceAddress, VkDeviceSize> buffer_map;
Tony-LunarG2ba1cb32019-09-25 15:16:11 -060093 GpuAssistedAccelerationStructureBuildValidationState acceleration_structure_validation_state;
Tony-LunarG1dce2392019-10-23 16:49:29 -060094
95 public:
Mark Lobodzinski0dcb87d2020-03-30 16:09:24 -060096 GpuAssisted() { container_type = LayerObjectTypeGpuAssisted; }
97
Tony-LunarG1dce2392019-10-23 16:49:29 -060098 bool aborted = false;
99 VkDevice device;
100 VkPhysicalDevice physicalDevice;
101 uint32_t adjusted_max_desc_sets;
102 uint32_t desc_set_bind_index;
103 VkDescriptorSetLayout debug_desc_layout;
104 VkDescriptorSetLayout dummy_desc_layout;
Tony-LunarGb5fae462020-03-05 12:43:25 -0700105 std::unique_ptr<UtilDescriptorSetManager> desc_set_manager;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600106 std::unordered_map<uint32_t, GpuAssistedShaderTracker> shader_map;
107 PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData;
108 VmaAllocator vmaAllocator = {};
Tony-LunarGb5fae462020-03-05 12:43:25 -0700109 std::map<VkQueue, UtilQueueBarrierCommandInfo> queue_barrier_command_infos;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600110 std::vector<GpuAssistedBufferInfo>& GetBufferInfo(const VkCommandBuffer command_buffer) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600111 auto buffer_list = command_buffer_map.find(command_buffer);
112 if (buffer_list == command_buffer_map.end()) {
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600113 std::vector<GpuAssistedBufferInfo> new_list{};
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600114 command_buffer_map[command_buffer] = new_list;
115 return command_buffer_map[command_buffer];
116 }
117 return buffer_list->second;
118 }
Mark Lobodzinskia8151b02020-02-27 13:38:08 -0700119
Tony-LunarG1dce2392019-10-23 16:49:29 -0600120 public:
Mark Lobodzinskia8151b02020-02-27 13:38:08 -0700121 template <typename T>
122 void ReportSetupProblem(T object, const char* const specific_message) const;
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600123 void PreCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
124 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice,
125 safe_VkDeviceCreateInfo* modified_create_info);
126 void PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
127 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice, VkResult result);
Tony-LunarG7e0842f2019-12-10 09:26:34 -0700128 void PostCallRecordGetBufferDeviceAddressKHR(VkDevice device, const VkBufferDeviceAddressInfoEXT* pInfo,
129 VkDeviceAddress address);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600130 void PostCallRecordGetBufferDeviceAddressEXT(VkDevice device, const VkBufferDeviceAddressInfoEXT* pInfo,
131 VkDeviceAddress address);
132 void PreCallRecordDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks* pAllocator);
133 void PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator);
134 void PostCallRecordBindAccelerationStructureMemoryNV(VkDevice device, uint32_t bindInfoCount,
135 const VkBindAccelerationStructureMemoryInfoNV* pBindInfos,
136 VkResult result);
137 void PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
138 const VkAllocationCallbacks* pAllocator, VkPipelineLayout* pPipelineLayout,
139 void* cpl_state_data);
140 void PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
141 const VkAllocationCallbacks* pAllocator, VkPipelineLayout* pPipelineLayout,
142 VkResult result);
143 void ResetCommandBuffer(VkCommandBuffer commandBuffer);
144 bool PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
145 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
146 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
147 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500148 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers) const;
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600149 void PreCallRecordCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
150 VkBuffer* pBuffer, void* cb_state_data);
151 void CreateAccelerationStructureBuildValidationState(GpuAssisted* device_GpuAssisted);
152 void DestroyAccelerationStructureBuildValidationState();
153 void PreCallRecordCmdBuildAccelerationStructureNV(VkCommandBuffer commandBuffer, const VkAccelerationStructureInfoNV* pInfo,
154 VkBuffer instanceData, VkDeviceSize instanceOffset, VkBool32 update,
155 VkAccelerationStructureNV dst, VkAccelerationStructureNV src,
156 VkBuffer scratch, VkDeviceSize scratchOffset);
157 void ProcessAccelerationStructureBuildValidationBuffer(VkQueue queue, CMD_BUFFER_STATE* cb_node);
158 void PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
159 const VkGraphicsPipelineCreateInfo* pCreateInfos,
160 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
161 void* cgpl_state_data);
162 void PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
163 const VkComputePipelineCreateInfo* pCreateInfos,
164 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
165 void* ccpl_state_data);
166 void PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
167 const VkRayTracingPipelineCreateInfoNV* pCreateInfos,
168 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
169 void* crtpl_state_data);
Jeff Bolz443c2ca2020-03-19 12:11:51 -0500170 void PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
171 const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
172 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
173 void* crtpl_state_data);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600174 void PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
175 const VkGraphicsPipelineCreateInfo* pCreateInfos,
176 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, VkResult result,
177 void* cgpl_state_data);
178 void PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
179 const VkComputePipelineCreateInfo* pCreateInfos,
180 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, VkResult result,
181 void* ccpl_state_data);
182 void PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
183 const VkRayTracingPipelineCreateInfoNV* pCreateInfos,
184 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, VkResult result,
185 void* crtpl_state_data);
Jeff Bolz443c2ca2020-03-19 12:11:51 -0500186 void PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
187 const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
188 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
189 VkResult result, void* crtpl_state_data);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600190 void PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks* pAllocator);
191 bool InstrumentShader(const VkShaderModuleCreateInfo* pCreateInfo, std::vector<unsigned int>& new_pgm,
192 uint32_t* unique_shader_id);
193 void PreCallRecordCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo* pCreateInfo,
194 const VkAllocationCallbacks* pAllocator, VkShaderModule* pShaderModule,
195 void* csm_state_data);
Tony-LunarG1dce2392019-10-23 16:49:29 -0600196 void AnalyzeAndGenerateMessages(VkCommandBuffer command_buffer, VkQueue queue, VkPipelineBindPoint pipeline_bind_point,
197 uint32_t operation_index, uint32_t* const debug_output_buffer);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600198 void UpdateInstrumentationBuffer(CMD_BUFFER_STATE* cb_node);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600199 void PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence);
200 void PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence,
201 VkResult result);
202 void PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex,
203 uint32_t firstInstance);
204 void PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
205 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance);
206 void PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count,
207 uint32_t stride);
208 void PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count,
209 uint32_t stride);
210 void PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z);
211 void PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset);
212 void PreCallRecordCmdTraceRaysNV(VkCommandBuffer commandBuffer, VkBuffer raygenShaderBindingTableBuffer,
213 VkDeviceSize raygenShaderBindingOffset, VkBuffer missShaderBindingTableBuffer,
214 VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride,
215 VkBuffer hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset,
216 VkDeviceSize hitShaderBindingStride, VkBuffer callableShaderBindingTableBuffer,
217 VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride,
218 uint32_t width, uint32_t height, uint32_t depth);
219 void PostCallRecordCmdTraceRaysNV(VkCommandBuffer commandBuffer, VkBuffer raygenShaderBindingTableBuffer,
220 VkDeviceSize raygenShaderBindingOffset, VkBuffer missShaderBindingTableBuffer,
221 VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride,
222 VkBuffer hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset,
223 VkDeviceSize hitShaderBindingStride, VkBuffer callableShaderBindingTableBuffer,
224 VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride,
225 uint32_t width, uint32_t height, uint32_t depth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -0500226 void PreCallRecordCmdTraceRaysKHR(VkCommandBuffer commandBuffer, const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
227 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
228 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
229 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, uint32_t width, uint32_t height,
230 uint32_t depth);
231 void PostCallRecordCmdTraceRaysKHR(VkCommandBuffer commandBuffer, const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
232 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
233 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
234 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, uint32_t width, uint32_t height,
235 uint32_t depth);
236 void PreCallRecordCmdTraceRaysIndirectKHR(VkCommandBuffer commandBuffer,
237 const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
238 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
239 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
240 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, VkBuffer buffer,
241 VkDeviceSize offset);
242 void PostCallRecordCmdTraceRaysIndirectKHR(VkCommandBuffer commandBuffer,
243 const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
244 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
245 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
246 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, VkBuffer buffer,
247 VkDeviceSize offset);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600248 void AllocateValidationResources(const VkCommandBuffer cmd_buffer, const VkPipelineBindPoint bind_point);
249 void PostCallRecordGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
250 VkPhysicalDeviceProperties* pPhysicalDeviceProperties);
251 void PostCallRecordGetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
252 VkPhysicalDeviceProperties2* pPhysicalDeviceProperties2);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600253};