blob: 4db97bd9ba62e9f0bfd97408d49c4e6dda711712 [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;
sjfricke9a209802022-08-03 17:57:40 +0900110 bool ValidateCmdWaitEvents(VkCommandBuffer command_buffer, VkPipelineStageFlags2 src_stage_mask, CMD_TYPE cmd_type) const;
111 bool PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
112 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
113 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
114 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
115 uint32_t imageMemoryBarrierCount,
116 const VkImageMemoryBarrier *pImageMemoryBarriers) const override;
117 bool PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
118 const VkDependencyInfoKHR *pDependencyInfos) const override;
119 bool PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
120 const VkDependencyInfo *pDependencyInfos) const override;
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600121 void PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
122 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
123 void *cpl_state_data) override;
124 void PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
125 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
126 VkResult result) override;
127
128 void PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
129 const VkGraphicsPipelineCreateInfo *pCreateInfos,
130 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
131 void *cgpl_state_data) override;
132 void PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
133 const VkComputePipelineCreateInfo *pCreateInfos,
134 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
135 void *ccpl_state_data) override;
136 void PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
137 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
138 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
139 void *crtpl_state_data) override;
140 void PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
141 VkPipelineCache pipelineCache, uint32_t count,
142 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
143 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
144 void *crtpl_state_data) override;
145 void PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
146 const VkGraphicsPipelineCreateInfo *pCreateInfos,
147 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
148 void *cgpl_state_data) override;
149 void PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
150 const VkComputePipelineCreateInfo *pCreateInfos,
151 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
152 void *ccpl_state_data) override;
153 void PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
154 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
155 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
156 void *crtpl_state_data) override;
157 void PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
158 VkPipelineCache pipelineCache, uint32_t count,
159 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
160 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
161 VkResult result, void *crtpl_state_data) override;
162 void PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) override;
163
Jeremy Gebben33717862022-03-28 15:53:56 -0600164 template <typename T>
Tony-LunarGa2aa78b2022-04-19 08:41:38 -0600165 void ReportSetupProblem(T object, const char *const specific_message, bool vma_fail = false) const {
sjfricke9a209802022-08-03 17:57:40 +0900166 std::string logit = specific_message;
Tony-LunarGa2aa78b2022-04-19 08:41:38 -0600167 if (vma_fail) {
168 char *stats_string;
169 vmaBuildStatsString(vmaAllocator, &stats_string, false);
170 logit += " VMA statistics = ";
171 logit += stats_string;
172 vmaFreeStatsString(vmaAllocator, stats_string);
173 }
174 LogError(object, setup_vuid, "Setup Error. Detail: (%s)", logit.c_str());
Jeremy Gebben33717862022-03-28 15:53:56 -0600175 }
Tony-LunarGbcfeccf2022-04-19 09:14:35 -0600176 bool GpuGetOption(const char *option, bool default_value) {
177 std::string option_string = getLayerOption(option);
178 transform(option_string.begin(), option_string.end(), option_string.begin(), ::tolower);
179 return !option_string.empty() ? !option_string.compare("true") : default_value;
180 }
Jeremy Gebben33717862022-03-28 15:53:56 -0600181
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600182 protected:
Jeremy Gebben5ca80b32022-04-11 10:58:39 -0600183 bool CommandBufferNeedsProcessing(VkCommandBuffer command_buffer) const;
184 void ProcessCommandBuffer(VkQueue queue, VkCommandBuffer command_buffer);
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600185
186 void SubmitBarrier(VkQueue queue) {
187 auto queue_state = Get<gpu_utils_state::Queue>(queue);
188 if (queue_state) {
189 queue_state->SubmitBarrier();
190 }
191 }
192
193 std::shared_ptr<QUEUE_STATE> CreateQueue(VkQueue q, uint32_t index, VkDeviceQueueCreateFlags flags) override {
194 return std::static_pointer_cast<QUEUE_STATE>(std::make_shared<gpu_utils_state::Queue>(*this, q, index, flags));
195 }
196
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600197 template <typename CreateInfo, typename SafeCreateInfo>
198 void PreCallRecordPipelineCreations(uint32_t count, const CreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator,
199 VkPipeline *pPipelines, std::vector<std::shared_ptr<PIPELINE_STATE>> &pipe_state,
200 std::vector<SafeCreateInfo> *new_pipeline_create_infos,
201 const VkPipelineBindPoint bind_point);
Tony-LunarGabe71bd2022-05-11 15:58:16 -0600202 template <typename CreateInfo, typename SafeCreateInfo>
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600203 void PostCallRecordPipelineCreations(const uint32_t count, const CreateInfo *pCreateInfos,
204 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
Tony-LunarG0f647382022-05-12 15:11:59 -0600205 const VkPipelineBindPoint bind_point, const SafeCreateInfo &modified_create_infos);
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600206
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600207 public:
Jeremy Gebben5160e032022-03-28 14:57:43 -0600208 bool aborted = false;
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600209 PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600210 const char *setup_vuid;
211 VkPhysicalDeviceFeatures supported_features{};
212 VkPhysicalDeviceFeatures desired_features{};
213 uint32_t adjusted_max_desc_sets = 0;
214 uint32_t unique_shader_module_id = 0;
215 uint32_t output_buffer_size = 0;
216 VkDescriptorSetLayout debug_desc_layout = VK_NULL_HANDLE;
217 VkDescriptorSetLayout dummy_desc_layout = VK_NULL_HANDLE;
218 uint32_t desc_set_bind_index = 0;
219 VmaAllocator vmaAllocator = {};
Jeremy Gebben5160e032022-03-28 14:57:43 -0600220 std::unique_ptr<UtilDescriptorSetManager> desc_set_manager;
Jeremy Gebben58cc9d12022-03-23 17:04:57 -0600221 vl_concurrent_unordered_map<uint32_t, GpuAssistedShaderTracker> shader_map;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600222 std::vector<VkDescriptorSetLayoutBinding> bindings_;
223};
224