1 #ifndef DALI_INTERNAL_RENDER_RENDERER_H
2 #define DALI_INTERNAL_RENDER_RENDERER_H
5 * Copyright (c) 2021 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/math/matrix.h>
23 #include <dali/public-api/math/vector4.h>
24 #include <dali/public-api/rendering/texture-set.h>
26 #include <dali/graphics-api/graphics-controller.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/internal/common/blending-options.h>
29 #include <dali/internal/common/const-string.h>
30 #include <dali/internal/common/message.h>
31 #include <dali/internal/common/type-abstraction-enums.h>
32 #include <dali/internal/event/common/property-input-impl.h>
33 #include <dali/internal/render/data-providers/render-data-provider.h>
34 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
35 #include <dali/internal/render/renderers/render-geometry.h>
36 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
37 #include <dali/internal/render/shaders/program.h>
38 #include <dali/internal/update/manager/render-instruction-processor.h>
50 class SceneController;
52 class NodeDataProvider;
54 class RenderInstruction; //for relfection effect
55 } // namespace SceneGraph
60 class UniformBufferManager;
63 * Renderers are used to render meshes
64 * These objects are used during RenderManager::Render(), so properties modified during
65 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
67 class Renderer : public GlResourceOwner
71 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
73 struct StencilParameters
75 StencilParameters(RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask, int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail, StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass)
76 : stencilFunctionMask(stencilFunctionMask),
77 stencilFunctionReference(stencilFunctionReference),
78 stencilMask(stencilMask),
79 renderMode(renderMode),
80 stencilFunction(stencilFunction),
81 stencilOperationOnFail(stencilOperationOnFail),
82 stencilOperationOnZFail(stencilOperationOnZFail),
83 stencilOperationOnZPass(stencilOperationOnZPass)
87 int stencilFunctionMask; ///< The stencil function mask
88 int stencilFunctionReference; ///< The stencil function reference
89 int stencilMask; ///< The stencil mask
90 RenderMode::Type renderMode : 4; ///< The render mode
91 StencilFunction::Type stencilFunction : 4; ///< The stencil function
92 StencilOperation::Type stencilOperationOnFail : 4; ///< The stencil operation for stencil test fail
93 StencilOperation::Type stencilOperationOnZFail : 4; ///< The stencil operation for depth test fail
94 StencilOperation::Type stencilOperationOnZPass : 4; ///< The stencil operation for depth test pass
98 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
100 void GlContextDestroyed() override;
103 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
105 void GlCleanup() override;
108 * Create a new renderer instance
109 * @param[in] dataProviders The data providers for the renderer
110 * @param[in] geometry The geometry for the renderer
111 * @param[in] blendingBitmask A bitmask of blending options.
112 * @param[in] blendColor The blend color to pass to GL
113 * @param[in] faceCullingMode The face-culling mode.
114 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
115 * @param[in] depthWriteMode Depth buffer write mode
116 * @param[in] depthTestMode Depth buffer test mode
117 * @param[in] depthFunction Depth function
118 * @param[in] stencilParameters Struct containing all stencil related options
120 static Renderer* New(SceneGraph::RenderDataProvider* dataProviders,
121 Render::Geometry* geometry,
122 uint32_t blendingBitmask,
123 const Vector4& blendColor,
124 FaceCullingMode::Type faceCullingMode,
125 bool preMultipliedAlphaEnabled,
126 DepthWriteMode::Type depthWriteMode,
127 DepthTestMode::Type depthTestMode,
128 DepthFunction::Type depthFunction,
129 StencilParameters& stencilParameters);
133 * @param[in] dataProviders The data providers for the renderer
134 * @param[in] geometry The geometry for the renderer
135 * @param[in] blendingBitmask A bitmask of blending options.
136 * @param[in] blendColor The blend color to pass to GL
137 * @param[in] faceCullingMode The face-culling mode.
138 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
139 * @param[in] depthWriteMode Depth buffer write mode
140 * @param[in] depthTestMode Depth buffer test mode
141 * @param[in] depthFunction Depth function
142 * @param[in] stencilParameters Struct containing all stencil related options
144 Renderer(SceneGraph::RenderDataProvider* dataProviders,
145 Render::Geometry* geometry,
146 uint32_t blendingBitmask,
147 const Vector4& blendColor,
148 FaceCullingMode::Type faceCullingMode,
149 bool preMultipliedAlphaEnabled,
150 DepthWriteMode::Type depthWriteMode,
151 DepthTestMode::Type depthTestMode,
152 DepthFunction::Type depthFunction,
153 StencilParameters& stencilParameters);
156 * Change the geometry used by the renderer
157 * @param[in] geometry The new geometry
159 void SetGeometry(Render::Geometry* geometry);
161 void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
164 * @brief Returns a reference to an array of draw commands
165 * @return Valid array of draw commands (may be empty)
167 const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
169 return mDrawCommands;
173 * Second-phase construction.
174 * This is called when the renderer is inside render thread
175 * @param[in] context Context used by the renderer (To be removed)
176 * @param[in] graphicsController The graphics controller to use
177 * @param[in] programCache Cache of program objects
178 * @param[in] shaderCache Cache of shaders
179 * @param[in] uniformBufferManager Uniform buffer manager
181 void Initialize(Context& context,
182 Graphics::Controller& graphicsController,
183 ProgramCache& programCache,
184 Render::ShaderCache& shaderCache,
185 Render::UniformBufferManager& uniformBufferManager);
190 ~Renderer() override;
193 * Set the face-culling mode.
194 * @param[in] mode The face-culling mode.
196 void SetFaceCullingMode(FaceCullingMode::Type mode);
199 * Set the bitmask for blending options
200 * @param[in] bitmask A bitmask of blending options.
202 void SetBlendingBitMask(uint32_t bitmask);
205 * Set the blend color for blending options
206 * @param[in] blendColor The blend color to pass to GL
208 void SetBlendColor(const Vector4& color);
211 * Set the first element index to draw by the indexed draw
212 * @param[in] firstElement index of first element to draw
214 void SetIndexedDrawFirstElement(uint32_t firstElement);
217 * Set the number of elements to draw by the indexed draw
218 * @param[in] elementsCount number of elements to draw
220 void SetIndexedDrawElementsCount(uint32_t elementsCount);
223 * @brief Set whether the Pre-multiplied Alpha Blending is required
225 * @param[in] preMultipled whether alpha is pre-multiplied.
227 void EnablePreMultipliedAlpha(bool preMultipled);
230 * Sets the depth write mode
231 * @param[in] depthWriteMode The depth write mode
233 void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
236 * Query the Renderer's depth write mode
237 * @return The renderer depth write mode
239 DepthWriteMode::Type GetDepthWriteMode() const;
242 * Sets the depth test mode
243 * @param[in] depthTestMode The depth test mode
245 void SetDepthTestMode(DepthTestMode::Type depthTestMode);
248 * Query the Renderer's depth test mode
249 * @return The renderer depth test mode
251 DepthTestMode::Type GetDepthTestMode() const;
254 * Sets the depth function
255 * @param[in] depthFunction The depth function
257 void SetDepthFunction(DepthFunction::Type depthFunction);
260 * Query the Renderer's depth function
261 * @return The renderer depth function
263 DepthFunction::Type GetDepthFunction() const;
266 * Sets the render mode
267 * @param[in] renderMode The render mode
269 void SetRenderMode(RenderMode::Type mode);
272 * Gets the render mode
273 * @return The render mode
275 RenderMode::Type GetRenderMode() const;
278 * Sets the stencil function
279 * @param[in] stencilFunction The stencil function
281 void SetStencilFunction(StencilFunction::Type stencilFunction);
284 * Gets the stencil function
285 * @return The stencil function
287 StencilFunction::Type GetStencilFunction() const;
290 * Sets the stencil function mask
291 * @param[in] stencilFunctionMask The stencil function mask
293 void SetStencilFunctionMask(int stencilFunctionMask);
296 * Gets the stencil function mask
297 * @return The stencil function mask
299 int GetStencilFunctionMask() const;
302 * Sets the stencil function reference
303 * @param[in] stencilFunctionReference The stencil function reference
305 void SetStencilFunctionReference(int stencilFunctionReference);
308 * Gets the stencil function reference
309 * @return The stencil function reference
311 int GetStencilFunctionReference() const;
314 * Sets the stencil mask
315 * @param[in] stencilMask The stencil mask
317 void SetStencilMask(int stencilMask);
320 * Gets the stencil mask
321 * @return The stencil mask
323 int GetStencilMask() const;
326 * Sets the stencil operation for when the stencil test fails
327 * @param[in] stencilOperationOnFail The stencil operation
329 void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
332 * Gets the stencil operation for when the stencil test fails
333 * @return The stencil operation
335 StencilOperation::Type GetStencilOperationOnFail() const;
338 * Sets the stencil operation for when the depth test fails
339 * @param[in] stencilOperationOnZFail The stencil operation
341 void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
344 * Gets the stencil operation for when the depth test fails
345 * @return The stencil operation
347 StencilOperation::Type GetStencilOperationOnZFail() const;
350 * Sets the stencil operation for when the depth test passes
351 * @param[in] stencilOperationOnZPass The stencil operation
353 void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
356 * Gets the stencil operation for when the depth test passes
357 * @return The stencil operation
359 StencilOperation::Type GetStencilOperationOnZPass() const;
362 * Called to upload during RenderManager::Render().
367 * Called to render during RenderManager::Render().
368 * @param[in] context The context used for rendering
369 * @param[in] bufferIndex The index of the previous update buffer.
370 * @param[in] node The node using this renderer
371 * @param[in] modelViewMatrix The model-view matrix.
372 * @param[in] viewMatrix The view matrix.
373 * @param[in] projectionMatrix The projection matrix.
374 * @param[in] size Size of the render item
375 * @param[in] blend If true, blending is enabled
376 * @param[in] boundTextures The textures bound for rendering
377 * @param[in] instruction. for use case like reflection where CullFace needs to be adjusted
379 * @return True if the content has been rendered, false if skipped.
381 bool Render(Context& context,
382 BufferIndex bufferIndex,
383 const SceneGraph::NodeDataProvider& node,
384 const Matrix& modelMatrix,
385 const Matrix& modelViewMatrix,
386 const Matrix& viewMatrix,
387 const Matrix& projectionMatrix,
390 Vector<Graphics::Texture*>& boundTextures,
391 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
392 uint32_t queueIndex);
395 * Write the renderer's sort attributes to the passed in reference
397 * @param[in] bufferIndex The current update buffer index.
398 * @param[out] sortAttributes
400 void SetSortAttributes(BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const;
403 * Sets the flag indicating whether shader changed.
405 * @param[in] value True if shader changed
407 void SetShaderChanged(bool value);
410 * Check if the renderer attributes/uniforms are updated and returns the flag
412 * @param[in] bufferIndex The current update buffer index.
413 * @param[in] node The node using this renderer
415 bool Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider* node);
418 bool WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo,
419 Render::UniformBuffer& ubo,
420 const std::vector<Graphics::UniformBufferBinding>& bindings,
424 void WriteUniform(Render::UniformBuffer& ubo,
425 const std::vector<Graphics::UniformBufferBinding>& bindings,
426 const Graphics::UniformInfo& uniformInfo,
429 void WriteUniform(Render::UniformBuffer& ubo,
430 const std::vector<Graphics::UniformBufferBinding>& bindings,
431 const Graphics::UniformInfo& uniformInfo,
436 * Returns the pointer to valid command buffer
438 * CommandBuffer associated with Renderer contains baked commands
439 * needed to issue draw call for this renderer.
441 * @return Pointer to valid CommandBuffer
443 [[nodiscard]] Graphics::CommandBuffer* GetGraphicsCommandBuffer() const
445 return mGraphicsCommandBuffer.get();
449 struct UniformIndexMap;
452 Renderer(const Renderer&);
455 Renderer& operator=(const Renderer& rhs);
458 * Sets blending options
459 * @param context to use
460 * @param blend Wheter blending should be enabled or not
462 void SetBlending(Context& context, bool blend);
465 * Builds a uniform map based on the index of the cached location in the Program.
466 * @param[in] bufferIndex The index of the previous update buffer.
467 * @param[in] node The node using the renderer
468 * @param[in] size The size of the renderer
469 * @param[in] program The shader program on which to set the uniforms.
471 void BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program);
474 * Set the program uniform in the map from the mapped property
475 * @param[in] bufferIndex The index of the previous update buffer.
476 * @param[in] program The shader program
477 * @param[in] map The uniform
479 void SetUniformFromProperty(BufferIndex bufferIndex, Program& program, UniformIndexMap& map);
482 * Bind the textures and setup the samplers
483 * @param[in] context The GL context
484 * @param[in] program The shader program
485 * @param[in] boundTextures The textures bound for rendering
487 void BindTextures(Program& program, Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures);
490 * Prepare a pipeline for this renderer
492 Graphics::UniquePtr<Graphics::Pipeline> PrepareGraphicsPipeline(
494 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
496 Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline);
499 * Setup and write data to the uniform buffer
501 * @param[in] bufferIndex The current buffer index
502 * @param[in] commandBuffer The command buffer to bind the uniform buffer to
503 * @param[in] node The node using this renderer
504 * @param[in] modelViewMatrix The model-view matrix.
505 * @param[in] viewMatrix The view matrix.
506 * @param[in] projectionMatrix The projection matrix.
507 * @param[in] size Size of the render item
508 * @param[in] blend If true, blending is enabled
509 * @param[in] instruction The render instruction
511 void WriteUniformBuffer(BufferIndex bufferIndex,
512 Graphics::CommandBuffer& commandBuffer,
514 const SceneGraph::RenderInstruction& instruction,
515 const SceneGraph::NodeDataProvider& node,
516 const Matrix& modelMatrix,
517 const Matrix& modelViewMatrix,
518 const Matrix& viewMatrix,
519 const Matrix& projectionMatrix,
520 const Vector3& size);
523 * @brief Fill uniform buffer at index. Writes uniforms into given memory address
525 * @param[in] instruction The render instruction
526 * @param[in,out] ubo Target uniform buffer object
527 * @param[out] outBindings output bindings vector
528 * @param[out] offset output offset of the next uniform buffer memory address
529 * @param[in] updateBufferIndex update buffer index
531 void FillUniformBuffer(Program& program,
532 const SceneGraph::RenderInstruction& instruction,
533 Render::UniformBuffer& ubo,
534 std::vector<Graphics::UniformBufferBinding>*& outBindings,
536 BufferIndex updateBufferIndex);
539 Graphics::Controller* mGraphicsController;
540 OwnerPointer<SceneGraph::RenderDataProvider> mRenderDataProvider;
543 Render::Geometry* mGeometry;
545 Graphics::UniquePtr<Graphics::CommandBuffer> mGraphicsCommandBuffer{};
547 ProgramCache* mProgramCache{nullptr};
548 Render::ShaderCache* mShaderCache{nullptr};
550 Render::UniformBufferManager* mUniformBufferManager{};
551 std::vector<Graphics::UniformBufferBinding> mUniformBufferBindings{};
553 Graphics::UniquePtr<Graphics::Pipeline> mGraphicsPipeline{}; ///< The graphics pipeline. (Cached implementation)
554 std::vector<Graphics::ShaderState> mShaderStates{};
556 using Hash = unsigned long;
557 struct UniformIndexMap
559 uint32_t uniformIndex; ///< The index of the cached location in the Program
560 ConstString uniformName; ///< The uniform name
561 const PropertyInputImpl* propertyValue; ///< The property value
562 Hash uniformNameHash{0u};
563 Hash uniformNameHashNoArray{0u};
564 int32_t arrayIndex; ///< The array index
567 using UniformIndexMappings = Dali::Vector<UniformIndexMap>;
569 UniformIndexMappings mUniformIndexMap;
570 Vector<int32_t> mAttributeLocations;
572 uint64_t mUniformsHash;
574 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
575 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
577 uint32_t mIndexedDrawFirstElement; ///< Offset of first element to draw
578 uint32_t mIndexedDrawElementsCount; ///< Number of elements to draw
580 DepthFunction::Type mDepthFunction : 4; ///< The depth function
581 FaceCullingMode::Type mFaceCullingMode : 3; ///< The mode of face culling
582 DepthWriteMode::Type mDepthWriteMode : 3; ///< The depth write mode
583 DepthTestMode::Type mDepthTestMode : 3; ///< The depth test mode
584 bool mUpdateAttributeLocations : 1; ///< Indicates attribute locations have changed
585 bool mPremultipledAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
586 bool mShaderChanged : 1; ///< Flag indicating the shader changed and uniform maps have to be updated
589 std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; // Devel stuff
591 struct LegacyProgram : Graphics::ExtensionCreateInfo
593 uint32_t programId{0};
596 LegacyProgram mLegacyProgram; ///< The structure to pass the program ID into Graphics::PipelineCreateInfo
598 Graphics::UniquePtr<Render::UniformBuffer> mUniformBuffer[2]{nullptr, nullptr}; ///< The double-buffered uniform buffer
601 } // namespace Render
603 } // namespace Internal
607 #endif // DALI_INTERNAL_RENDER_RENDERER_H