• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 RENDER_RENDER__NODE_CONTEXT_DESCRIPTOR_SET_MANAGER_H
17 #define RENDER_RENDER__NODE_CONTEXT_DESCRIPTOR_SET_MANAGER_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/string.h>
22 #include <base/containers/unordered_map.h>
23 #include <base/containers/vector.h>
24 #include <render/device/pipeline_layout_desc.h>
25 #include <render/namespace.h>
26 #include <render/nodecontext/intf_node_context_descriptor_set_manager.h>
27 #include <render/resource_handle.h>
28 
29 #include "device/gpu_resource_handle_util.h"
30 
31 RENDER_BEGIN_NAMESPACE()
32 class Device;
33 class IDescriptorSetBinder;
34 class IPipelineDescriptorSetBinder;
35 
36 struct DynamicOffsetDescriptors {
37     BASE_NS::array_view<const RenderHandle> resources;
38 };
39 
40 /**
41 class NodeContextDescriptorSetManager.
42 */
43 class NodeContextDescriptorSetManager : public INodeContextDescriptorSetManager {
44 public:
45     ~NodeContextDescriptorSetManager() override = default;
46 
IsDynamicDescriptor(const DescriptorType descType)47     static constexpr inline bool IsDynamicDescriptor(const DescriptorType descType)
48     {
49         return ((descType == DescriptorType::CORE_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
50                 (descType == DescriptorType::CORE_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC));
51     }
52 
53     void ResetAndReserve(const DescriptorCounts& aDescriptorCounts) override;
54 
55     virtual RenderHandle CreateDescriptorSet(
56         const BASE_NS::array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings) override = 0;
57     BASE_NS::vector<RenderHandle> CreateDescriptorSets(
58         const BASE_NS::array_view<const DescriptorSetLayoutBindings> descriptorSetsLayoutBindings) override;
59     RenderHandle CreateDescriptorSet(const uint32_t set, const PipelineLayout& pipelineLayout) override;
60 
61     IDescriptorSetBinder::Ptr CreateDescriptorSetBinder(const RenderHandle handle,
62         const BASE_NS::array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings) override;
63 
64     IPipelineDescriptorSetBinder::Ptr CreatePipelineDescriptorSetBinder(const PipelineLayout& pipelineLayout) override;
65     IPipelineDescriptorSetBinder::Ptr CreatePipelineDescriptorSetBinder(const PipelineLayout& pipelineLayout,
66         const BASE_NS::array_view<const RenderHandle> handles,
67         const BASE_NS::array_view<const DescriptorSetLayoutBindings> descriptorSetsLayoutBindings) override;
68 
69     virtual RenderHandle CreateOneFrameDescriptorSet(
70         const BASE_NS::array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings) override = 0;
71     virtual BASE_NS::vector<RenderHandle> CreateOneFrameDescriptorSets(
72         const BASE_NS::array_view<const DescriptorSetLayoutBindings> descriptorSetsLayoutBindings) override;
73     virtual RenderHandle CreateOneFrameDescriptorSet(const uint32_t set, const PipelineLayout& pipelineLayout) override;
74 
75     DescriptorSetLayoutBindingResources GetCpuDescriptorSetData(const RenderHandle handle) const;
76     DynamicOffsetDescriptors GetDynamicOffsetDescriptors(const RenderHandle handle) const;
77 
78     virtual void BeginFrame();
79 
80     // information for barrier creation what kind of descriptors does the descriptor set contain
81     // if returns false -> no need for barriers and/or layout changes
82     bool HasDynamicBarrierResources(const RenderHandle handle) const;
83     // count of dynamic offset needed when binding descriptor set
84     uint32_t GetDynamicOffsetDescriptorCount(const RenderHandle handle) const;
85 
86     // information that this descriptor has a special platform hwbuffer resources bound
87     // they might need some special platform specific handling
88     bool HasPlatformBufferBindings(const RenderHandle handle) const;
89 
90     // update descriptor sets for cpu data (adds correct gpu queue as well)
91     void UpdateCpuDescriptorSet(const RenderHandle handle, const DescriptorSetLayoutBindingResources& bindingResources,
92         const GpuQueue& gpuQueue);
93     // call from backend before actual graphics api updateDescriptorset()
94     // advances the gpu handle to the next available descriptor set (ring buffer)
95     virtual void UpdateDescriptorSetGpuHandle(const RenderHandle handle) = 0;
96 
97     struct CpuDescriptorSet {
98         uint32_t currentGpuBufferingIndex { 0 };
99         bool gpuDescriptorSetCreated { false };
100         bool isDirty { false };
101         bool hasDynamicBarrierResources { false };
102         bool hasPlatformConversionBindings { false }; // e.g. hwbuffers with ycbcr / OES
103 
104         BASE_NS::vector<DescriptorSetLayoutBindingResource> bindings;
105 
106         BASE_NS::vector<BufferDescriptor> buffers;
107         BASE_NS::vector<ImageDescriptor> images;
108         BASE_NS::vector<SamplerDescriptor> samplers;
109 
110         // gpu buffers with dynamic offsets
111         BASE_NS::vector<RenderHandle> dynamicOffsetDescriptors;
112     };
113 
114 #if (RENDER_VALIDATION_ENABLED == 1)
115     struct DescriptorCountValidation {
116         BASE_NS::unordered_map<uint32_t, int32_t> typeToCount;
117     };
118 #endif
119 #if ((RENDER_VALIDATION_ENABLED == 1) || (RENDER_VULKAN_VALIDATION_ENABLED == 1))
120     void SetValidationDebugName(const BASE_NS::string_view debugName);
121 #endif
122 protected:
123     static constexpr uint32_t ONE_FRAME_DESC_SET_BIT { 1u };
124     enum DescriptorSetIndexType : uint8_t {
125         DESCRIPTOR_SET_INDEX_TYPE_STATIC = 0,
126         DESCRIPTOR_SET_INDEX_TYPE_ONE_FRAME,
127         DESCRIPTOR_SET_INDEX_TYPE_COUNT,
128     };
129 
130     NodeContextDescriptorSetManager() = default;
131     BASE_NS::vector<CpuDescriptorSet> cpuDescriptorSets_[DESCRIPTOR_SET_INDEX_TYPE_COUNT];
132 
133     uint32_t maxSets_ { 0 };
134     // indicates if there are some sets updated on CPU which have platfrom conversion bindings
135     bool hasPlatformConversionBindings_ { false };
136 
137     void UpdateCpuDescriptorSetImpl(const uint32_t index, const DescriptorSetLayoutBindingResources& bindingResources,
138         const GpuQueue& gpuQueue, BASE_NS::vector<CpuDescriptorSet>& cpuDescriptorSets);
139     DescriptorSetLayoutBindingResources GetCpuDescriptorSetDataImpl(
140         const uint32_t index, const BASE_NS::vector<CpuDescriptorSet>& cpuDescriptorSet) const;
141     DynamicOffsetDescriptors GetDynamicOffsetDescriptorsImpl(
142         const uint32_t index, const BASE_NS::vector<CpuDescriptorSet>& cpuDescriptorSet) const;
143     bool HasDynamicBarrierResourcesImpl(
144         const uint32_t index, const BASE_NS::vector<CpuDescriptorSet>& cpuDescriptorSet) const;
145     uint32_t GetDynamicOffsetDescriptorCountImpl(
146         const uint32_t index, const BASE_NS::vector<CpuDescriptorSet>& cpuDescriptorSet) const;
147     bool HasPlatformBufferBindingsImpl(
148         const uint32_t index, const BASE_NS::vector<CpuDescriptorSet>& cpuDescriptorSet) const;
149 
150 #if ((RENDER_VALIDATION_ENABLED == 1) || (RENDER_VULKAN_VALIDATION_ENABLED == 1))
151     BASE_NS::string debugName_;
152 #endif
153 private:
154 #if (RENDER_VALIDATION_ENABLED == 1)
155     DescriptorCountValidation descriptorCountValidation_;
156 #endif
157 };
158 RENDER_END_NAMESPACE()
159 
160 #endif // CORE__RENDER__NODE_CONTEXT_DESCRIPTOR_SET_MANAGER_H
161