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/integration-api/debug.h>
23 #include <dali/internal/common/blending-options.h>
24 #include <dali/internal/common/message.h>
25 #include <dali/internal/common/type-abstraction-enums.h>
26 #include <dali/internal/event/common/property-input-impl.h>
27 #include <dali/internal/render/data-providers/render-data-provider.h>
28 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
29 #include <dali/internal/render/renderers/render-geometry.h>
30 #include <dali/internal/update/manager/render-instruction-processor.h>
31 #include <dali/public-api/math/matrix.h>
32 #include <dali/public-api/math/vector4.h>
33 #include <dali/public-api/rendering/texture-set.h>
46 class SceneController;
48 class NodeDataProvider;
50 class RenderInstruction; //for relfection effect
51 } // namespace SceneGraph
56 * Renderers are used to render meshes
57 * These objects are used during RenderManager::Render(), so properties modified during
58 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
60 class Renderer : public GlResourceOwner
64 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
66 struct StencilParameters
68 StencilParameters(RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask, int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail, StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass)
69 : stencilFunctionMask(stencilFunctionMask),
70 stencilFunctionReference(stencilFunctionReference),
71 stencilMask(stencilMask),
72 renderMode(renderMode),
73 stencilFunction(stencilFunction),
74 stencilOperationOnFail(stencilOperationOnFail),
75 stencilOperationOnZFail(stencilOperationOnZFail),
76 stencilOperationOnZPass(stencilOperationOnZPass)
80 int stencilFunctionMask; ///< The stencil function mask
81 int stencilFunctionReference; ///< The stencil function reference
82 int stencilMask; ///< The stencil mask
83 RenderMode::Type renderMode : 4; ///< The render mode
84 StencilFunction::Type stencilFunction : 4; ///< The stencil function
85 StencilOperation::Type stencilOperationOnFail : 4; ///< The stencil operation for stencil test fail
86 StencilOperation::Type stencilOperationOnZFail : 4; ///< The stencil operation for depth test fail
87 StencilOperation::Type stencilOperationOnZPass : 4; ///< The stencil operation for depth test pass
91 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
93 void GlContextDestroyed() override;
96 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
98 void GlCleanup() override;
101 * Create a new renderer instance
102 * @param[in] dataProviders The data providers for the renderer
103 * @param[in] geometry The geometry for the renderer
104 * @param[in] blendingBitmask A bitmask of blending options.
105 * @param[in] blendColor The blend color to pass to GL
106 * @param[in] faceCullingMode The face-culling mode.
107 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
108 * @param[in] depthWriteMode Depth buffer write mode
109 * @param[in] depthTestMode Depth buffer test mode
110 * @param[in] depthFunction Depth function
111 * @param[in] stencilParameters Struct containing all stencil related options
113 static Renderer* New(SceneGraph::RenderDataProvider* dataProviders,
114 Render::Geometry* geometry,
115 uint32_t blendingBitmask,
116 const Vector4& blendColor,
117 FaceCullingMode::Type faceCullingMode,
118 bool preMultipliedAlphaEnabled,
119 DepthWriteMode::Type depthWriteMode,
120 DepthTestMode::Type depthTestMode,
121 DepthFunction::Type depthFunction,
122 StencilParameters& stencilParameters);
126 * @param[in] dataProviders The data providers for the renderer
127 * @param[in] geometry The geometry for the renderer
128 * @param[in] blendingBitmask A bitmask of blending options.
129 * @param[in] blendColor The blend color to pass to GL
130 * @param[in] faceCullingMode The face-culling mode.
131 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
132 * @param[in] depthWriteMode Depth buffer write mode
133 * @param[in] depthTestMode Depth buffer test mode
134 * @param[in] depthFunction Depth function
135 * @param[in] stencilParameters Struct containing all stencil related options
137 Renderer(SceneGraph::RenderDataProvider* dataProviders,
138 Render::Geometry* geometry,
139 uint32_t blendingBitmask,
140 const Vector4& blendColor,
141 FaceCullingMode::Type faceCullingMode,
142 bool preMultipliedAlphaEnabled,
143 DepthWriteMode::Type depthWriteMode,
144 DepthTestMode::Type depthTestMode,
145 DepthFunction::Type depthFunction,
146 StencilParameters& stencilParameters);
149 * Change the geometry used by the renderer
150 * @param[in] geometry The new geometry
152 void SetGeometry(Render::Geometry* geometry);
154 void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
157 * @brief Returns a reference to an array of draw commands
158 * @return Valid array of draw commands (may be empty)
160 const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
162 return mDrawCommands;
166 * Second-phase construction.
167 * This is called when the renderer is inside render thread
168 * @param[in] context Context used by the renderer
169 * @param[in] programCache Cache of program objects
171 void Initialize(Context& context, ProgramCache& programCache);
176 ~Renderer() override;
179 * Set the face-culling mode.
180 * @param[in] mode The face-culling mode.
182 void SetFaceCullingMode(FaceCullingMode::Type mode);
185 * Set the bitmask for blending options
186 * @param[in] bitmask A bitmask of blending options.
188 void SetBlendingBitMask(uint32_t bitmask);
191 * Set the blend color for blending options
192 * @param[in] blendColor The blend color to pass to GL
194 void SetBlendColor(const Vector4& color);
197 * Set the first element index to draw by the indexed draw
198 * @param[in] firstElement index of first element to draw
200 void SetIndexedDrawFirstElement(uint32_t firstElement);
203 * Set the number of elements to draw by the indexed draw
204 * @param[in] elementsCount number of elements to draw
206 void SetIndexedDrawElementsCount(uint32_t elementsCount);
209 * @brief Set whether the Pre-multiplied Alpha Blending is required
211 * @param[in] preMultipled whether alpha is pre-multiplied.
213 void EnablePreMultipliedAlpha(bool preMultipled);
216 * Sets the depth write mode
217 * @param[in] depthWriteMode The depth write mode
219 void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
222 * Query the Renderer's depth write mode
223 * @return The renderer depth write mode
225 DepthWriteMode::Type GetDepthWriteMode() const;
228 * Sets the depth test mode
229 * @param[in] depthTestMode The depth test mode
231 void SetDepthTestMode(DepthTestMode::Type depthTestMode);
234 * Query the Renderer's depth test mode
235 * @return The renderer depth test mode
237 DepthTestMode::Type GetDepthTestMode() const;
240 * Sets the depth function
241 * @param[in] depthFunction The depth function
243 void SetDepthFunction(DepthFunction::Type depthFunction);
246 * Query the Renderer's depth function
247 * @return The renderer depth function
249 DepthFunction::Type GetDepthFunction() const;
252 * Sets the render mode
253 * @param[in] renderMode The render mode
255 void SetRenderMode(RenderMode::Type mode);
258 * Gets the render mode
259 * @return The render mode
261 RenderMode::Type GetRenderMode() const;
264 * Sets the stencil function
265 * @param[in] stencilFunction The stencil function
267 void SetStencilFunction(StencilFunction::Type stencilFunction);
270 * Gets the stencil function
271 * @return The stencil function
273 StencilFunction::Type GetStencilFunction() const;
276 * Sets the stencil function mask
277 * @param[in] stencilFunctionMask The stencil function mask
279 void SetStencilFunctionMask(int stencilFunctionMask);
282 * Gets the stencil function mask
283 * @return The stencil function mask
285 int GetStencilFunctionMask() const;
288 * Sets the stencil function reference
289 * @param[in] stencilFunctionReference The stencil function reference
291 void SetStencilFunctionReference(int stencilFunctionReference);
294 * Gets the stencil function reference
295 * @return The stencil function reference
297 int GetStencilFunctionReference() const;
300 * Sets the stencil mask
301 * @param[in] stencilMask The stencil mask
303 void SetStencilMask(int stencilMask);
306 * Gets the stencil mask
307 * @return The stencil mask
309 int GetStencilMask() const;
312 * Sets the stencil operation for when the stencil test fails
313 * @param[in] stencilOperationOnFail The stencil operation
315 void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
318 * Gets the stencil operation for when the stencil test fails
319 * @return The stencil operation
321 StencilOperation::Type GetStencilOperationOnFail() const;
324 * Sets the stencil operation for when the depth test fails
325 * @param[in] stencilOperationOnZFail The stencil operation
327 void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
330 * Gets the stencil operation for when the depth test fails
331 * @return The stencil operation
333 StencilOperation::Type GetStencilOperationOnZFail() const;
336 * Sets the stencil operation for when the depth test passes
337 * @param[in] stencilOperationOnZPass The stencil operation
339 void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
342 * Gets the stencil operation for when the depth test passes
343 * @return The stencil operation
345 StencilOperation::Type GetStencilOperationOnZPass() const;
348 * Called to upload during RenderManager::Render().
349 * @param[in] context The context used for uploading
351 void Upload(Context& context);
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<GLuint>& 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, Vector<GLuint>& boundTextures);
446 OwnerPointer<SceneGraph::RenderDataProvider> mRenderDataProvider;
449 Render::Geometry* mGeometry;
451 ProgramCache* mProgramCache;
453 struct UniformIndexMap
455 uint32_t uniformIndex; ///< The index of the cached location in the Program
456 const PropertyInputImpl* propertyValue;
459 using UniformIndexMappings = Dali::Vector<UniformIndexMap>;
461 UniformIndexMappings mUniformIndexMap;
462 Vector<GLint> mAttributesLocation;
464 uint64_t mUniformsHash;
466 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
467 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
469 uint32_t mIndexedDrawFirstElement; ///< Offset of first element to draw
470 uint32_t mIndexedDrawElementsCount; ///< Number of elements to draw
472 DepthFunction::Type mDepthFunction : 4; ///< The depth function
473 FaceCullingMode::Type mFaceCullingMode : 3; ///< The mode of face culling
474 DepthWriteMode::Type mDepthWriteMode : 3; ///< The depth write mode
475 DepthTestMode::Type mDepthTestMode : 3; ///< The depth test mode
476 bool mUpdateAttributesLocation : 1; ///< Indicates attribute locations have changed
477 bool mPremultipledAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
478 bool mShaderChanged : 1; ///< Flag indicating the shader changed and uniform maps have to be updated
481 std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; // Devel stuff
484 } // namespace Render
486 } // namespace Internal
490 #endif // DALI_INTERNAL_RENDER_RENDERER_H