• 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__PIPELINE_DESCRIPTOR_SET_BINDER_H
17 #define RENDER_RENDER__PIPELINE_DESCRIPTOR_SET_BINDER_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/array_view.h>
22 #include <base/containers/vector.h>
23 #include <render/device/pipeline_layout_desc.h>
24 #include <render/device/pipeline_state_desc.h>
25 #include <render/namespace.h>
26 #include <render/nodecontext/intf_pipeline_descriptor_set_binder.h>
27 #include <render/resource_handle.h>
28 
29 #include "device/gpu_resource_handle_util.h"
30 #include "util/log.h"
31 
RENDER_BEGIN_NAMESPACE()32 RENDER_BEGIN_NAMESPACE()
33 /** DescriptorSetBinder.
34  */
35 class DescriptorSetBinder final : public IDescriptorSetBinder {
36 public:
37     DescriptorSetBinder(const RenderHandle handle,
38         const BASE_NS::array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings);
39     ~DescriptorSetBinder() = default;
40 
41     void ClearBindings() override;
42     RenderHandle GetDescriptorSetHandle() const override;
43     DescriptorSetLayoutBindingResources GetDescriptorSetLayoutBindingResources() const override;
44 
45     bool GetDescriptorSetLayoutBindingValidity() const override;
46 
47     // all must call this
48     void BindBuffer(const uint32_t binding, const BindableBuffer& resource) override;
49     void BindBuffer(const uint32_t binding, const RenderHandle handle, const uint32_t byteOffset) override;
50     void BindBuffer(
51         const uint32_t binding, const RenderHandle handle, const uint32_t byteOffset, const uint32_t byteSize) override;
52     // for descriptor array binding
53     void BindBuffers(const uint32_t binding, const BASE_NS::array_view<const BindableBuffer> resources) override;
54 
55     // all must call this
56     void BindImage(const uint32_t binding, const BindableImage& resource) override;
57     void BindImage(const uint32_t binding, const RenderHandle handle) override;
58     void BindImage(const uint32_t binding, const RenderHandle handle, const RenderHandle samplerHandle) override;
59     // for descriptor array binding
60     void BindImages(const uint32_t binding, const BASE_NS::array_view<const BindableImage> resources) override;
61 
62     // all must call this
63     void BindSampler(const uint32_t binding, const BindableSampler& resource) override;
64     void BindSampler(const uint32_t binding, const RenderHandle handle) override;
65     // for descriptor array binding
66     void BindSamplers(const uint32_t binding, const BASE_NS::array_view<const BindableSampler> resources) override;
67 
68 protected:
69     void Destroy() override;
70 
71 private:
72     void InitFillBindings(const BASE_NS::array_view<const DescriptorSetLayoutBinding> descriptorSetLayoutBindings,
73         const uint32_t bufferCount, const uint32_t imageCount, const uint32_t samplerCount);
74 
75     RenderHandle handle_;
76     BASE_NS::vector<DescriptorSetLayoutBindingResource> bindings_;
77 
78     BASE_NS::vector<BufferDescriptor> buffers_;
79     BASE_NS::vector<ImageDescriptor> images_;
80     BASE_NS::vector<SamplerDescriptor> samplers_;
81 
82     // accesses array with binding number and retuns index;
83     PLUGIN_STATIC_ASSERT(PipelineLayoutConstants::MAX_DESCRIPTOR_SET_BINDING_COUNT == 16u);
84     uint8_t bindingToIndex_[PipelineLayoutConstants::MAX_DESCRIPTOR_SET_BINDING_COUNT] { 16, 16, 16, 16, 16, 16, 16, 16,
85         16, 16, 16, 16, 16, 16, 16, 16 };
86     uint32_t maxBindingCount_ { 0u };
87 
88     uint32_t descriptorBindingMask_ { 0 };
89     uint32_t bindingMask_ { 0 };
90 };
91 
92 /** PipelineDescriptorSetBinder.
93  */
94 class PipelineDescriptorSetBinder final : public IPipelineDescriptorSetBinder {
95 public:
96     PipelineDescriptorSetBinder(const PipelineLayout& pipelineLayout,
97         const BASE_NS::array_view<const RenderHandle> handles,
98         const BASE_NS::array_view<const DescriptorSetLayoutBindings> descriptorSetsLayoutBindings);
99     ~PipelineDescriptorSetBinder() = default;
100 
101     void ClearBindings() override;
102 
103     RenderHandle GetDescriptorSetHandle(const uint32_t set) const override;
104     DescriptorSetLayoutBindingResources GetDescriptorSetLayoutBindingResources(const uint32_t set) const override;
105     bool GetPipelineDescriptorSetLayoutBindingValidity() const override;
106 
107     uint32_t GetDescriptorSetCount() const override;
108     BASE_NS::array_view<const uint32_t> GetSetIndices() const override;
109     BASE_NS::array_view<const RenderHandle> GetDescriptorSetHandles() const override;
110 
111     uint32_t GetFirstSet() const override;
112     BASE_NS::array_view<const RenderHandle> GetDescriptorSetHandles(
113         const uint32_t beginSet, const uint32_t count) const override;
114 
115     void BindBuffer(const uint32_t set, const uint32_t binding, const BindableBuffer& resource) override;
116     void BindBuffers(
117         const uint32_t set, const uint32_t binding, const BASE_NS::array_view<const BindableBuffer> resources) override;
118 
119     void BindImage(const uint32_t set, const uint32_t binding, const BindableImage& resource) override;
120     void BindImages(
121         const uint32_t set, const uint32_t binding, const BASE_NS::array_view<const BindableImage> resources) override;
122 
123     void BindSampler(const uint32_t set, const uint32_t binding, const BindableSampler& resource) override;
124     void BindSamplers(const uint32_t set, const uint32_t binding,
125         const BASE_NS::array_view<const BindableSampler> resources) override;
126 
127 protected:
128     void Destroy() override;
129 
130 private:
131     // set -> actual vector index
132     uint32_t setToBinderIndex_[PipelineLayoutConstants::MAX_DESCRIPTOR_SET_COUNT] { ~0u, ~0u, ~0u, ~0u };
133 
134     BASE_NS::vector<DescriptorSetBinder> descriptorSetBinders_;
135 
136     BASE_NS::vector<uint32_t> setIndices_;
137     BASE_NS::vector<RenderHandle> descriptorSetHandles_;
138 };
139 RENDER_END_NAMESPACE()
140 
141 #endif // CORE__RENDER__PIPELINE_DESCRIPTOR_SET_BINDER_H
142