blob: 4653098afb3b17bb12b611f7cf95a2ed16e24f09 [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
sjfricke43b340c2022-08-04 22:18:38 +090029static 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-LunarGb5fae462020-03-05 12:43:25 -070033class UtilDescriptorSetManager {
Tony-LunarG1dce2392019-10-23 16:49:29 -060034 public:
sjfricke43b340c2022-08-04 22:18:38 +090035 UtilDescriptorSetManager(VkDevice device, uint32_t num_bindings_in_set);
Tony-LunarGb5fae462020-03-05 12:43:25 -070036 ~UtilDescriptorSetManager();
Tony-LunarG1dce2392019-10-23 16:49:29 -060037
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 Gebbenfcfc33c2022-03-28 15:31:29 -060044 std::unique_lock<std::mutex> Lock() const { return std::unique_lock<std::mutex>(lock_); }
45
Tony-LunarG1dce2392019-10-23 16:49:29 -060046 static const uint32_t kItemsPerChunk = 512;
47 struct PoolTracker {
48 uint32_t size;
49 uint32_t used;
50 };
51 VkDevice device;
sjfricke43b340c2022-08-04 22:18:38 +090052 uint32_t num_bindings_in_set;
Jeremy Gebbencbf22862021-03-03 12:01:22 -070053 layer_data::unordered_map<VkDescriptorPool, struct PoolTracker> desc_pool_map_;
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060054 mutable std::mutex lock_;
Tony-LunarG1dce2392019-10-23 16:49:29 -060055};
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060056
57namespace gpu_utils_state {
58class 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-LunarG1dce2392019-10-23 16:49:29 -060068};
Jeremy Gebben5ca80b32022-04-11 10:58:39 -060069
70class 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 Gebbenfcfc33c2022-03-28 15:31:29 -060078} // namespace gpu_utils_state
79VALSTATETRACK_DERIVED_STATE_OBJECT(VkQueue, gpu_utils_state::Queue, QUEUE_STATE);
Jeremy Gebben5ca80b32022-04-11 10:58:39 -060080VALSTATETRACK_DERIVED_STATE_OBJECT(VkCommandBuffer, gpu_utils_state::CommandBuffer, CMD_BUFFER_STATE);
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060081
Tony-LunarGb5fae462020-03-05 12:43:25 -070082VkResult UtilInitializeVma(VkPhysicalDevice physical_device, VkDevice device, VmaAllocator *pAllocator);
Tony-LunarG1dce2392019-10-23 16:49:29 -060083
Tony-LunarGb5fae462020-03-05 12:43:25 -070084void UtilGenerateStageMessage(const uint32_t *debug_record, std::string &msg);
85void 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-samsung7fac88a2022-01-26 11:44:22 -080089void UtilGenerateSourceMessages(const std::vector<uint32_t> &pgm, const uint32_t *debug_record, bool from_printf,
Tony-LunarGb5fae462020-03-05 12:43:25 -070090 std::string &filename_msg, std::string &source_msg);
Jeremy Gebben5160e032022-03-28 14:57:43 -060091
92struct GpuAssistedShaderTracker {
93 VkPipeline pipeline;
94 VkShaderModule shader_module;
95 std::vector<uint32_t> pgm;
96};
97
98class GpuAssistedBase : public ValidationStateTracker {
99 public:
Jeremy Gebben04697b02022-03-23 16:18:12 -0600100 ReadLockGuard ReadLock() override;
101 WriteLockGuard WriteLock() override;
Jeremy Gebben33717862022-03-28 15:53:56 -0600102 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 Gebben33717862022-03-28 15:53:56 -0600105 void PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) override;
106
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600107 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;
sjfricke9a209802022-08-03 17:57:40 +0900114 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 Gebbenefd97802022-03-28 16:45:05 -0600125 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 Gebben33717862022-03-28 15:53:56 -0600168 template <typename T>
Tony-LunarGa2aa78b2022-04-19 08:41:38 -0600169 void ReportSetupProblem(T object, const char *const specific_message, bool vma_fail = false) const {
sjfricke9a209802022-08-03 17:57:40 +0900170 std::string logit = specific_message;
Tony-LunarGa2aa78b2022-04-19 08:41:38 -0600171 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 Gebben33717862022-03-28 15:53:56 -0600179 }
Tony-LunarGbcfeccf2022-04-19 09:14:35 -0600180 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 Gebben33717862022-03-28 15:53:56 -0600185
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600186 protected:
Jeremy Gebben5ca80b32022-04-11 10:58:39 -0600187 bool CommandBufferNeedsProcessing(VkCommandBuffer command_buffer) const;
188 void ProcessCommandBuffer(VkQueue queue, VkCommandBuffer command_buffer);
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600189
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 Gebbenefd97802022-03-28 16:45:05 -0600201 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-LunarGabe71bd2022-05-11 15:58:16 -0600206 template <typename CreateInfo, typename SafeCreateInfo>
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600207 void PostCallRecordPipelineCreations(const uint32_t count, const CreateInfo *pCreateInfos,
208 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
Tony-LunarG0f647382022-05-12 15:11:59 -0600209 const VkPipelineBindPoint bind_point, const SafeCreateInfo &modified_create_infos);
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600210
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600211 public:
Jeremy Gebben5160e032022-03-28 14:57:43 -0600212 bool aborted = false;
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600213 PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600214 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 Gebben5160e032022-03-28 14:57:43 -0600224 std::unique_ptr<UtilDescriptorSetManager> desc_set_manager;
Jeremy Gebben58cc9d12022-03-23 17:04:57 -0600225 vl_concurrent_unordered_map<uint32_t, GpuAssistedShaderTracker> shader_map;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600226 std::vector<VkDescriptorSetLayoutBinding> bindings_;
227};
228