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
60 struct PipelineCacheL2;
61 class UniformBufferManager;
62 class UniformBufferV2;
65 using PipelineCacheL2Container = std::list<PipelineCacheL2>;
66 using PipelineCachePtr = PipelineCacheL2Container::iterator;
68 using RendererKey = MemoryPoolKey<Render::Renderer>;
70 } //namespace Internal
72 // Ensure RendererKey can be used in Dali::Vector
74 struct TypeTraits<Internal::Render::RendererKey> : public BasicTypes<Internal::Render::RendererKey>
78 IS_TRIVIAL_TYPE = true
87 * Renderers are used to render meshes
88 * These objects are used during RenderManager::Render(), so properties modified during
89 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
95 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
97 struct StencilParameters
99 StencilParameters(RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask, int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail, StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass)
100 : stencilFunctionMask(stencilFunctionMask),
101 stencilFunctionReference(stencilFunctionReference),
102 stencilMask(stencilMask),
103 renderMode(renderMode),
104 stencilFunction(stencilFunction),
105 stencilOperationOnFail(stencilOperationOnFail),
106 stencilOperationOnZFail(stencilOperationOnZFail),
107 stencilOperationOnZPass(stencilOperationOnZPass)
111 int stencilFunctionMask; ///< The stencil function mask
112 int stencilFunctionReference; ///< The stencil function reference
113 int stencilMask; ///< The stencil mask
114 RenderMode::Type renderMode : 4; ///< The render mode
115 StencilFunction::Type stencilFunction : 4; ///< The stencil function
116 StencilOperation::Type stencilOperationOnFail : 4; ///< The stencil operation for stencil test fail
117 StencilOperation::Type stencilOperationOnZFail : 4; ///< The stencil operation for depth test fail
118 StencilOperation::Type stencilOperationOnZPass : 4; ///< The stencil operation for depth test pass
122 * @brief Global static initialize for Render::Renderer before new CommandBuffer's Render fill start.
124 static void PrepareCommandBuffer();
127 * Create a new renderer instance
128 * @param[in] dataProviders The data providers for the renderer
129 * @param[in] geometry The geometry for the renderer
130 * @param[in] blendingBitmask A bitmask of blending options.
131 * @param[in] blendColor The blend color
132 * @param[in] faceCullingMode The face-culling mode.
133 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
134 * @param[in] depthWriteMode Depth buffer write mode
135 * @param[in] depthTestMode Depth buffer test mode
136 * @param[in] depthFunction Depth function
137 * @param[in] stencilParameters Struct containing all stencil related options
139 static RendererKey NewKey(SceneGraph::RenderDataProvider* dataProviders,
140 Render::Geometry* geometry,
141 uint32_t blendingBitmask,
142 const Vector4& blendColor,
143 FaceCullingMode::Type faceCullingMode,
144 bool preMultipliedAlphaEnabled,
145 DepthWriteMode::Type depthWriteMode,
146 DepthTestMode::Type depthTestMode,
147 DepthFunction::Type depthFunction,
148 StencilParameters& stencilParameters);
152 * @param[in] dataProviders The data providers for the renderer
153 * @param[in] geometry The geometry for the renderer
154 * @param[in] blendingBitmask A bitmask of blending options.
155 * @param[in] blendColor The blend color
156 * @param[in] faceCullingMode The face-culling mode.
157 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
158 * @param[in] depthWriteMode Depth buffer write mode
159 * @param[in] depthTestMode Depth buffer test mode
160 * @param[in] depthFunction Depth function
161 * @param[in] stencilParameters Struct containing all stencil related options
163 Renderer(SceneGraph::RenderDataProvider* dataProviders,
164 Render::Geometry* geometry,
165 uint32_t blendingBitmask,
166 const Vector4& blendColor,
167 FaceCullingMode::Type faceCullingMode,
168 bool preMultipliedAlphaEnabled,
169 DepthWriteMode::Type depthWriteMode,
170 DepthTestMode::Type depthTestMode,
171 DepthFunction::Type depthFunction,
172 StencilParameters& stencilParameters);
175 * Second-phase construction.
176 * This is called when the renderer is inside render thread
177 * @param[in] graphicsController The graphics controller to use
178 * @param[in] programCache Cache of program objects
179 * @param[in] uniformBufferManager Uniform buffer manager
180 * @param[in] pipelineCache Cache of pipelines
182 void Initialize(Graphics::Controller& graphicsController,
183 ProgramCache& programCache,
184 Render::UniformBufferManager& uniformBufferManager,
185 Render::PipelineCache& pipelineCache);
193 * Overriden delete operator
194 * Deletes the renderer from its global memory pool
196 void operator delete(void* ptr);
199 * Get a pointer to the object from the given key.
200 * Used by MemoryPoolKey to provide pointer semantics.
202 static Renderer* Get(RendererKey::KeyType rendererKey);
205 * Change the geometry used by the renderer
206 * @param[in] geometry The new geometry
208 void SetGeometry(Render::Geometry* geometry);
210 void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
213 * @brief Returns a reference to an array of draw commands
214 * @return Valid array of draw commands (may be empty)
216 [[nodiscard]] const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
218 return mDrawCommands;
222 * Set the face-culling mode.
223 * @param[in] mode The face-culling mode.
225 void SetFaceCullingMode(FaceCullingMode::Type mode);
228 * Set the bitmask for blending options
229 * @param[in] bitmask A bitmask of blending options.
231 void SetBlendingBitMask(uint32_t bitmask);
234 * Set the blend color for blending options
235 * @param[in] blendColor The blend color
237 void SetBlendColor(const Vector4& color);
240 * Set the first element index to draw by the indexed draw
241 * @param[in] firstElement index of first element to draw
243 void SetIndexedDrawFirstElement(uint32_t firstElement);
246 * Set the number of elements to draw by the indexed draw
247 * @param[in] elementsCount number of elements to draw
249 void SetIndexedDrawElementsCount(uint32_t elementsCount);
252 * @brief Set whether the Pre-multiplied Alpha Blending is required
254 * @param[in] preMultipled whether alpha is pre-multiplied.
256 void EnablePreMultipliedAlpha(bool preMultipled);
259 * Sets the depth write mode
260 * @param[in] depthWriteMode The depth write mode
262 void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
265 * Query the Renderer's depth write mode
266 * @return The renderer depth write mode
268 [[nodiscard]] DepthWriteMode::Type GetDepthWriteMode() const;
271 * Sets the depth test mode
272 * @param[in] depthTestMode The depth test mode
274 void SetDepthTestMode(DepthTestMode::Type depthTestMode);
277 * Query the Renderer's depth test mode
278 * @return The renderer depth test mode
280 [[nodiscard]] DepthTestMode::Type GetDepthTestMode() const;
283 * Sets the depth function
284 * @param[in] depthFunction The depth function
286 void SetDepthFunction(DepthFunction::Type depthFunction);
289 * Query the Renderer's depth function
290 * @return The renderer depth function
292 [[nodiscard]] DepthFunction::Type GetDepthFunction() const;
295 * Sets the render mode
296 * @param[in] renderMode The render mode
298 void SetRenderMode(RenderMode::Type mode);
301 * Gets the render mode
302 * @return The render mode
304 [[nodiscard]] RenderMode::Type GetRenderMode() const;
307 * Sets the stencil function
308 * @param[in] stencilFunction The stencil function
310 void SetStencilFunction(StencilFunction::Type stencilFunction);
313 * Gets the stencil function
314 * @return The stencil function
316 [[nodiscard]] StencilFunction::Type GetStencilFunction() const;
319 * Sets the stencil function mask
320 * @param[in] stencilFunctionMask The stencil function mask
322 void SetStencilFunctionMask(int stencilFunctionMask);
325 * Gets the stencil function mask
326 * @return The stencil function mask
328 [[nodiscard]] int GetStencilFunctionMask() const;
331 * Sets the stencil function reference
332 * @param[in] stencilFunctionReference The stencil function reference
334 void SetStencilFunctionReference(int stencilFunctionReference);
337 * Gets the stencil function reference
338 * @return The stencil function reference
340 [[nodiscard]] int GetStencilFunctionReference() const;
343 * Sets the stencil mask
344 * @param[in] stencilMask The stencil mask
346 void SetStencilMask(int stencilMask);
349 * Gets the stencil mask
350 * @return The stencil mask
352 [[nodiscard]] int GetStencilMask() const;
355 * Sets the stencil operation for when the stencil test fails
356 * @param[in] stencilOperationOnFail The stencil operation
358 void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
361 * Gets the stencil operation for when the stencil test fails
362 * @return The stencil operation
364 [[nodiscard]] StencilOperation::Type GetStencilOperationOnFail() const;
367 * Sets the stencil operation for when the depth test fails
368 * @param[in] stencilOperationOnZFail The stencil operation
370 void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
373 * Gets the stencil operation for when the depth test fails
374 * @return The stencil operation
376 [[nodiscard]] StencilOperation::Type GetStencilOperationOnZFail() const;
379 * Sets the stencil operation for when the depth test passes
380 * @param[in] stencilOperationOnZPass The stencil operation
382 void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
385 * Gets the stencil operation for when the depth test passes
386 * @return The stencil operation
388 [[nodiscard]] StencilOperation::Type GetStencilOperationOnZPass() const;
391 * Called to upload during RenderManager::Render().
396 * Called to render during RenderManager::Render().
397 * @param[in,out] commandBuffer The command buffer to write into
398 * @param[in] bufferIndex The index of the previous update buffer.
399 * @param[in] node The node using this renderer
400 * @param[in] modelViewMatrix The model-view matrix.
401 * @param[in] viewMatrix The view matrix.
402 * @param[in] projectionMatrix The projection matrix.
403 * @param[in] scale Scale factor of the render item
404 * @param[in] size Size of the render item
405 * @param[in] blend If true, blending is enabled
406 * @param[in] instruction. for use case like reflection where CullFace needs to be adjusted
408 * @return True if commands have been added to the command buffer
410 bool Render(Graphics::CommandBuffer& commandBuffer,
411 BufferIndex bufferIndex,
412 const SceneGraph::NodeDataProvider& node,
413 const Matrix& modelMatrix,
414 const Matrix& modelViewMatrix,
415 const Matrix& viewMatrix,
416 const Matrix& projectionMatrix,
417 const Vector3& scale,
420 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
421 uint32_t queueIndex);
424 * Returns true if this will create a draw command with it's own geometry
425 * and shader. Some renderers don't have a shader/geometry, e.g. drawable-actor
426 * creates an empty renderer.
428 bool NeedsProgram() const;
431 * If we need a program, prepare it and return it.
432 * @param[in] instruction The render instruction
433 * @return the prepared program, or nullptr.
435 Program* PrepareProgram(const SceneGraph::RenderInstruction& instruction);
438 * Sets RenderCallback object
440 * @param[in] callback Valid pointer to RenderCallback object
442 void SetRenderCallback(RenderCallback* callback);
445 * Returns currently set RenderCallback object
447 * @return Valid pointer to RenderCallback object or nullptr
449 RenderCallback* GetRenderCallback()
451 return mRenderCallback;
455 * Returns internal RenderCallbackInput structure
456 * @return Valid reference to the RenderCallbackInput
458 RenderCallbackInput& GetRenderCallbackInput()
460 if(!mRenderCallbackInput)
462 mRenderCallbackInput = std::unique_ptr<RenderCallbackInput>(new RenderCallbackInput);
464 return *mRenderCallbackInput;
468 * Write the renderer's sort attributes to the passed in reference
470 * @param[out] sortAttributes
472 void SetSortAttributes(SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const;
475 * Sets the flag indicating whether shader changed.
477 * @param[in] value True if shader changed
479 void SetShaderChanged(bool value);
482 * Check if the renderer attributes/uniforms are updated and returns the flag
487 bool WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo,
488 Render::UniformBufferView& ubo,
492 bool WriteDefaultUniformV2(const Graphics::UniformInfo* uniformInfo,
493 const std::vector<std::unique_ptr<Render::UniformBufferView>>& uboViews,
497 void WriteUniform(Render::UniformBufferView& ubo,
498 const Graphics::UniformInfo& uniformInfo,
501 static void WriteUniform(Render::UniformBufferView& ubo,
502 const Graphics::UniformInfo& uniformInfo,
506 [[nodiscard]] FaceCullingMode::Type GetFaceCullMode() const
508 return mFaceCullingMode;
512 * @brief Gets update area after visual properties applied.
514 * @param[in] bufferIndex The index of the previous update buffer.
515 * @param[in] originalUpdateArea The original update area before apply the visual properties.
517 * @return The recalculated update area after visual properties applied.
519 Vector4 GetVisualTransformedUpdateArea(BufferIndex bufferIndex, const Vector4& originalUpdateArea) const noexcept;
522 * Detach a Renderer from the node provider.
523 * @param[in] node The node data provider to be detached renderer.
525 void DetachFromNodeDataProvider(const SceneGraph::NodeDataProvider& node);
528 * @brief Gets the update area of textures.
530 * @return The updated area of textures
532 Vector4 GetTextureUpdateArea() const noexcept;
535 struct UniformIndexMap;
538 Renderer(const Renderer&);
541 Renderer& operator=(const Renderer& rhs);
544 * Builds a uniform map based on the index of the cached location in the Program.
545 * @param[in] bufferIndex The index of the previous update buffer.
546 * @param[in] node The node using the renderer
547 * @param[in] program The shader program on which to set the uniforms.
549 * @return the index of the node in change counters store / uniform maps store.
551 std::size_t BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, Program& program);
554 * Bind the textures and setup the samplers
555 * @param[in] commandBuffer The command buffer to record binding into
557 * @return True if all textures are bounded successfully. False otherwise.
559 bool BindTextures(Graphics::CommandBuffer& commandBuffer);
562 * Prepare a pipeline for this renderer.
564 * As a renderer can be re-used in a single frame (e.g. being used by multiple nodes, or
565 * by non-exclusive render tasks), we store a pipeline per node/instruction.
566 * In practice, the implementation will cached pipelines, so we normally only have
569 Graphics::Pipeline& PrepareGraphicsPipeline(
571 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
572 const SceneGraph::NodeDataProvider& node,
576 * Setup and write data to the uniform buffer
578 * @param[in] bufferIndex The current buffer index
579 * @param[in] commandBuffer The command buffer to bind the uniform buffer to
580 * @param[in] node The node using this renderer
581 * @param[in] modelViewMatrix The model-view matrix.
582 * @param[in] viewMatrix The view matrix.
583 * @param[in] projectionMatrix The projection matrix.
584 * @param[in] scale Scale factor of the render item
585 * @param[in] size Size of the render item
586 * @param[in] blend If true, blending is enabled
587 * @param[in] instruction The render instruction
588 * @param[in] The node index
590 void WriteUniformBuffer(BufferIndex bufferIndex,
591 Graphics::CommandBuffer& commandBuffer,
593 const SceneGraph::RenderInstruction& instruction,
594 const SceneGraph::NodeDataProvider& node,
595 const Matrix& modelMatrix,
596 const Matrix& modelViewMatrix,
597 const Matrix& viewMatrix,
598 const Matrix& projectionMatrix,
599 const Vector3& scale,
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};
625 Render::UniformBufferManager* mUniformBufferManager{};
626 std::vector<Graphics::UniformBufferBinding> mUniformBufferBindings{};
628 Render::PipelineCache* mPipelineCache{nullptr};
629 PipelineCachePtr mPipeline{};
631 using Hash = std::size_t;
633 struct UniformIndexMap
635 ConstString uniformName; ///< The uniform name
636 const PropertyInputImpl* propertyValue{nullptr}; ///< The property value
637 Hash uniformNameHash{0u};
638 Hash uniformNameHashNoArray{0u};
639 int32_t arrayIndex{-1}; ///< The array index
641 int16_t uniformLocation{0u};
642 uint16_t uniformOffset{0u};
643 uint16_t uniformBlockIndex{0u};
644 bool initialized{false};
647 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
648 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
650 uint32_t mIndexedDrawFirstElement; ///< Offset of first element to draw
651 uint32_t mIndexedDrawElementsCount; ///< Number of elements to draw
653 /** Struct to map node to index into mNodeMapCounters and mUniformIndexMaps */
654 struct RenderItemLookup
656 const SceneGraph::NodeDataProvider* node{nullptr}; ///< Node key. It can be nullptr if this NodeIndex don't need node uniform
657 const Program* program{nullptr}; ///< Program key.
659 std::size_t index{0}; ///<Index into mUniformIndexMap
660 std::size_t nodeChangeCounter{0}; ///<The last known change counter for this node's uniform map
661 std::size_t renderItemMapChangeCounter{0u}; ///< Change counter of the renderer & shader collected uniform map for this render item (node/renderer pair)
663 std::vector<RenderItemLookup> mNodeIndexMap; ///< usually only 1 element.
664 using UniformIndexMappings = std::vector<UniformIndexMap>;
665 std::vector<UniformIndexMappings> mUniformIndexMaps; ///< Cached map per node/renderer/shader.
667 DepthFunction::Type mDepthFunction : 4; ///< The depth function
668 FaceCullingMode::Type mFaceCullingMode : 3; ///< The mode of face culling
669 DepthWriteMode::Type mDepthWriteMode : 3; ///< The depth write mode
670 DepthTestMode::Type mDepthTestMode : 3; ///< The depth test mode
671 bool mPremultipliedAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
672 bool mShaderChanged : 1; ///< Flag indicating the shader changed and uniform maps have to be updated
673 bool mPipelineCached : 1; ///< Flag indicating whether renderer cache valid pipeline or not.
675 std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; // Devel stuff
676 RenderCallback* mRenderCallback{nullptr};
677 std::unique_ptr<RenderCallbackInput> mRenderCallbackInput{nullptr};
678 std::vector<Graphics::Texture*> mRenderCallbackTextureBindings{};
680 Program* mCurrentProgram{nullptr}; ///< Prefetched program
683 } // namespace Render
685 } // namespace Internal
689 #endif // DALI_INTERNAL_RENDER_RENDERER_H