blob: ae001b97592964fd66f3ab4ffb77f0eebc2596f8 [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 Gebben33717862022-03-28 15:53:56 -060096 void PreCallRecordCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
97 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, void *modified_create_info) override;
98 void CreateDevice(const VkDeviceCreateInfo *pCreateInfo) override;
Jeremy Gebben33717862022-03-28 15:53:56 -060099 void PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) override;
100
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600101 void PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence,
102 VkResult result) override;
103 void RecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, VkFence fence, VkResult result);
104 void PostCallRecordQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, VkFence fence,
105 VkResult result) override;
106 void PostCallRecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence,
107 VkResult result) override;
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600108 void PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
109 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
110 void *cpl_state_data) override;
111 void PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
112 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
113 VkResult result) override;
114
115 void PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
116 const VkGraphicsPipelineCreateInfo *pCreateInfos,
117 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
118 void *cgpl_state_data) override;
119 void PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
120 const VkComputePipelineCreateInfo *pCreateInfos,
121 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
122 void *ccpl_state_data) override;
123 void PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
124 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
125 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
126 void *crtpl_state_data) override;
127 void PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
128 VkPipelineCache pipelineCache, uint32_t count,
129 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
130 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
131 void *crtpl_state_data) override;
132 void PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
133 const VkGraphicsPipelineCreateInfo *pCreateInfos,
134 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
135 void *cgpl_state_data) override;
136 void PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
137 const VkComputePipelineCreateInfo *pCreateInfos,
138 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
139 void *ccpl_state_data) override;
140 void PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
141 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
142 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
143 void *crtpl_state_data) override;
144 void PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
145 VkPipelineCache pipelineCache, uint32_t count,
146 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
147 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
148 VkResult result, void *crtpl_state_data) override;
149 void PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) override;
150
Jeremy Gebben33717862022-03-28 15:53:56 -0600151 template <typename T>
152 void ReportSetupProblem(T object, const char *const specific_message) const {
153 LogError(object, setup_vuid, "Setup Error. Detail: (%s)", specific_message);
154 }
155
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600156 protected:
Jeremy Gebben5ca80b32022-04-11 10:58:39 -0600157 bool CommandBufferNeedsProcessing(VkCommandBuffer command_buffer) const;
158 void ProcessCommandBuffer(VkQueue queue, VkCommandBuffer command_buffer);
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600159
160 void SubmitBarrier(VkQueue queue) {
161 auto queue_state = Get<gpu_utils_state::Queue>(queue);
162 if (queue_state) {
163 queue_state->SubmitBarrier();
164 }
165 }
166
167 std::shared_ptr<QUEUE_STATE> CreateQueue(VkQueue q, uint32_t index, VkDeviceQueueCreateFlags flags) override {
168 return std::static_pointer_cast<QUEUE_STATE>(std::make_shared<gpu_utils_state::Queue>(*this, q, index, flags));
169 }
170
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600171 template <typename CreateInfo, typename SafeCreateInfo>
172 void PreCallRecordPipelineCreations(uint32_t count, const CreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator,
173 VkPipeline *pPipelines, std::vector<std::shared_ptr<PIPELINE_STATE>> &pipe_state,
174 std::vector<SafeCreateInfo> *new_pipeline_create_infos,
175 const VkPipelineBindPoint bind_point);
176 template <typename CreateInfo>
177 void PostCallRecordPipelineCreations(const uint32_t count, const CreateInfo *pCreateInfos,
178 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
179 const VkPipelineBindPoint bind_point);
180
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600181 public:
Jeremy Gebben5160e032022-03-28 14:57:43 -0600182 bool aborted = false;
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600183 PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600184 const char *setup_vuid;
185 VkPhysicalDeviceFeatures supported_features{};
186 VkPhysicalDeviceFeatures desired_features{};
187 uint32_t adjusted_max_desc_sets = 0;
188 uint32_t unique_shader_module_id = 0;
189 uint32_t output_buffer_size = 0;
190 VkDescriptorSetLayout debug_desc_layout = VK_NULL_HANDLE;
191 VkDescriptorSetLayout dummy_desc_layout = VK_NULL_HANDLE;
192 uint32_t desc_set_bind_index = 0;
193 VmaAllocator vmaAllocator = {};
Jeremy Gebben5160e032022-03-28 14:57:43 -0600194 std::unique_ptr<UtilDescriptorSetManager> desc_set_manager;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600195 layer_data::unordered_map<uint32_t, GpuAssistedShaderTracker> shader_map;
196 std::vector<VkDescriptorSetLayoutBinding> bindings_;
197};
198