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_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 <core/property/intf_property_handle.h>
22 #include <render/device/pipeline_layout_desc.h>
23 #include <render/device/pipeline_state_desc.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 
RENDER_BEGIN_NAMESPACE()29 RENDER_BEGIN_NAMESPACE()
30 /** \addtogroup group_ishaderpipelinebinder
31  *  @{
32  */
33 /** Shader pipeline binder object
34  */
35 class IShaderPipelineBinder {
36 public:
37     /** Dispatch data */
38     struct DispatchCommand {
39         /**
40          * Render handle for dispatching.
41          * 1. If image -> imageSize / tgs
42          * 2. If buffer -> dispatch indirect
43          */
44         RenderHandleReference handle {};
45         /**
46          * Thread group count.
47          * Only used if the handle is invalid.
48          */
49         ShaderThreadGroup threadGroupCount;
50 
51         /** Indirect args buffer offset */
52         uint32_t argsOffset { 0U };
53     };
54     /** Draw command */
55     struct DrawCommand {
56         /** Vertex count */
57         uint32_t vertexCount { 0U };
58         /** Index count */
59         uint32_t indexCount { 0U };
60         /** Instance count */
61         uint32_t instanceCount { 1U };
62 
63         /** Indirect args */
64         RenderHandleReference argsHandle {};
65         /** Indirect args buffer offset */
66         uint32_t argsOffset { 0U };
67     };
68     /** Property binding data */
69     struct PropertyBindingView {
70         /** Descriptor set index */
71         uint32_t set { ~0U };
72         /** Descriptor set binding index */
73         uint32_t binding { ~0U };
74 
75         /** Array view to data */
76         BASE_NS::array_view<const uint8_t> data;
77     };
78     /** Resource binding */
79     struct ResourceBinding {
80         /** Descriptor set index */
81         uint32_t set { PipelineLayoutConstants::INVALID_INDEX };
82         /** Descriptor set binding index */
83         uint32_t binding { PipelineLayoutConstants::INVALID_INDEX };
84         /** Descriptor count */
85         uint32_t descriptorCount { 0U };
86         /** Descriptor set binding array offset for array resources. Offset to first array resource. */
87         uint32_t arrayOffset { 0U };
88 
89         /** Resource handle */
90         RenderHandleReference handle;
91     };
92 
93     /** Clear bindings.
94      */
95     virtual void ClearBindings() = 0;
96 
97     /** Get binding validity. Checks through all bindings that they have valid handles.
98      */
99     virtual bool GetBindingValidity() const = 0;
100 
101     /** Get shader handle.
102      * Shader handle is given with the creation with shader manager and it cannot be changed.
103      */
104     virtual RenderHandleReference GetShaderHandle() const = 0;
105 
106     /** Bind any resource with defaults. Automatically checks needed flags from shader pipeline layout.
107      * @param set Set index
108      * @param binding Binding index
109      * @param handle Binding resource handle
110      */
111     virtual void Bind(uint32_t set, uint32_t binding, const RenderHandleReference& handle) = 0;
112 
113     /** Set push constant data for shader access.
114      * @param data Uniform data which is bind to shader with UBO in correct set/binding.
115      */
116     virtual void SetPushConstantData(BASE_NS::array_view<const uint8_t> data) = 0;
117 
118     /** Bind buffer.
119      * @param set Set index
120      * @param binding Binding index
121      * @param resource Binding resource
122      * buffer)
123      */
124     virtual void BindBuffer(uint32_t set, uint32_t binding, const BindableBufferWithHandleReference& resource) = 0;
125 
126     /** Bind buffers for array binding.
127      * @param set Set index
128      * @param binding Binding index
129      * @param resources Binding resources
130      * buffer)
131      */
132     virtual void BindBuffers(
133         uint32_t set, uint32_t binding, BASE_NS::array_view<const BindableBufferWithHandleReference> resources) = 0;
134 
135     /** Bind image.
136      * @param set Set index
137      * @param binding Binding index
138      * @param resource Binding resource handle
139      */
140     virtual void BindImage(uint32_t set, uint32_t binding, const BindableImageWithHandleReference& resource) = 0;
141 
142     /** Bind images for array bindings.
143      * @param set Set index
144      * @param binding Binding index
145      * @param resource Binding resource handle
146      */
147     virtual void BindImages(
148         uint32_t set, uint32_t binding, BASE_NS::array_view<const BindableImageWithHandleReference> resources) = 0;
149 
150     /** Bind sampler
151      * @param set Set index
152      * @param binding Binding index
153      * @param handle Binding resource handle
154      */
155     virtual void BindSampler(uint32_t set, uint32_t binding, const BindableSamplerWithHandleReference& resource) = 0;
156 
157     /** Bind sampler for array bindings.
158      * @param set Set index
159      * @param binding Binding index
160      * @param handle Binding resource handle
161      */
162     virtual void BindSamplers(
163         uint32_t set, uint32_t binding, BASE_NS::array_view<const BindableSamplerWithHandleReference> resources) = 0;
164 
165     /** Bind vertex buffers
166      * @param vertexBuffers vertex buffers
167      */
168     virtual void BindVertexBuffers(BASE_NS::array_view<const VertexBufferWithHandleReference> vertexBuffers) = 0;
169 
170     /** Bind index Buffer
171      * @param indexBuffer vertex Buffer
172      */
173     virtual void BindIndexBuffer(const IndexBufferWithHandleReference& indexBuffer) = 0;
174 
175     /** Set draw command
176      * @param indexBuffer vertex Buffer
177      */
178     virtual void SetDrawCommand(const DrawCommand& drawCommand) = 0;
179 
180     /** Bind dispatch command
181      * @param indexBuffer vertex Buffer
182      */
183     virtual void SetDispatchCommand(const DispatchCommand& dispatchCommand) = 0;
184 
185     /** Get render time bindable resources
186      * @return DescriptorSetLayoutBindingResources to resources
187      */
188     virtual DescriptorSetLayoutBindingResources GetDescriptorSetLayoutBindingResources(uint32_t set) const = 0;
189 
190     /** Get push constant data
191      * @return array_view of push data
192      */
193     virtual BASE_NS::array_view<const uint8_t> GetPushConstantData() const = 0;
194 
195     /** Get vertex buffers
196      * @return View of vertex buffers
197      */
198     virtual BASE_NS::array_view<const VertexBufferWithHandleReference> GetVertexBuffers() const = 0;
199 
200     /** Get index buffers
201      * @return IndexBuffer
202      */
203     virtual IndexBufferWithHandleReference GetIndexBuffer() const = 0;
204 
205     /** Get draw command
206      * @return DrawCommand
207      */
208     virtual DrawCommand GetDrawCommand() const = 0;
209 
210     /** Get dispatch command
211      * @return DispatchCommand
212      */
213     virtual DispatchCommand GetDispatchCommand() const = 0;
214 
215     /** Get pipeline Layout
216      * @return PipelineLayout
217      */
218     virtual PipelineLayout GetPipelineLayout() const = 0;
219 
220     /** Get property handle for built-in custom, material properties. Check th pointer always.
221      *  @return Pointer to property handle if properties present, nullptr otherwise.
222      */
223    virtual CORE_NS::IPropertyHandle* GetProperties() = 0;
224 
225    /** Get binding property handle for built-in binding properties. Check the pointer always.
226     * @return Pointer to property handle if properties present, nullptr otherwise.
227     */
228    virtual CORE_NS::IPropertyHandle* GetBindingProperties() = 0;
229 
230     /** Get resource binding.
231      * @param set Set index
232      * @param binding Binding index
233      * @return Resource binding
234      */
235     virtual ResourceBinding GetResourceBinding(uint32_t set, uint32_t binding) const = 0;
236 
237     /** Get property binding data and binding information
238      *  the data is not automatically move to GPU access (render node(s) should handle that)
239      * @return PropertyBindingView
240      */
241     virtual PropertyBindingView GetPropertyBindingView() const = 0;
242 
243     /** Get property binding byte size.
244      * @return Data byte size.
245      */
246     virtual uint32_t GetPropertyBindingByteSize() const = 0;
247 
248     using Ptr = BASE_NS::refcnt_ptr<IShaderPipelineBinder>;
249 
250 protected:
251     // reference counting
252     // take a new reference of the object
253     virtual void Ref() = 0;
254     // releases one reference of the object.
255     // no methods of the class shall be called after unref.
256     // The object could be destroyed, if last reference
257     virtual void Unref() = 0;
258     // allow refcnt_ptr to call Ref/Unref.
259     friend Ptr;
260 
261     IShaderPipelineBinder() = default;
262     virtual ~IShaderPipelineBinder() = default;
263 
264     IShaderPipelineBinder(const IShaderPipelineBinder&) = delete;
265     IShaderPipelineBinder& operator=(const IShaderPipelineBinder&) = delete;
266     IShaderPipelineBinder(IShaderPipelineBinder&&) = delete;
267     IShaderPipelineBinder& operator=(IShaderPipelineBinder&&) = delete;
268 };
269 /** @} */
270 RENDER_END_NAMESPACE()
271 
272 #endif // API_RENDER_DEVICE_ISHADER_PIPELINE_BINDER_H
273