1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_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.
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/update/rendering/data-providers/render-data-provider.h>
29 #include <dali/internal/update/rendering/stencil-parameters.h>
30 #include <dali/graphics-api/graphics-api-render-command.h>
31 #include <dali/graphics-api/graphics-api-controller.h>
44 class SceneController;
48 typedef Dali::Vector< Renderer* > RendererContainer;
49 typedef RendererContainer::Iterator RendererIter;
50 typedef RendererContainer::ConstIterator RendererConstIter;
53 class Renderer : public PropertyOwner,
54 public UniformMapDataProvider,
55 public UniformMap::Observer,
56 public ConnectionChangePropagator::Observer
68 * Construct a new Renderer
70 static Renderer* New();
78 * Overriden delete operator
79 * Deletes the renderer from its global memory pool
81 void operator delete( void* ptr );
84 * Set the texture set for the renderer
85 * @param[in] textureSet The texture set this renderer will use
87 void SetTextures( TextureSet* textureSet );
90 * Returns current texture set object
91 * @return Pointer to the texture set
93 const TextureSet* GetTextures() const
99 * Set the shader for the renderer
100 * @param[in] shader The shader this renderer will use
102 void SetShader( Shader* shader );
105 * Get the shader used by this renderer
106 * @return the shader this renderer uses
108 const Shader& GetShader() const
114 * Set the geometry for the renderer
115 * @param[in] geometry The geometry this renderer will use
117 void SetGeometry( SceneGraph::Geometry* geometry );
120 * Get the geometry for the renderer
121 * @return The geometry this renderer will use
123 SceneGraph::Geometry* GetGeometry() const
129 * Set the depth index
130 * @param[in] depthIndex the new depth index to use
132 void SetDepthIndex( int depthIndex );
135 * @brief Get the depth index
136 * @return The depth index
138 int GetDepthIndex() const
144 * Set the face culling mode
145 * @param[in] faceCullingMode to use
147 void SetFaceCullingMode( FaceCullingMode::Type faceCullingMode );
150 * Set the blending mode
151 * @param[in] blendingMode to use
153 void SetBlendMode( BlendMode::Type blendingMode );
156 * Set the blending options. This should only be called from the update thread.
157 * @param[in] options A bitmask of blending options.
159 void SetBlendingOptions( unsigned int options );
162 * Set the blend color for blending operation
163 * @param blendColor to pass to GL
165 void SetBlendColor( const Vector4& blendColor );
168 * Set the index of first element for indexed draw
169 * @param[in] firstElement index of first element to draw
171 void SetIndexedDrawFirstElement( size_t firstElement );
174 * Set the number of elements to draw by indexed draw
175 * @param[in] elementsCount number of elements to draw
177 void SetIndexedDrawElementsCount( size_t elementsCount );
180 * @brief Set whether the Pre-multiplied Alpha Blending is required
181 * @param[in] preMultipled whether alpha is pre-multiplied.
183 void EnablePreMultipliedAlpha( bool preMultipled );
186 * Sets the depth buffer write mode
187 * @param[in] depthWriteMode The depth buffer write mode
189 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
192 * Sets the depth buffer test mode
193 * @param[in] depthTestMode The depth buffer test mode
195 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
198 * Sets the depth function
199 * @param[in] depthFunction The depth function
201 void SetDepthFunction( DepthFunction::Type depthFunction );
204 * Sets the render mode
205 * @param[in] mode The render mode
207 void SetRenderMode( RenderMode::Type mode );
210 * Sets the stencil function
211 * @param[in] stencilFunction The stencil function
213 void SetStencilFunction( StencilFunction::Type stencilFunction );
216 * Sets the stencil function mask
217 * @param[in] stencilFunctionMask The stencil function mask
219 void SetStencilFunctionMask( int stencilFunctionMask );
222 * Sets the stencil function reference
223 * @param[in] stencilFunctionReference The stencil function reference
225 void SetStencilFunctionReference( int stencilFunctionReference );
228 * Sets the stencil mask
229 * @param[in] stencilMask The stencil mask
231 void SetStencilMask( int stencilMask );
234 * Sets the stencil operation for when the stencil test fails
235 * @param[in] stencilOperationOnFail The stencil operation
237 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
240 * Sets the stencil operation for when the depth test fails
241 * @param[in] stencilOperationOnZFail The stencil operation
243 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
246 * Sets the stencil operation for when the depth test passes
247 * @param[in] stencilOperationOnZPass The stencil operation
249 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
252 * Prepare the object for rendering.
253 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
254 * @param[in] updateBufferIndex The current update buffer index.
256 void PrepareRender( BufferIndex updateBufferIndex );
259 * AB: preparing the command data
261 * @param updateBufferIndex
263 void PrepareRender( Graphics::API::Controller& controller, BufferIndex updateBufferIndex );
265 Graphics::API::RenderCommand& GetGfxRenderCommand()
267 return *mGfxRenderCommand.get();
271 void WriteUniform( const std::string& name, const T& data )
273 WriteUniform( name, &data, sizeof(T) );
276 void WriteUniform( const std::string& name, const void* data, uint32_t size );
280 * Query whether the renderer is fully opaque, fully transparent or transparent.
281 * @param[in] updateBufferIndex The current update buffer index.
282 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
284 Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
287 * Called by the TextureSet to notify to the renderer that it has changed
289 void TextureSetChanged();
292 * Called by the TextureSet to notify to the renderer that it is about to be deleted
294 void TextureSetDeleted();
297 * Connect the object to the scene graph
299 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
301 void ConnectToSceneGraph( BufferIndex bufferIndex );
304 * Disconnect the object from the scene graph
305 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
307 void DisconnectFromSceneGraph( BufferIndex bufferIndex );
309 public: // Implementation of ConnectionChangePropagator
311 * @copydoc ConnectionChangePropagator::AddObserver
313 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
316 * @copydoc ConnectionChangePropagator::RemoveObserver
318 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
320 public: // UniformMap::Observer
322 * @copydoc UniformMap::Observer::UniformMappingsChanged
324 virtual void UniformMappingsChanged( const UniformMap& mappings );
326 public: // ConnectionChangePropagator::Observer
329 * @copydoc ConnectionChangePropagator::ConnectionsChanged
331 virtual void ConnectionsChanged( PropertyOwner& owner );
334 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
336 virtual void ConnectedUniformMapChanged( );
339 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
341 virtual void ObservedObjectDestroyed(PropertyOwner& owner);
343 public: // PropertyOwner implementation
345 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
347 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
349 public: // From UniformMapDataProvider
352 * @copydoc UniformMapDataProvider::GetUniformMapChanged
354 virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
357 * @copydoc UniformMapDataProvider::GetUniformMap
359 virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
364 * Protected constructor; See also Renderer::New()
369 * Helper function to create a new render data provider
370 * @return the new (initialized) data provider
372 RenderDataProvider* NewRenderDataProvider();
375 * Helper function to retrieve the blend color.
376 * @return The blend color.
378 const Vector4& GetBlendColor() const;
383 CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
384 std::unique_ptr<RenderDataProvider> mRenderDataProvider; ///< Contains data for graphics renderer @todo Refactor
385 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
386 SceneGraph::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
387 Shader* mShader; ///< The shader this renderer uses. (Not owned)
388 OwnerPointer< Vector4 > mBlendColor; ///< The blend color for blending operation
390 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
392 size_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
393 size_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
394 unsigned int mBlendBitmask; ///< The bitmask of blending options
395 unsigned int mRegenerateUniformMap; ///< 2 if the map should be regenerated, 1 if it should be copied.
396 unsigned int mResendFlag; ///< Indicate whether data should be resent to the renderer
398 DepthFunction::Type mDepthFunction:3; ///< Local copy of the depth function
399 FaceCullingMode::Type mFaceCullingMode:2; ///< Local copy of the mode of face culling
400 BlendMode::Type mBlendMode:2; ///< Local copy of the mode of blending
401 DepthWriteMode::Type mDepthWriteMode:2; ///< Local copy of the depth write mode
402 DepthTestMode::Type mDepthTestMode:2; ///< Local copy of the depth test mode
404 bool mUniformMapChanged[2]; ///< Records if the uniform map has been altered this frame
405 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
407 std::vector<std::vector<char>> mUboMemory; ///< Transient memory allocated for each UBO
408 std::unique_ptr<Graphics::API::RenderCommand> mGfxRenderCommand;
411 int mDepthIndex; ///< Used only in PrepareRenderInstructions
416 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
418 typedef MessageValue1< Renderer, TextureSet* > LocalType;
420 // Reserve some memory inside the message queue
421 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
423 // Construct message in the message queue memory; note that delete should not be called on the return value
424 new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
427 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const SceneGraph::Geometry& geometry )
429 typedef MessageValue1< Renderer, SceneGraph::Geometry* > LocalType;
431 // Reserve some memory inside the message queue
432 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
434 // Construct message in the message queue memory; note that delete should not be called on the return value
435 new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<SceneGraph::Geometry*>(&geometry) );
438 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
440 typedef MessageValue1< Renderer, Shader* > LocalType;
442 // Reserve some memory inside the message queue
443 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
445 // Construct message in the message queue memory; note that delete should not be called on the return value
446 new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
449 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int depthIndex )
451 typedef MessageValue1< Renderer, int > LocalType;
453 // Reserve some memory inside the message queue
454 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
456 // Construct message in the message queue memory; note that delete should not be called on the return value
457 new (slot) LocalType( &renderer, &Renderer::SetDepthIndex, depthIndex );
460 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, FaceCullingMode::Type faceCullingMode )
462 typedef MessageValue1< Renderer, FaceCullingMode::Type > LocalType;
464 // Reserve some memory inside the message queue
465 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
467 new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
470 inline void SetBlendModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendMode::Type blendingMode )
472 typedef MessageValue1< Renderer, BlendMode::Type > LocalType;
474 // Reserve some memory inside the message queue
475 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
477 new (slot) LocalType( &renderer, &Renderer::SetBlendMode, blendingMode );
480 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
482 typedef MessageValue1< Renderer, unsigned int > LocalType;
484 // Reserve some memory inside the message queue
485 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
487 new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
490 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
492 typedef MessageValue1< Renderer, Vector4 > LocalType;
494 // Reserve some memory inside the message queue
495 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
497 new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
500 inline void SetIndexedDrawFirstElementMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t firstElement )
502 typedef MessageValue1< Renderer, size_t > LocalType;
504 // Reserve some memory inside the message queue
505 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
507 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawFirstElement, firstElement );
510 inline void SetIndexedDrawElementsCountMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t elementsCount )
512 typedef MessageValue1< Renderer, size_t > LocalType;
514 // Reserve some memory inside the message queue
515 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
517 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawElementsCount, elementsCount );
520 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
522 typedef MessageValue1< Renderer, bool > LocalType;
524 // Reserve some memory inside the message queue
525 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
527 new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
530 inline void SetDepthWriteModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthWriteMode::Type depthWriteMode )
532 typedef MessageValue1< Renderer, DepthWriteMode::Type > LocalType;
534 // Reserve some memory inside the message queue
535 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
537 new (slot) LocalType( &renderer, &Renderer::SetDepthWriteMode, depthWriteMode );
540 inline void SetDepthTestModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthTestMode::Type depthTestMode )
542 typedef MessageValue1< Renderer, DepthTestMode::Type > LocalType;
544 // Reserve some memory inside the message queue
545 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
547 new (slot) LocalType( &renderer, &Renderer::SetDepthTestMode, depthTestMode );
550 inline void SetDepthFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthFunction::Type depthFunction )
552 typedef MessageValue1< Renderer, DepthFunction::Type > LocalType;
554 // Reserve some memory inside the message queue
555 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
557 new (slot) LocalType( &renderer, &Renderer::SetDepthFunction, depthFunction );
560 inline void SetRenderModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, RenderMode::Type mode )
562 typedef MessageValue1< Renderer, RenderMode::Type > LocalType;
564 // Reserve some memory inside the message queue
565 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
567 new (slot) LocalType( &renderer, &Renderer::SetRenderMode, mode );
570 inline void SetStencilFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilFunction::Type stencilFunction )
572 typedef MessageValue1< Renderer, StencilFunction::Type > LocalType;
574 // Reserve some memory inside the message queue
575 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
577 new (slot) LocalType( &renderer, &Renderer::SetStencilFunction, stencilFunction );
580 inline void SetStencilFunctionMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int mask )
582 typedef MessageValue1< Renderer, int > LocalType;
584 // Reserve some memory inside the message queue
585 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
587 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionMask, mask );
590 inline void SetStencilFunctionReferenceMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilFunctionReference )
592 typedef MessageValue1< Renderer, int > LocalType;
594 // Reserve some memory inside the message queue
595 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
597 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionReference, stencilFunctionReference );
600 inline void SetStencilMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilMask )
602 typedef MessageValue1< Renderer, int > LocalType;
604 // Reserve some memory inside the message queue
605 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
607 new (slot) LocalType( &renderer, &Renderer::SetStencilMask, stencilMask );
610 inline void SetStencilOperationOnFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
612 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
614 // Reserve some memory inside the message queue
615 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
617 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnFail, stencilOperation );
620 inline void SetStencilOperationOnZFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
622 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
624 // Reserve some memory inside the message queue
625 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
627 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZFail, stencilOperation );
630 inline void SetStencilOperationOnZPassMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
632 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
634 // Reserve some memory inside the message queue
635 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
637 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZPass, stencilOperation );
640 } // namespace SceneGraph
641 } // namespace Internal
644 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H