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 DEVICE_SHADER_PIPELINE_BINDER_H 17 #define DEVICE_SHADER_PIPELINE_BINDER_H 18 19 #include <atomic> 20 21 #include <base/containers/array_view.h> 22 #include <base/containers/refcnt_ptr.h> 23 #include <render/device/intf_shader_pipeline_binder.h> 24 #include <render/namespace.h> 25 #include <render/resource_handle.h> 26 27 #include "util/property_util.h" 28 29 RENDER_BEGIN_NAMESPACE() 30 class ShaderPipelineBinder; 31 32 class ShaderPipelineBinderPropertyBindingSignal final : public CustomPropertyWriteSignal { 33 public: 34 explicit ShaderPipelineBinderPropertyBindingSignal(ShaderPipelineBinder& shaderPipelineBinder); 35 ~ShaderPipelineBinderPropertyBindingSignal() override = default; 36 37 void Signal() override; 38 39 private: 40 ShaderPipelineBinder& shaderPipelineBinder_; 41 }; 42 43 class ShaderPipelineBinder : public IShaderPipelineBinder { 44 public: 45 ShaderPipelineBinder() = delete; 46 ShaderPipelineBinder( 47 IShaderManager& shaderMgr, const RenderHandleReference& shader, const PipelineLayout& pipelineLayout); 48 ~ShaderPipelineBinder() override = default; 49 50 void ClearBindings() override; 51 bool GetBindingValidity() const override; 52 RenderHandleReference GetShaderHandle() const override; 53 54 void Bind(uint32_t set, uint32_t binding, const RenderHandleReference& handle) override; 55 // Not yet in the API 56 void SetUniformData(uint32_t set, uint32_t binding, BASE_NS::array_view<const uint8_t> data); 57 void SetPushConstantData(BASE_NS::array_view<const uint8_t> data) override; 58 59 void BindBuffer(uint32_t set, uint32_t binding, const BindableBufferWithHandleReference& resource) override; 60 void BindBuffers(uint32_t set, uint32_t binding, 61 BASE_NS::array_view<const BindableBufferWithHandleReference> resources) override; 62 void BindImage(uint32_t set, uint32_t binding, const BindableImageWithHandleReference& resource) override; 63 void BindImages( 64 uint32_t set, uint32_t binding, BASE_NS::array_view<const BindableImageWithHandleReference> resources) override; 65 void BindSampler(uint32_t set, uint32_t binding, const BindableSamplerWithHandleReference& resource) override; 66 void BindSamplers(uint32_t set, uint32_t binding, 67 BASE_NS::array_view<const BindableSamplerWithHandleReference> resources) override; 68 69 void BindVertexBuffers(BASE_NS::array_view<const VertexBufferWithHandleReference> vertexBuffers) override; 70 void BindIndexBuffer(const IndexBufferWithHandleReference& indexBuffer) override; 71 void SetDrawCommand(const DrawCommand& drawCommand) override; 72 void SetDispatchCommand(const DispatchCommand& dispatchCommand) override; 73 74 DescriptorSetLayoutBindingResources GetDescriptorSetLayoutBindingResources(uint32_t set) const override; 75 76 BASE_NS::array_view<const uint8_t> GetPushConstantData() const override; 77 78 BASE_NS::array_view<const VertexBufferWithHandleReference> GetVertexBuffers() const override; 79 IndexBufferWithHandleReference GetIndexBuffer() const override; 80 DrawCommand GetDrawCommand() const override; 81 DispatchCommand GetDispatchCommand() const override; 82 83 PipelineLayout GetPipelineLayout() const override; 84 IPipelineDescriptorSetBinder* GetPipelineDescriptorSetBinder() const; 85 86 CORE_NS::IPropertyHandle* GetProperties() override; 87 CORE_NS::IPropertyHandle* GetBindingProperties() override; 88 ResourceBinding GetResourceBinding(uint32_t set, uint32_t binding) const override; 89 PropertyBindingView GetPropertyBindingView() const override; 90 uint32_t GetPropertyBindingByteSize() const override; 91 92 void BindPropertyBindings(); 93 94 struct CustomPropertyData { 95 uint32_t set { ~0U }; 96 uint32_t binding { ~0U }; 97 98 BASE_NS::unique_ptr<CustomPropertyPodContainer> properties; 99 CORE_NS::IPropertyHandle* handle { nullptr }; 100 }; 101 struct BindingPropertyData { 102 struct SetAndBinding { 103 uint32_t set { ~0U }; 104 uint32_t binding { ~0U }; 105 }; 106 107 BASE_NS::unique_ptr<ShaderPipelineBinderPropertyBindingSignal> bindingSignal; 108 109 BASE_NS::unique_ptr<CustomPropertyBindingContainer> properties; 110 CORE_NS::IPropertyHandle* handle { nullptr }; 111 BASE_NS::vector<SetAndBinding> setAndBindings; 112 }; 113 114 struct BindableBinding { 115 uint32_t binding { ~0U }; 116 uint32_t descriptorCount { ~0U }; 117 uint32_t resIdx { ~0U }; 118 uint32_t arrayoffset { ~0U }; 119 RenderHandleType type { RenderHandleType::UNDEFINED }; 120 }; 121 122 protected: 123 void Ref() override; 124 void Unref() override; 125 126 private: 127 void InitCustomProperties(); 128 void EvaluateCustomPropertyBindings(); 129 130 IShaderManager& shaderMgr_; 131 132 std::atomic<int32_t> refcnt_ { 0 }; 133 134 RenderHandleReference shader_; 135 PipelineLayout pipelineLayout_; 136 137 RenderHandleType renderHandleType_ { RenderHandleType::UNDEFINED }; 138 139 DispatchCommand dispatchCommand_; 140 DrawCommand drawCommand_; 141 142 BASE_NS::vector<VertexBufferWithHandleReference> vertexBuffers_; 143 IndexBufferWithHandleReference indexBuffer_; 144 145 // NOTE: initial version with multiple vectors per descriptor set 146 // vectors are resized in the constructor 147 struct DescriptorSetResources { 148 BASE_NS::vector<BindableBufferWithHandleReference> buffers; 149 BASE_NS::vector<BindableImageWithHandleReference> images; 150 BASE_NS::vector<BindableSamplerWithHandleReference> samplers; 151 152 BASE_NS::vector<BindableBinding> bindings; 153 154 uint8_t bindingToIndex[PipelineLayoutConstants::MAX_DESCRIPTOR_SET_BINDING_COUNT] { 16, 16, 16, 16, 16, 16, 16, 155 16, 16, 16, 16, 16, 16, 16, 16, 16 }; 156 }; 157 BASE_NS::vector<DescriptorSetResources> descriptorSetResources_; 158 159 BASE_NS::vector<uint8_t> pushData_; 160 161 CustomPropertyData customPropertyData_; 162 BindingPropertyData bindingPropertyData_; 163 164 // real binder object which can be re-used immediately during rendering 165 IPipelineDescriptorSetBinder::Ptr pipelineDescriptorSetBinder_; 166 // set -> actual vector index 167 uint32_t setToBinderIndex_[PipelineLayoutConstants::MAX_DESCRIPTOR_SET_COUNT] { ~0u, ~0u, ~0u, ~0u }; 168 }; 169 RENDER_END_NAMESPACE() 170 171 #endif // DEVICE_SHADER_PIPELINE_BINDER_H 172