1 #ifndef DALI_INTERNAL_RENDER_RENDERER_H
2 #define DALI_INTERNAL_RENDER_RENDERER_H
5 * Copyright (c) 2022 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 UniformBufferManager;
64 * Renderers are used to render meshes
65 * These objects are used during RenderManager::Render(), so properties modified during
66 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
72 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
74 struct StencilParameters
76 StencilParameters(RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask, int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail, StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass)
77 : stencilFunctionMask(stencilFunctionMask),
78 stencilFunctionReference(stencilFunctionReference),
79 stencilMask(stencilMask),
80 renderMode(renderMode),
81 stencilFunction(stencilFunction),
82 stencilOperationOnFail(stencilOperationOnFail),
83 stencilOperationOnZFail(stencilOperationOnZFail),
84 stencilOperationOnZPass(stencilOperationOnZPass)
88 int stencilFunctionMask; ///< The stencil function mask
89 int stencilFunctionReference; ///< The stencil function reference
90 int stencilMask; ///< The stencil mask
91 RenderMode::Type renderMode : 4; ///< The render mode
92 StencilFunction::Type stencilFunction : 4; ///< The stencil function
93 StencilOperation::Type stencilOperationOnFail : 4; ///< The stencil operation for stencil test fail
94 StencilOperation::Type stencilOperationOnZFail : 4; ///< The stencil operation for depth test fail
95 StencilOperation::Type stencilOperationOnZPass : 4; ///< The stencil operation for depth test pass
99 * Create a new renderer instance
100 * @param[in] dataProviders The data providers for the renderer
101 * @param[in] geometry The geometry for the renderer
102 * @param[in] blendingBitmask A bitmask of blending options.
103 * @param[in] blendColor The blend color
104 * @param[in] faceCullingMode The face-culling mode.
105 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
106 * @param[in] depthWriteMode Depth buffer write mode
107 * @param[in] depthTestMode Depth buffer test mode
108 * @param[in] depthFunction Depth function
109 * @param[in] stencilParameters Struct containing all stencil related options
111 static Renderer* New(SceneGraph::RenderDataProvider* dataProviders,
112 Render::Geometry* geometry,
113 uint32_t blendingBitmask,
114 const Vector4& blendColor,
115 FaceCullingMode::Type faceCullingMode,
116 bool preMultipliedAlphaEnabled,
117 DepthWriteMode::Type depthWriteMode,
118 DepthTestMode::Type depthTestMode,
119 DepthFunction::Type depthFunction,
120 StencilParameters& stencilParameters);
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 Renderer(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);
147 * Change the geometry used by the renderer
148 * @param[in] geometry The new geometry
150 void SetGeometry(Render::Geometry* geometry);
152 void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
155 * @brief Returns a reference to an array of draw commands
156 * @return Valid array of draw commands (may be empty)
158 [[nodiscard]] const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
160 return mDrawCommands;
164 * Second-phase construction.
165 * This is called when the renderer is inside render thread
166 * @param[in] graphicsController The graphics controller to use
167 * @param[in] programCache Cache of program objects
168 * @param[in] shaderCache Cache of shaders
169 * @param[in] uniformBufferManager Uniform buffer manager
170 * @param[in] pipelineCache Cache of pipelines
172 void Initialize(Graphics::Controller& graphicsController,
173 ProgramCache& programCache,
174 Render::ShaderCache& shaderCache,
175 Render::UniformBufferManager& uniformBufferManager,
176 Render::PipelineCache& pipelineCache);
184 * Set the face-culling mode.
185 * @param[in] mode The face-culling mode.
187 void SetFaceCullingMode(FaceCullingMode::Type mode);
190 * Set the bitmask for blending options
191 * @param[in] bitmask A bitmask of blending options.
193 void SetBlendingBitMask(uint32_t bitmask);
196 * Set the blend color for blending options
197 * @param[in] blendColor The blend color
199 void SetBlendColor(const Vector4& color);
202 * Set the first element index to draw by the indexed draw
203 * @param[in] firstElement index of first element to draw
205 void SetIndexedDrawFirstElement(uint32_t firstElement);
208 * Set the number of elements to draw by the indexed draw
209 * @param[in] elementsCount number of elements to draw
211 void SetIndexedDrawElementsCount(uint32_t elementsCount);
214 * @brief Set whether the Pre-multiplied Alpha Blending is required
216 * @param[in] preMultipled whether alpha is pre-multiplied.
218 void EnablePreMultipliedAlpha(bool preMultipled);
221 * Sets the depth write mode
222 * @param[in] depthWriteMode The depth write mode
224 void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
227 * Query the Renderer's depth write mode
228 * @return The renderer depth write mode
230 [[nodiscard]] DepthWriteMode::Type GetDepthWriteMode() const;
233 * Sets the depth test mode
234 * @param[in] depthTestMode The depth test mode
236 void SetDepthTestMode(DepthTestMode::Type depthTestMode);
239 * Query the Renderer's depth test mode
240 * @return The renderer depth test mode
242 [[nodiscard]] DepthTestMode::Type GetDepthTestMode() const;
245 * Sets the depth function
246 * @param[in] depthFunction The depth function
248 void SetDepthFunction(DepthFunction::Type depthFunction);
251 * Query the Renderer's depth function
252 * @return The renderer depth function
254 [[nodiscard]] DepthFunction::Type GetDepthFunction() const;
257 * Sets the render mode
258 * @param[in] renderMode The render mode
260 void SetRenderMode(RenderMode::Type mode);
263 * Gets the render mode
264 * @return The render mode
266 [[nodiscard]] RenderMode::Type GetRenderMode() const;
269 * Sets the stencil function
270 * @param[in] stencilFunction The stencil function
272 void SetStencilFunction(StencilFunction::Type stencilFunction);
275 * Gets the stencil function
276 * @return The stencil function
278 [[nodiscard]] StencilFunction::Type GetStencilFunction() const;
281 * Sets the stencil function mask
282 * @param[in] stencilFunctionMask The stencil function mask
284 void SetStencilFunctionMask(int stencilFunctionMask);
287 * Gets the stencil function mask
288 * @return The stencil function mask
290 [[nodiscard]] int GetStencilFunctionMask() const;
293 * Sets the stencil function reference
294 * @param[in] stencilFunctionReference The stencil function reference
296 void SetStencilFunctionReference(int stencilFunctionReference);
299 * Gets the stencil function reference
300 * @return The stencil function reference
302 [[nodiscard]] int GetStencilFunctionReference() const;
305 * Sets the stencil mask
306 * @param[in] stencilMask The stencil mask
308 void SetStencilMask(int stencilMask);
311 * Gets the stencil mask
312 * @return The stencil mask
314 [[nodiscard]] int GetStencilMask() const;
317 * Sets the stencil operation for when the stencil test fails
318 * @param[in] stencilOperationOnFail The stencil operation
320 void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
323 * Gets the stencil operation for when the stencil test fails
324 * @return The stencil operation
326 [[nodiscard]] StencilOperation::Type GetStencilOperationOnFail() const;
329 * Sets the stencil operation for when the depth test fails
330 * @param[in] stencilOperationOnZFail The stencil operation
332 void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
335 * Gets the stencil operation for when the depth test fails
336 * @return The stencil operation
338 [[nodiscard]] StencilOperation::Type GetStencilOperationOnZFail() const;
341 * Sets the stencil operation for when the depth test passes
342 * @param[in] stencilOperationOnZPass The stencil operation
344 void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
347 * Gets the stencil operation for when the depth test passes
348 * @return The stencil operation
350 [[nodiscard]] StencilOperation::Type GetStencilOperationOnZPass() const;
353 * Called to upload during RenderManager::Render().
358 * Called to render during RenderManager::Render().
359 * @param[in,out] commandBuffer The command buffer to write into
360 * @param[in] bufferIndex The index of the previous update buffer.
361 * @param[in] node The node using this renderer
362 * @param[in] modelViewMatrix The model-view matrix.
363 * @param[in] viewMatrix The view matrix.
364 * @param[in] projectionMatrix The projection matrix.
365 * @param[in] size Size of the render item
366 * @param[in] blend If true, blending is enabled
367 * @param[in] boundTextures The textures bound for rendering
368 * @param[in] instruction. for use case like reflection where CullFace needs to be adjusted
370 * @return True if commands have been added to the command buffer
372 bool Render(Graphics::CommandBuffer& commandBuffer,
373 BufferIndex bufferIndex,
374 const SceneGraph::NodeDataProvider& node,
375 const Matrix& modelMatrix,
376 const Matrix& modelViewMatrix,
377 const Matrix& viewMatrix,
378 const Matrix& projectionMatrix,
381 Vector<Graphics::Texture*>& boundTextures,
382 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
383 uint32_t queueIndex);
386 * Sets RenderCallback object
388 * @param[in] callback Valid pointer to RenderCallback object
390 void SetRenderCallback(RenderCallback* callback);
393 * Returns currently set RenderCallback object
395 * @return Valid pointer to RenderCallback object or nullptr
397 RenderCallback* GetRenderCallback()
399 return mRenderCallback;
403 * Returns internal RenderCallbackInput structure
404 * @return Valid reference to the RenderCallbackInput
406 RenderCallbackInput& GetRenderCallbackInput()
408 return mRenderCallbackInput;
412 * Write the renderer's sort attributes to the passed in reference
414 * @param[out] sortAttributes
416 void SetSortAttributes(SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const;
419 * Sets the flag indicating whether shader changed.
421 * @param[in] value True if shader changed
423 void SetShaderChanged(bool value);
426 * Check if the renderer attributes/uniforms are updated and returns the flag
428 * @param[in] bufferIndex The current update buffer index.
429 * @param[in] node The node using this renderer
431 bool Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider* node);
434 bool WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo,
435 Render::UniformBufferView& ubo,
439 void WriteUniform(Render::UniformBufferView& ubo,
440 const Graphics::UniformInfo& uniformInfo,
443 void WriteUniform(Render::UniformBufferView& ubo,
444 const Graphics::UniformInfo& uniformInfo,
448 [[nodiscard]] FaceCullingMode::Type GetFaceCullMode() const
450 return mFaceCullingMode;
454 struct UniformIndexMap;
457 Renderer(const Renderer&);
460 Renderer& operator=(const Renderer& rhs);
463 * Builds a uniform map based on the index of the cached location in the Program.
464 * @param[in] bufferIndex The index of the previous update buffer.
465 * @param[in] node The node using the renderer
466 * @param[in] size The size of the renderer
467 * @param[in] program The shader program on which to set the uniforms.
469 * @return the index of the node in change counters store / uniform maps store.
471 std::size_t BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program);
474 * Bind the textures and setup the samplers
475 * @param[in] commandBuffer The command buffer to record binding into
476 * @param[in] boundTextures The textures bound for rendering
478 void BindTextures(Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures);
481 * Prepare a pipeline for this renderer.
483 * As a renderer can be re-used in a single frame (e.g. being used by multiple nodes, or
484 * by non-exclusive render tasks), we store a pipeline per node/instruction.
485 * In practice, the implementation will cached pipelines, so we normally only have
488 Graphics::Pipeline& PrepareGraphicsPipeline(
490 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
491 const SceneGraph::NodeDataProvider& node,
495 * Setup and write data to the uniform buffer
497 * @param[in] bufferIndex The current buffer index
498 * @param[in] commandBuffer The command buffer to bind the uniform buffer to
499 * @param[in] node The node using this renderer
500 * @param[in] modelViewMatrix The model-view matrix.
501 * @param[in] viewMatrix The view matrix.
502 * @param[in] projectionMatrix The projection matrix.
503 * @param[in] size Size of the render item
504 * @param[in] blend If true, blending is enabled
505 * @param[in] instruction The render instruction
506 * @param[in] The node index
508 void WriteUniformBuffer(BufferIndex bufferIndex,
509 Graphics::CommandBuffer& commandBuffer,
511 const SceneGraph::RenderInstruction& instruction,
512 const SceneGraph::NodeDataProvider& node,
513 const Matrix& modelMatrix,
514 const Matrix& modelViewMatrix,
515 const Matrix& viewMatrix,
516 const Matrix& projectionMatrix,
518 std::size_t nodeIndex);
521 * @brief Fill uniform buffer at index. Writes uniforms into given memory address
523 * @param[in] instruction The render instruction
524 * @param[in,out] ubo Target uniform buffer object
525 * @param[out] outBindings output bindings vector
526 * @param[out] offset output offset of the next uniform buffer memory address
527 * @param[in] updateBufferIndex update buffer index
529 void FillUniformBuffer(Program& program,
530 const SceneGraph::RenderInstruction& instruction,
531 Render::UniformBufferView& ubo,
532 std::vector<Graphics::UniformBufferBinding>*& outBindings,
534 BufferIndex updateBufferIndex,
535 std::size_t nodeIndex);
538 Graphics::Controller* mGraphicsController;
539 SceneGraph::RenderDataProvider* mRenderDataProvider;
541 Render::Geometry* mGeometry;
543 ProgramCache* mProgramCache{nullptr};
544 Render::ShaderCache* mShaderCache{nullptr};
546 Render::UniformBufferManager* mUniformBufferManager{};
547 std::vector<Graphics::UniformBufferBinding> mUniformBufferBindings{};
549 Render::PipelineCache* mPipelineCache{nullptr};
551 using Hash = std::size_t;
553 typedef const float& (PropertyInputImpl::*FuncGetter)(BufferIndex) const;
555 struct UniformIndexMap
557 ConstString uniformName; ///< The uniform name
558 const PropertyInputImpl* propertyValue{nullptr}; ///< The property value
559 Hash uniformNameHash{0u};
560 Hash uniformNameHashNoArray{0u};
561 int32_t arrayIndex{-1}; ///< The array index
563 int16_t uniformLocation{0u};
564 uint16_t uniformOffset{0u};
565 uint16_t uniformSize{0u};
566 FuncGetter uniformFunc{0};
569 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
570 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
572 uint32_t mIndexedDrawFirstElement; ///< Offset of first element to draw
573 uint32_t mIndexedDrawElementsCount; ///< Number of elements to draw
575 /** Struct to map node to index into mNodeMapCounters and mUniformIndexMaps */
576 struct RenderItemLookup
578 const SceneGraph::NodeDataProvider* node{nullptr}; ///<Node key. It can be nullptr if this NodeIndex don't need node uniform
580 std::size_t index{0}; ///<Index into mUniformIndexMap
581 std::size_t nodeChangeCounter{0}; ///<The last known change counter for this node's uniform map
582 std::size_t renderItemMapChangeCounter{0u}; ///< Change counter of the renderer & shader collected uniform map for this render item (node/renderer pair)
584 std::vector<RenderItemLookup> mNodeIndexMap; ///< usually only 1 element.
585 using UniformIndexMappings = std::vector<UniformIndexMap>;
586 std::vector<UniformIndexMappings> mUniformIndexMaps; ///< Cached map per node/renderer/shader.
588 uint64_t mUniformsHash{0}; ///< Hash of uniform map property values
590 DepthFunction::Type mDepthFunction : 4; ///< The depth function
591 FaceCullingMode::Type mFaceCullingMode : 3; ///< The mode of face culling
592 DepthWriteMode::Type mDepthWriteMode : 3; ///< The depth write mode
593 DepthTestMode::Type mDepthTestMode : 3; ///< The depth test mode
594 bool mPremultipliedAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
595 bool mShaderChanged : 1; ///< Flag indicating the shader changed and uniform maps have to be updated
598 std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; // Devel stuff
599 RenderCallback* mRenderCallback{nullptr};
600 RenderCallbackInput mRenderCallbackInput{};
601 std::vector<Graphics::Texture*> mRenderCallbackTextureBindings{};
604 } // namespace Render
606 } // namespace Internal
610 #endif // DALI_INTERNAL_RENDER_RENDERER_H