blob: e73b5e50a2f87caffd5bf4b52160507c17b75e02 [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:
96 bool aborted = false;
97 VkDevice device;
98 VkPhysicalDevice physicalDevice;
99 uint32_t adjusted_max_desc_sets;
100 uint32_t desc_set_bind_index;
101 VkDescriptorSetLayout debug_desc_layout;
102 VkDescriptorSetLayout dummy_desc_layout;
Tony-LunarGb5fae462020-03-05 12:43:25 -0700103 std::unique_ptr<UtilDescriptorSetManager> desc_set_manager;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600104 std::unordered_map<uint32_t, GpuAssistedShaderTracker> shader_map;
105 PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData;
106 VmaAllocator vmaAllocator = {};
Tony-LunarGb5fae462020-03-05 12:43:25 -0700107 std::map<VkQueue, UtilQueueBarrierCommandInfo> queue_barrier_command_infos;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600108 std::vector<GpuAssistedBufferInfo>& GetBufferInfo(const VkCommandBuffer command_buffer) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600109 auto buffer_list = command_buffer_map.find(command_buffer);
110 if (buffer_list == command_buffer_map.end()) {
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600111 std::vector<GpuAssistedBufferInfo> new_list{};
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600112 command_buffer_map[command_buffer] = new_list;
113 return command_buffer_map[command_buffer];
114 }
115 return buffer_list->second;
116 }
Mark Lobodzinskia8151b02020-02-27 13:38:08 -0700117
Tony-LunarG1dce2392019-10-23 16:49:29 -0600118 public:
Mark Lobodzinskia8151b02020-02-27 13:38:08 -0700119 template <typename T>
120 void ReportSetupProblem(T object, const char* const specific_message) const;
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600121 void PreCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
122 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice,
123 safe_VkDeviceCreateInfo* modified_create_info);
124 void PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
125 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice, VkResult result);
Tony-LunarG7e0842f2019-12-10 09:26:34 -0700126 void PostCallRecordGetBufferDeviceAddressKHR(VkDevice device, const VkBufferDeviceAddressInfoEXT* pInfo,
127 VkDeviceAddress address);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600128 void PostCallRecordGetBufferDeviceAddressEXT(VkDevice device, const VkBufferDeviceAddressInfoEXT* pInfo,
129 VkDeviceAddress address);
130 void PreCallRecordDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks* pAllocator);
131 void PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator);
132 void PostCallRecordBindAccelerationStructureMemoryNV(VkDevice device, uint32_t bindInfoCount,
133 const VkBindAccelerationStructureMemoryInfoNV* pBindInfos,
134 VkResult result);
135 void PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
136 const VkAllocationCallbacks* pAllocator, VkPipelineLayout* pPipelineLayout,
137 void* cpl_state_data);
138 void PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
139 const VkAllocationCallbacks* pAllocator, VkPipelineLayout* pPipelineLayout,
140 VkResult result);
141 void ResetCommandBuffer(VkCommandBuffer commandBuffer);
142 bool PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
143 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
144 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
145 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500146 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers) const;
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600147 void PreCallRecordCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
148 VkBuffer* pBuffer, void* cb_state_data);
149 void CreateAccelerationStructureBuildValidationState(GpuAssisted* device_GpuAssisted);
150 void DestroyAccelerationStructureBuildValidationState();
151 void PreCallRecordCmdBuildAccelerationStructureNV(VkCommandBuffer commandBuffer, const VkAccelerationStructureInfoNV* pInfo,
152 VkBuffer instanceData, VkDeviceSize instanceOffset, VkBool32 update,
153 VkAccelerationStructureNV dst, VkAccelerationStructureNV src,
154 VkBuffer scratch, VkDeviceSize scratchOffset);
155 void ProcessAccelerationStructureBuildValidationBuffer(VkQueue queue, CMD_BUFFER_STATE* cb_node);
156 void PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
157 const VkGraphicsPipelineCreateInfo* pCreateInfos,
158 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
159 void* cgpl_state_data);
160 void PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
161 const VkComputePipelineCreateInfo* pCreateInfos,
162 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
163 void* ccpl_state_data);
164 void PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
165 const VkRayTracingPipelineCreateInfoNV* pCreateInfos,
166 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
167 void* crtpl_state_data);
Jeff Bolz443c2ca2020-03-19 12:11:51 -0500168 void PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
169 const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
170 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
171 void* crtpl_state_data);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600172 void PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
173 const VkGraphicsPipelineCreateInfo* pCreateInfos,
174 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, VkResult result,
175 void* cgpl_state_data);
176 void PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
177 const VkComputePipelineCreateInfo* pCreateInfos,
178 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, VkResult result,
179 void* ccpl_state_data);
180 void PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
181 const VkRayTracingPipelineCreateInfoNV* pCreateInfos,
182 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, VkResult result,
183 void* crtpl_state_data);
Jeff Bolz443c2ca2020-03-19 12:11:51 -0500184 void PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
185 const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
186 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
187 VkResult result, void* crtpl_state_data);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600188 void PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks* pAllocator);
189 bool InstrumentShader(const VkShaderModuleCreateInfo* pCreateInfo, std::vector<unsigned int>& new_pgm,
190 uint32_t* unique_shader_id);
191 void PreCallRecordCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo* pCreateInfo,
192 const VkAllocationCallbacks* pAllocator, VkShaderModule* pShaderModule,
193 void* csm_state_data);
Tony-LunarG1dce2392019-10-23 16:49:29 -0600194 void AnalyzeAndGenerateMessages(VkCommandBuffer command_buffer, VkQueue queue, VkPipelineBindPoint pipeline_bind_point,
195 uint32_t operation_index, uint32_t* const debug_output_buffer);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600196 void UpdateInstrumentationBuffer(CMD_BUFFER_STATE* cb_node);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600197 void PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence);
198 void PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence,
199 VkResult result);
200 void PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex,
201 uint32_t firstInstance);
202 void PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
203 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance);
204 void PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count,
205 uint32_t stride);
206 void PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count,
207 uint32_t stride);
208 void PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z);
209 void PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset);
210 void PreCallRecordCmdTraceRaysNV(VkCommandBuffer commandBuffer, VkBuffer raygenShaderBindingTableBuffer,
211 VkDeviceSize raygenShaderBindingOffset, VkBuffer missShaderBindingTableBuffer,
212 VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride,
213 VkBuffer hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset,
214 VkDeviceSize hitShaderBindingStride, VkBuffer callableShaderBindingTableBuffer,
215 VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride,
216 uint32_t width, uint32_t height, uint32_t depth);
217 void PostCallRecordCmdTraceRaysNV(VkCommandBuffer commandBuffer, VkBuffer raygenShaderBindingTableBuffer,
218 VkDeviceSize raygenShaderBindingOffset, VkBuffer missShaderBindingTableBuffer,
219 VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride,
220 VkBuffer hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset,
221 VkDeviceSize hitShaderBindingStride, VkBuffer callableShaderBindingTableBuffer,
222 VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride,
223 uint32_t width, uint32_t height, uint32_t depth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -0500224 void PreCallRecordCmdTraceRaysKHR(VkCommandBuffer commandBuffer, const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
225 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
226 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
227 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, uint32_t width, uint32_t height,
228 uint32_t depth);
229 void PostCallRecordCmdTraceRaysKHR(VkCommandBuffer commandBuffer, const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
230 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
231 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
232 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, uint32_t width, uint32_t height,
233 uint32_t depth);
234 void PreCallRecordCmdTraceRaysIndirectKHR(VkCommandBuffer commandBuffer,
235 const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
236 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
237 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
238 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, VkBuffer buffer,
239 VkDeviceSize offset);
240 void PostCallRecordCmdTraceRaysIndirectKHR(VkCommandBuffer commandBuffer,
241 const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
242 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
243 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
244 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, VkBuffer buffer,
245 VkDeviceSize offset);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600246 void AllocateValidationResources(const VkCommandBuffer cmd_buffer, const VkPipelineBindPoint bind_point);
247 void PostCallRecordGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
248 VkPhysicalDeviceProperties* pPhysicalDeviceProperties);
249 void PostCallRecordGetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
250 VkPhysicalDeviceProperties2* pPhysicalDeviceProperties2);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600251};