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