1 #ifndef DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H
2 #define DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H
5 * Copyright (c) 2015 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.
21 #include <dali/devel-api/rendering/material.h>
22 #include <dali/internal/common/buffer-index.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/update/common/animatable-property.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/common/scene-graph-connection-change-propagator.h>
27 #include <dali/internal/update/common/uniform-map.h>
28 #include <dali/internal/update/resources/resource-manager-declarations.h>
34 class ResourceManager;
44 class ConnectionObserver;
45 class SceneController;
47 class Material : public PropertyOwner, public UniformMap::Observer, public ConnectionChangePropagator::Observer
52 * Construct a new Material.
54 static Material* New();
62 * Overriden delete operator
63 * Deletes the material from its global memory pool
65 void operator delete( void* ptr );
68 * Prepare material, check texture loading status, opacity etc
69 * @param resourceManager for checking texture details and loading status
71 void Prepare( const ResourceManager& resourceManager );
74 * Set the shader effect for this material
75 * @param[in] shader The shader effect to use
77 void SetShader( Shader* shader );
80 * Get the shader effect of this material
81 * @return the shader effect;
83 Shader* GetShader() const;
86 * Adds a new texture to be used by the material
87 * @param[in] image The image used by the texture
88 * @param[in] uniformName The uniform name of the texture
89 * @param[in] sampler Sampling parameters. If not provided the default sampling parameters will be used
91 void AddTexture( const std::string& name, ResourceId id, Render::Sampler* sampler );
94 * Removes a texture from the material
95 * @param[in] index The index of the texture in the array of textures
97 void RemoveTexture( size_t index );
100 * Sets the image to be used by a given texture
101 * @param[in] index The index of the texture in the array of textures
102 * @param[in] image The new image
104 void SetTextureImage( size_t index, ResourceId id );
107 * Set the sampler used by a given texture
108 * @param[in] index The index of the texture in the array of textures
109 * @param[in] sampler The new sampler
111 void SetTextureSampler( size_t index, Render::Sampler* sampler);
114 * Set the uniform name of a given texture
115 * @param[in] index The index of the texture in the array of textures
116 * @param[in] uniformName The new uniform name
118 void SetTextureUniformName( size_t index, const std::string& uniformName );
121 * Return whether the textures or the shader require the opacity to be translucent
122 * @return The material's blend policy
124 bool IsTranslucent() const;
127 * Get the material resource status
128 * Note, we need two values as it's possible that some resource failed to load
129 * in which case resourcesReady is false (the material is not good to be rendered)
130 * but finishedResourceAcquisition if true as there is no more loading going on
131 * @param[out] resourcesReady if the material is ready to be rendered
132 * @param[out] finishedResourceAcquisition if
134 void GetResourcesStatus( bool& resourcesReady, bool& finishedResourceAcquisition );
137 public: // Implementation of ObjectOwnerContainer template methods
140 * Connect the object to the scene graph
142 * @param[in] sceneController The scene controller - used for sending messages to render thread
143 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
145 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
148 * Disconnect the object from the scene graph
149 * @param[in] sceneController The scene controller - used for sending messages to render thread
150 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
152 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
154 public: // Implementation of ConnectionChangePropagator
157 * @copydoc ConnectionChangePropagator::AddObserver
159 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer);
162 * @copydoc ConnectionChangePropagator::RemoveObserver
164 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer);
169 * Get the ResourceId of a texture used by the material
170 * @param[in] index The index of the texture in the textures array
171 * @return the ResourceId
173 ResourceId GetTextureId( size_t index )
175 return mTextureId[index];
179 * Get the uniform name of a texture used by the material
180 * @param[in] index The index of the texture in the textures array
181 * @return the uniform name
183 const std::string& GetTextureUniformName( size_t index )
185 return mUniformName[index];
189 * Get the sampler of a texture used by the material
190 * @param[in] index The index of the texture in the textures array
191 * @return the sampler used by the texture
193 Render::Sampler* GetTextureSampler( size_t index )
195 return mSamplers[index];
199 * Get the number of textures used by the material
200 * @return The number of textures
202 size_t GetTextureCount()
204 return mTextureId.Size();
207 public: // UniformMap::Observer
209 * @copydoc UniformMap::Observer::UniformMappingsChanged
211 virtual void UniformMappingsChanged( const UniformMap& mappings );
213 public: // ConnectionChangePropagator::Observer
216 * @copydoc ConnectionChangePropagator::ConnectionsChanged
218 virtual void ConnectionsChanged( PropertyOwner& owner );
221 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
223 virtual void ConnectedUniformMapChanged();
228 * Protected constructor; See also Material::New()
235 Vector< Render::Sampler* > mSamplers; // Not owned
236 Vector< ResourceId > mTextureId;
237 std::vector< std::string > mUniformName;
238 ConnectionChangePropagator mConnectionObservers;
239 bool mResourcesReady; ///< if the material is ready to be rendered
240 bool mFinishedResourceAcquisition; ///< if resource loading is completed
241 bool mMaterialChanged; ///< if the material has changed since the last frame
242 bool mIsTranslucent; ///< if the textures or the shader require the opacity to be translucent
245 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Material& material, Shader& shader )
247 typedef MessageValue1< Material, Shader* > LocalType;
249 // Reserve some memory inside the message queue
250 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
252 // Construct message in the message queue memory; note that delete should not be called on the return value
253 new (slot) LocalType( &material, &Material::SetShader, &shader );
256 inline void AddTextureMessage( EventThreadServices& eventThreadServices, const Material& material, const std::string& uniformName, ResourceId id, Render::Sampler* sampler )
258 typedef MessageValue3< Material, std::string, ResourceId, Render::Sampler* > LocalType;
260 // Reserve some memory inside the message queue
261 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
263 // Construct message in the message queue memory; note that delete should not be called on the return value
264 new (slot) LocalType( &material, &Material::AddTexture, uniformName, id, sampler );
267 inline void RemoveTextureMessage( EventThreadServices& eventThreadServices, const Material& material, size_t index )
269 typedef MessageValue1< Material, size_t > LocalType;
271 // Reserve some memory inside the message queue
272 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
274 // Construct message in the message queue memory; note that delete should not be called on the return value
275 new (slot) LocalType( &material, &Material::RemoveTexture, index );
278 inline void SetTextureImageMessage( EventThreadServices& eventThreadServices, const Material& material, size_t index, ResourceId id )
280 typedef MessageValue2< Material, size_t, ResourceId > LocalType;
282 // Reserve some memory inside the message queue
283 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
285 // Construct message in the message queue memory; note that delete should not be called on the return value
286 new (slot) LocalType( &material, &Material::SetTextureImage, index, id );
289 inline void SetTextureSamplerMessage( EventThreadServices& eventThreadServices, const Material& material, size_t index, Render::Sampler* sampler )
291 typedef MessageValue2< Material, size_t, Render::Sampler* > LocalType;
293 // Reserve some memory inside the message queue
294 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
296 // Construct message in the message queue memory; note that delete should not be called on the return value
297 new (slot) LocalType( &material, &Material::SetTextureSampler, index, sampler );
300 inline void SetTextureUniformNameMessage( EventThreadServices& eventThreadServices, const Material& material, size_t index, const std::string& uniformName )
302 typedef MessageValue2< Material, size_t, std::string > LocalType;
304 // Reserve some memory inside the message queue
305 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
307 // Construct message in the message queue memory; note that delete should not be called on the return value
308 new (slot) LocalType( &material, &Material::SetTextureUniformName, index, uniformName );
311 } // namespace SceneGraph
313 } // namespace Internal
317 #endif // DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H