1 /* 2 * Copyright (C) 2023 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 struct RenderPassAttachmentCompatibilityDesc; 36 37 struct LowLevelCommandBufferVk { 38 VkCommandBuffer commandBuffer { VK_NULL_HANDLE }; 39 VkSemaphore semaphore { VK_NULL_HANDLE }; 40 }; 41 42 struct ContextCommandPoolVk { 43 // every command buffer is taken from the single pool and pre-allocated 44 VkCommandPool commandPool { VK_NULL_HANDLE }; 45 LowLevelCommandBufferVk commandBuffer; 46 bool isRecorded { false }; 47 }; 48 49 struct ContextFramebufferCacheVk { 50 struct CacheValues { 51 uint64_t frameUseIndex { 0 }; 52 VkFramebuffer frameBuffer { VK_NULL_HANDLE }; 53 }; 54 BASE_NS::unordered_map<uint64_t, CacheValues> hashToElement; 55 }; 56 57 struct ContextRenderPassCacheVk { 58 struct CacheValues { 59 uint64_t frameUseIndex { 0 }; 60 VkRenderPass renderPass { VK_NULL_HANDLE }; 61 }; 62 BASE_NS::unordered_map<uint64_t, CacheValues> hashToElement; 63 }; 64 65 class NodeContextPoolManagerVk final : public NodeContextPoolManager { 66 public: 67 explicit NodeContextPoolManagerVk(Device& device, GpuResourceManager& gpuResourceManager, const GpuQueue& gpuQueue); 68 ~NodeContextPoolManagerVk(); 69 70 void BeginFrame() override; 71 72 const ContextCommandPoolVk& GetContextCommandPool() const; 73 74 LowLevelRenderPassDataVk GetRenderPassData(const RenderCommandBeginRenderPass& beginRenderPass); 75 76 private: 77 Device& device_; 78 GpuResourceManager& gpuResourceMgr_; 79 80 uint32_t bufferingIndex_ { 0 }; 81 82 BASE_NS::vector<ContextCommandPoolVk> commandPools_; 83 ContextFramebufferCacheVk framebufferCache_; 84 ContextRenderPassCacheVk renderPassCache_; 85 ContextRenderPassCacheVk renderPassCompatibilityCache_; 86 RenderPassCreatorVk renderPassCreator_; 87 #if ((RENDER_VALIDATION_ENABLED == 1) || (RENDER_VULKAN_VALIDATION_ENABLED == 1)) 88 void SetValidationDebugName(const BASE_NS::string_view debugName) override; 89 BASE_NS::string debugName_; 90 bool firstFrame_ { true }; 91 #endif 92 }; 93 RENDER_END_NAMESPACE() 94 95 #endif // VULKAN_NODE_CONTEXT_POOL_MANAGER_VK_H 96