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 API_RENDER_DEVICE_ISHADER_PIPELINE_BINDER_H
17 #define API_RENDER_DEVICE_ISHADER_PIPELINE_BINDER_H
18
19 #include <base/containers/array_view.h>
20 #include <base/containers/unique_ptr.h>
21 #include <render/device/pipeline_layout_desc.h>
22 #include <render/device/pipeline_state_desc.h>
23 #include <render/namespace.h>
24 #include <render/nodecontext/intf_pipeline_descriptor_set_binder.h>
25 #include <render/resource_handle.h>
26
RENDER_BEGIN_NAMESPACE()27 RENDER_BEGIN_NAMESPACE()
28 /** \addtogroup group_ishaderpipelinebinder
29 * @{
30 */
31 /** Shader pipeline binder object
32 */
33 class IShaderPipelineBinder {
34 public:
35 /** Bindable buffer */
36 struct Buffer {
37 /** Handle */
38 RenderHandleReference handle;
39 /** Byte offset to buffer */
40 uint32_t byteOffset { 0u };
41 /** Byte size for buffer binding */
42 uint32_t byteSize { PipelineStateConstants::GPU_BUFFER_WHOLE_SIZE };
43 };
44
45 /** Bindable image */
46 struct Image {
47 /** Handle */
48 RenderHandleReference handle;
49 /** Mip level for specific binding */
50 uint32_t mip { PipelineStateConstants::GPU_IMAGE_ALL_MIP_LEVELS };
51 /** Layer level for specific binding */
52 uint32_t layer { PipelineStateConstants::GPU_IMAGE_ALL_LAYERS };
53 /** Sampler handle for combined image sampler */
54 RenderHandleReference samplerHandle;
55 };
56
57 /** Bindable sampler */
58 struct Sampler {
59 /** Handle */
60 RenderHandleReference handle;
61 };
62
63 /** Bindable binding */
64 struct Binding {
65 /** Binding */
66 uint32_t binding { ~0u };
67 /** Index to resources arrays */
68 uint32_t resIdx { ~0u };
69 /** Type for resources arrays */
70 RenderHandleType type { RenderHandleType::UNDEFINED };
71 };
72
73 /** Bindable resource view
74 */
75 struct DescriptorSetView {
76 BASE_NS::array_view<const Buffer> buffers;
77 BASE_NS::array_view<const Image> images;
78 BASE_NS::array_view<const Sampler> samplers;
79
80 BASE_NS::array_view<const Binding> bindings;
81 };
82
83 /** Get binding validity. Checks through all bindings that they have valid handles.
84 */
85 virtual bool GetBindingValidity() const = 0;
86
87 /** Get shader handle.
88 * Shader handle is given with the creation with shader manager and it cannot be changed.
89 */
90 virtual RenderHandleReference GetShaderHandle() const = 0;
91
92 /** Bind any resource with defaults. Automatically checks needed flags from shader pipeline layout.
93 * @param set Set index
94 * @param binding Binding index
95 * @param handle Binding resource handle
96 */
97 virtual void Bind(const uint32_t set, const uint32_t binding, const RenderHandleReference& handle) = 0;
98
99 /** Set uniform data which will be bind to shader with UBO in set/binding.
100 * @param set Set index
101 * @param binding Binding index
102 * @param data Uniform data which is bind to shader with UBO in correct set/binding.
103 */
104 virtual void SetUniformData(
105 const uint32_t set, const uint32_t binding, const BASE_NS::array_view<const uint8_t> data) = 0;
106
107 /** Set push constant data for shader access.
108 * @param data Uniform data which is bind to shader with UBO in correct set/binding.
109 */
110 virtual void SetPushConstantData(const BASE_NS::array_view<const uint8_t> data) = 0;
111
112 /** Bind buffer.
113 * @param set Set index
114 * @param binding Binding index
115 * @param resource Binding resource
116 * buffer)
117 */
118 virtual void BindBuffer(const uint32_t set, const uint32_t binding, const Buffer& resource) = 0;
119
120 /** Bind image.
121 * @param set Set index
122 * @param binding Binding index
123 * @param resource Binding resource handle
124 */
125 virtual void BindImage(const uint32_t set, const uint32_t binding, const Image& resource) = 0;
126
127 /** Bind sampler
128 * @param set Set index
129 * @param binding Binding index
130 * @param handle Binding resource handle
131 */
132 virtual void BindSampler(const uint32_t set, const uint32_t binding, const Sampler& resource) = 0;
133
134 /** Get bindable resources
135 * @return BindableResourceView to resources
136 */
137 virtual DescriptorSetView GetDescriptorSetView(const uint32_t set) const = 0;
138
139 /** Get push constant data
140 * @return array_view of push data
141 */
142 virtual BASE_NS::array_view<const uint8_t> GetPushData() const = 0;
143
144 using Ptr = BASE_NS::refcnt_ptr<IShaderPipelineBinder>;
145
146 protected:
147 // reference counting
148 // take a new reference of the object
149 virtual void Ref() = 0;
150 // releases one reference of the object.
151 // no methods of the class shall be called after unref.
152 // The object could be destroyed, if last reference
153 virtual void Unref() = 0;
154 // allow refcnt_ptr to call Ref/Unref.
155 friend Ptr;
156
157 IShaderPipelineBinder() = default;
158 virtual ~IShaderPipelineBinder() = default;
159
160 IShaderPipelineBinder(const IShaderPipelineBinder&) = delete;
161 IShaderPipelineBinder& operator=(const IShaderPipelineBinder&) = delete;
162 IShaderPipelineBinder(IShaderPipelineBinder&&) = delete;
163 IShaderPipelineBinder& operator=(IShaderPipelineBinder&&) = delete;
164 };
165 /** @} */
166 RENDER_END_NAMESPACE()
167
168 #endif // API_RENDER_DEVICE_ISHADER_PIPELINE_BINDER_H
169