1 /*
2  * Copyright (c) 2024 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 VUKAN_PIPELINE_CREATE_FUNCTIONS_VK_H
17 #define VUKAN_PIPELINE_CREATE_FUNCTIONS_VK_H
18 
19 #include <vulkan/vulkan_core.h>
20 
21 #include <base/containers/vector.h>
22 #include <render/device/pipeline_layout_desc.h>
23 #include <render/device/pipeline_state_desc.h>
24 #include <render/namespace.h>
25 
26 #include "device/device.h"
27 #include "util/log.h"
28 #include "vulkan/node_context_descriptor_set_manager_vk.h"
29 
30 RENDER_BEGIN_NAMESPACE()
31 class DeviceVk;
32 struct RenderCommandBeginRenderPass;
33 struct RenderPassDesc;
34 struct RenderPassSubpassDesc;
35 
36 struct LowLevelRenderPassCompatibilityDescVk final {
37     struct Attachment {
38         VkFormat format { VkFormat::VK_FORMAT_UNDEFINED };
39         VkSampleCountFlagBits sampleCountFlags { VK_SAMPLE_COUNT_1_BIT };
40         VkImageAspectFlags aspectFlags { 0u };
41     };
42     Attachment attachments[PipelineStateConstants::MAX_RENDER_PASS_ATTACHMENT_COUNT];
43 };
44 
45 struct LowLevelRenderPassDataVk final : public LowLevelRenderPassData {
46     LowLevelRenderPassCompatibilityDescVk renderPassCompatibilityDesc;
47 
48     // these are not dynamic state values
49     VkViewport viewport { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
50     VkRect2D scissor { { 0, 0 }, { 0, 0 } };
51     VkExtent2D framebufferSize { 0, 0 };
52     uint32_t viewMask { 0u };
53 
54     uint64_t renderPassCompatibilityHash { 0 };
55     uint64_t renderPassHash { 0 };
56 
57     VkRenderPass renderPassCompatibility { VK_NULL_HANDLE };
58     VkRenderPass renderPass { VK_NULL_HANDLE };
59     uint32_t subpassIndex { 0u };
60 
61     uint64_t frameBufferHash { 0 };
62 
63     VkFramebuffer framebuffer { VK_NULL_HANDLE };
64 };
65 
66 struct LowLevelPipelineLayoutDataVk final : public LowLevelPipelineLayoutData {
67     VkPipelineLayout pipelineLayout { VK_NULL_HANDLE };
68     PLUGIN_STATIC_ASSERT(PipelineLayoutConstants::MAX_DESCRIPTOR_SET_COUNT == 4u);
69     LowLevelDescriptorSetVk descriptorSetLayouts[PipelineLayoutConstants::MAX_DESCRIPTOR_SET_COUNT];
70 };
71 
72 // Can be used only from a single thread.
73 // Typically one context creates of RenderPassCreatorVk to control render pass creation.
74 class RenderPassCreatorVk final {
75 public:
76     RenderPassCreatorVk() = default;
77     ~RenderPassCreatorVk() = default;
78 
79     // self-contained, only uses member vector for temporary storage
80     VkRenderPass CreateRenderPass(const DeviceVk& deviceVk, const RenderCommandBeginRenderPass& beginRenderPass,
81         const LowLevelRenderPassDataVk& lowLevelRenderPassData);
82     VkRenderPass CreateRenderPassCompatibility(const DeviceVk& deviceVk,
83         const RenderCommandBeginRenderPass& beginRenderPass, const LowLevelRenderPassDataVk& lowLevelRenderPassData);
84     void DestroyRenderPass(VkDevice device, VkRenderPass renderPass);
85 
86     struct RenderPassStorage1 {
87         BASE_NS::vector<VkSubpassDescription> subpassDescriptions;
88         BASE_NS::vector<VkSubpassDependency> subpassDependencies;
89         BASE_NS::vector<VkAttachmentReference> attachmentReferences;
90 
91         BASE_NS::vector<uint32_t> multiViewMasks;
92     };
93     struct RenderPassStorage2 {
94         BASE_NS::vector<VkSubpassDescription2KHR> subpassDescriptions;
95         BASE_NS::vector<VkSubpassDependency2KHR> subpassDependencies;
96         BASE_NS::vector<VkAttachmentReference2KHR> attachmentReferences;
97         BASE_NS::vector<VkSubpassDescriptionDepthStencilResolveKHR> subpassDescriptionsDepthStencilResolve;
98 #if (RENDER_VULKAN_FSR_ENABLED == 1)
99         BASE_NS::vector<VkFragmentShadingRateAttachmentInfoKHR> fragmentShadingRateAttachmentInfos;
100 #endif
101     };
102 
103 private:
104     RenderPassStorage1 rps1_;
105     RenderPassStorage2 rps2_;
106 };
107 RENDER_END_NAMESPACE()
108 
109 #endif // VUKAN_PIPELINE_CREATE_FUNCTIONS_VK_H
110