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 API_RENDER_INODE_CONTEXT_DESCRIPTORSET_MANAGER_H
17 #define API_RENDER_INODE_CONTEXT_DESCRIPTORSET_MANAGER_H
18 
19 #include <cstddef>
20 
21 #include <base/containers/array_view.h>
22 #include <base/containers/unique_ptr.h>
23 #include <base/containers/vector.h>
24 #include <render/namespace.h>
25 #include <render/nodecontext/intf_pipeline_descriptor_set_binder.h>
26 #include <render/render_data_structures.h>
27 #include <render/resource_handle.h>
28 
29 RENDER_BEGIN_NAMESPACE()
30 class Device;
31 struct PipelineLayout;
32 
33 /** @ingroup group_render_inodecontextdescriptorsetmanager */
34 /**
35  * INodeContextDescriptorSetManager
36  * Creates and caches descriptor sets.
37  *
38  * Single descriptor set can only be updated once per frame. I.e you need to create as many descriptor sets as you need
39  * per frame. (Descriptor sets are internally buffered for GPU frames).
40  *
41  * One should use normal descriptor sets and use ResetAndReserve when one needs more sets.
42  *
43  * There is a possiblity to use one frame descriptor sets which do not need reserving.
44  * The methods CreateOneFrameXXX creates a descriptor set which is valid for current frame.
45  * This could be used with user inputs where one cannot pre-calculate the need of descriptors properly.
46  */
47 class INodeContextDescriptorSetManager {
48 public:
49     INodeContextDescriptorSetManager(const INodeContextDescriptorSetManager&) = delete;
50     INodeContextDescriptorSetManager& operator=(const INodeContextDescriptorSetManager&) = delete;
51 
52     /** Reset non-dynamic (not one frame) descriptor sets and reserve descriptors.
53      * Reset only when a new pool is needed. (Currently allocated descriptors are not enough)
54      * Needs to reserve all the descriptors which are used in a single frame.
55      * To reiterate: All old descriptor set handles become invalid when this is called. */
56     virtual void ResetAndReserve(const DescriptorCounts& descriptorCounts) = 0;
57 
58     /** Reset non-dynamic (not one frame) descriptor sets and reserve descriptors.
59      * Reset only when a new pool is needed. (Currently allocated descriptors are not enough)
60      * Needs to reserve all the descriptors which are used in a single frame.
61      * To reiterate: All old descriptor set handles become invalid when this is called. */
62     virtual void ResetAndReserve(const BASE_NS::array_view<DescriptorCounts> descriptorCounts) = 0;
63 
64     /** Creates a new descriptor set and gives its handle. */
65     virtual RenderHandle CreateDescriptorSet(
66         const BASE_NS::array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings) = 0;
67     /** Creates new descriptor sets for all given descriptor sets. */
68     virtual BASE_NS::vector<RenderHandle> CreateDescriptorSets(
69         const BASE_NS::array_view<const DescriptorSetLayoutBindings> descriptorSetsLayoutBindings) = 0;
70     /** Creates a single descriptor set from pipeline layout. */
71     virtual RenderHandle CreateDescriptorSet(const uint32_t set, const PipelineLayout& pipelineLayout) = 0;
72 
73     /** Create a IDescriptorSetBinder based on a descriptor set handle and layout bindings.
74      * Layout bindings are expected to be sorted starting from the smallest index.
75      */
76     virtual IDescriptorSetBinder::Ptr CreateDescriptorSetBinder(const RenderHandle handle,
77         const BASE_NS::array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings) = 0;
78 
79     /** Create a IPipelineDescriptorSetBinder based on pipeline layout.
80      * @param pipelineLayout PipelineLayout
81      */
82     virtual IPipelineDescriptorSetBinder::Ptr CreatePipelineDescriptorSetBinder(
83         const PipelineLayout& pipelineLayout) = 0;
84 
85     /** Create a IPipelineDescriptorSetBinder based on pipeline layout, handles, and layout bindings.
86      * Pipeline layout sets and their bindings are expected to be sorted starting from smallest index.
87      * @param pipelineLayout PipelineLayout
88      * @param handles Descriptor set handles
89      * @param descriptorSetsLayoutBindings Descriptor set layout bindings
90      */
91     virtual IPipelineDescriptorSetBinder::Ptr CreatePipelineDescriptorSetBinder(const PipelineLayout& pipelineLayout,
92         const BASE_NS::array_view<const RenderHandle> handles,
93         const BASE_NS::array_view<const DescriptorSetLayoutBindings> descriptorSetsLayoutBindings) = 0;
94 
95     /** Creates a one frame new descriptor set and gives its handle. Valid for a one frame and does not need reserve. */
96     virtual RenderHandle CreateOneFrameDescriptorSet(
97         const BASE_NS::array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings) = 0;
98     /** Creates a one frame new dynamic descriptor sets for all given descriptor sets. Valid for a one frame and does
99      * not need reserve. */
100     virtual BASE_NS::vector<RenderHandle> CreateOneFrameDescriptorSets(
101         const BASE_NS::array_view<const DescriptorSetLayoutBindings> descriptorSetsLayoutBindings) = 0;
102     /** Creates a one frame dynamic single descriptor set from pipeline layout. Valid for a one frame and does not need
103      * reserve. */
104     virtual RenderHandle CreateOneFrameDescriptorSet(const uint32_t set, const PipelineLayout& pipelineLayout) = 0;
105 
106 protected:
107     INodeContextDescriptorSetManager() = default;
108     virtual ~INodeContextDescriptorSetManager() = default;
109 };
110 RENDER_END_NAMESPACE()
111 
112 #endif // API_RENDER_INODE_CONTEXT_DESCRIPTORSET_MANAGER_H
113