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/message.h>
30 #include <dali/internal/common/type-abstraction-enums.h>
31 #include <dali/internal/event/common/property-input-impl.h>
32 #include <dali/internal/render/data-providers/render-data-provider.h>
33 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
34 #include <dali/internal/render/renderers/render-geometry.h>
35 #include <dali/internal/update/manager/render-instruction-processor.h>
47 class SceneController;
49 class NodeDataProvider;
51 class RenderInstruction; //for relfection effect
52 } // namespace SceneGraph
57 * Renderers are used to render meshes
58 * These objects are used during RenderManager::Render(), so properties modified during
59 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
61 class Renderer : public GlResourceOwner
65 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
67 struct StencilParameters
69 StencilParameters(RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask, int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail, StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass)
70 : stencilFunctionMask(stencilFunctionMask),
71 stencilFunctionReference(stencilFunctionReference),
72 stencilMask(stencilMask),
73 renderMode(renderMode),
74 stencilFunction(stencilFunction),
75 stencilOperationOnFail(stencilOperationOnFail),
76 stencilOperationOnZFail(stencilOperationOnZFail),
77 stencilOperationOnZPass(stencilOperationOnZPass)
81 int stencilFunctionMask; ///< The stencil function mask
82 int stencilFunctionReference; ///< The stencil function reference
83 int stencilMask; ///< The stencil mask
84 RenderMode::Type renderMode : 4; ///< The render mode
85 StencilFunction::Type stencilFunction : 4; ///< The stencil function
86 StencilOperation::Type stencilOperationOnFail : 4; ///< The stencil operation for stencil test fail
87 StencilOperation::Type stencilOperationOnZFail : 4; ///< The stencil operation for depth test fail
88 StencilOperation::Type stencilOperationOnZPass : 4; ///< The stencil operation for depth test pass
92 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
94 void GlContextDestroyed() override;
97 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
99 void GlCleanup() override;
102 * Create a new renderer instance
103 * @param[in] dataProviders The data providers for the renderer
104 * @param[in] geometry The geometry for the renderer
105 * @param[in] blendingBitmask A bitmask of blending options.
106 * @param[in] blendColor The blend color to pass to GL
107 * @param[in] faceCullingMode The face-culling mode.
108 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
109 * @param[in] depthWriteMode Depth buffer write mode
110 * @param[in] depthTestMode Depth buffer test mode
111 * @param[in] depthFunction Depth function
112 * @param[in] stencilParameters Struct containing all stencil related options
114 static Renderer* New(SceneGraph::RenderDataProvider* dataProviders,
115 Render::Geometry* geometry,
116 uint32_t blendingBitmask,
117 const Vector4& blendColor,
118 FaceCullingMode::Type faceCullingMode,
119 bool preMultipliedAlphaEnabled,
120 DepthWriteMode::Type depthWriteMode,
121 DepthTestMode::Type depthTestMode,
122 DepthFunction::Type depthFunction,
123 StencilParameters& stencilParameters);
127 * @param[in] dataProviders The data providers for the renderer
128 * @param[in] geometry The geometry for the renderer
129 * @param[in] blendingBitmask A bitmask of blending options.
130 * @param[in] blendColor The blend color to pass to GL
131 * @param[in] faceCullingMode The face-culling mode.
132 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
133 * @param[in] depthWriteMode Depth buffer write mode
134 * @param[in] depthTestMode Depth buffer test mode
135 * @param[in] depthFunction Depth function
136 * @param[in] stencilParameters Struct containing all stencil related options
138 Renderer(SceneGraph::RenderDataProvider* dataProviders,
139 Render::Geometry* geometry,
140 uint32_t blendingBitmask,
141 const Vector4& blendColor,
142 FaceCullingMode::Type faceCullingMode,
143 bool preMultipliedAlphaEnabled,
144 DepthWriteMode::Type depthWriteMode,
145 DepthTestMode::Type depthTestMode,
146 DepthFunction::Type depthFunction,
147 StencilParameters& stencilParameters);
150 * Change the geometry used by the renderer
151 * @param[in] geometry The new geometry
153 void SetGeometry(Render::Geometry* geometry);
155 void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
158 * @brief Returns a reference to an array of draw commands
159 * @return Valid array of draw commands (may be empty)
161 const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
163 return mDrawCommands;
167 * Second-phase construction.
168 * This is called when the renderer is inside render thread
169 * @param[in] context Context used by the renderer (To be removed)
170 * @param[in] graphicsController The graphics controller to use
172 void Initialize(Context& context, Graphics::Controller& graphicsController);
177 ~Renderer() override;
180 * Set the face-culling mode.
181 * @param[in] mode The face-culling mode.
183 void SetFaceCullingMode(FaceCullingMode::Type mode);
186 * Set the bitmask for blending options
187 * @param[in] bitmask A bitmask of blending options.
189 void SetBlendingBitMask(uint32_t bitmask);
192 * Set the blend color for blending options
193 * @param[in] blendColor The blend color to pass to GL
195 void SetBlendColor(const Vector4& color);
198 * Set the first element index to draw by the indexed draw
199 * @param[in] firstElement index of first element to draw
201 void SetIndexedDrawFirstElement(uint32_t firstElement);
204 * Set the number of elements to draw by the indexed draw
205 * @param[in] elementsCount number of elements to draw
207 void SetIndexedDrawElementsCount(uint32_t elementsCount);
210 * @brief Set whether the Pre-multiplied Alpha Blending is required
212 * @param[in] preMultipled whether alpha is pre-multiplied.
214 void EnablePreMultipliedAlpha(bool preMultipled);
217 * Sets the depth write mode
218 * @param[in] depthWriteMode The depth write mode
220 void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
223 * Query the Renderer's depth write mode
224 * @return The renderer depth write mode
226 DepthWriteMode::Type GetDepthWriteMode() const;
229 * Sets the depth test mode
230 * @param[in] depthTestMode The depth test mode
232 void SetDepthTestMode(DepthTestMode::Type depthTestMode);
235 * Query the Renderer's depth test mode
236 * @return The renderer depth test mode
238 DepthTestMode::Type GetDepthTestMode() const;
241 * Sets the depth function
242 * @param[in] depthFunction The depth function
244 void SetDepthFunction(DepthFunction::Type depthFunction);
247 * Query the Renderer's depth function
248 * @return The renderer depth function
250 DepthFunction::Type GetDepthFunction() const;
253 * Sets the render mode
254 * @param[in] renderMode The render mode
256 void SetRenderMode(RenderMode::Type mode);
259 * Gets the render mode
260 * @return The render mode
262 RenderMode::Type GetRenderMode() const;
265 * Sets the stencil function
266 * @param[in] stencilFunction The stencil function
268 void SetStencilFunction(StencilFunction::Type stencilFunction);
271 * Gets the stencil function
272 * @return The stencil function
274 StencilFunction::Type GetStencilFunction() const;
277 * Sets the stencil function mask
278 * @param[in] stencilFunctionMask The stencil function mask
280 void SetStencilFunctionMask(int stencilFunctionMask);
283 * Gets the stencil function mask
284 * @return The stencil function mask
286 int GetStencilFunctionMask() const;
289 * Sets the stencil function reference
290 * @param[in] stencilFunctionReference The stencil function reference
292 void SetStencilFunctionReference(int stencilFunctionReference);
295 * Gets the stencil function reference
296 * @return The stencil function reference
298 int GetStencilFunctionReference() const;
301 * Sets the stencil mask
302 * @param[in] stencilMask The stencil mask
304 void SetStencilMask(int stencilMask);
307 * Gets the stencil mask
308 * @return The stencil mask
310 int GetStencilMask() const;
313 * Sets the stencil operation for when the stencil test fails
314 * @param[in] stencilOperationOnFail The stencil operation
316 void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
319 * Gets the stencil operation for when the stencil test fails
320 * @return The stencil operation
322 StencilOperation::Type GetStencilOperationOnFail() const;
325 * Sets the stencil operation for when the depth test fails
326 * @param[in] stencilOperationOnZFail The stencil operation
328 void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
331 * Gets the stencil operation for when the depth test fails
332 * @return The stencil operation
334 StencilOperation::Type GetStencilOperationOnZFail() const;
337 * Sets the stencil operation for when the depth test passes
338 * @param[in] stencilOperationOnZPass The stencil operation
340 void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
343 * Gets the stencil operation for when the depth test passes
344 * @return The stencil operation
346 StencilOperation::Type GetStencilOperationOnZPass() const;
349 * Called to upload during RenderManager::Render().
354 * Called to render during RenderManager::Render().
355 * @param[in] context The context used for rendering
356 * @param[in] bufferIndex The index of the previous update buffer.
357 * @param[in] node The node using this renderer
358 * @param[in] modelViewMatrix The model-view matrix.
359 * @param[in] viewMatrix The view matrix.
360 * @param[in] projectionMatrix The projection matrix.
361 * @param[in] size Size of the render item
362 * @param[in] blend If true, blending is enabled
363 * @param[in] boundTextures The textures bound for rendering
364 * @param[in] instruction. for use case like reflection where CullFace needs to be adjusted
367 void Render(Context& context,
368 BufferIndex bufferIndex,
369 const SceneGraph::NodeDataProvider& node,
370 const Matrix& modelMatrix,
371 const Matrix& modelViewMatrix,
372 const Matrix& viewMatrix,
373 const Matrix& projectionMatrix,
376 Vector<Graphics::Texture*>& boundTextures,
377 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
378 uint32_t queueIndex);
381 * Write the renderer's sort attributes to the passed in reference
383 * @param[in] bufferIndex The current update buffer index.
384 * @param[out] sortAttributes
386 void SetSortAttributes(BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const;
389 * Sets the flag indicating whether shader changed.
391 * @param[in] value True if shader changed
393 void SetShaderChanged(bool value);
396 * Check if the renderer attributes/uniforms are updated and returns the flag
398 * @param[in] bufferIndex The current update buffer index.
399 * @param[in] node The node using this renderer
401 bool Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider* node);
404 struct UniformIndexMap;
407 Renderer(const Renderer&);
410 Renderer& operator=(const Renderer& rhs);
413 * Sets blending options
414 * @param context to use
415 * @param blend Wheter blending should be enabled or not
417 void SetBlending(Context& context, bool blend);
420 * Set the uniforms from properties according to the uniform map
421 * @param[in] bufferIndex The index of the previous update buffer.
422 * @param[in] node The node using the renderer
423 * @param[in] size The size of the renderer
424 * @param[in] program The shader program on which to set the uniforms.
426 void SetUniforms(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program);
429 * Set the program uniform in the map from the mapped property
430 * @param[in] bufferIndex The index of the previous update buffer.
431 * @param[in] program The shader program
432 * @param[in] map The uniform
434 void SetUniformFromProperty(BufferIndex bufferIndex, Program& program, UniformIndexMap& map);
437 * Bind the textures and setup the samplers
438 * @param[in] context The GL context
439 * @param[in] program The shader program
440 * @param[in] boundTextures The textures bound for rendering
441 * @return False if create or bind failed, true if success.
443 bool BindTextures(Context& context, Program& program, Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures);
446 Graphics::Controller* mGraphicsController;
447 OwnerPointer<SceneGraph::RenderDataProvider> mRenderDataProvider;
450 Render::Geometry* mGeometry;
452 struct UniformIndexMap
454 uint32_t uniformIndex; ///< The index of the cached location in the Program
455 const PropertyInputImpl* propertyValue;
458 using UniformIndexMappings = Dali::Vector<UniformIndexMap>;
460 UniformIndexMappings mUniformIndexMap;
461 Vector<GLint> mAttributesLocation;
463 uint64_t mUniformsHash;
465 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
466 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
468 uint32_t mIndexedDrawFirstElement; ///< Offset of first element to draw
469 uint32_t mIndexedDrawElementsCount; ///< Number of elements to draw
471 DepthFunction::Type mDepthFunction : 4; ///< The depth function
472 FaceCullingMode::Type mFaceCullingMode : 3; ///< The mode of face culling
473 DepthWriteMode::Type mDepthWriteMode : 3; ///< The depth write mode
474 DepthTestMode::Type mDepthTestMode : 3; ///< The depth test mode
475 bool mUpdateAttributesLocation : 1; ///< Indicates attribute locations have changed
476 bool mPremultipledAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
477 bool mShaderChanged : 1; ///< Flag indicating the shader changed and uniform maps have to be updated
480 std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; // Devel stuff
483 } // namespace Render
485 } // namespace Internal
489 #endif // DALI_INTERNAL_RENDER_RENDERER_H