blob: e3a6fd01d00edc81960c4600eabe61bcff350591 [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;
Tony-LunarG5c38b182020-06-10 16:15:32 -060099 bool descriptor_indexing = false;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600100 VkDevice device;
101 VkPhysicalDevice physicalDevice;
102 uint32_t adjusted_max_desc_sets;
103 uint32_t desc_set_bind_index;
Tony-LunarG57ada962020-05-01 16:21:00 -0600104 VkDescriptorSetLayout debug_desc_layout = VK_NULL_HANDLE;
105 VkDescriptorSetLayout dummy_desc_layout = VK_NULL_HANDLE;
Tony-LunarGb5fae462020-03-05 12:43:25 -0700106 std::unique_ptr<UtilDescriptorSetManager> desc_set_manager;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600107 std::unordered_map<uint32_t, GpuAssistedShaderTracker> shader_map;
108 PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData;
109 VmaAllocator vmaAllocator = {};
Tony-LunarGb5fae462020-03-05 12:43:25 -0700110 std::map<VkQueue, UtilQueueBarrierCommandInfo> queue_barrier_command_infos;
Tony-LunarG1dce2392019-10-23 16:49:29 -0600111 std::vector<GpuAssistedBufferInfo>& GetBufferInfo(const VkCommandBuffer command_buffer) {
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600112 auto buffer_list = command_buffer_map.find(command_buffer);
113 if (buffer_list == command_buffer_map.end()) {
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600114 std::vector<GpuAssistedBufferInfo> new_list{};
Mark Lobodzinski2a3ee4a2019-03-13 13:11:39 -0600115 command_buffer_map[command_buffer] = new_list;
116 return command_buffer_map[command_buffer];
117 }
118 return buffer_list->second;
119 }
Mark Lobodzinskia8151b02020-02-27 13:38:08 -0700120
Tony-LunarG1dce2392019-10-23 16:49:29 -0600121 public:
Mark Lobodzinskia8151b02020-02-27 13:38:08 -0700122 template <typename T>
123 void ReportSetupProblem(T object, const char* const specific_message) const;
Tony-LunarG5c38b182020-06-10 16:15:32 -0600124 bool CheckForDescriptorIndexing(DeviceFeatures enabled_features) const;
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600125 void PreCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
126 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice,
127 safe_VkDeviceCreateInfo* modified_create_info);
128 void PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
129 const VkAllocationCallbacks* pAllocator, VkDevice* pDevice, VkResult result);
Tony-LunarG588c7052020-04-23 10:47:21 -0600130 void PostCallRecordGetBufferDeviceAddress(VkDevice device, const VkBufferDeviceAddressInfoEXT* pInfo, VkDeviceAddress address);
Tony-LunarG7e0842f2019-12-10 09:26:34 -0700131 void PostCallRecordGetBufferDeviceAddressKHR(VkDevice device, const VkBufferDeviceAddressInfoEXT* pInfo,
132 VkDeviceAddress address);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600133 void PostCallRecordGetBufferDeviceAddressEXT(VkDevice device, const VkBufferDeviceAddressInfoEXT* pInfo,
134 VkDeviceAddress address);
135 void PreCallRecordDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks* pAllocator);
136 void PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator);
137 void PostCallRecordBindAccelerationStructureMemoryNV(VkDevice device, uint32_t bindInfoCount,
138 const VkBindAccelerationStructureMemoryInfoNV* pBindInfos,
139 VkResult result);
140 void PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
141 const VkAllocationCallbacks* pAllocator, VkPipelineLayout* pPipelineLayout,
142 void* cpl_state_data);
143 void PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo* pCreateInfo,
144 const VkAllocationCallbacks* pAllocator, VkPipelineLayout* pPipelineLayout,
145 VkResult result);
146 void ResetCommandBuffer(VkCommandBuffer commandBuffer);
147 bool PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
148 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
149 uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
150 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier* pBufferMemoryBarriers,
Jeff Bolz5c801d12019-10-09 10:38:45 -0500151 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier* pImageMemoryBarriers) const;
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600152 void PreCallRecordCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator,
153 VkBuffer* pBuffer, void* cb_state_data);
154 void CreateAccelerationStructureBuildValidationState(GpuAssisted* device_GpuAssisted);
155 void DestroyAccelerationStructureBuildValidationState();
156 void PreCallRecordCmdBuildAccelerationStructureNV(VkCommandBuffer commandBuffer, const VkAccelerationStructureInfoNV* pInfo,
157 VkBuffer instanceData, VkDeviceSize instanceOffset, VkBool32 update,
158 VkAccelerationStructureNV dst, VkAccelerationStructureNV src,
159 VkBuffer scratch, VkDeviceSize scratchOffset);
160 void ProcessAccelerationStructureBuildValidationBuffer(VkQueue queue, CMD_BUFFER_STATE* cb_node);
161 void PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
162 const VkGraphicsPipelineCreateInfo* pCreateInfos,
163 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
164 void* cgpl_state_data);
165 void PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
166 const VkComputePipelineCreateInfo* pCreateInfos,
167 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
168 void* ccpl_state_data);
169 void PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
170 const VkRayTracingPipelineCreateInfoNV* pCreateInfos,
171 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
172 void* crtpl_state_data);
Jeff Bolz443c2ca2020-03-19 12:11:51 -0500173 void PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
174 const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
175 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
176 void* crtpl_state_data);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600177 void PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
178 const VkGraphicsPipelineCreateInfo* pCreateInfos,
179 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, VkResult result,
180 void* cgpl_state_data);
181 void PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
182 const VkComputePipelineCreateInfo* pCreateInfos,
183 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, VkResult result,
184 void* ccpl_state_data);
185 void PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
186 const VkRayTracingPipelineCreateInfoNV* pCreateInfos,
187 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines, VkResult result,
188 void* crtpl_state_data);
Jeff Bolz443c2ca2020-03-19 12:11:51 -0500189 void PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
190 const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
191 const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
192 VkResult result, void* crtpl_state_data);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600193 void PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks* pAllocator);
194 bool InstrumentShader(const VkShaderModuleCreateInfo* pCreateInfo, std::vector<unsigned int>& new_pgm,
195 uint32_t* unique_shader_id);
196 void PreCallRecordCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo* pCreateInfo,
197 const VkAllocationCallbacks* pAllocator, VkShaderModule* pShaderModule,
198 void* csm_state_data);
Tony-LunarG1dce2392019-10-23 16:49:29 -0600199 void AnalyzeAndGenerateMessages(VkCommandBuffer command_buffer, VkQueue queue, VkPipelineBindPoint pipeline_bind_point,
200 uint32_t operation_index, uint32_t* const debug_output_buffer);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600201 void UpdateInstrumentationBuffer(CMD_BUFFER_STATE* cb_node);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600202 void PreCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence);
203 void PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence,
204 VkResult result);
205 void PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex,
206 uint32_t firstInstance);
207 void PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
208 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance);
209 void PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count,
210 uint32_t stride);
211 void PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t count,
212 uint32_t stride);
213 void PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z);
214 void PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset);
215 void PreCallRecordCmdTraceRaysNV(VkCommandBuffer commandBuffer, VkBuffer raygenShaderBindingTableBuffer,
216 VkDeviceSize raygenShaderBindingOffset, VkBuffer missShaderBindingTableBuffer,
217 VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride,
218 VkBuffer hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset,
219 VkDeviceSize hitShaderBindingStride, VkBuffer callableShaderBindingTableBuffer,
220 VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride,
221 uint32_t width, uint32_t height, uint32_t depth);
222 void PostCallRecordCmdTraceRaysNV(VkCommandBuffer commandBuffer, VkBuffer raygenShaderBindingTableBuffer,
223 VkDeviceSize raygenShaderBindingOffset, VkBuffer missShaderBindingTableBuffer,
224 VkDeviceSize missShaderBindingOffset, VkDeviceSize missShaderBindingStride,
225 VkBuffer hitShaderBindingTableBuffer, VkDeviceSize hitShaderBindingOffset,
226 VkDeviceSize hitShaderBindingStride, VkBuffer callableShaderBindingTableBuffer,
227 VkDeviceSize callableShaderBindingOffset, VkDeviceSize callableShaderBindingStride,
228 uint32_t width, uint32_t height, uint32_t depth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -0500229 void PreCallRecordCmdTraceRaysKHR(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 PostCallRecordCmdTraceRaysKHR(VkCommandBuffer commandBuffer, const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
235 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
236 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
237 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, uint32_t width, uint32_t height,
238 uint32_t depth);
239 void PreCallRecordCmdTraceRaysIndirectKHR(VkCommandBuffer commandBuffer,
240 const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
241 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
242 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
243 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, VkBuffer buffer,
244 VkDeviceSize offset);
245 void PostCallRecordCmdTraceRaysIndirectKHR(VkCommandBuffer commandBuffer,
246 const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
247 const VkStridedBufferRegionKHR* pMissShaderBindingTable,
248 const VkStridedBufferRegionKHR* pHitShaderBindingTable,
249 const VkStridedBufferRegionKHR* pCallableShaderBindingTable, VkBuffer buffer,
250 VkDeviceSize offset);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600251 void AllocateValidationResources(const VkCommandBuffer cmd_buffer, const VkPipelineBindPoint bind_point);
252 void PostCallRecordGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
253 VkPhysicalDeviceProperties* pPhysicalDeviceProperties);
254 void PostCallRecordGetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
255 VkPhysicalDeviceProperties2* pPhysicalDeviceProperties2);
Tony-LunarG2ba1cb32019-09-25 15:16:11 -0600256};