1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
5 * Copyright (c) 2016 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.
20 #include <dali/public-api/rendering/geometry.h>
21 #include <dali/public-api/rendering/renderer.h> // Dali::Renderer
22 #include <dali/internal/common/blending-options.h>
23 #include <dali/internal/common/type-abstraction-enums.h>
24 #include <dali/internal/event/common/event-thread-services.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/common/uniform-map.h>
27 #include <dali/internal/update/common/scene-graph-connection-change-propagator.h>
28 #include <dali/internal/render/data-providers/render-data-provider.h>
29 #include <dali/internal/render/renderers/render-renderer.h>
44 class SceneController;
47 typedef Dali::Vector< Renderer* > RendererContainer;
48 typedef RendererContainer::Iterator RendererIter;
49 typedef RendererContainer::ConstIterator RendererConstIter;
54 class Renderer : public PropertyOwner,
55 public UniformMapDataProvider,
56 public UniformMap::Observer,
57 public ConnectionChangePropagator::Observer
69 * Construct a new Renderer
71 static Renderer* New();
79 * Overriden delete operator
80 * Deletes the renderer from its global memory pool
82 void operator delete( void* ptr );
85 * Set the texture set for the renderer
86 * @param[in] textureSet The texture set this renderer will use
88 void SetTextures( TextureSet* textureSet );
91 * Returns current texture set object
92 * @return Pointer to the texture set
94 const TextureSet* GetTextures() const
100 * Set the shader for the renderer
101 * @param[in] shader The shader this renderer will use
103 void SetShader( Shader* shader );
106 * Get the shader used by this renderer
107 * @return the shader this renderer uses
109 const Shader& GetShader() const
115 * Set the geometry for the renderer
116 * @param[in] geometry The geometry this renderer will use
118 void SetGeometry( Render::Geometry* geometry );
121 * Get the geometry of this renderer
122 * @return the geometry this renderer uses
124 const Render::Geometry& GetGeometry() const
130 * Set the depth index
131 * @param[in] depthIndex the new depth index to use
133 void SetDepthIndex( int depthIndex );
136 * @brief Get the depth index
137 * @return The depth index
139 int GetDepthIndex() const
145 * Set the face culling mode
146 * @param[in] faceCullingMode to use
148 void SetFaceCullingMode( FaceCullingMode::Type faceCullingMode );
151 * Set the blending mode
152 * @param[in] blendingMode to use
154 void SetBlendMode( BlendMode::Type blendingMode );
157 * Set the blending options. This should only be called from the update thread.
158 * @param[in] options A bitmask of blending options.
160 void SetBlendingOptions( unsigned int options );
163 * Set the blend color for blending operation
164 * @param blendColor to pass to GL
166 void SetBlendColor( const Vector4& blendColor );
169 * Set the index of first element for indexed draw
170 * @param[in] firstElement index of first element to draw
172 void SetIndexedDrawFirstElement( size_t firstElement );
175 * Set the number of elements to draw by indexed draw
176 * @param[in] elementsCount number of elements to draw
178 void SetIndexedDrawElementsCount( size_t elementsCount );
181 * @brief Set whether the Pre-multiplied Alpha Blending is required
182 * @param[in] preMultipled whether alpha is pre-multiplied.
184 void EnablePreMultipliedAlpha( bool preMultipled );
187 * Sets the depth buffer write mode
188 * @param[in] depthWriteMode The depth buffer write mode
190 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
193 * Sets the depth buffer test mode
194 * @param[in] depthTestMode The depth buffer test mode
196 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
199 * Sets the depth function
200 * @param[in] depthFunction The depth function
202 void SetDepthFunction( DepthFunction::Type depthFunction );
205 * Sets the stencil mode
206 * @param[in] mode The stencil function
208 void SetStencilMode( StencilMode::Type mode );
211 * Sets the stencil function
212 * @param[in] stencilFunction The stencil function
214 void SetStencilFunction( StencilFunction::Type stencilFunction );
217 * Sets the stencil function mask
218 * @param[in] stencilFunctionMask The stencil function mask
220 void SetStencilFunctionMask( int stencilFunctionMask );
223 * Sets the stencil function reference
224 * @param[in] stencilFunctionReference The stencil function reference
226 void SetStencilFunctionReference( int stencilFunctionReference );
229 * Sets the stencil mask
230 * @param[in] stencilMask The stencil mask
232 void SetStencilMask( int stencilMask );
235 * Sets the stencil operation for when the stencil test fails
236 * @param[in] stencilOperationOnFail The stencil operation
238 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
241 * Sets the stencil operation for when the depth test fails
242 * @param[in] stencilOperationOnZFail The stencil operation
244 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
247 * Sets the stencil operation for when the depth test passes
248 * @param[in] stencilOperationOnZPass The stencil operation
250 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
253 * Sets whether or not to write to the color buffer
254 * @param[in] writeToColorBuffer True to write to the color buffer
256 void SetWriteToColorBuffer( bool writeToColorBuffer );
259 * Turns on batching feature for the renderer
260 * @param[in] batchingEnabled if true, enables the batching mode for the renderer
262 void SetBatchingEnabled( bool batchingEnabled );
265 * Tests whether batching feature is enabled for this renderer
266 * @return batching state
268 bool IsBatchingEnabled() const
270 return mBatchingEnabled;
274 * Prepare the object for rendering.
275 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
276 * @param[in] updateBufferIndex The current update buffer index.
278 void PrepareRender( BufferIndex updateBufferIndex );
281 * Retrieve the Render thread renderer
282 * @return The associated render thread renderer
284 Render::Renderer& GetRenderer();
287 * Check whether the renderer has been marked as ready to render
288 * ready means that renderer has all resources and should produce correct result
289 * complete means all resources have finished loading
290 * It's possible that renderer is complete but not ready,
291 * for example in case of resource load failed
292 * @param[out] ready TRUE if the renderer has resources to render
293 * @param[out] complete TRUE if the renderer resources are complete
295 void GetReadyAndComplete( bool& ready, bool& complete ) const;
298 * Query whether the renderer is fully opaque, fully transparent or transparent.
299 * @param[in] updateBufferIndex The current update buffer index.
300 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
302 Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
305 * Called by the TextureSet to notify to the renderer that it has changed
307 void TextureSetChanged();
309 public: // Implementation of ObjectOwnerContainer template methods
311 * Connect the object to the scene graph
313 * @param[in] sceneController The scene controller - used for sending messages to render thread
314 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
316 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
319 * Disconnect the object from the scene graph
320 * @param[in] sceneController The scene controller - used for sending messages to render thread
321 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
323 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
325 public: // Implementation of ConnectionChangePropagator
327 * @copydoc ConnectionChangePropagator::AddObserver
329 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
332 * @copydoc ConnectionChangePropagator::RemoveObserver
334 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
339 public: // UniformMap::Observer
341 * @copydoc UniformMap::Observer::UniformMappingsChanged
343 virtual void UniformMappingsChanged( const UniformMap& mappings );
345 public: // ConnectionChangePropagator::Observer
348 * @copydoc ConnectionChangePropagator::ConnectionsChanged
350 virtual void ConnectionsChanged( PropertyOwner& owner );
353 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
355 virtual void ConnectedUniformMapChanged( );
358 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
360 virtual void ObservedObjectDestroyed(PropertyOwner& owner);
362 public: // PropertyOwner implementation
364 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
366 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
368 public: // From UniformMapDataProvider
371 * @copydoc UniformMapDataProvider::GetUniformMapChanged
373 virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
376 * @copydoc UniformMapDataProvider::GetUniformMap
378 virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
383 * Protected constructor; See also Renderer::New()
388 * Helper function to create a new render data provider
389 * @return the new (initialized) data provider
391 RenderDataProvider* NewRenderDataProvider();
395 CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
396 SceneController* mSceneController; ///< Used for initializing renderers
397 Render::Renderer* mRenderer; ///< Raw pointer to the renderer (that's owned by RenderManager)
398 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
399 Render::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
400 Shader* mShader; ///< The shader this renderer uses. (Not owned)
401 Vector4* mBlendColor; ///< The blend color for blending operation
403 Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
405 size_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
406 size_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
407 unsigned int mBlendBitmask; ///< The bitmask of blending options
408 unsigned int mRegenerateUniformMap; ///< 2 if the map should be regenerated, 1 if it should be copied.
409 unsigned int mResendFlag; ///< Indicate whether data should be resent to the renderer
411 DepthFunction::Type mDepthFunction:3; ///< Local copy of the depth function
412 FaceCullingMode::Type mFaceCullingMode:2; ///< Local copy of the mode of face culling
413 BlendMode::Type mBlendMode:2; ///< Local copy of the mode of blending
414 DepthWriteMode::Type mDepthWriteMode:2; ///< Local copy of the depth write mode
415 DepthTestMode::Type mDepthTestMode:2; ///< Local copy of the depth test mode
416 bool mWriteToColorBuffer:1; ///< Local copy of the write to color buffer flag
418 bool mUniformMapChanged[2]; ///< Records if the uniform map has been altered this frame
419 bool mResourcesReady; ///< Set during the Update algorithm; true if the renderer has resources ready for the current frame.
420 bool mFinishedResourceAcquisition; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
421 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
425 bool mBatchingEnabled : 1; ///< Flag indicating whether the render supports batching
426 int mDepthIndex; ///< Used only in PrepareRenderInstructions
431 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
433 typedef MessageValue1< Renderer, TextureSet* > LocalType;
435 // Reserve some memory inside the message queue
436 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
438 // Construct message in the message queue memory; note that delete should not be called on the return value
439 new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
442 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Render::Geometry& geometry )
444 typedef MessageValue1< Renderer, Render::Geometry* > LocalType;
446 // Reserve some memory inside the message queue
447 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
449 // Construct message in the message queue memory; note that delete should not be called on the return value
450 new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Render::Geometry*>(&geometry) );
453 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
455 typedef MessageValue1< Renderer, Shader* > LocalType;
457 // Reserve some memory inside the message queue
458 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
460 // Construct message in the message queue memory; note that delete should not be called on the return value
461 new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
464 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int depthIndex )
466 typedef MessageValue1< Renderer, int > LocalType;
468 // Reserve some memory inside the message queue
469 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
471 // Construct message in the message queue memory; note that delete should not be called on the return value
472 new (slot) LocalType( &renderer, &Renderer::SetDepthIndex, depthIndex );
475 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, FaceCullingMode::Type faceCullingMode )
477 typedef MessageValue1< Renderer, FaceCullingMode::Type > LocalType;
479 // Reserve some memory inside the message queue
480 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
482 new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
485 inline void SetBlendModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendMode::Type blendingMode )
487 typedef MessageValue1< Renderer, BlendMode::Type > LocalType;
489 // Reserve some memory inside the message queue
490 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
492 new (slot) LocalType( &renderer, &Renderer::SetBlendMode, blendingMode );
495 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
497 typedef MessageValue1< Renderer, unsigned int > LocalType;
499 // Reserve some memory inside the message queue
500 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
502 new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
505 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
507 typedef MessageValue1< Renderer, Vector4 > LocalType;
509 // Reserve some memory inside the message queue
510 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
512 new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
515 inline void SetIndexedDrawFirstElementMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t firstElement )
517 typedef MessageValue1< Renderer, size_t > LocalType;
519 // Reserve some memory inside the message queue
520 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
522 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawFirstElement, firstElement );
525 inline void SetIndexedDrawElementsCountMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t elementsCount )
527 typedef MessageValue1< Renderer, size_t > LocalType;
529 // Reserve some memory inside the message queue
530 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
532 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawElementsCount, elementsCount );
535 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
537 typedef MessageValue1< Renderer, bool > LocalType;
539 // Reserve some memory inside the message queue
540 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
542 new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
545 inline void SetDepthWriteModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthWriteMode::Type depthWriteMode )
547 typedef MessageValue1< Renderer, DepthWriteMode::Type > LocalType;
549 // Reserve some memory inside the message queue
550 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
552 new (slot) LocalType( &renderer, &Renderer::SetDepthWriteMode, depthWriteMode );
555 inline void SetDepthTestModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthTestMode::Type depthTestMode )
557 typedef MessageValue1< Renderer, DepthTestMode::Type > LocalType;
559 // Reserve some memory inside the message queue
560 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
562 new (slot) LocalType( &renderer, &Renderer::SetDepthTestMode, depthTestMode );
565 inline void SetDepthFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthFunction::Type depthFunction )
567 typedef MessageValue1< Renderer, DepthFunction::Type > LocalType;
569 // Reserve some memory inside the message queue
570 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
572 new (slot) LocalType( &renderer, &Renderer::SetDepthFunction, depthFunction );
575 inline void SetStencilModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilMode::Type mode )
577 typedef MessageValue1< Renderer, StencilMode::Type > LocalType;
579 // Reserve some memory inside the message queue
580 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
582 new (slot) LocalType( &renderer, &Renderer::SetStencilMode, mode );
585 inline void SetStencilFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilFunction::Type stencilFunction )
587 typedef MessageValue1< Renderer, StencilFunction::Type > LocalType;
589 // Reserve some memory inside the message queue
590 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
592 new (slot) LocalType( &renderer, &Renderer::SetStencilFunction, stencilFunction );
595 inline void SetStencilFunctionMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int mask )
597 typedef MessageValue1< Renderer, int > LocalType;
599 // Reserve some memory inside the message queue
600 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
602 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionMask, mask );
605 inline void SetStencilFunctionReferenceMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilFunctionReference )
607 typedef MessageValue1< Renderer, int > LocalType;
609 // Reserve some memory inside the message queue
610 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
612 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionReference, stencilFunctionReference );
615 inline void SetStencilMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilMask )
617 typedef MessageValue1< Renderer, int > LocalType;
619 // Reserve some memory inside the message queue
620 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
622 new (slot) LocalType( &renderer, &Renderer::SetStencilMask, stencilMask );
625 inline void SetStencilOperationOnFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
627 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
629 // Reserve some memory inside the message queue
630 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
632 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnFail, stencilOperation );
635 inline void SetStencilOperationOnZFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
637 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
639 // Reserve some memory inside the message queue
640 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
642 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZFail, stencilOperation );
645 inline void SetStencilOperationOnZPassMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
647 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
649 // Reserve some memory inside the message queue
650 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
652 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZPass, stencilOperation );
655 inline void SetWriteToColorBufferMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool writeToColorBuffer )
657 typedef MessageValue1< Renderer, bool > LocalType;
659 // Reserve some memory inside the message queue
660 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
662 new (slot) LocalType( &renderer, &Renderer::SetWriteToColorBuffer, writeToColorBuffer );
665 inline void SetBatchingEnabledMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool batchable )
667 typedef MessageValue1< Renderer, bool > LocalType;
669 // Reserve some memory inside the message queue
670 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
672 new (slot) LocalType( &renderer, &Renderer::SetBatchingEnabled, batchable );
675 } // namespace SceneGraph
676 } // namespace Internal
679 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H