blob: 8c7f0511184d4dff26655114fc34d20ece8f4d6e [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"
Tony-LunarGca5cd032022-07-25 16:03:21 -060024#define VMA_VULKAN_VERSION 1001000
Jeremy Gebben5160e032022-03-28 14:57:43 -060025#include "vk_mem_alloc.h"
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060026#include "queue_state.h"
Jeremy Gebben5160e032022-03-28 14:57:43 -060027
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060028class GpuAssistedBase;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060029
sjfricke43b340c2022-08-04 22:18:38 +090030static const VkShaderStageFlags kShaderStageAllRayTracing =
31 VK_SHADER_STAGE_ANY_HIT_BIT_KHR | VK_SHADER_STAGE_CALLABLE_BIT_KHR | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR |
32 VK_SHADER_STAGE_INTERSECTION_BIT_KHR | VK_SHADER_STAGE_MISS_BIT_KHR | VK_SHADER_STAGE_RAYGEN_BIT_KHR;
33
Tony-LunarGb5fae462020-03-05 12:43:25 -070034class UtilDescriptorSetManager {
Tony-LunarG1dce2392019-10-23 16:49:29 -060035 public:
sjfricke43b340c2022-08-04 22:18:38 +090036 UtilDescriptorSetManager(VkDevice device, uint32_t num_bindings_in_set);
Tony-LunarGb5fae462020-03-05 12:43:25 -070037 ~UtilDescriptorSetManager();
Tony-LunarG1dce2392019-10-23 16:49:29 -060038
39 VkResult GetDescriptorSet(VkDescriptorPool *desc_pool, VkDescriptorSetLayout ds_layout, VkDescriptorSet *desc_sets);
40 VkResult GetDescriptorSets(uint32_t count, VkDescriptorPool *pool, VkDescriptorSetLayout ds_layout,
41 std::vector<VkDescriptorSet> *desc_sets);
42 void PutBackDescriptorSet(VkDescriptorPool desc_pool, VkDescriptorSet desc_set);
43
44 private:
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060045 std::unique_lock<std::mutex> Lock() const { return std::unique_lock<std::mutex>(lock_); }
46
Tony-LunarG1dce2392019-10-23 16:49:29 -060047 static const uint32_t kItemsPerChunk = 512;
48 struct PoolTracker {
49 uint32_t size;
50 uint32_t used;
51 };
52 VkDevice device;
sjfricke43b340c2022-08-04 22:18:38 +090053 uint32_t num_bindings_in_set;
Jeremy Gebbencbf22862021-03-03 12:01:22 -070054 layer_data::unordered_map<VkDescriptorPool, struct PoolTracker> desc_pool_map_;
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060055 mutable std::mutex lock_;
Tony-LunarG1dce2392019-10-23 16:49:29 -060056};
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060057
58namespace gpu_utils_state {
59class Queue : public QUEUE_STATE {
60 public:
Rodrigo Locatti7ab778d2022-03-09 18:57:15 -030061 Queue(GpuAssistedBase &state, VkQueue q, uint32_t index, VkDeviceQueueCreateFlags flags, const VkQueueFamilyProperties &queueFamilyProperties);
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060062 virtual ~Queue();
63 void SubmitBarrier();
64
65 private:
66 GpuAssistedBase &state_;
67 VkCommandPool barrier_command_pool_{VK_NULL_HANDLE};
68 VkCommandBuffer barrier_command_buffer_{VK_NULL_HANDLE};
Tony-LunarG1dce2392019-10-23 16:49:29 -060069};
Jeremy Gebben5ca80b32022-04-11 10:58:39 -060070
71class CommandBuffer : public CMD_BUFFER_STATE {
72 public:
73 CommandBuffer(GpuAssistedBase *ga, VkCommandBuffer cb, const VkCommandBufferAllocateInfo *pCreateInfo,
74 const COMMAND_POOL_STATE *pool);
75
76 virtual bool NeedsProcessing() const = 0;
77 virtual void Process(VkQueue queue) = 0;
78};
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060079} // namespace gpu_utils_state
80VALSTATETRACK_DERIVED_STATE_OBJECT(VkQueue, gpu_utils_state::Queue, QUEUE_STATE);
Jeremy Gebben5ca80b32022-04-11 10:58:39 -060081VALSTATETRACK_DERIVED_STATE_OBJECT(VkCommandBuffer, gpu_utils_state::CommandBuffer, CMD_BUFFER_STATE);
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060082
Tony-LunarGca5cd032022-07-25 16:03:21 -060083VkResult UtilInitializeVma(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, VmaAllocator *pAllocator);
Tony-LunarG1dce2392019-10-23 16:49:29 -060084
Tony-LunarGb5fae462020-03-05 12:43:25 -070085void UtilGenerateStageMessage(const uint32_t *debug_record, std::string &msg);
86void UtilGenerateCommonMessage(const debug_report_data *report_data, const VkCommandBuffer commandBuffer,
87 const uint32_t *debug_record, const VkShaderModule shader_module_handle,
88 const VkPipeline pipeline_handle, const VkPipelineBindPoint pipeline_bind_point,
89 const uint32_t operation_index, std::string &msg);
sfricke-samsung7fac88a2022-01-26 11:44:22 -080090void UtilGenerateSourceMessages(const std::vector<uint32_t> &pgm, const uint32_t *debug_record, bool from_printf,
Tony-LunarGb5fae462020-03-05 12:43:25 -070091 std::string &filename_msg, std::string &source_msg);
Jeremy Gebben5160e032022-03-28 14:57:43 -060092
93struct GpuAssistedShaderTracker {
94 VkPipeline pipeline;
95 VkShaderModule shader_module;
96 std::vector<uint32_t> pgm;
97};
98
99class GpuAssistedBase : public ValidationStateTracker {
100 public:
Jeremy Gebben04697b02022-03-23 16:18:12 -0600101 ReadLockGuard ReadLock() override;
102 WriteLockGuard WriteLock() override;
Jeremy Gebben33717862022-03-28 15:53:56 -0600103 void PreCallRecordCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
104 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, void *modified_create_info) override;
105 void CreateDevice(const VkDeviceCreateInfo *pCreateInfo) override;
Jeremy Gebben33717862022-03-28 15:53:56 -0600106 void PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) override;
107
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600108 void PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence,
109 VkResult result) override;
110 void RecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, VkFence fence, VkResult result);
111 void PostCallRecordQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, VkFence fence,
112 VkResult result) override;
113 void PostCallRecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence,
114 VkResult result) override;
sjfricke9a209802022-08-03 17:57:40 +0900115 bool ValidateCmdWaitEvents(VkCommandBuffer command_buffer, VkPipelineStageFlags2 src_stage_mask, CMD_TYPE cmd_type) const;
116 bool PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
117 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
118 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
119 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
120 uint32_t imageMemoryBarrierCount,
121 const VkImageMemoryBarrier *pImageMemoryBarriers) const override;
122 bool PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
123 const VkDependencyInfoKHR *pDependencyInfos) const override;
124 bool PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
125 const VkDependencyInfo *pDependencyInfos) const override;
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600126 void PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
127 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
128 void *cpl_state_data) override;
129 void PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
130 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
131 VkResult result) override;
132
133 void PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
134 const VkGraphicsPipelineCreateInfo *pCreateInfos,
135 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
136 void *cgpl_state_data) override;
137 void PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
138 const VkComputePipelineCreateInfo *pCreateInfos,
139 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
140 void *ccpl_state_data) override;
141 void PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
142 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
143 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
144 void *crtpl_state_data) override;
145 void PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
146 VkPipelineCache pipelineCache, uint32_t count,
147 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
148 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
149 void *crtpl_state_data) override;
150 void PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
151 const VkGraphicsPipelineCreateInfo *pCreateInfos,
152 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
153 void *cgpl_state_data) override;
154 void PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
155 const VkComputePipelineCreateInfo *pCreateInfos,
156 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
157 void *ccpl_state_data) override;
158 void PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
159 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
160 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
161 void *crtpl_state_data) override;
162 void PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
163 VkPipelineCache pipelineCache, uint32_t count,
164 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
165 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
166 VkResult result, void *crtpl_state_data) override;
167 void PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) override;
168
Jeremy Gebben33717862022-03-28 15:53:56 -0600169 template <typename T>
Tony-LunarGa2aa78b2022-04-19 08:41:38 -0600170 void ReportSetupProblem(T object, const char *const specific_message, bool vma_fail = false) const {
sjfricke9a209802022-08-03 17:57:40 +0900171 std::string logit = specific_message;
Tony-LunarGa2aa78b2022-04-19 08:41:38 -0600172 if (vma_fail) {
173 char *stats_string;
174 vmaBuildStatsString(vmaAllocator, &stats_string, false);
175 logit += " VMA statistics = ";
176 logit += stats_string;
177 vmaFreeStatsString(vmaAllocator, stats_string);
178 }
179 LogError(object, setup_vuid, "Setup Error. Detail: (%s)", logit.c_str());
Jeremy Gebben33717862022-03-28 15:53:56 -0600180 }
Tony-LunarGbcfeccf2022-04-19 09:14:35 -0600181 bool GpuGetOption(const char *option, bool default_value) {
182 std::string option_string = getLayerOption(option);
183 transform(option_string.begin(), option_string.end(), option_string.begin(), ::tolower);
184 return !option_string.empty() ? !option_string.compare("true") : default_value;
185 }
Jeremy Gebben33717862022-03-28 15:53:56 -0600186
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600187 protected:
Jeremy Gebben5ca80b32022-04-11 10:58:39 -0600188 bool CommandBufferNeedsProcessing(VkCommandBuffer command_buffer) const;
189 void ProcessCommandBuffer(VkQueue queue, VkCommandBuffer command_buffer);
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600190
191 void SubmitBarrier(VkQueue queue) {
192 auto queue_state = Get<gpu_utils_state::Queue>(queue);
193 if (queue_state) {
194 queue_state->SubmitBarrier();
195 }
196 }
197
Rodrigo Locatti7ab778d2022-03-09 18:57:15 -0300198 std::shared_ptr<QUEUE_STATE> CreateQueue(VkQueue q, uint32_t index, VkDeviceQueueCreateFlags flags, const VkQueueFamilyProperties &queueFamilyProperties) override {
199 return std::static_pointer_cast<QUEUE_STATE>(std::make_shared<gpu_utils_state::Queue>(*this, q, index, flags, queueFamilyProperties));
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600200 }
201
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600202 template <typename CreateInfo, typename SafeCreateInfo>
203 void PreCallRecordPipelineCreations(uint32_t count, const CreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator,
204 VkPipeline *pPipelines, std::vector<std::shared_ptr<PIPELINE_STATE>> &pipe_state,
205 std::vector<SafeCreateInfo> *new_pipeline_create_infos,
206 const VkPipelineBindPoint bind_point);
Tony-LunarGabe71bd2022-05-11 15:58:16 -0600207 template <typename CreateInfo, typename SafeCreateInfo>
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600208 void PostCallRecordPipelineCreations(const uint32_t count, const CreateInfo *pCreateInfos,
209 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
Tony-LunarG0f647382022-05-12 15:11:59 -0600210 const VkPipelineBindPoint bind_point, const SafeCreateInfo &modified_create_infos);
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600211
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600212 public:
Jeremy Gebben5160e032022-03-28 14:57:43 -0600213 bool aborted = false;
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600214 PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600215 const char *setup_vuid;
216 VkPhysicalDeviceFeatures supported_features{};
217 VkPhysicalDeviceFeatures desired_features{};
218 uint32_t adjusted_max_desc_sets = 0;
219 uint32_t unique_shader_module_id = 0;
220 uint32_t output_buffer_size = 0;
221 VkDescriptorSetLayout debug_desc_layout = VK_NULL_HANDLE;
222 VkDescriptorSetLayout dummy_desc_layout = VK_NULL_HANDLE;
223 uint32_t desc_set_bind_index = 0;
224 VmaAllocator vmaAllocator = {};
Jeremy Gebben5160e032022-03-28 14:57:43 -0600225 std::unique_ptr<UtilDescriptorSetManager> desc_set_manager;
Jeremy Gebben58cc9d12022-03-23 17:04:57 -0600226 vl_concurrent_unordered_map<uint32_t, GpuAssistedShaderTracker> shader_map;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600227 std::vector<VkDescriptorSetLayoutBinding> bindings_;
228};
229