• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2018-2019 The Khronos Group Inc.
2  * Copyright (c) 2018-2019 Valve Corporation
3  * Copyright (c) 2018-2019 LunarG, Inc.
4  * Copyright (C) 2018-2019 Google Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 
20 #include "vk_mem_alloc.h"
21 
22 #ifndef VULKAN_GPU_VALIDATION_H
23 #define VULKAN_GPU_VALIDATION_H
24 
25 struct GpuDeviceMemoryBlock {
26     VkBuffer buffer;
27     VmaAllocation allocation;
28     std::unordered_map<uint32_t, const cvdescriptorset::Descriptor *> update_at_submit;
29 };
30 
31 struct GpuBufferInfo {
32     GpuDeviceMemoryBlock output_mem_block;
33     GpuDeviceMemoryBlock input_mem_block;
34     VkDescriptorSet desc_set;
35     VkDescriptorPool desc_pool;
36     VkPipelineBindPoint pipeline_bind_point;
GpuBufferInfoGpuBufferInfo37     GpuBufferInfo(GpuDeviceMemoryBlock output_mem_block, GpuDeviceMemoryBlock input_mem_block, VkDescriptorSet desc_set,
38                   VkDescriptorPool desc_pool, VkPipelineBindPoint pipeline_bind_point)
39         : output_mem_block(output_mem_block),
40           input_mem_block(input_mem_block),
41           desc_set(desc_set),
42           desc_pool(desc_pool),
43           pipeline_bind_point(pipeline_bind_point){};
44 };
45 
46 struct GpuQueueBarrierCommandInfo {
47     VkCommandPool barrier_command_pool = VK_NULL_HANDLE;
48     VkCommandBuffer barrier_command_buffer = VK_NULL_HANDLE;
49 };
50 
51 // Class to encapsulate Descriptor Set allocation.  This manager creates and destroys Descriptor Pools
52 // as needed to satisfy requests for descriptor sets.
53 class GpuDescriptorSetManager {
54    public:
55     GpuDescriptorSetManager(CoreChecks *dev_data);
56     ~GpuDescriptorSetManager();
57 
58     VkResult GetDescriptorSets(uint32_t count, VkDescriptorPool *pool, std::vector<VkDescriptorSet> *desc_sets);
59     void PutBackDescriptorSet(VkDescriptorPool desc_pool, VkDescriptorSet desc_set);
60 
61    private:
62     static const uint32_t kItemsPerChunk = 512;
63     struct PoolTracker {
64         uint32_t size;
65         uint32_t used;
66     };
67 
68     CoreChecks *dev_data_;
69     std::unordered_map<VkDescriptorPool, struct PoolTracker> desc_pool_map_;
70 };
71 
72 struct GpuValidationState {
73     bool aborted;
74     bool reserve_binding_slot;
75     VkDescriptorSetLayout debug_desc_layout;
76     VkDescriptorSetLayout dummy_desc_layout;
77     uint32_t adjusted_max_desc_sets;
78     uint32_t desc_set_bind_index;
79     uint32_t unique_shader_module_id;
80     std::unordered_map<uint32_t, ShaderTracker> shader_map;
81     std::unique_ptr<GpuDescriptorSetManager> desc_set_manager;
82     std::map<VkQueue, GpuQueueBarrierCommandInfo> queue_barrier_command_infos;
83     std::unordered_map<VkCommandBuffer, std::vector<GpuBufferInfo>> command_buffer_map;  // gpu_buffer_list;
84     uint32_t output_buffer_size;
85     VmaAllocator vmaAllocator;
86     PFN_vkSetDeviceLoaderData vkSetDeviceLoaderData;
87     GpuValidationState(bool aborted = false, bool reserve_binding_slot = false, uint32_t unique_shader_module_id = 0,
88                        VmaAllocator vmaAllocator = {})
abortedGpuValidationState89         : aborted(aborted),
90           reserve_binding_slot(reserve_binding_slot),
91           unique_shader_module_id(unique_shader_module_id),
92           vmaAllocator(vmaAllocator){};
93 
GetGpuBufferInfoGpuValidationState94     std::vector<GpuBufferInfo> &GetGpuBufferInfo(const VkCommandBuffer command_buffer) {
95         auto buffer_list = command_buffer_map.find(command_buffer);
96         if (buffer_list == command_buffer_map.end()) {
97             std::vector<GpuBufferInfo> new_list{};
98             command_buffer_map[command_buffer] = new_list;
99             return command_buffer_map[command_buffer];
100         }
101         return buffer_list->second;
102     }
103 };
104 
105 using mutex_t = std::mutex;
106 using lock_guard_t = std::lock_guard<mutex_t>;
107 using unique_lock_t = std::unique_lock<mutex_t>;
108 
109 #endif  // VULKAN_GPU_VALIDATION_H
110