blob: b0daef944df049e374530f79ff688c8f266c2c45 [file] [log] [blame]
sfricke-samsungef15e482022-01-26 11:32:49 -08001/* Copyright (c) 2020-2022 The Khronos Group Inc.
2 * Copyright (c) 2020-2022 Valve Corporation
3 * Copyright (c) 2020-2022 LunarG, Inc.
Tony-LunarG1dce2392019-10-23 16:49:29 -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 *
17 * Author: Tony Barbour <tony@lunarg.com>
18 */
19#pragma once
20#include "chassis.h"
21#include "shader_validation.h"
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060022#include "cmd_buffer_state.h"
Jeremy Gebben5160e032022-03-28 14:57:43 -060023#include "state_tracker.h"
24#include "vk_mem_alloc.h"
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060025#include "queue_state.h"
Jeremy Gebben5160e032022-03-28 14:57:43 -060026
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060027class GpuAssistedBase;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060028
Tony-LunarGb5fae462020-03-05 12:43:25 -070029class UtilDescriptorSetManager {
Tony-LunarG1dce2392019-10-23 16:49:29 -060030 public:
Tony-LunarGb5fae462020-03-05 12:43:25 -070031 UtilDescriptorSetManager(VkDevice device, uint32_t numBindingsInSet);
32 ~UtilDescriptorSetManager();
Tony-LunarG1dce2392019-10-23 16:49:29 -060033
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 Gebbenfcfc33c2022-03-28 15:31:29 -060040 std::unique_lock<std::mutex> Lock() const { return std::unique_lock<std::mutex>(lock_); }
41
Tony-LunarG1dce2392019-10-23 16:49:29 -060042 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 Gebbencbf22862021-03-03 12:01:22 -070049 layer_data::unordered_map<VkDescriptorPool, struct PoolTracker> desc_pool_map_;
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060050 mutable std::mutex lock_;
Tony-LunarG1dce2392019-10-23 16:49:29 -060051};
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060052
53namespace gpu_utils_state {
54class 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-LunarG1dce2392019-10-23 16:49:29 -060064};
Jeremy Gebben5ca80b32022-04-11 10:58:39 -060065
66class 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 Gebbenfcfc33c2022-03-28 15:31:29 -060074} // namespace gpu_utils_state
75VALSTATETRACK_DERIVED_STATE_OBJECT(VkQueue, gpu_utils_state::Queue, QUEUE_STATE);
Jeremy Gebben5ca80b32022-04-11 10:58:39 -060076VALSTATETRACK_DERIVED_STATE_OBJECT(VkCommandBuffer, gpu_utils_state::CommandBuffer, CMD_BUFFER_STATE);
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060077
Tony-LunarGb5fae462020-03-05 12:43:25 -070078VkResult UtilInitializeVma(VkPhysicalDevice physical_device, VkDevice device, VmaAllocator *pAllocator);
Tony-LunarG1dce2392019-10-23 16:49:29 -060079
Tony-LunarGb5fae462020-03-05 12:43:25 -070080void UtilGenerateStageMessage(const uint32_t *debug_record, std::string &msg);
81void 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-samsung7fac88a2022-01-26 11:44:22 -080085void UtilGenerateSourceMessages(const std::vector<uint32_t> &pgm, const uint32_t *debug_record, bool from_printf,
Tony-LunarGb5fae462020-03-05 12:43:25 -070086 std::string &filename_msg, std::string &source_msg);
Jeremy Gebben5160e032022-03-28 14:57:43 -060087
88struct GpuAssistedShaderTracker {
89 VkPipeline pipeline;
90 VkShaderModule shader_module;
91 std::vector<uint32_t> pgm;
92};
93
94class GpuAssistedBase : public ValidationStateTracker {
95 public:
Jeremy Gebben04697b02022-03-23 16:18:12 -060096 ReadLockGuard ReadLock() override;
97 WriteLockGuard WriteLock() override;
Jeremy Gebben33717862022-03-28 15:53:56 -060098 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 Gebben33717862022-03-28 15:53:56 -0600101 void PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) override;
102
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600103 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 Gebbenefd97802022-03-28 16:45:05 -0600110 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 Gebben33717862022-03-28 15:53:56 -0600153 template <typename T>
Tony-LunarGa2aa78b2022-04-19 08:41:38 -0600154 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 Gebben33717862022-03-28 15:53:56 -0600164 }
165
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600166 protected:
Jeremy Gebben5ca80b32022-04-11 10:58:39 -0600167 bool CommandBufferNeedsProcessing(VkCommandBuffer command_buffer) const;
168 void ProcessCommandBuffer(VkQueue queue, VkCommandBuffer command_buffer);
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600169
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 Gebbenefd97802022-03-28 16:45:05 -0600181 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 Gebbenfcfc33c2022-03-28 15:31:29 -0600191 public:
Jeremy Gebben5160e032022-03-28 14:57:43 -0600192 bool aborted = false;
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600193 PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600194 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 Gebben5160e032022-03-28 14:57:43 -0600204 std::unique_ptr<UtilDescriptorSetManager> desc_set_manager;
Jeremy Gebben58cc9d12022-03-23 17:04:57 -0600205 vl_concurrent_unordered_map<uint32_t, GpuAssistedShaderTracker> shader_map;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600206 std::vector<VkDescriptorSetLayoutBinding> bindings_;
207};
208