1 #ifndef DALI_INTERNAL_RENDER_RENDERER_H
2 #define DALI_INTERNAL_RENDER_RENDERER_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/common/list-wrapper.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <dali/public-api/math/matrix.h>
25 #include <dali/public-api/math/vector4.h>
26 #include <dali/public-api/rendering/texture-set.h>
27 #include <dali/public-api/signals/render-callback.h>
29 #include <dali/graphics-api/graphics-controller.h>
30 #include <dali/integration-api/debug.h>
31 #include <dali/internal/common/blending-options.h>
32 #include <dali/internal/common/const-string.h>
33 #include <dali/internal/common/message.h>
34 #include <dali/internal/common/type-abstraction-enums.h>
35 #include <dali/internal/event/common/property-input-impl.h>
36 #include <dali/internal/render/data-providers/render-data-provider.h>
37 #include <dali/internal/render/renderers/render-geometry.h>
38 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
39 #include <dali/internal/render/shaders/program.h>
40 #include <dali/internal/update/manager/render-instruction-processor.h>
51 class SceneController;
53 class NodeDataProvider;
54 class RenderInstruction; //for reflection effect
55 } // namespace SceneGraph
61 class PipelineCacheL2;
62 class UniformBufferManager;
63 class UniformBufferV2;
66 using PipelineCacheL2Container = std::list<PipelineCacheL2>;
67 using PipelineCachePtr = PipelineCacheL2Container::iterator;
69 using RendererKey = MemoryPoolKey<Render::Renderer>;
71 } //namespace Internal
73 // Ensure RendererKey can be used in Dali::Vector
75 struct TypeTraits<Internal::Render::RendererKey> : public BasicTypes<Internal::Render::RendererKey>
79 IS_TRIVIAL_TYPE = true
88 * Renderers are used to render meshes
89 * These objects are used during RenderManager::Render(), so properties modified during
90 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
96 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
98 struct StencilParameters
100 StencilParameters(RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask, int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail, StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass)
101 : stencilFunctionMask(stencilFunctionMask),
102 stencilFunctionReference(stencilFunctionReference),
103 stencilMask(stencilMask),
104 renderMode(renderMode),
105 stencilFunction(stencilFunction),
106 stencilOperationOnFail(stencilOperationOnFail),
107 stencilOperationOnZFail(stencilOperationOnZFail),
108 stencilOperationOnZPass(stencilOperationOnZPass)
112 int stencilFunctionMask; ///< The stencil function mask
113 int stencilFunctionReference; ///< The stencil function reference
114 int stencilMask; ///< The stencil mask
115 RenderMode::Type renderMode : 4; ///< The render mode
116 StencilFunction::Type stencilFunction : 4; ///< The stencil function
117 StencilOperation::Type stencilOperationOnFail : 4; ///< The stencil operation for stencil test fail
118 StencilOperation::Type stencilOperationOnZFail : 4; ///< The stencil operation for depth test fail
119 StencilOperation::Type stencilOperationOnZPass : 4; ///< The stencil operation for depth test pass
123 * @brief Global static initialize for Render::Renderer before new CommandBuffer's Render fill start.
125 static void PrepareCommandBuffer();
128 * Create a new renderer instance
129 * @param[in] dataProviders The data providers for the renderer
130 * @param[in] geometry The geometry for the renderer
131 * @param[in] blendingBitmask A bitmask of blending options.
132 * @param[in] blendColor The blend color
133 * @param[in] faceCullingMode The face-culling mode.
134 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
135 * @param[in] depthWriteMode Depth buffer write mode
136 * @param[in] depthTestMode Depth buffer test mode
137 * @param[in] depthFunction Depth function
138 * @param[in] stencilParameters Struct containing all stencil related options
140 static RendererKey NewKey(SceneGraph::RenderDataProvider* dataProviders,
141 Render::Geometry* geometry,
142 uint32_t blendingBitmask,
143 const Vector4& blendColor,
144 FaceCullingMode::Type faceCullingMode,
145 bool preMultipliedAlphaEnabled,
146 DepthWriteMode::Type depthWriteMode,
147 DepthTestMode::Type depthTestMode,
148 DepthFunction::Type depthFunction,
149 StencilParameters& stencilParameters);
153 * @param[in] dataProviders The data providers for the renderer
154 * @param[in] geometry The geometry for the renderer
155 * @param[in] blendingBitmask A bitmask of blending options.
156 * @param[in] blendColor The blend color
157 * @param[in] faceCullingMode The face-culling mode.
158 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
159 * @param[in] depthWriteMode Depth buffer write mode
160 * @param[in] depthTestMode Depth buffer test mode
161 * @param[in] depthFunction Depth function
162 * @param[in] stencilParameters Struct containing all stencil related options
164 Renderer(SceneGraph::RenderDataProvider* dataProviders,
165 Render::Geometry* geometry,
166 uint32_t blendingBitmask,
167 const Vector4& blendColor,
168 FaceCullingMode::Type faceCullingMode,
169 bool preMultipliedAlphaEnabled,
170 DepthWriteMode::Type depthWriteMode,
171 DepthTestMode::Type depthTestMode,
172 DepthFunction::Type depthFunction,
173 StencilParameters& stencilParameters);
176 * Second-phase construction.
177 * This is called when the renderer is inside render thread
178 * @param[in] graphicsController The graphics controller to use
179 * @param[in] programCache Cache of program objects
180 * @param[in] shaderCache Cache of shaders
181 * @param[in] uniformBufferManager Uniform buffer manager
182 * @param[in] pipelineCache Cache of pipelines
184 void Initialize(Graphics::Controller& graphicsController,
185 ProgramCache& programCache,
186 Render::ShaderCache& shaderCache,
187 Render::UniformBufferManager& uniformBufferManager,
188 Render::PipelineCache& pipelineCache);
196 * Overriden delete operator
197 * Deletes the renderer from its global memory pool
199 void operator delete(void* ptr);
202 * Get a pointer to the object from the given key.
203 * Used by MemoryPoolKey to provide pointer semantics.
205 static Renderer* Get(RendererKey::KeyType rendererKey);
208 * Change the geometry used by the renderer
209 * @param[in] geometry The new geometry
211 void SetGeometry(Render::Geometry* geometry);
213 void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
216 * @brief Returns a reference to an array of draw commands
217 * @return Valid array of draw commands (may be empty)
219 [[nodiscard]] const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
221 return mDrawCommands;
225 * Set the face-culling mode.
226 * @param[in] mode The face-culling mode.
228 void SetFaceCullingMode(FaceCullingMode::Type mode);
231 * Set the bitmask for blending options
232 * @param[in] bitmask A bitmask of blending options.
234 void SetBlendingBitMask(uint32_t bitmask);
237 * Set the blend color for blending options
238 * @param[in] blendColor The blend color
240 void SetBlendColor(const Vector4& color);
243 * Set the first element index to draw by the indexed draw
244 * @param[in] firstElement index of first element to draw
246 void SetIndexedDrawFirstElement(uint32_t firstElement);
249 * Set the number of elements to draw by the indexed draw
250 * @param[in] elementsCount number of elements to draw
252 void SetIndexedDrawElementsCount(uint32_t elementsCount);
255 * @brief Set whether the Pre-multiplied Alpha Blending is required
257 * @param[in] preMultipled whether alpha is pre-multiplied.
259 void EnablePreMultipliedAlpha(bool preMultipled);
262 * Sets the depth write mode
263 * @param[in] depthWriteMode The depth write mode
265 void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
268 * Query the Renderer's depth write mode
269 * @return The renderer depth write mode
271 [[nodiscard]] DepthWriteMode::Type GetDepthWriteMode() const;
274 * Sets the depth test mode
275 * @param[in] depthTestMode The depth test mode
277 void SetDepthTestMode(DepthTestMode::Type depthTestMode);
280 * Query the Renderer's depth test mode
281 * @return The renderer depth test mode
283 [[nodiscard]] DepthTestMode::Type GetDepthTestMode() const;
286 * Sets the depth function
287 * @param[in] depthFunction The depth function
289 void SetDepthFunction(DepthFunction::Type depthFunction);
292 * Query the Renderer's depth function
293 * @return The renderer depth function
295 [[nodiscard]] DepthFunction::Type GetDepthFunction() const;
298 * Sets the render mode
299 * @param[in] renderMode The render mode
301 void SetRenderMode(RenderMode::Type mode);
304 * Gets the render mode
305 * @return The render mode
307 [[nodiscard]] RenderMode::Type GetRenderMode() const;
310 * Sets the stencil function
311 * @param[in] stencilFunction The stencil function
313 void SetStencilFunction(StencilFunction::Type stencilFunction);
316 * Gets the stencil function
317 * @return The stencil function
319 [[nodiscard]] StencilFunction::Type GetStencilFunction() const;
322 * Sets the stencil function mask
323 * @param[in] stencilFunctionMask The stencil function mask
325 void SetStencilFunctionMask(int stencilFunctionMask);
328 * Gets the stencil function mask
329 * @return The stencil function mask
331 [[nodiscard]] int GetStencilFunctionMask() const;
334 * Sets the stencil function reference
335 * @param[in] stencilFunctionReference The stencil function reference
337 void SetStencilFunctionReference(int stencilFunctionReference);
340 * Gets the stencil function reference
341 * @return The stencil function reference
343 [[nodiscard]] int GetStencilFunctionReference() const;
346 * Sets the stencil mask
347 * @param[in] stencilMask The stencil mask
349 void SetStencilMask(int stencilMask);
352 * Gets the stencil mask
353 * @return The stencil mask
355 [[nodiscard]] int GetStencilMask() const;
358 * Sets the stencil operation for when the stencil test fails
359 * @param[in] stencilOperationOnFail The stencil operation
361 void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
364 * Gets the stencil operation for when the stencil test fails
365 * @return The stencil operation
367 [[nodiscard]] StencilOperation::Type GetStencilOperationOnFail() const;
370 * Sets the stencil operation for when the depth test fails
371 * @param[in] stencilOperationOnZFail The stencil operation
373 void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
376 * Gets the stencil operation for when the depth test fails
377 * @return The stencil operation
379 [[nodiscard]] StencilOperation::Type GetStencilOperationOnZFail() const;
382 * Sets the stencil operation for when the depth test passes
383 * @param[in] stencilOperationOnZPass The stencil operation
385 void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
388 * Gets the stencil operation for when the depth test passes
389 * @return The stencil operation
391 [[nodiscard]] StencilOperation::Type GetStencilOperationOnZPass() const;
394 * Called to upload during RenderManager::Render().
399 * Called to render during RenderManager::Render().
400 * @param[in,out] commandBuffer The command buffer to write into
401 * @param[in] bufferIndex The index of the previous update buffer.
402 * @param[in] node The node using this renderer
403 * @param[in] modelViewMatrix The model-view matrix.
404 * @param[in] viewMatrix The view matrix.
405 * @param[in] projectionMatrix The projection matrix.
406 * @param[in] size Size of the render item
407 * @param[in] blend If true, blending is enabled
408 * @param[in] instruction. for use case like reflection where CullFace needs to be adjusted
410 * @return True if commands have been added to the command buffer
412 bool Render(Graphics::CommandBuffer& commandBuffer,
413 BufferIndex bufferIndex,
414 const SceneGraph::NodeDataProvider& node,
415 const Matrix& modelMatrix,
416 const Matrix& modelViewMatrix,
417 const Matrix& viewMatrix,
418 const Matrix& projectionMatrix,
421 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
422 uint32_t queueIndex);
425 * Returns true if this will create a draw command with it's own geometry
426 * and shader. Some renderers don't have a shader/geometry, e.g. drawable-actor
427 * creates an empty renderer.
429 bool NeedsProgram() const;
432 * If we need a program, prepare it and return it.
433 * @param[in] instruction The render instruction
434 * @return the prepared program, or nullptr.
436 Program* PrepareProgram(const SceneGraph::RenderInstruction& instruction);
439 * Sets RenderCallback object
441 * @param[in] callback Valid pointer to RenderCallback object
443 void SetRenderCallback(RenderCallback* callback);
446 * Returns currently set RenderCallback object
448 * @return Valid pointer to RenderCallback object or nullptr
450 RenderCallback* GetRenderCallback()
452 return mRenderCallback;
456 * Returns internal RenderCallbackInput structure
457 * @return Valid reference to the RenderCallbackInput
459 RenderCallbackInput& GetRenderCallbackInput()
461 if(!mRenderCallbackInput)
463 mRenderCallbackInput = std::unique_ptr<RenderCallbackInput>(new RenderCallbackInput);
465 return *mRenderCallbackInput;
469 * Write the renderer's sort attributes to the passed in reference
471 * @param[out] sortAttributes
473 void SetSortAttributes(SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const;
476 * Sets the flag indicating whether shader changed.
478 * @param[in] value True if shader changed
480 void SetShaderChanged(bool value);
483 * Check if the renderer attributes/uniforms are updated and returns the flag
488 bool WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo,
489 Render::UniformBufferView& ubo,
493 bool WriteDefaultUniformV2(const Graphics::UniformInfo* uniformInfo,
494 const std::vector<std::unique_ptr<Render::UniformBufferView>>& uboViews,
498 void WriteUniform(Render::UniformBufferView& ubo,
499 const Graphics::UniformInfo& uniformInfo,
502 static void WriteUniform(Render::UniformBufferView& ubo,
503 const Graphics::UniformInfo& uniformInfo,
507 [[nodiscard]] FaceCullingMode::Type GetFaceCullMode() const
509 return mFaceCullingMode;
513 * @brief Gets update area after visual properties applied.
515 * @param[in] bufferIndex The index of the previous update buffer.
516 * @param[in] originalUpdateArea The original update area before apply the visual properties.
518 * @return The recalculated update area after visual properties applied.
520 Vector4 GetVisualTransformedUpdateArea(BufferIndex bufferIndex, const Vector4& originalUpdateArea) const noexcept;
523 * Detach a Renderer from the node provider.
524 * @param[in] node The node data provider to be detached renderer.
526 void DetachFromNodeDataProvider(const SceneGraph::NodeDataProvider& node);
529 * @brief Gets the update area of textures.
531 * @return The updated area of textures
533 Vector4 GetTextureUpdateArea() const noexcept;
536 struct UniformIndexMap;
539 Renderer(const Renderer&);
542 Renderer& operator=(const Renderer& rhs);
545 * Builds a uniform map based on the index of the cached location in the Program.
546 * @param[in] bufferIndex The index of the previous update buffer.
547 * @param[in] node The node using the renderer
548 * @param[in] size The size of the renderer
549 * @param[in] program The shader program on which to set the uniforms.
551 * @return the index of the node in change counters store / uniform maps store.
553 std::size_t BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program);
556 * Bind the textures and setup the samplers
557 * @param[in] commandBuffer The command buffer to record binding into
559 * @return True if all textures are bounded successfully. False otherwise.
561 bool BindTextures(Graphics::CommandBuffer& commandBuffer);
564 * Prepare a pipeline for this renderer.
566 * As a renderer can be re-used in a single frame (e.g. being used by multiple nodes, or
567 * by non-exclusive render tasks), we store a pipeline per node/instruction.
568 * In practice, the implementation will cached pipelines, so we normally only have
571 Graphics::Pipeline& PrepareGraphicsPipeline(
573 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
574 const SceneGraph::NodeDataProvider& node,
578 * Setup and write data to the uniform buffer
580 * @param[in] bufferIndex The current buffer index
581 * @param[in] commandBuffer The command buffer to bind the uniform buffer to
582 * @param[in] node The node using this renderer
583 * @param[in] modelViewMatrix The model-view matrix.
584 * @param[in] viewMatrix The view matrix.
585 * @param[in] projectionMatrix The projection matrix.
586 * @param[in] size Size of the render item
587 * @param[in] blend If true, blending is enabled
588 * @param[in] instruction The render instruction
589 * @param[in] The node index
591 void WriteUniformBuffer(BufferIndex bufferIndex,
592 Graphics::CommandBuffer& commandBuffer,
594 const SceneGraph::RenderInstruction& instruction,
595 const SceneGraph::NodeDataProvider& node,
596 const Matrix& modelMatrix,
597 const Matrix& modelViewMatrix,
598 const Matrix& viewMatrix,
599 const Matrix& projectionMatrix,
601 std::size_t nodeIndex);
604 * @brief Fill uniform buffer at index. Writes uniforms into given memory address
606 * @param[in] instruction The render instruction
607 * @param[in] uboViews Target uniform buffer object
608 * @param[in] updateBufferIndex update buffer index
609 * @param[in] nodeIndex Index of node/renderer pair in mUniformIndexMaps
611 void FillUniformBuffer(Program& program,
612 const SceneGraph::RenderInstruction& instruction,
613 const std::vector<std::unique_ptr<Render::UniformBufferView>>& uboViews,
614 BufferIndex updateBufferIndex,
615 std::size_t nodeIndex);
618 Graphics::Controller* mGraphicsController;
619 SceneGraph::RenderDataProvider* mRenderDataProvider;
621 Render::Geometry* mGeometry;
623 ProgramCache* mProgramCache{nullptr};
624 Render::ShaderCache* mShaderCache{nullptr};
626 Render::UniformBufferManager* mUniformBufferManager{};
627 std::vector<Graphics::UniformBufferBinding> mUniformBufferBindings{};
629 Render::PipelineCache* mPipelineCache{nullptr};
630 PipelineCachePtr mPipeline{};
632 using Hash = std::size_t;
634 typedef const float& (PropertyInputImpl::*FuncGetter)(BufferIndex) const;
636 struct UniformIndexMap
638 ConstString uniformName; ///< The uniform name
639 const PropertyInputImpl* propertyValue{nullptr}; ///< The property value
640 Hash uniformNameHash{0u};
641 Hash uniformNameHashNoArray{0u};
642 int32_t arrayIndex{-1}; ///< The array index
644 int16_t uniformLocation{0u};
645 uint16_t uniformOffset{0u};
646 uint16_t uniformSize{0u};
647 uint16_t uniformBlockIndex{0u};
648 FuncGetter uniformFunc{0};
651 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
652 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
654 uint32_t mIndexedDrawFirstElement; ///< Offset of first element to draw
655 uint32_t mIndexedDrawElementsCount; ///< Number of elements to draw
657 /** Struct to map node to index into mNodeMapCounters and mUniformIndexMaps */
658 struct RenderItemLookup
660 const SceneGraph::NodeDataProvider* node{nullptr}; ///<Node key. It can be nullptr if this NodeIndex don't need node uniform
662 std::size_t index{0}; ///<Index into mUniformIndexMap
663 std::size_t nodeChangeCounter{0}; ///<The last known change counter for this node's uniform map
664 std::size_t renderItemMapChangeCounter{0u}; ///< Change counter of the renderer & shader collected uniform map for this render item (node/renderer pair)
666 std::vector<RenderItemLookup> mNodeIndexMap; ///< usually only 1 element.
667 using UniformIndexMappings = std::vector<UniformIndexMap>;
668 std::vector<UniformIndexMappings> mUniformIndexMaps; ///< Cached map per node/renderer/shader.
670 DepthFunction::Type mDepthFunction : 4; ///< The depth function
671 FaceCullingMode::Type mFaceCullingMode : 3; ///< The mode of face culling
672 DepthWriteMode::Type mDepthWriteMode : 3; ///< The depth write mode
673 DepthTestMode::Type mDepthTestMode : 3; ///< The depth test mode
674 bool mPremultipliedAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
675 bool mShaderChanged : 1; ///< Flag indicating the shader changed and uniform maps have to be updated
676 bool mPipelineCached : 1; ///< Flag indicating whether renderer cache valid pipeline or not.
678 std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; // Devel stuff
679 RenderCallback* mRenderCallback{nullptr};
680 std::unique_ptr<RenderCallbackInput> mRenderCallbackInput{nullptr};
681 std::vector<Graphics::Texture*> mRenderCallbackTextureBindings{};
683 Program* mCurrentProgram{nullptr}; ///< Prefetched program
686 } // namespace Render
688 } // namespace Internal
692 #endif // DALI_INTERNAL_RENDER_RENDERER_H