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/vector-wrapper.h>
23 #include <dali/public-api/math/matrix.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/public-api/rendering/texture-set.h>
26 #include <dali/public-api/signals/render-callback.h>
28 #include <dali/graphics-api/graphics-controller.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/internal/common/blending-options.h>
31 #include <dali/internal/common/const-string.h>
32 #include <dali/internal/common/message.h>
33 #include <dali/internal/common/type-abstraction-enums.h>
34 #include <dali/internal/event/common/property-input-impl.h>
35 #include <dali/internal/render/data-providers/render-data-provider.h>
36 #include <dali/internal/render/renderers/render-geometry.h>
37 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
38 #include <dali/internal/render/shaders/program.h>
39 #include <dali/internal/update/manager/render-instruction-processor.h>
50 class SceneController;
52 class NodeDataProvider;
53 class RenderInstruction; //for reflection effect
54 } // namespace SceneGraph
60 class PipelineCacheL2;
61 class UniformBufferManager;
64 using RendererKey = MemoryPoolKey<Render::Renderer>;
66 } //namespace Internal
68 // Ensure RendererKey can be used in Dali::Vector
70 struct TypeTraits<Internal::Render::RendererKey> : public BasicTypes<Internal::Render::RendererKey>
74 IS_TRIVIAL_TYPE = true
83 * Renderers are used to render meshes
84 * These objects are used during RenderManager::Render(), so properties modified during
85 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
91 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
93 struct StencilParameters
95 StencilParameters(RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask, int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail, StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass)
96 : stencilFunctionMask(stencilFunctionMask),
97 stencilFunctionReference(stencilFunctionReference),
98 stencilMask(stencilMask),
99 renderMode(renderMode),
100 stencilFunction(stencilFunction),
101 stencilOperationOnFail(stencilOperationOnFail),
102 stencilOperationOnZFail(stencilOperationOnZFail),
103 stencilOperationOnZPass(stencilOperationOnZPass)
107 int stencilFunctionMask; ///< The stencil function mask
108 int stencilFunctionReference; ///< The stencil function reference
109 int stencilMask; ///< The stencil mask
110 RenderMode::Type renderMode : 4; ///< The render mode
111 StencilFunction::Type stencilFunction : 4; ///< The stencil function
112 StencilOperation::Type stencilOperationOnFail : 4; ///< The stencil operation for stencil test fail
113 StencilOperation::Type stencilOperationOnZFail : 4; ///< The stencil operation for depth test fail
114 StencilOperation::Type stencilOperationOnZPass : 4; ///< The stencil operation for depth test pass
118 * @brief Global static initialize for Render::Renderer before new CommandBuffer's Render fill start.
120 static void PrepareCommandBuffer();
123 * Create a new renderer instance
124 * @param[in] dataProviders The data providers for the renderer
125 * @param[in] geometry The geometry for the renderer
126 * @param[in] blendingBitmask A bitmask of blending options.
127 * @param[in] blendColor The blend color
128 * @param[in] faceCullingMode The face-culling mode.
129 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
130 * @param[in] depthWriteMode Depth buffer write mode
131 * @param[in] depthTestMode Depth buffer test mode
132 * @param[in] depthFunction Depth function
133 * @param[in] stencilParameters Struct containing all stencil related options
135 static RendererKey NewKey(SceneGraph::RenderDataProvider* dataProviders,
136 Render::Geometry* geometry,
137 uint32_t blendingBitmask,
138 const Vector4& blendColor,
139 FaceCullingMode::Type faceCullingMode,
140 bool preMultipliedAlphaEnabled,
141 DepthWriteMode::Type depthWriteMode,
142 DepthTestMode::Type depthTestMode,
143 DepthFunction::Type depthFunction,
144 StencilParameters& stencilParameters);
148 * @param[in] dataProviders The data providers for the renderer
149 * @param[in] geometry The geometry for the renderer
150 * @param[in] blendingBitmask A bitmask of blending options.
151 * @param[in] blendColor The blend color
152 * @param[in] faceCullingMode The face-culling mode.
153 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
154 * @param[in] depthWriteMode Depth buffer write mode
155 * @param[in] depthTestMode Depth buffer test mode
156 * @param[in] depthFunction Depth function
157 * @param[in] stencilParameters Struct containing all stencil related options
159 Renderer(SceneGraph::RenderDataProvider* dataProviders,
160 Render::Geometry* geometry,
161 uint32_t blendingBitmask,
162 const Vector4& blendColor,
163 FaceCullingMode::Type faceCullingMode,
164 bool preMultipliedAlphaEnabled,
165 DepthWriteMode::Type depthWriteMode,
166 DepthTestMode::Type depthTestMode,
167 DepthFunction::Type depthFunction,
168 StencilParameters& stencilParameters);
171 * Second-phase construction.
172 * This is called when the renderer is inside render thread
173 * @param[in] graphicsController The graphics controller to use
174 * @param[in] programCache Cache of program objects
175 * @param[in] shaderCache Cache of shaders
176 * @param[in] uniformBufferManager Uniform buffer manager
177 * @param[in] pipelineCache Cache of pipelines
179 void Initialize(Graphics::Controller& graphicsController,
180 ProgramCache& programCache,
181 Render::ShaderCache& shaderCache,
182 Render::UniformBufferManager& uniformBufferManager,
183 Render::PipelineCache& pipelineCache);
191 * Overriden delete operator
192 * Deletes the renderer from its global memory pool
194 void operator delete(void* ptr);
197 * Get a pointer to the object from the given key.
198 * Used by MemoryPoolKey to provide pointer semantics.
200 static Renderer* Get(RendererKey::KeyType rendererKey);
203 * Change the geometry used by the renderer
204 * @param[in] geometry The new geometry
206 void SetGeometry(Render::Geometry* geometry);
208 void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
211 * @brief Returns a reference to an array of draw commands
212 * @return Valid array of draw commands (may be empty)
214 [[nodiscard]] const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
216 return mDrawCommands;
220 * Set the face-culling mode.
221 * @param[in] mode The face-culling mode.
223 void SetFaceCullingMode(FaceCullingMode::Type mode);
226 * Set the bitmask for blending options
227 * @param[in] bitmask A bitmask of blending options.
229 void SetBlendingBitMask(uint32_t bitmask);
232 * Set the blend color for blending options
233 * @param[in] blendColor The blend color
235 void SetBlendColor(const Vector4& color);
238 * Set the first element index to draw by the indexed draw
239 * @param[in] firstElement index of first element to draw
241 void SetIndexedDrawFirstElement(uint32_t firstElement);
244 * Set the number of elements to draw by the indexed draw
245 * @param[in] elementsCount number of elements to draw
247 void SetIndexedDrawElementsCount(uint32_t elementsCount);
250 * @brief Set whether the Pre-multiplied Alpha Blending is required
252 * @param[in] preMultipled whether alpha is pre-multiplied.
254 void EnablePreMultipliedAlpha(bool preMultipled);
257 * Sets the depth write mode
258 * @param[in] depthWriteMode The depth write mode
260 void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
263 * Query the Renderer's depth write mode
264 * @return The renderer depth write mode
266 [[nodiscard]] DepthWriteMode::Type GetDepthWriteMode() const;
269 * Sets the depth test mode
270 * @param[in] depthTestMode The depth test mode
272 void SetDepthTestMode(DepthTestMode::Type depthTestMode);
275 * Query the Renderer's depth test mode
276 * @return The renderer depth test mode
278 [[nodiscard]] DepthTestMode::Type GetDepthTestMode() const;
281 * Sets the depth function
282 * @param[in] depthFunction The depth function
284 void SetDepthFunction(DepthFunction::Type depthFunction);
287 * Query the Renderer's depth function
288 * @return The renderer depth function
290 [[nodiscard]] DepthFunction::Type GetDepthFunction() const;
293 * Sets the render mode
294 * @param[in] renderMode The render mode
296 void SetRenderMode(RenderMode::Type mode);
299 * Gets the render mode
300 * @return The render mode
302 [[nodiscard]] RenderMode::Type GetRenderMode() const;
305 * Sets the stencil function
306 * @param[in] stencilFunction The stencil function
308 void SetStencilFunction(StencilFunction::Type stencilFunction);
311 * Gets the stencil function
312 * @return The stencil function
314 [[nodiscard]] StencilFunction::Type GetStencilFunction() const;
317 * Sets the stencil function mask
318 * @param[in] stencilFunctionMask The stencil function mask
320 void SetStencilFunctionMask(int stencilFunctionMask);
323 * Gets the stencil function mask
324 * @return The stencil function mask
326 [[nodiscard]] int GetStencilFunctionMask() const;
329 * Sets the stencil function reference
330 * @param[in] stencilFunctionReference The stencil function reference
332 void SetStencilFunctionReference(int stencilFunctionReference);
335 * Gets the stencil function reference
336 * @return The stencil function reference
338 [[nodiscard]] int GetStencilFunctionReference() const;
341 * Sets the stencil mask
342 * @param[in] stencilMask The stencil mask
344 void SetStencilMask(int stencilMask);
347 * Gets the stencil mask
348 * @return The stencil mask
350 [[nodiscard]] int GetStencilMask() const;
353 * Sets the stencil operation for when the stencil test fails
354 * @param[in] stencilOperationOnFail The stencil operation
356 void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
359 * Gets the stencil operation for when the stencil test fails
360 * @return The stencil operation
362 [[nodiscard]] StencilOperation::Type GetStencilOperationOnFail() const;
365 * Sets the stencil operation for when the depth test fails
366 * @param[in] stencilOperationOnZFail The stencil operation
368 void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
371 * Gets the stencil operation for when the depth test fails
372 * @return The stencil operation
374 [[nodiscard]] StencilOperation::Type GetStencilOperationOnZFail() const;
377 * Sets the stencil operation for when the depth test passes
378 * @param[in] stencilOperationOnZPass The stencil operation
380 void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
383 * Gets the stencil operation for when the depth test passes
384 * @return The stencil operation
386 [[nodiscard]] StencilOperation::Type GetStencilOperationOnZPass() const;
389 * Called to upload during RenderManager::Render().
394 * Called to render during RenderManager::Render().
395 * @param[in,out] commandBuffer The command buffer to write into
396 * @param[in] bufferIndex The index of the previous update buffer.
397 * @param[in] node The node using this renderer
398 * @param[in] modelViewMatrix The model-view matrix.
399 * @param[in] viewMatrix The view matrix.
400 * @param[in] projectionMatrix The projection matrix.
401 * @param[in] size Size of the render item
402 * @param[in] blend If true, blending is enabled
403 * @param[in] boundTextures The textures bound for rendering
404 * @param[in] instruction. for use case like reflection where CullFace needs to be adjusted
406 * @return True if commands have been added to the command buffer
408 bool Render(Graphics::CommandBuffer& commandBuffer,
409 BufferIndex bufferIndex,
410 const SceneGraph::NodeDataProvider& node,
411 const Matrix& modelMatrix,
412 const Matrix& modelViewMatrix,
413 const Matrix& viewMatrix,
414 const Matrix& projectionMatrix,
417 Vector<Graphics::Texture*>& boundTextures,
418 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
419 uint32_t queueIndex);
422 * Sets RenderCallback object
424 * @param[in] callback Valid pointer to RenderCallback object
426 void SetRenderCallback(RenderCallback* callback);
429 * Returns currently set RenderCallback object
431 * @return Valid pointer to RenderCallback object or nullptr
433 RenderCallback* GetRenderCallback()
435 return mRenderCallback;
439 * Returns internal RenderCallbackInput structure
440 * @return Valid reference to the RenderCallbackInput
442 RenderCallbackInput& GetRenderCallbackInput()
444 if(!mRenderCallbackInput)
446 mRenderCallbackInput = std::unique_ptr<RenderCallbackInput>(new RenderCallbackInput);
448 return *mRenderCallbackInput;
452 * Write the renderer's sort attributes to the passed in reference
454 * @param[out] sortAttributes
456 void SetSortAttributes(SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const;
459 * Sets the flag indicating whether shader changed.
461 * @param[in] value True if shader changed
463 void SetShaderChanged(bool value);
466 * Check if the renderer attributes/uniforms are updated and returns the flag
468 * @param[in] bufferIndex The current update buffer index.
470 bool Updated(BufferIndex bufferIndex);
473 bool WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo,
474 Render::UniformBufferView& ubo,
478 void WriteUniform(Render::UniformBufferView& ubo,
479 const Graphics::UniformInfo& uniformInfo,
482 void WriteUniform(Render::UniformBufferView& ubo,
483 const Graphics::UniformInfo& uniformInfo,
487 [[nodiscard]] FaceCullingMode::Type GetFaceCullMode() const
489 return mFaceCullingMode;
493 * @brief Gets update area after visual properties applied.
495 * @param[in] bufferIndex The index of the previous update buffer.
496 * @param[in] originalUpdateArea The original update area before apply the visual properties.
498 * @return The recalculated update area after visual properties applied.
500 Vector4 GetVisualTransformedUpdateArea(BufferIndex bufferIndex, const Vector4& originalUpdateArea) const noexcept;
503 struct UniformIndexMap;
506 Renderer(const Renderer&);
509 Renderer& operator=(const Renderer& rhs);
512 * Builds a uniform map based on the index of the cached location in the Program.
513 * @param[in] bufferIndex The index of the previous update buffer.
514 * @param[in] node The node using the renderer
515 * @param[in] size The size of the renderer
516 * @param[in] program The shader program on which to set the uniforms.
518 * @return the index of the node in change counters store / uniform maps store.
520 std::size_t BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program);
523 * Bind the textures and setup the samplers
524 * @param[in] commandBuffer The command buffer to record binding into
525 * @param[in] boundTextures The textures bound for rendering
527 void BindTextures(Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures);
530 * Prepare a pipeline for this renderer.
532 * As a renderer can be re-used in a single frame (e.g. being used by multiple nodes, or
533 * by non-exclusive render tasks), we store a pipeline per node/instruction.
534 * In practice, the implementation will cached pipelines, so we normally only have
537 Graphics::Pipeline& PrepareGraphicsPipeline(
539 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
540 const SceneGraph::NodeDataProvider& node,
544 * Setup and write data to the uniform buffer
546 * @param[in] bufferIndex The current buffer index
547 * @param[in] commandBuffer The command buffer to bind the uniform buffer to
548 * @param[in] node The node using this renderer
549 * @param[in] modelViewMatrix The model-view matrix.
550 * @param[in] viewMatrix The view matrix.
551 * @param[in] projectionMatrix The projection matrix.
552 * @param[in] size Size of the render item
553 * @param[in] blend If true, blending is enabled
554 * @param[in] instruction The render instruction
555 * @param[in] The node index
557 void WriteUniformBuffer(BufferIndex bufferIndex,
558 Graphics::CommandBuffer& commandBuffer,
560 const SceneGraph::RenderInstruction& instruction,
561 const SceneGraph::NodeDataProvider& node,
562 const Matrix& modelMatrix,
563 const Matrix& modelViewMatrix,
564 const Matrix& viewMatrix,
565 const Matrix& projectionMatrix,
567 std::size_t nodeIndex);
570 * @brief Fill uniform buffer at index. Writes uniforms into given memory address
572 * @param[in] instruction The render instruction
573 * @param[in,out] ubo Target uniform buffer object
574 * @param[out] outBindings output bindings vector
575 * @param[out] offset output offset of the next uniform buffer memory address
576 * @param[in] updateBufferIndex update buffer index
578 void FillUniformBuffer(Program& program,
579 const SceneGraph::RenderInstruction& instruction,
580 Render::UniformBufferView& ubo,
581 std::vector<Graphics::UniformBufferBinding>*& outBindings,
583 BufferIndex updateBufferIndex,
584 std::size_t nodeIndex);
587 Graphics::Controller* mGraphicsController;
588 SceneGraph::RenderDataProvider* mRenderDataProvider;
590 Render::Geometry* mGeometry;
592 ProgramCache* mProgramCache{nullptr};
593 Render::ShaderCache* mShaderCache{nullptr};
595 Render::UniformBufferManager* mUniformBufferManager{};
596 std::vector<Graphics::UniformBufferBinding> mUniformBufferBindings{};
598 Render::PipelineCache* mPipelineCache{nullptr};
599 PipelineCacheL2* mPipeline{nullptr};
601 using Hash = std::size_t;
603 typedef const float& (PropertyInputImpl::*FuncGetter)(BufferIndex) const;
605 struct UniformIndexMap
607 ConstString uniformName; ///< The uniform name
608 const PropertyInputImpl* propertyValue{nullptr}; ///< The property value
609 Hash uniformNameHash{0u};
610 Hash uniformNameHashNoArray{0u};
611 int32_t arrayIndex{-1}; ///< The array index
613 int16_t uniformLocation{0u};
614 uint16_t uniformOffset{0u};
615 uint16_t uniformSize{0u};
616 FuncGetter uniformFunc{0};
619 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
620 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
622 uint32_t mIndexedDrawFirstElement; ///< Offset of first element to draw
623 uint32_t mIndexedDrawElementsCount; ///< Number of elements to draw
625 /** Struct to map node to index into mNodeMapCounters and mUniformIndexMaps */
626 struct RenderItemLookup
628 const SceneGraph::NodeDataProvider* node{nullptr}; ///<Node key. It can be nullptr if this NodeIndex don't need node uniform
630 std::size_t index{0}; ///<Index into mUniformIndexMap
631 std::size_t nodeChangeCounter{0}; ///<The last known change counter for this node's uniform map
632 std::size_t renderItemMapChangeCounter{0u}; ///< Change counter of the renderer & shader collected uniform map for this render item (node/renderer pair)
634 std::vector<RenderItemLookup> mNodeIndexMap; ///< usually only 1 element.
635 using UniformIndexMappings = std::vector<UniformIndexMap>;
636 std::vector<UniformIndexMappings> mUniformIndexMaps; ///< Cached map per node/renderer/shader.
638 DepthFunction::Type mDepthFunction : 4; ///< The depth function
639 FaceCullingMode::Type mFaceCullingMode : 3; ///< The mode of face culling
640 DepthWriteMode::Type mDepthWriteMode : 3; ///< The depth write mode
641 DepthTestMode::Type mDepthTestMode : 3; ///< The depth test mode
642 bool mPremultipliedAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
643 bool mShaderChanged : 1; ///< Flag indicating the shader changed and uniform maps have to be updated
645 std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; // Devel stuff
646 RenderCallback* mRenderCallback{nullptr};
647 std::unique_ptr<RenderCallbackInput> mRenderCallbackInput{nullptr};
648 std::vector<Graphics::Texture*> mRenderCallbackTextureBindings{};
651 } // namespace Render
653 } // namespace Internal
657 #endif // DALI_INTERNAL_RENDER_RENDERER_H