1 #ifndef DALI_INTERNAL_RENDER_RENDERER_H
2 #define DALI_INTERNAL_RENDER_RENDERER_H
5 * Copyright (c) 2018 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>
25 #include <dali/internal/common/blending-options.h>
26 #include <dali/internal/common/message.h>
27 #include <dali/internal/common/type-abstraction-enums.h>
28 #include <dali/internal/event/common/property-input-impl.h>
29 #include <dali/internal/render/data-providers/render-data-provider.h>
30 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
31 #include <dali/internal/render/renderers/render-geometry.h>
32 #include <dali/internal/update/manager/render-instruction-processor.h>
33 #include <dali/integration-api/debug.h>
46 class SceneController;
48 class NodeDataProvider;
55 * Renderers are used to render meshes
56 * These objects are used during RenderManager::Render(), so properties modified during
57 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
59 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,
69 int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail,
70 StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass )
71 : stencilFunctionMask ( stencilFunctionMask ),
72 stencilFunctionReference ( stencilFunctionReference ),
73 stencilMask ( stencilMask ),
74 renderMode ( renderMode ),
75 stencilFunction ( stencilFunction ),
76 stencilOperationOnFail ( stencilOperationOnFail ),
77 stencilOperationOnZFail ( stencilOperationOnZFail ),
78 stencilOperationOnZPass ( stencilOperationOnZPass )
82 int stencilFunctionMask; ///< The stencil function mask
83 int stencilFunctionReference; ///< The stencil function reference
84 int stencilMask; ///< The stencil mask
85 RenderMode::Type renderMode:4; ///< The render mode
86 StencilFunction::Type stencilFunction:4; ///< The stencil function
87 StencilOperation::Type stencilOperationOnFail:4; ///< The stencil operation for stencil test fail
88 StencilOperation::Type stencilOperationOnZFail:4; ///< The stencil operation for depth test fail
89 StencilOperation::Type stencilOperationOnZPass:4; ///< The stencil operation for depth test pass
93 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
95 void GlContextDestroyed();
98 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
103 * Create a new renderer instance
104 * @param[in] dataProviders The data providers for the renderer
105 * @param[in] geometry The geometry for the renderer
106 * @param[in] blendingBitmask A bitmask of blending options.
107 * @param[in] blendColor The blend color to pass to GL
108 * @param[in] faceCullingMode The face-culling mode.
109 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
110 * @param[in] depthWriteMode Depth buffer write mode
111 * @param[in] depthTestMode Depth buffer test mode
112 * @param[in] depthFunction Depth function
113 * @param[in] stencilParameters Struct containing all stencil related options
115 static Renderer* New( SceneGraph::RenderDataProvider* dataProviders,
116 Render::Geometry* geometry,
117 unsigned int blendingBitmask,
118 const Vector4& blendColor,
119 FaceCullingMode::Type faceCullingMode,
120 bool preMultipliedAlphaEnabled,
121 DepthWriteMode::Type depthWriteMode,
122 DepthTestMode::Type depthTestMode,
123 DepthFunction::Type depthFunction,
124 StencilParameters& stencilParameters );
128 * @param[in] dataProviders The data providers for the renderer
129 * @param[in] geometry The geometry for the renderer
130 * @param[in] blendingBitmask A bitmask of blending options.
131 * @param[in] blendColor The blend color to pass to GL
132 * @param[in] faceCullingMode The face-culling mode.
133 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
134 * @param[in] depthWriteMode Depth buffer write mode
135 * @param[in] depthTestMode Depth buffer test mode
136 * @param[in] depthFunction Depth function
137 * @param[in] stencilParameters Struct containing all stencil related options
139 Renderer( SceneGraph::RenderDataProvider* dataProviders,
140 Render::Geometry* geometry,
141 unsigned int blendingBitmask,
142 const Vector4& blendColor,
143 FaceCullingMode::Type faceCullingMode,
144 bool preMultipliedAlphaEnabled,
145 DepthWriteMode::Type depthWriteMode,
146 DepthTestMode::Type depthTestMode,
147 DepthFunction::Type depthFunction,
148 StencilParameters& stencilParameters );
151 * Change the geometry used by the renderer
152 * @param[in] geometry The new geometry
154 void SetGeometry( Render::Geometry* geometry );
157 * Second-phase construction.
158 * This is called when the renderer is inside render thread
159 * @param[in] context Context used by the renderer
161 void Initialize( Context& context );
169 * Set the face-culling mode.
170 * @param[in] mode The face-culling mode.
172 void SetFaceCullingMode( FaceCullingMode::Type mode );
175 * Set the bitmask for blending options
176 * @param[in] bitmask A bitmask of blending options.
178 void SetBlendingBitMask( unsigned int bitmask );
181 * Set the blend color for blending options
182 * @param[in] blendColor The blend color to pass to GL
184 void SetBlendColor( const Vector4& color );
187 * Set the first element index to draw by the indexed draw
188 * @param[in] firstElement index of first element to draw
190 void SetIndexedDrawFirstElement( size_t firstElement );
193 * Set the number of elements to draw by the indexed draw
194 * @param[in] elementsCount number of elements to draw
196 void SetIndexedDrawElementsCount( size_t elementsCount );
199 * @brief Set whether the Pre-multiplied Alpha Blending is required
201 * @param[in] preMultipled whether alpha is pre-multiplied.
203 void EnablePreMultipliedAlpha( bool preMultipled );
206 * Sets the depth write mode
207 * @param[in] depthWriteMode The depth write mode
209 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
212 * Query the Renderer's depth write mode
213 * @return The renderer depth write mode
215 DepthWriteMode::Type GetDepthWriteMode() const;
218 * Sets the depth test mode
219 * @param[in] depthTestMode The depth test mode
221 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
224 * Query the Renderer's depth test mode
225 * @return The renderer depth test mode
227 DepthTestMode::Type GetDepthTestMode() const;
230 * Sets the depth function
231 * @param[in] depthFunction The depth function
233 void SetDepthFunction( DepthFunction::Type depthFunction );
236 * Query the Renderer's depth function
237 * @return The renderer depth function
239 DepthFunction::Type GetDepthFunction() const;
242 * Sets the render mode
243 * @param[in] renderMode The render mode
245 void SetRenderMode( RenderMode::Type mode );
248 * Gets the render mode
249 * @return The render mode
251 RenderMode::Type GetRenderMode() const;
254 * Sets the stencil function
255 * @param[in] stencilFunction The stencil function
257 void SetStencilFunction( StencilFunction::Type stencilFunction );
260 * Gets the stencil function
261 * @return The stencil function
263 StencilFunction::Type GetStencilFunction() const;
266 * Sets the stencil function mask
267 * @param[in] stencilFunctionMask The stencil function mask
269 void SetStencilFunctionMask( int stencilFunctionMask );
272 * Gets the stencil function mask
273 * @return The stencil function mask
275 int GetStencilFunctionMask() const;
278 * Sets the stencil function reference
279 * @param[in] stencilFunctionReference The stencil function reference
281 void SetStencilFunctionReference( int stencilFunctionReference );
284 * Gets the stencil function reference
285 * @return The stencil function reference
287 int GetStencilFunctionReference() const;
290 * Sets the stencil mask
291 * @param[in] stencilMask The stencil mask
293 void SetStencilMask( int stencilMask );
296 * Gets the stencil mask
297 * @return The stencil mask
299 int GetStencilMask() const;
302 * Sets the stencil operation for when the stencil test fails
303 * @param[in] stencilOperationOnFail The stencil operation
305 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
308 * Gets the stencil operation for when the stencil test fails
309 * @return The stencil operation
311 StencilOperation::Type GetStencilOperationOnFail() const;
314 * Sets the stencil operation for when the depth test fails
315 * @param[in] stencilOperationOnZFail The stencil operation
317 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
320 * Gets the stencil operation for when the depth test fails
321 * @return The stencil operation
323 StencilOperation::Type GetStencilOperationOnZFail() const;
326 * Sets the stencil operation for when the depth test passes
327 * @param[in] stencilOperationOnZPass The stencil operation
329 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
332 * Gets the stencil operation for when the depth test passes
333 * @return The stencil operation
335 StencilOperation::Type GetStencilOperationOnZPass() const;
338 * Called to render during RenderManager::Render().
339 * @param[in] context The context used for rendering
340 * @param[in] bufferIndex The index of the previous update buffer.
341 * @param[in] node The node using this renderer
342 * @param[in] modelViewMatrix The model-view matrix.
343 * @param[in] viewMatrix The view matrix.
344 * @param[in] projectionMatrix The projection matrix.
345 * @param[in] size Size of the render item
346 * @param[in] blend If true, blending is enabled
348 void Render( Context& context,
349 BufferIndex bufferIndex,
350 const SceneGraph::NodeDataProvider& node,
351 const Matrix& modelMatrix,
352 const Matrix& modelViewMatrix,
353 const Matrix& viewMatrix,
354 const Matrix& projectionMatrix,
359 * Write the renderer's sort attributes to the passed in reference
361 * @param[in] bufferIndex The current update buffer index.
362 * @param[out] sortAttributes
364 void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const;
368 struct UniformIndexMap;
371 Renderer( const Renderer& );
374 Renderer& operator=( const Renderer& rhs );
377 * Sets blending options
378 * @param context to use
379 * @param blend Wheter blending should be enabled or not
381 void SetBlending( Context& context, bool blend );
384 * Set the uniforms from properties according to the uniform map
385 * @param[in] bufferIndex The index of the previous update buffer.
386 * @param[in] node The node using the renderer
387 * @param[in] size The size of the renderer
388 * @param[in] program The shader program on which to set the uniforms.
390 void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
393 * Set the program uniform in the map from the mapped property
394 * @param[in] bufferIndex The index of the previous update buffer.
395 * @param[in] program The shader program
396 * @param[in] map The uniform
398 void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
401 * Bind the textures and setup the samplers
402 * @param[in] context The GL context
403 * @param[in] program The shader program
404 * @return False if create or bind failed, true if success.
406 bool BindTextures( Context& context, Program& program );
410 OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
413 Render::Geometry* mGeometry;
415 struct UniformIndexMap
417 unsigned int uniformIndex; ///< The index of the cached location in the Program
418 const PropertyInputImpl* propertyValue;
421 typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
423 UniformIndexMappings mUniformIndexMap;
424 Vector<GLint> mAttributesLocation;
426 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
427 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
429 size_t mIndexedDrawFirstElement; ///< Offset of first element to draw
430 size_t mIndexedDrawElementsCount; ///< Number of elements to draw
432 DepthFunction::Type mDepthFunction:4; ///< The depth function
433 FaceCullingMode::Type mFaceCullingMode:3; ///< The mode of face culling
434 DepthWriteMode::Type mDepthWriteMode:3; ///< The depth write mode
435 DepthTestMode::Type mDepthTestMode:3; ///< The depth test mode
436 bool mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
437 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
441 } // namespace SceneGraph
443 } // namespace Internal
447 #endif // DALI_INTERNAL_RENDER_RENDERER_H