blob: 533f8531d8a0a798544b590fdb4d16d60f7af120 [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 Gebbenfcfc33c2022-03-28 15:31:29 -060065} // namespace gpu_utils_state
66VALSTATETRACK_DERIVED_STATE_OBJECT(VkQueue, gpu_utils_state::Queue, QUEUE_STATE);
67
Tony-LunarGb5fae462020-03-05 12:43:25 -070068VkResult UtilInitializeVma(VkPhysicalDevice physical_device, VkDevice device, VmaAllocator *pAllocator);
Tony-LunarG1dce2392019-10-23 16:49:29 -060069
Tony-LunarGb5fae462020-03-05 12:43:25 -070070void UtilGenerateStageMessage(const uint32_t *debug_record, std::string &msg);
71void UtilGenerateCommonMessage(const debug_report_data *report_data, const VkCommandBuffer commandBuffer,
72 const uint32_t *debug_record, const VkShaderModule shader_module_handle,
73 const VkPipeline pipeline_handle, const VkPipelineBindPoint pipeline_bind_point,
74 const uint32_t operation_index, std::string &msg);
sfricke-samsung7fac88a2022-01-26 11:44:22 -080075void UtilGenerateSourceMessages(const std::vector<uint32_t> &pgm, const uint32_t *debug_record, bool from_printf,
Tony-LunarGb5fae462020-03-05 12:43:25 -070076 std::string &filename_msg, std::string &source_msg);
Jeremy Gebben5160e032022-03-28 14:57:43 -060077
78struct GpuAssistedShaderTracker {
79 VkPipeline pipeline;
80 VkShaderModule shader_module;
81 std::vector<uint32_t> pgm;
82};
83
84class GpuAssistedBase : public ValidationStateTracker {
85 public:
Jeremy Gebben33717862022-03-28 15:53:56 -060086 void PreCallRecordCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
87 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, void *modified_create_info) override;
88 void CreateDevice(const VkDeviceCreateInfo *pCreateInfo) override;
Jeremy Gebben33717862022-03-28 15:53:56 -060089 void PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) override;
90
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -060091 void PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence,
92 VkResult result) override;
93 void RecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, VkFence fence, VkResult result);
94 void PostCallRecordQueueSubmit2KHR(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2KHR *pSubmits, VkFence fence,
95 VkResult result) override;
96 void PostCallRecordQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence,
97 VkResult result) override;
Jeremy Gebbenefd97802022-03-28 16:45:05 -060098 void PreCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
99 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
100 void *cpl_state_data) override;
101 void PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
102 const VkAllocationCallbacks *pAllocator, VkPipelineLayout *pPipelineLayout,
103 VkResult result) override;
104
105 void PreCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
106 const VkGraphicsPipelineCreateInfo *pCreateInfos,
107 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
108 void *cgpl_state_data) override;
109 void PreCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
110 const VkComputePipelineCreateInfo *pCreateInfos,
111 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
112 void *ccpl_state_data) override;
113 void PreCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
114 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
115 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
116 void *crtpl_state_data) override;
117 void PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
118 VkPipelineCache pipelineCache, uint32_t count,
119 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
120 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
121 void *crtpl_state_data) override;
122 void PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
123 const VkGraphicsPipelineCreateInfo *pCreateInfos,
124 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
125 void *cgpl_state_data) override;
126 void PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
127 const VkComputePipelineCreateInfo *pCreateInfos,
128 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
129 void *ccpl_state_data) override;
130 void PostCallRecordCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
131 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
132 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result,
133 void *crtpl_state_data) override;
134 void PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
135 VkPipelineCache pipelineCache, uint32_t count,
136 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
137 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
138 VkResult result, void *crtpl_state_data) override;
139 void PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) override;
140
Jeremy Gebben33717862022-03-28 15:53:56 -0600141 template <typename T>
142 void ReportSetupProblem(T object, const char *const specific_message) const {
143 LogError(object, setup_vuid, "Setup Error. Detail: (%s)", specific_message);
144 }
145
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600146 protected:
147 virtual bool CommandBufferNeedsProcessing(VkCommandBuffer command_buffer) = 0;
148 virtual void ProcessCommandBuffer(VkQueue queue, VkCommandBuffer command_buffer) = 0;
149
150 void SubmitBarrier(VkQueue queue) {
151 auto queue_state = Get<gpu_utils_state::Queue>(queue);
152 if (queue_state) {
153 queue_state->SubmitBarrier();
154 }
155 }
156
157 std::shared_ptr<QUEUE_STATE> CreateQueue(VkQueue q, uint32_t index, VkDeviceQueueCreateFlags flags) override {
158 return std::static_pointer_cast<QUEUE_STATE>(std::make_shared<gpu_utils_state::Queue>(*this, q, index, flags));
159 }
160
Jeremy Gebbenefd97802022-03-28 16:45:05 -0600161 template <typename CreateInfo, typename SafeCreateInfo>
162 void PreCallRecordPipelineCreations(uint32_t count, const CreateInfo *pCreateInfos, const VkAllocationCallbacks *pAllocator,
163 VkPipeline *pPipelines, std::vector<std::shared_ptr<PIPELINE_STATE>> &pipe_state,
164 std::vector<SafeCreateInfo> *new_pipeline_create_infos,
165 const VkPipelineBindPoint bind_point);
166 template <typename CreateInfo>
167 void PostCallRecordPipelineCreations(const uint32_t count, const CreateInfo *pCreateInfos,
168 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
169 const VkPipelineBindPoint bind_point);
170
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600171 public:
Jeremy Gebben5160e032022-03-28 14:57:43 -0600172 bool aborted = false;
Jeremy Gebbenfcfc33c2022-03-28 15:31:29 -0600173 PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600174 const char *setup_vuid;
175 VkPhysicalDeviceFeatures supported_features{};
176 VkPhysicalDeviceFeatures desired_features{};
177 uint32_t adjusted_max_desc_sets = 0;
178 uint32_t unique_shader_module_id = 0;
179 uint32_t output_buffer_size = 0;
180 VkDescriptorSetLayout debug_desc_layout = VK_NULL_HANDLE;
181 VkDescriptorSetLayout dummy_desc_layout = VK_NULL_HANDLE;
182 uint32_t desc_set_bind_index = 0;
183 VmaAllocator vmaAllocator = {};
Jeremy Gebben5160e032022-03-28 14:57:43 -0600184 std::unique_ptr<UtilDescriptorSetManager> desc_set_manager;
Jeremy Gebben5160e032022-03-28 14:57:43 -0600185 layer_data::unordered_map<uint32_t, GpuAssistedShaderTracker> shader_map;
186 std::vector<VkDescriptorSetLayoutBinding> bindings_;
187};
188