1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__
5 * Copyright (c) 2014 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/internal/common/blending-options.h>
23 #include <dali/internal/common/event-to-update.h>
24 #include <dali/internal/common/internal-constants.h>
25 #include <dali/internal/common/type-abstraction-enums.h>
26 #include <dali/internal/update/controllers/scene-controller.h>
27 #include <dali/internal/update/nodes/node.h>
28 #include <dali/internal/update/node-attachments/node-attachment.h>
29 #include <dali/internal/update/resources/resource-manager-declarations.h>
30 #include <dali/internal/render/renderers/scene-graph-renderer-declarations.h>
37 class ResourceManager;
38 class ResourceTracker;
46 * RenderableAttachments are responsible for preparing textures, meshes, matrices etc. during the Update.
47 * These resources are then passed to a renderer, for use in the next Render.
49 class RenderableAttachment : public NodeAttachment
54 * Set the sort-modifier for the attachment.
55 * @param[in] modifier The depth-sort modifier.
57 void SetSortModifier(float modifier);
60 * Retrieve the sort-modifier for the attachment.
61 * @return The sort-modifier.
63 float GetSortModifier() const
65 // inlined as its called a lot when sorting transparent renderers
70 * @See Dali::RenderableActor::SetBlendMode().
72 void SetBlendingMode( BlendingMode::Type mode );
75 * @copydoc Dali::RenderableActor::GetBlendMode().
77 BlendingMode::Type GetBlendingMode() const;
80 * Check if the blending mode has changed - if it has, send message to renderer
81 * @param[in] updateBufferIndex The current update buffer index.
82 * @param[in] useBlending True if the renderer should use blending option
84 void ChangeBlending( BufferIndex updateBufferIndex, bool useBlending );
87 * Set the blending options. This should only be called from the update-thread.
88 * @param[in] updateBufferIndex The current update buffer index.
89 * @param[in] options A bitmask of blending options.
91 void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
94 * Set the blend-color. This should only be called from the update-thread.
95 * @param[in] updateBufferIndex The current update buffer index.
96 * @param[in] color The new blend-color.
98 void SetBlendColor( BufferIndex updateBufferIndex, const Vector4& color );
101 * Set the face-culling mode.
102 * @param[in] updateBufferIndex The current update buffer index.
103 * @param[in] mode The face-culling mode.
105 void SetCullFace( BufferIndex updateBufferIndex, CullFaceMode mode );
108 * Set the sampler used to render the texture for this renderable.
109 * @param[in] updateBufferIndex The current update buffer index.
110 * @param[in] samplerBitfield The image sampler packed options to set.
112 void SetSampler( BufferIndex updateBufferIndex, unsigned int samplerBitfield );
115 * Flag to check if any geometry scaling is needed, inlined as called from update algorithm often
116 * @return true if the derived renderable uses geometry scaling
118 bool UsesGeometryScaling() const
120 return mUsesGeometryScaling;
124 * Triggers scale for size update. GetScaleForSize will be called in this frame
126 void SetRecalculateScaleForSize();
129 * Returns the scaling dirty flag, inlined as called from update algorithm often
130 * @return if scale for size is dirty, i.e. scaling has changed
132 bool IsScaleForSizeDirty() const
134 return mScaleForSizeDirty;
138 * Retrieve scale-for-size for given node size
139 * Clears the scale for size flag
140 * @param[in] nodeSize to scale to
141 * @param[out] scaling factors
143 void GetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
146 * Apply a shader on the renderable
147 * @param[in] updateBufferIndex The current update buffer index.
148 * @param[in] shader to apply.
150 void ApplyShader( BufferIndex updateBufferIndex, Shader* shader );
153 * Remove the shader from the renderable
154 * @param[in] updateBufferIndex The current update buffer index.
156 void RemoveShader( BufferIndex updateBufferIndex );
158 public: // For use during in the update algorithm only
161 * TODO this method should not be virtual but because mesh attachment is a mess, it needs to be
162 * considered visible regardless of its size... need to remove geometry scaling to fix this!!!
163 * @param[in] updateBufferIndex The current update buffer index.
164 * @return visible tells if this renderer can be potentially seen
166 virtual bool ResolveVisibility( BufferIndex updateBufferIndex )
168 mHasSizeAndColorFlag = false;
169 const Vector4& color = mParent->GetWorldColor( updateBufferIndex );
170 if( color.a > FULLY_TRANSPARENT ) // not fully transparent
172 const Vector3& size = mParent->GetSize( updateBufferIndex );
173 if( ( size.width > Math::MACHINE_EPSILON_1000 )&& // width is greater than a very small number
174 ( size.height > Math::MACHINE_EPSILON_1000 ) ) // height is greater than a very small number
176 mHasSizeAndColorFlag = true;
179 return mHasSizeAndColorFlag;
183 * if this renderable actor has visible size and color
184 * @return true if you can potentially see this actor
186 bool HasVisibleSizeAndColor() const
188 return mHasSizeAndColorFlag;
192 * Check whether the attachment has been marked as ready to render
193 * @param[out] ready TRUE if the attachment has resources to render
194 * @param[out] complete TRUE if the attachment's resources are complete
195 * (e.g. image has finished loading, framebuffer is ready to render, native image
196 * framebuffer has been rendered)
198 void GetReadyAndComplete(bool& ready, bool& complete) const;
201 * Query whether the attachment has blending enabled.
202 * @param[in] updateBufferIndex The current update buffer index.
203 * @return true if blending is enabled, false otherwise.
205 bool IsBlendingOn( BufferIndex updateBufferIndex );
208 * Prepare the object for rendering.
209 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
210 * @param[in] updateBufferIndex The current update buffer index.
212 void PrepareRender( BufferIndex updateBufferIndex );
214 public: // API for derived classes
217 * Retrieve a Renderer used by this attachment; implemented in derived classes.
218 * @note The first Renderer is the main renderer for the attachment, and
219 * should always exist during the lifetime of the RenderableAttachment.
220 * @return A Renderer.
222 virtual Renderer& GetRenderer() = 0;
225 * Retrieve a Renderer used by this attachment.
226 * @note The first Renderer is the main renderer for the attachment, and
227 * should always exist during the lifetime of the RenderableAttachment.
228 * Other renderers are for effects such as shadows and reflections.
229 * @return A Renderer.
231 virtual const Renderer& GetRenderer() const = 0;
234 * Prepare the object resources.
235 * This must be called by the UpdateManager before calling PrepareRender, for each frame.
236 * @param[in] updateBufferIndex The current update buffer index.
237 * @param[in] resourceManager The resource manager.
239 void PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
242 * If the resource is being tracked, then follow it. ( Further ready tests will use this
243 * list ) Otherwise, if it's not complete, set mHasUntrackedResources.
244 * @param[in] The resource id
246 void FollowTracker( Integration::ResourceId id );
249 * @copydoc RenderableAttachment::PrepareRender()
251 virtual void DoPrepareRender( BufferIndex updateBufferIndex ) = 0;
254 * Query whether the attachment is fully opaque.
255 * @param[in] updateBufferIndex The current update buffer index.
256 * @return True if fully opaque.
258 virtual bool IsFullyOpaque( BufferIndex updateBufferIndex ) = 0;
261 * Called to notify that the shader might have been changed
262 * The implementation should recalculate geometry and scale based on the
263 * hints from the new shader
264 * @param[in] updateBufferIndex The current update buffer index.
265 * @return Return true if the geometry changed.
267 virtual void ShaderChanged( BufferIndex updateBufferIndex ) = 0;
270 * Called to notify that the size has been changed
271 * The implementation may tell the renderer to recalculate geometry and scale based on the new size
272 * @param[in] updateBufferIndex The current update buffer index.
274 virtual void SizeChanged( BufferIndex updateBufferIndex ) = 0;
277 * Chained from NodeAttachment::ConnectToSceneGraph()
279 virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex ) = 0;
282 * Chained from NodeAttachment::OnDestroy()
284 virtual void OnDestroy2() = 0;
287 * Retrieve the scale-for-size for given node size. Default implementation returns Vector3::ZERO
288 * @param[in] nodeSize to scale to
289 * @param[out] scaling factors
291 virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
296 * Protected constructor; only base classes can be instantiated.
297 * @param usesGeometryScaling should be false if the derived class does not need geometry scaling
299 RenderableAttachment( bool usesGeometryScaling );
302 * Virtual destructor, no deletion through this interface
304 virtual ~RenderableAttachment();
306 private: // From NodeAttachment
309 * @copydoc NodeAttachment::ConnectToSceneGraph().
311 virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
314 * @copydoc NodeAttachment::DisconnectFromSceneGraph().
316 virtual void OnDestroy();
319 * @copydoc NodeAttachment::GetRenderable()
321 virtual RenderableAttachment* GetRenderable();
326 * Prepare the object resources.
327 * This must be called by the UpdateManager before calling PrepareRender, for each frame.
328 * @param[in] updateBufferIndex The current buffer index.
329 * @param[in] resourceManager The resource manager.
330 * @return True if resources are ready, false will prevent PrepareRender being called for this attachment.
332 virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager ) = 0;
335 * Sends the shader to the renderer
336 * @param updateBufferIndex for the message buffer
338 void SendShaderChangeMessage( BufferIndex updateBufferIndex );
341 RenderableAttachment( const RenderableAttachment& );
344 RenderableAttachment& operator=( const RenderableAttachment& rhs );
348 SceneController* mSceneController; ///< Used for initializing renderers whilst attached
349 Shader* mShader; ///< A pointer to the shader
351 Dali::Vector< Integration::ResourceId > mTrackedResources; ///< Filled during PrepareResources if there are uncomplete, tracked resources.
355 BlendingMode::Type mBlendingMode:2; ///< Whether blending is used to render the renderable attachment. 2 bits is enough for 3 values
357 bool mUsesGeometryScaling:1; ///< True if the derived renderer uses scaling.
358 bool mScaleForSizeDirty:1; ///< True if mScaleForSize has changed in the current frame.
359 bool mUseBlend:1; ///< True if the attachment & renderer should be considered opaque for sorting and blending.
360 bool mHasSizeAndColorFlag:1; ///< Set during the update algorithm to tell whether this renderer can potentially be seen
361 bool mResourcesReady:1; ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
362 bool mFinishedResourceAcquisition:1; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
363 bool mHasUntrackedResources:1; ///< Set during PrepareResources, true if have tried to follow untracked resources
364 CullFaceMode mCullFaceMode:3; ///< Cullface mode, 3 bits is enough for 4 values
368 // Messages for RenderableAttachment
370 inline void SetSortModifierMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, float modifier )
372 typedef MessageValue1< RenderableAttachment, float > LocalType;
374 // Reserve some memory inside the message queue
375 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
377 // Construct message in the message queue memory; note that delete should not be called on the return value
378 new (slot) LocalType( &attachment, &RenderableAttachment::SetSortModifier, modifier );
381 inline void SetCullFaceMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, CullFaceMode mode )
383 typedef MessageDoubleBuffered1< RenderableAttachment, CullFaceMode > LocalType;
385 // Reserve some memory inside the message queue
386 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
388 // Construct message in the message queue memory; note that delete should not be called on the return value
389 new (slot) LocalType( &attachment, &RenderableAttachment::SetCullFace, mode );
392 inline void SetBlendingModeMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, BlendingMode::Type mode )
394 typedef MessageValue1< RenderableAttachment, BlendingMode::Type > LocalType;
396 // Reserve some memory inside the message queue
397 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
399 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingMode, mode );
402 inline void SetBlendingOptionsMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, unsigned int options )
404 typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
406 // Reserve some memory inside the message queue
407 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
409 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingOptions, options );
412 inline void SetBlendColorMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, const Vector4& color )
414 typedef MessageDoubleBuffered1< RenderableAttachment, Vector4 > LocalType;
416 // Reserve some memory inside the message queue
417 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
419 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendColor, color );
422 inline void SetSamplerMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, unsigned int samplerBitfield )
424 typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
426 // Reserve some memory inside the message queue
427 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
429 // Construct message in the message queue memory; note that delete should not be called on the return value
430 new (slot) LocalType( &attachment, &RenderableAttachment::SetSampler, samplerBitfield );
433 inline void ApplyShaderMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, const Shader& constShader )
435 // Update thread can edit the object
436 Shader& shader = const_cast< Shader& >( constShader );
438 typedef MessageDoubleBuffered1< RenderableAttachment, Shader* > LocalType;
440 // Reserve some memory inside the message queue
441 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
443 // Construct message in the message queue memory; note that delete should not be called on the return value
444 new (slot) LocalType( &attachment, &RenderableAttachment::ApplyShader, &shader );
447 inline void RemoveShaderMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment )
449 typedef MessageDoubleBuffered0< RenderableAttachment > LocalType;
451 // Reserve some memory inside the message queue
452 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
454 // Construct message in the message queue memory; note that delete should not be called on the return value
455 new (slot) LocalType( &attachment, &RenderableAttachment::RemoveShader );
458 } // namespace SceneGraph
460 } // namespace Internal
464 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__