1 #ifndef __DALI_INTERNAL_SHADER_EFFECT_H__
2 #define __DALI_INTERNAL_SHADER_EFFECT_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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/public-api/object/ref-object.h>
22 #include <dali/public-api/shader-effects/shader-effect.h>
23 #include <dali/internal/event/effects/shader-declarations.h>
24 #include <dali/internal/event/common/proxy-object.h>
25 #include <dali/internal/render/shaders/uniform-meta.h>
26 #include <dali/internal/event/resources/resource-ticket.h>
27 #include <dali/internal/render/shaders/shader.h>
44 * An abstract base class for a shader effect proxy.
45 * The corresponding scene-graph object is a collection of shader programs,
46 * which can apply the same effect to different geometry types.
48 class ShaderEffect : public ProxyObject
52 typedef Dali::ShaderEffect::UniformCoordinateType UniformCoordinateType;
55 * Create a new ShaderEffect with no programs
56 * @param hints GeometryHints to define the geometry of the rendered object
57 * @return A smart-pointer to a newly allocated shader effect.
59 static ShaderEffectPtr New(Dali::ShaderEffect::GeometryHints hints = Dali::ShaderEffect::HINT_NONE);
62 * Create a new ShaderEffect
63 * @param vertexShader code for the effect. If empty, the default version will be used
64 * @param fragmentShader code for the effect. If empty, the default version will be used
65 * @param type GeometryType
66 * @param hints GeometryHints to define the geometry of the rendered object
67 * @return A smart-pointer to a newly allocated shader effect.
69 static ShaderEffectPtr New( const std::string& vertexShader,
70 const std::string& fragmentShader,
72 Dali::ShaderEffect::GeometryHints hints );
74 * Create a new ShaderEffect
75 * @param vertexShaderPrefix code for the effect. It will be inserted before the default uniforms.
76 * @param vertexShader code for the effect. If empty, the default version will be used
77 * @param fragmentShaderPrefix code for the effect. It will be inserted before the default uniforms.
78 * @param fragmentShader code for the effect. If empty, the default version will be used
79 * @param type GeometryType
80 * @param hints GeometryHints to define the geometry of the rendered object
81 * @return A smart-pointer to a newly allocated shader effect.
83 static ShaderEffectPtr NewWithPrefix( const std::string& vertexShaderPrefix,
84 const std::string& vertexShader,
85 const std::string& fragmentShaderPrefix,
86 const std::string& fragmentShader,
88 Dali::ShaderEffect::GeometryHints hints );
91 * Create a new ShaderEffect.
92 * If you pass in an empty string in the following arguments, the default version will be used instead.
93 * @param imageVertexShader code for the effect.
94 * @param imageFragmentShader code for the effect.
95 * @param textVertexShader code for the effect.
96 * @param textFragmentShader code for the effect.
97 * @param texturedMeshVertexShader code for the effect.
98 * @param texturedMeshFragmentShader code for the effect.
99 * @param meshVertexShader code for the effect.
100 * @param meshFragmentShader code for the effect.
101 * @param hints GeometryHints to define the geometry of the rendered object
102 * @return A handle to a shader effect
104 static ShaderEffectPtr New( const std::string& imageVertexShader,
105 const std::string& imageFragmentShader,
106 const std::string& textVertexShader,
107 const std::string& textFragmentShader,
108 const std::string& texturedMeshVertexShader,
109 const std::string& texturedMeshFragmentShader,
110 const std::string& meshVertexShader,
111 const std::string& meshFragmentShader,
112 Dali::ShaderEffect::GeometryHints hints );
115 * Creates object with data from the property value map
116 * @param [in] map The property value map with fields such as 'vertex-filename' '..'
117 * @return a pointer to a newly created object.
119 static ShaderEffectPtr New( const Property::Value& map );
122 * @copydoc Dali::ShaderEffect::SetEffectImage
124 void SetEffectImage( Dali::Image image );
127 * @copydoc Dali::ShaderEffect::SetUniform( const std::string& name, Property::Value value, UniformCoordinateType uniformCoordinateType )
129 void SetUniform( const std::string& name,
130 Property::Value value,
131 UniformCoordinateType uniformCoordinateType );
134 * @copydoc Dali::ShaderEffect::AttachExtension()
136 void AttachExtension( Dali::ShaderEffect::Extension *object );
139 * @copydoc Dali::ShaderEffect::GetExtension()
141 Dali::ShaderEffect::Extension& GetExtension();
144 * @copydoc Dali::ShaderEffect::GetExtension() const
146 const Dali::ShaderEffect::Extension& GetExtension() const;
149 * Add a GeometryType specific default program to this ShaderEffect
150 * @param[in] geometryType The GeometryType rendered by the shader program
151 * @param[in] subType The subtype, one of ShaderSubTypes.
152 * @param[in] vertexSource The source code for the vertex shader
153 * @param[in] fragmentSource The source code for the fragment shader
155 void SetProgram( GeometryType geometryType, ShaderSubTypes subType,
156 const std::string& vertexSource, const std::string& fragmentSource );
159 * Add a GeometryType specific default program to this ShaderEffect.
160 * This overload allows the optional prefixing for both the vertex and fragment shader.
161 * A useful prefix may be shader \#defines for conditional compilation.
162 * @param[in] geometryType The GeometryType rendered by the shader program
163 * @param[in] subType The subtype, one of ShaderSubTypes.
164 * @param[in] vertexPrefix The prefix source code for the vertex shader
165 * @param[in] fragmentPrefix The prefix source code for the fragment shader
166 * @param[in] vertexSource The source code for the vertex shader
167 * @param[in] fragmentSource The source code for the fragment shader
169 void SetProgram( GeometryType geometryType, ShaderSubTypes subType,
170 const std::string& vertexPrefix, const std::string& fragmentPrefix,
171 const std::string& vertexSource, const std::string& fragmentSource );
174 * Notify ShaderEffect that it's being used by an Actor.
179 * Notify ShaderEffect that an Actor is no longer using it.
183 public: // Default property extensions from ProxyObject
186 * @copydoc Dali::Internal::ProxyObject::IsSceneObjectRemovable()
188 virtual bool IsSceneObjectRemovable() const;
191 * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyCount()
193 virtual unsigned int GetDefaultPropertyCount() const;
196 * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndices()
198 virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const;
201 * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyName()
203 virtual const std::string& GetDefaultPropertyName( Property::Index index ) const;
206 * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndex()
208 virtual Property::Index GetDefaultPropertyIndex( const std::string& name ) const;
211 * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyWritable()
213 virtual bool IsDefaultPropertyWritable( Property::Index index ) const;
216 * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAnimatable()
218 virtual bool IsDefaultPropertyAnimatable( Property::Index index ) const;
221 * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAConstraintInput()
223 virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const;
226 * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyType()
228 virtual Property::Type GetDefaultPropertyType( Property::Index index ) const;
231 * @copydoc Dali::Internal::ProxyObject::SetDefaultProperty()
233 virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue );
236 * @copydoc Dali::Internal::ProxyObject::GetDefaultProperty()
238 virtual Property::Value GetDefaultProperty( Property::Index index ) const;
241 * @copydoc Dali::Internal::ProxyObject::InstallSceneObjectProperty()
243 virtual void InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index );
246 * @copydoc Dali::Internal::ProxyObject::GetSceneObject()
248 virtual const SceneGraph::PropertyOwner* GetSceneObject() const;
251 * @copydoc Dali::Internal::ProxyObject::GetSceneObjectAnimatableProperty()
253 virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
256 * @copydoc Dali::Internal::ProxyObject::GetSceneObjectInputProperty()
258 virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
263 * Protected constructor.
265 ShaderEffect( SceneGraph::UpdateManager& updateManager, ShaderFactory& shaderFactory, SceneGraph::Shader& sceneObject );
268 * A reference counted object may only be deleted by calling Unreference()
270 virtual ~ShaderEffect();
274 // No copying allowed, thus these are undefined
275 ShaderEffect( const ShaderEffect& );
276 ShaderEffect& operator=( const ShaderEffect& rhs );
278 void OnImageLoaded( Dali::Image image ); ///< just a helper for image loaded callback
282 SceneGraph::UpdateManager& mUpdateManager; ///< reference to the update manager
283 ShaderFactory& mShaderFactory; ///< reference to the shader factory
285 SceneGraph::Shader* mSceneObject; ///< pointer to the scene shader, should not be changed on this thread
287 Dali::Image mImage; ///< Client-side handle for the effect image
289 CustomUniformMetaLookup mCustomMetadata; ///< Used for accessing metadata for custom Shader properties
291 IntrusivePtr<Dali::ShaderEffect::Extension> mExtension;
293 std::vector<ResourceTicketPtr> mTickets; ///< Collection of shader program tickets
295 unsigned int mConnectionCount; ///< number of on-stage ImageActors using this shader effect
297 // Default properties
298 typedef std::map<std::string, Property::Index> DefaultPropertyLookup;
299 static DefaultPropertyLookup* mDefaultPropertyLookup;
302 } // namespace Internal
304 // Helpers for public-api forwarding methods
306 inline Internal::ShaderEffect& GetImplementation(Dali::ShaderEffect& effect)
308 DALI_ASSERT_ALWAYS(effect && "ShaderEffect handle is empty");
310 BaseObject& handle = effect.GetBaseObject();
312 return static_cast<Internal::ShaderEffect&>(handle);
315 inline const Internal::ShaderEffect& GetImplementation(const Dali::ShaderEffect& effect)
317 DALI_ASSERT_ALWAYS(effect && "ShaderEffect handle is empty");
319 const BaseObject& handle = effect.GetBaseObject();
321 return static_cast<const Internal::ShaderEffect&>(handle);
326 #endif // __DALI_INTERNAL_SHADER_EFFECT_H__