• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef VULKAN_NODE_CONTEXT_POOL_MANAGER_VK_H
17 #define VULKAN_NODE_CONTEXT_POOL_MANAGER_VK_H
18 
19 #include <cstdint>
20 #include <vulkan/vulkan_core.h>
21 
22 #include <base/containers/string.h>
23 #include <base/containers/unordered_map.h>
24 #include <base/containers/vector.h>
25 #include <render/device/pipeline_state_desc.h>
26 #include <render/namespace.h>
27 
28 #include "nodecontext/node_context_pool_manager.h"
29 #include "vulkan/pipeline_create_functions_vk.h"
30 
31 RENDER_BEGIN_NAMESPACE()
32 class Device;
33 class GpuResourceManager;
34 struct RenderCommandBeginRenderPass;
35 
36 struct LowLevelCommandBufferVk {
37     VkCommandBuffer commandBuffer { VK_NULL_HANDLE };
38     VkSemaphore semaphore { VK_NULL_HANDLE };
39 };
40 
41 struct ContextCommandPoolVk {
42     // every command buffer is taken from the single pool and pre-allocated
43     VkCommandPool commandPool { VK_NULL_HANDLE };
44     LowLevelCommandBufferVk commandBuffer;
45 };
46 
47 struct ContextFramebufferCacheVk {
48     struct CacheValues {
49         uint64_t frameUseIndex { 0 };
50         VkFramebuffer frameBuffer { VK_NULL_HANDLE };
51     };
52     BASE_NS::unordered_map<uint64_t, CacheValues> hashToElement;
53 };
54 
55 struct ContextRenderPassCacheVk {
56     struct CacheValues {
57         uint64_t frameUseIndex { 0 };
58         VkRenderPass renderPass { VK_NULL_HANDLE };
59     };
60     BASE_NS::unordered_map<uint64_t, CacheValues> hashToElement;
61 };
62 
63 class NodeContextPoolManagerVk final : public NodeContextPoolManager {
64 public:
65     explicit NodeContextPoolManagerVk(Device& device, GpuResourceManager& gpuResourceManager, const GpuQueue& gpuQueue);
66     ~NodeContextPoolManagerVk() override;
67 
68     void BeginFrame() override;
69     void BeginBackendFrame() override;
70 
71     const ContextCommandPoolVk& GetContextCommandPool() const;
72     const ContextCommandPoolVk& GetContextSecondaryCommandPool() const;
73 
74     LowLevelRenderPassDataVk GetRenderPassData(const RenderCommandBeginRenderPass& beginRenderPass);
75 
76 private:
77     Device& device_;
78     GpuResourceManager& gpuResourceMgr_;
79     const GpuQueue gpuQueue_ {};
80 
81     uint32_t bufferingIndex_ { 0 };
82 
83     BASE_NS::vector<ContextCommandPoolVk> commandPools_;
84     BASE_NS::vector<ContextCommandPoolVk> commandSecondaryPools_;
85     ContextFramebufferCacheVk framebufferCache_;
86     ContextRenderPassCacheVk renderPassCache_;
87     ContextRenderPassCacheVk renderPassCompatibilityCache_;
88     RenderPassCreatorVk renderPassCreator_;
89 #if ((RENDER_VALIDATION_ENABLED == 1) || (RENDER_VULKAN_VALIDATION_ENABLED == 1))
90     void SetValidationDebugName(BASE_NS::string_view debugName) override;
91     BASE_NS::string debugName_;
92     bool firstFrame_ { true };
93 #endif
94 #if (RENDER_VALIDATION_ENABLED == 1)
95     uint64_t frameIndexFront_ { 0 };
96     uint64_t frameIndexBack_ { 0 };
97 #endif
98 };
99 RENDER_END_NAMESPACE()
100 
101 #endif // VULKAN_NODE_CONTEXT_POOL_MANAGER_VK_H
102