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 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/public-api/object/ref-object.h>
23 #include <dali/public-api/shader-effects/shader-effect.h>
24 #include <dali/internal/event/effects/shader-declarations.h>
25 #include <dali/internal/event/common/proxy-object.h>
26 #include <dali/internal/render/shaders/uniform-meta.h>
27 #include <dali/internal/event/resources/resource-ticket.h>
28 #include <dali/internal/render/shaders/shader.h>
45 * An abstract base class for a shader effect proxy.
46 * The corresponding scene-graph object is a collection of shader programs,
47 * which can apply the same effect to different geometry types.
49 class ShaderEffect : public ProxyObject
53 typedef Dali::ShaderEffect::UniformCoordinateType UniformCoordinateType;
56 * Create a new ShaderEffect with no programs
57 * @param hints GeometryHints to define the geometry of the rendered object
58 * @return A smart-pointer to a newly allocated shader effect.
60 static ShaderEffectPtr New(Dali::ShaderEffect::GeometryHints hints = Dali::ShaderEffect::HINT_NONE);
63 * Create a new ShaderEffect
64 * @param vertexShader code for the effect. If empty, the default version will be used
65 * @param fragmentShader code for the effect. If empty, the default version will be used
66 * @param type GeometryType
67 * @param hints GeometryHints to define the geometry of the rendered object
68 * @return A smart-pointer to a newly allocated shader effect.
70 static ShaderEffectPtr New( const std::string& vertexShader,
71 const std::string& fragmentShader,
73 Dali::ShaderEffect::GeometryHints hints );
75 * Create a new ShaderEffect
76 * @param vertexShaderPrefix code for the effect. It will be inserted before the default uniforms.
77 * @param vertexShader code for the effect. If empty, the default version will be used
78 * @param fragmentShaderPrefix code for the effect. It will be inserted before the default uniforms.
79 * @param fragmentShader code for the effect. If empty, the default version will be used
80 * @param type GeometryType
81 * @param hints GeometryHints to define the geometry of the rendered object
82 * @return A smart-pointer to a newly allocated shader effect.
84 static ShaderEffectPtr NewWithPrefix( const std::string& vertexShaderPrefix,
85 const std::string& vertexShader,
86 const std::string& fragmentShaderPrefix,
87 const std::string& fragmentShader,
89 Dali::ShaderEffect::GeometryHints hints );
92 * Create a new ShaderEffect.
93 * If you pass in an empty string in the following arguments, the default version will be used instead.
94 * @param imageVertexShader code for the effect.
95 * @param imageFragmentShader code for the effect.
96 * @param textVertexShader code for the effect.
97 * @param textFragmentShader code for the effect.
98 * @param texturedMeshVertexShader code for the effect.
99 * @param texturedMeshFragmentShader code for the effect.
100 * @param meshVertexShader code for the effect.
101 * @param meshFragmentShader code for the effect.
102 * @param hints GeometryHints to define the geometry of the rendered object
103 * @return A handle to a shader effect
105 static ShaderEffectPtr New( const std::string& imageVertexShader,
106 const std::string& imageFragmentShader,
107 const std::string& textVertexShader,
108 const std::string& textFragmentShader,
109 const std::string& texturedMeshVertexShader,
110 const std::string& texturedMeshFragmentShader,
111 const std::string& meshVertexShader,
112 const std::string& meshFragmentShader,
113 Dali::ShaderEffect::GeometryHints hints );
116 * Creates object with data from the property value map
117 * @param [in] map The property value map with fields such as 'vertex-filename' '..'
118 * @return a pointer to a newly created object.
120 static ShaderEffectPtr New( const Property::Value& map );
123 * @copydoc Dali::ShaderEffect::SetEffectImage
125 void SetEffectImage( Dali::Image image );
128 * @copydoc Dali::ShaderEffect::SetUniform( const std::string& name, Property::Value value, UniformCoordinateType uniformCoordinateType )
130 void SetUniform( const std::string& name,
131 Property::Value value,
132 UniformCoordinateType uniformCoordinateType );
135 * @copydoc Dali::ShaderEffect::AttachExtension()
137 void AttachExtension( Dali::ShaderEffect::Extension *object );
140 * @copydoc Dali::ShaderEffect::GetExtension()
142 Dali::ShaderEffect::Extension& GetExtension();
145 * @copydoc Dali::ShaderEffect::GetExtension() const
147 const Dali::ShaderEffect::Extension& GetExtension() const;
150 * Add a GeometryType specific default program to this ShaderEffect
151 * @param[in] geometryType The GeometryType rendered by the shader program
152 * @param[in] subType The subtype, one of ShaderSubTypes.
153 * @param[in] vertexSource The source code for the vertex shader
154 * @param[in] fragmentSource The source code for the fragment shader
156 void SetProgram( GeometryType geometryType, ShaderSubTypes subType,
157 const std::string& vertexSource, const std::string& fragmentSource );
160 * Add a GeometryType specific default program to this ShaderEffect.
161 * This overload allows the optional prefixing for both the vertex and fragment shader.
162 * A useful prefix may be shader \#defines for conditional compilation.
163 * @param[in] geometryType The GeometryType rendered by the shader program
164 * @param[in] subType The subtype, one of ShaderSubTypes.
165 * @param[in] vertexPrefix The prefix source code for the vertex shader
166 * @param[in] fragmentPrefix The prefix source code for the fragment shader
167 * @param[in] vertexSource The source code for the vertex shader
168 * @param[in] fragmentSource The source code for the fragment shader
170 void SetProgram( GeometryType geometryType, ShaderSubTypes subType,
171 const std::string& vertexPrefix, const std::string& fragmentPrefix,
172 const std::string& vertexSource, const std::string& fragmentSource );
175 * Notify ShaderEffect that it's being used by an Actor.
180 * Notify ShaderEffect that an Actor is no longer using it.
184 public: // Default property extensions from ProxyObject
187 * @copydoc Dali::Internal::ProxyObject::IsSceneObjectRemovable()
189 virtual bool IsSceneObjectRemovable() const;
192 * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyCount()
194 virtual unsigned int GetDefaultPropertyCount() const;
197 * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndices()
199 virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const;
202 * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyName()
204 virtual const std::string& GetDefaultPropertyName( Property::Index index ) const;
207 * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndex()
209 virtual Property::Index GetDefaultPropertyIndex( const std::string& name ) const;
212 * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyWritable()
214 virtual bool IsDefaultPropertyWritable( Property::Index index ) const;
217 * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAnimatable()
219 virtual bool IsDefaultPropertyAnimatable( Property::Index index ) const;
222 * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAConstraintInput()
224 virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const;
227 * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyType()
229 virtual Property::Type GetDefaultPropertyType( Property::Index index ) const;
232 * @copydoc Dali::Internal::ProxyObject::SetDefaultProperty()
234 virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue );
237 * @copydoc Dali::Internal::ProxyObject::GetDefaultProperty()
239 virtual Property::Value GetDefaultProperty( Property::Index index ) const;
242 * @copydoc Dali::Internal::ProxyObject::InstallSceneObjectProperty()
244 virtual void InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index );
247 * @copydoc Dali::Internal::ProxyObject::GetSceneObject()
249 virtual const SceneGraph::PropertyOwner* GetSceneObject() const;
252 * @copydoc Dali::Internal::ProxyObject::GetSceneObjectAnimatableProperty()
254 virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
257 * @copydoc Dali::Internal::ProxyObject::GetSceneObjectInputProperty()
259 virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
264 * Protected constructor.
266 ShaderEffect( SceneGraph::UpdateManager& updateManager, ShaderFactory& shaderFactory, SceneGraph::Shader& sceneObject );
269 * A reference counted object may only be deleted by calling Unreference()
271 virtual ~ShaderEffect();
275 // No copying allowed, thus these are undefined
276 ShaderEffect( const ShaderEffect& );
277 ShaderEffect& operator=( const ShaderEffect& rhs );
279 void OnImageLoaded( Dali::Image image ); ///< just a helper for image loaded callback
283 SceneGraph::UpdateManager& mUpdateManager; ///< reference to the update manager
284 ShaderFactory& mShaderFactory; ///< reference to the shader factory
286 SceneGraph::Shader* mSceneObject; ///< pointer to the scene shader, should not be changed on this thread
288 Dali::Image mImage; ///< Client-side handle for the effect image
290 CustomUniformMetaLookup mCustomMetadata; ///< Used for accessing metadata for custom Shader properties
292 IntrusivePtr<Dali::ShaderEffect::Extension> mExtension;
294 std::vector<ResourceTicketPtr> mTickets; ///< Collection of shader program tickets
296 unsigned int mConnectionCount; ///< number of on-stage ImageActors using this shader effect
298 // Default properties
299 typedef std::map<std::string, Property::Index> DefaultPropertyLookup;
300 static DefaultPropertyLookup* mDefaultPropertyLookup;
303 } // namespace Internal
305 // Helpers for public-api forwarding methods
307 inline Internal::ShaderEffect& GetImplementation(Dali::ShaderEffect& effect)
309 DALI_ASSERT_ALWAYS(effect && "ShaderEffect handle is empty");
311 BaseObject& handle = effect.GetBaseObject();
313 return static_cast<Internal::ShaderEffect&>(handle);
316 inline const Internal::ShaderEffect& GetImplementation(const Dali::ShaderEffect& effect)
318 DALI_ASSERT_ALWAYS(effect && "ShaderEffect handle is empty");
320 const BaseObject& handle = effect.GetBaseObject();
322 return static_cast<const Internal::ShaderEffect&>(handle);
327 #endif // __DALI_INTERNAL_SHADER_EFFECT_H__