sfricke-samsung | ef15e48 | 2022-01-26 11:32:49 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2020-2022 The Khronos Group Inc. |
| 2 | * Copyright (c) 2020-2022 Valve Corporation |
| 3 | * Copyright (c) 2020-2022 LunarG, Inc. |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -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 | * |
| 17 | * Author: Tony Barbour <tony@lunarg.com> |
| 18 | */ |
| 19 | #pragma once |
| 20 | #include "chassis.h" |
| 21 | #include "shader_validation.h" |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 22 | #include "cmd_buffer_state.h" |
Jeremy Gebben | 5160e03 | 2022-03-28 14:57:43 -0600 | [diff] [blame] | 23 | #include "state_tracker.h" |
| 24 | #include "vk_mem_alloc.h" |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 25 | #include "queue_state.h" |
Jeremy Gebben | 5160e03 | 2022-03-28 14:57:43 -0600 | [diff] [blame] | 26 | |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 27 | class GpuAssistedBase; |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 28 | |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame^] | 29 | static const VkShaderStageFlags kShaderStageAllRayTracing = |
| 30 | VK_SHADER_STAGE_ANY_HIT_BIT_KHR | VK_SHADER_STAGE_CALLABLE_BIT_KHR | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | |
| 31 | VK_SHADER_STAGE_INTERSECTION_BIT_KHR | VK_SHADER_STAGE_MISS_BIT_KHR | VK_SHADER_STAGE_RAYGEN_BIT_KHR; |
| 32 | |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 33 | class UtilDescriptorSetManager { |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 34 | public: |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame^] | 35 | UtilDescriptorSetManager(VkDevice device, uint32_t num_bindings_in_set); |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 36 | ~UtilDescriptorSetManager(); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 37 | |
| 38 | VkResult GetDescriptorSet(VkDescriptorPool *desc_pool, VkDescriptorSetLayout ds_layout, VkDescriptorSet *desc_sets); |
| 39 | VkResult GetDescriptorSets(uint32_t count, VkDescriptorPool *pool, VkDescriptorSetLayout ds_layout, |
| 40 | std::vector<VkDescriptorSet> *desc_sets); |
| 41 | void PutBackDescriptorSet(VkDescriptorPool desc_pool, VkDescriptorSet desc_set); |
| 42 | |
| 43 | private: |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 44 | std::unique_lock<std::mutex> Lock() const { return std::unique_lock<std::mutex>(lock_); } |
| 45 | |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 46 | static const uint32_t kItemsPerChunk = 512; |
| 47 | struct PoolTracker { |
| 48 | uint32_t size; |
| 49 | uint32_t used; |
| 50 | }; |
| 51 | VkDevice device; |
sjfricke | 43b340c | 2022-08-04 22:18:38 +0900 | [diff] [blame^] | 52 | uint32_t num_bindings_in_set; |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 53 | layer_data::unordered_map<VkDescriptorPool, struct PoolTracker> desc_pool_map_; |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 54 | mutable std::mutex lock_; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 55 | }; |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 56 | |
| 57 | namespace gpu_utils_state { |
| 58 | class Queue : public QUEUE_STATE { |
| 59 | public: |
| 60 | Queue(GpuAssistedBase &state, VkQueue q, uint32_t index, VkDeviceQueueCreateFlags flags); |
| 61 | virtual ~Queue(); |
| 62 | void SubmitBarrier(); |
| 63 | |
| 64 | private: |
| 65 | GpuAssistedBase &state_; |
| 66 | VkCommandPool barrier_command_pool_{VK_NULL_HANDLE}; |
| 67 | VkCommandBuffer barrier_command_buffer_{VK_NULL_HANDLE}; |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 68 | }; |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 69 | |
| 70 | class CommandBuffer : public CMD_BUFFER_STATE { |
| 71 | public: |
| 72 | CommandBuffer(GpuAssistedBase *ga, VkCommandBuffer cb, const VkCommandBufferAllocateInfo *pCreateInfo, |
| 73 | const COMMAND_POOL_STATE *pool); |
| 74 | |
| 75 | virtual bool NeedsProcessing() const = 0; |
| 76 | virtual void Process(VkQueue queue) = 0; |
| 77 | }; |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 78 | } // namespace gpu_utils_state |
| 79 | VALSTATETRACK_DERIVED_STATE_OBJECT(VkQueue, gpu_utils_state::Queue, QUEUE_STATE); |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 80 | VALSTATETRACK_DERIVED_STATE_OBJECT(VkCommandBuffer, gpu_utils_state::CommandBuffer, CMD_BUFFER_STATE); |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 81 | |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 82 | VkResult UtilInitializeVma(VkPhysicalDevice physical_device, VkDevice device, VmaAllocator *pAllocator); |
Tony-LunarG | 1dce239 | 2019-10-23 16:49:29 -0600 | [diff] [blame] | 83 | |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 84 | void UtilGenerateStageMessage(const uint32_t *debug_record, std::string &msg); |
| 85 | void UtilGenerateCommonMessage(const debug_report_data *report_data, const VkCommandBuffer commandBuffer, |
| 86 | const uint32_t *debug_record, const VkShaderModule shader_module_handle, |
| 87 | const VkPipeline pipeline_handle, const VkPipelineBindPoint pipeline_bind_point, |
| 88 | const uint32_t operation_index, std::string &msg); |
sfricke-samsung | 7fac88a | 2022-01-26 11:44:22 -0800 | [diff] [blame] | 89 | void UtilGenerateSourceMessages(const std::vector<uint32_t> &pgm, const uint32_t *debug_record, bool from_printf, |
Tony-LunarG | b5fae46 | 2020-03-05 12:43:25 -0700 | [diff] [blame] | 90 | std::string &filename_msg, std::string &source_msg); |
Jeremy Gebben | 5160e03 | 2022-03-28 14:57:43 -0600 | [diff] [blame] | 91 | |
| 92 | struct GpuAssistedShaderTracker { |
| 93 | VkPipeline pipeline; |
| 94 | VkShaderModule shader_module; |
| 95 | std::vector<uint32_t> pgm; |
| 96 | }; |
| 97 | |
| 98 | class GpuAssistedBase : public ValidationStateTracker { |
| 99 | public: |
Jeremy Gebben | 04697b0 | 2022-03-23 16:18:12 -0600 | [diff] [blame] | 100 | ReadLockGuard ReadLock() override; |
| 101 | WriteLockGuard WriteLock() override; |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 102 | void PreCallRecordCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo, |
| 103 | const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, void *modified_create_info) override; |
| 104 | void CreateDevice(const VkDeviceCreateInfo *pCreateInfo) override; |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 105 | void PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) override; |
| 106 | |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 107 | void PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence, |
| 108 | VkResult result) override; |
| 109 | void RecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, VkFence fence, VkResult result); |
| 110 | void PostCallRecordQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, VkFence fence, |
| 111 | VkResult result) override; |
| 112 | void PostCallRecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence, |
| 113 | VkResult result) override; |
sjfricke | 9a20980 | 2022-08-03 17:57:40 +0900 | [diff] [blame] | 114 | bool ValidateCmdWaitEvents(VkCommandBuffer command_buffer, VkPipelineStageFlags2 src_stage_mask, CMD_TYPE cmd_type) const; |
| 115 | bool PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents, |
| 116 | VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, |
| 117 | uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, |
| 118 | uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers, |
| 119 | uint32_t imageMemoryBarrierCount, |
| 120 | const VkImageMemoryBarrier *pImageMemoryBarriers) const override; |
| 121 | bool PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents, |
| 122 | const VkDependencyInfoKHR *pDependencyInfos) const override; |
| 123 | bool PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents, |
| 124 | const VkDependencyInfo *pDependencyInfos) const override; |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 125 | void PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo, |
| 126 | const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout, |
| 127 | void *cpl_state_data) override; |
| 128 | void PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo, |
| 129 | const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout, |
| 130 | VkResult result) override; |
| 131 | |
| 132 | void PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 133 | const VkGraphicsPipelineCreateInfo *pCreateInfos, |
| 134 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 135 | void *cgpl_state_data) override; |
| 136 | void PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 137 | const VkComputePipelineCreateInfo *pCreateInfos, |
| 138 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 139 | void *ccpl_state_data) override; |
| 140 | void PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 141 | const VkRayTracingPipelineCreateInfoNV *pCreateInfos, |
| 142 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 143 | void *crtpl_state_data) override; |
| 144 | void PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation, |
| 145 | VkPipelineCache pipelineCache, uint32_t count, |
| 146 | const VkRayTracingPipelineCreateInfoKHR *pCreateInfos, |
| 147 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 148 | void *crtpl_state_data) override; |
| 149 | void PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 150 | const VkGraphicsPipelineCreateInfo *pCreateInfos, |
| 151 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result, |
| 152 | void *cgpl_state_data) override; |
| 153 | void PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 154 | const VkComputePipelineCreateInfo *pCreateInfos, |
| 155 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result, |
| 156 | void *ccpl_state_data) override; |
| 157 | void PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count, |
| 158 | const VkRayTracingPipelineCreateInfoNV *pCreateInfos, |
| 159 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result, |
| 160 | void *crtpl_state_data) override; |
| 161 | void PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation, |
| 162 | VkPipelineCache pipelineCache, uint32_t count, |
| 163 | const VkRayTracingPipelineCreateInfoKHR *pCreateInfos, |
| 164 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
| 165 | VkResult result, void *crtpl_state_data) override; |
| 166 | void PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) override; |
| 167 | |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 168 | template <typename T> |
Tony-LunarG | a2aa78b | 2022-04-19 08:41:38 -0600 | [diff] [blame] | 169 | void ReportSetupProblem(T object, const char *const specific_message, bool vma_fail = false) const { |
sjfricke | 9a20980 | 2022-08-03 17:57:40 +0900 | [diff] [blame] | 170 | std::string logit = specific_message; |
Tony-LunarG | a2aa78b | 2022-04-19 08:41:38 -0600 | [diff] [blame] | 171 | if (vma_fail) { |
| 172 | char *stats_string; |
| 173 | vmaBuildStatsString(vmaAllocator, &stats_string, false); |
| 174 | logit += " VMA statistics = "; |
| 175 | logit += stats_string; |
| 176 | vmaFreeStatsString(vmaAllocator, stats_string); |
| 177 | } |
| 178 | LogError(object, setup_vuid, "Setup Error. Detail: (%s)", logit.c_str()); |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 179 | } |
Tony-LunarG | bcfeccf | 2022-04-19 09:14:35 -0600 | [diff] [blame] | 180 | bool GpuGetOption(const char *option, bool default_value) { |
| 181 | std::string option_string = getLayerOption(option); |
| 182 | transform(option_string.begin(), option_string.end(), option_string.begin(), ::tolower); |
| 183 | return !option_string.empty() ? !option_string.compare("true") : default_value; |
| 184 | } |
Jeremy Gebben | 3371786 | 2022-03-28 15:53:56 -0600 | [diff] [blame] | 185 | |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 186 | protected: |
Jeremy Gebben | 5ca80b3 | 2022-04-11 10:58:39 -0600 | [diff] [blame] | 187 | bool CommandBufferNeedsProcessing(VkCommandBuffer command_buffer) const; |
| 188 | void ProcessCommandBuffer(VkQueue queue, VkCommandBuffer command_buffer); |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 189 | |
| 190 | void SubmitBarrier(VkQueue queue) { |
| 191 | auto queue_state = Get<gpu_utils_state::Queue>(queue); |
| 192 | if (queue_state) { |
| 193 | queue_state->SubmitBarrier(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | std::shared_ptr<QUEUE_STATE> CreateQueue(VkQueue q, uint32_t index, VkDeviceQueueCreateFlags flags) override { |
| 198 | return std::static_pointer_cast<QUEUE_STATE>(std::make_shared<gpu_utils_state::Queue>(*this, q, index, flags)); |
| 199 | } |
| 200 | |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 201 | template <typename CreateInfo, typename SafeCreateInfo> |
| 202 | void PreCallRecordPipelineCreations(uint32_t count, const CreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator, |
| 203 | VkPipeline *pPipelines, std::vector<std::shared_ptr<PIPELINE_STATE>> &pipe_state, |
| 204 | std::vector<SafeCreateInfo> *new_pipeline_create_infos, |
| 205 | const VkPipelineBindPoint bind_point); |
Tony-LunarG | abe71bd | 2022-05-11 15:58:16 -0600 | [diff] [blame] | 206 | template <typename CreateInfo, typename SafeCreateInfo> |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 207 | void PostCallRecordPipelineCreations(const uint32_t count, const CreateInfo *pCreateInfos, |
| 208 | const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, |
Tony-LunarG | 0f64738 | 2022-05-12 15:11:59 -0600 | [diff] [blame] | 209 | const VkPipelineBindPoint bind_point, const SafeCreateInfo &modified_create_infos); |
Jeremy Gebben | efd9780 | 2022-03-28 16:45:05 -0600 | [diff] [blame] | 210 | |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 211 | public: |
Jeremy Gebben | 5160e03 | 2022-03-28 14:57:43 -0600 | [diff] [blame] | 212 | bool aborted = false; |
Jeremy Gebben | fcfc33c | 2022-03-28 15:31:29 -0600 | [diff] [blame] | 213 | PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData; |
Jeremy Gebben | 5160e03 | 2022-03-28 14:57:43 -0600 | [diff] [blame] | 214 | const char *setup_vuid; |
| 215 | VkPhysicalDeviceFeatures supported_features{}; |
| 216 | VkPhysicalDeviceFeatures desired_features{}; |
| 217 | uint32_t adjusted_max_desc_sets = 0; |
| 218 | uint32_t unique_shader_module_id = 0; |
| 219 | uint32_t output_buffer_size = 0; |
| 220 | VkDescriptorSetLayout debug_desc_layout = VK_NULL_HANDLE; |
| 221 | VkDescriptorSetLayout dummy_desc_layout = VK_NULL_HANDLE; |
| 222 | uint32_t desc_set_bind_index = 0; |
| 223 | VmaAllocator vmaAllocator = {}; |
Jeremy Gebben | 5160e03 | 2022-03-28 14:57:43 -0600 | [diff] [blame] | 224 | std::unique_ptr<UtilDescriptorSetManager> desc_set_manager; |
Jeremy Gebben | 58cc9d1 | 2022-03-23 17:04:57 -0600 | [diff] [blame] | 225 | vl_concurrent_unordered_map<uint32_t, GpuAssistedShaderTracker> shader_map; |
Jeremy Gebben | 5160e03 | 2022-03-28 14:57:43 -0600 | [diff] [blame] | 226 | std::vector<VkDescriptorSetLayoutBinding> bindings_; |
| 227 | }; |
| 228 | |