1ab4755638cf4f0ac1653ba30691250f62b8e30a
[platform/core/uifw/dali-core.git] / dali / internal / event / effects / shader-effect-impl.h
1 #ifndef __DALI_INTERNAL_SHADER_EFFECT_H__
2 #define __DALI_INTERNAL_SHADER_EFFECT_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
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
10 //
11 //     http://floralicense.org/license/
12 //
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.
18 //
19
20 // INTERNAL INCLUDES
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>
28
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 class ShaderFactory;
37
38 namespace SceneGraph
39 {
40 class UpdateManager;
41 }
42
43 /**
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.
47  */
48 class ShaderEffect : public ProxyObject
49 {
50 public:
51
52   typedef Dali::ShaderEffect::UniformCoordinateType UniformCoordinateType;
53
54   /**
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.
58    */
59   static ShaderEffectPtr New(Dali::ShaderEffect::GeometryHints hints = Dali::ShaderEffect::HINT_NONE);
60
61   /**
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.
68    */
69   static ShaderEffectPtr New( const std::string& vertexShader,
70                               const std::string& fragmentShader,
71                               GeometryType type,
72                               Dali::ShaderEffect::GeometryHints hints );
73   /**
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.
82    */
83   static ShaderEffectPtr NewWithPrefix( const std::string& vertexShaderPrefix,
84                                         const std::string& vertexShader,
85                                         const std::string& fragmentShaderPrefix,
86                                         const std::string& fragmentShader,
87                                         GeometryType type,
88                                         Dali::ShaderEffect::GeometryHints hints );
89
90   /**
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
103    */
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 );
113
114   /**
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.
118    */
119   static ShaderEffectPtr New( const Property::Value& map );
120
121   /**
122    * @copydoc Dali::ShaderEffect::SetEffectImage
123    */
124   void SetEffectImage( Dali::Image image );
125
126   /**
127    * @copydoc Dali::ShaderEffect::SetUniform( const std::string& name, Property::Value value, UniformCoordinateType uniformCoordinateType )
128    */
129   void SetUniform( const std::string& name,
130                    Property::Value value,
131                    UniformCoordinateType uniformCoordinateType );
132
133   /**
134    * @copydoc Dali::ShaderEffect::AttachExtension()
135    */
136   void AttachExtension( Dali::ShaderEffect::Extension *object );
137
138   /**
139    * @copydoc Dali::ShaderEffect::GetExtension()
140    */
141   Dali::ShaderEffect::Extension& GetExtension();
142
143   /**
144    * @copydoc Dali::ShaderEffect::GetExtension() const
145    */
146   const Dali::ShaderEffect::Extension& GetExtension() const;
147
148   /**
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
154    */
155   void SetProgram( GeometryType geometryType, ShaderSubTypes subType,
156                    const std::string& vertexSource, const std::string& fragmentSource );
157
158   /**
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
168    */
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 );
172
173   /**
174    * Notify ShaderEffect that it's being used by an Actor.
175    */
176   void Connect();
177
178   /**
179    * Notify ShaderEffect that an Actor is no longer using it.
180    */
181   void Disconnect();
182
183 public: // Default property extensions from ProxyObject
184
185   /**
186    * @copydoc Dali::Internal::ProxyObject::IsSceneObjectRemovable()
187    */
188   virtual bool IsSceneObjectRemovable() const;
189
190   /**
191    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyCount()
192    */
193   virtual unsigned int GetDefaultPropertyCount() const;
194
195   /**
196    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndices()
197    */
198   virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const;
199
200   /**
201    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyName()
202    */
203   virtual const std::string& GetDefaultPropertyName( Property::Index index ) const;
204
205   /**
206    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyIndex()
207    */
208   virtual Property::Index GetDefaultPropertyIndex( const std::string& name ) const;
209
210   /**
211    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyWritable()
212    */
213   virtual bool IsDefaultPropertyWritable( Property::Index index ) const;
214
215   /**
216    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAnimatable()
217    */
218   virtual bool IsDefaultPropertyAnimatable( Property::Index index ) const;
219
220   /**
221    * @copydoc Dali::Internal::ProxyObject::IsDefaultPropertyAConstraintInput()
222    */
223   virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const;
224
225   /**
226    * @copydoc Dali::Internal::ProxyObject::GetDefaultPropertyType()
227    */
228   virtual Property::Type GetDefaultPropertyType( Property::Index index ) const;
229
230   /**
231    * @copydoc Dali::Internal::ProxyObject::SetDefaultProperty()
232    */
233   virtual void SetDefaultProperty( Property::Index index, const Property::Value& propertyValue );
234
235   /**
236    * @copydoc Dali::Internal::ProxyObject::GetDefaultProperty()
237    */
238   virtual Property::Value GetDefaultProperty( Property::Index index ) const;
239
240   /**
241    * @copydoc Dali::Internal::ProxyObject::InstallSceneObjectProperty()
242    */
243   virtual void InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index );
244
245   /**
246    * @copydoc Dali::Internal::ProxyObject::GetSceneObject()
247    */
248   virtual const SceneGraph::PropertyOwner* GetSceneObject() const;
249
250   /**
251    * @copydoc Dali::Internal::ProxyObject::GetSceneObjectAnimatableProperty()
252    */
253   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
254
255   /**
256    * @copydoc Dali::Internal::ProxyObject::GetSceneObjectInputProperty()
257    */
258   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
259
260 protected:
261
262   /**
263    * Protected constructor.
264    */
265   ShaderEffect( SceneGraph::UpdateManager& updateManager, ShaderFactory& shaderFactory, SceneGraph::Shader& sceneObject );
266
267   /**
268    * A reference counted object may only be deleted by calling Unreference()
269    */
270   virtual ~ShaderEffect();
271
272 private:
273
274   // No copying allowed, thus these are undefined
275   ShaderEffect( const ShaderEffect& );
276   ShaderEffect& operator=( const ShaderEffect& rhs );
277
278   void OnImageLoaded( Dali::Image image ); ///< just a helper for image loaded callback
279
280 private: // Data
281
282   SceneGraph::UpdateManager& mUpdateManager;            ///< reference to the update manager
283   ShaderFactory& mShaderFactory;                        ///< reference to the shader factory
284
285   SceneGraph::Shader* mSceneObject;                     ///< pointer to the scene shader, should not be changed on this thread
286
287   Dali::Image mImage;                                   ///< Client-side handle for the effect image
288
289   CustomUniformMetaLookup mCustomMetadata;            ///< Used for accessing metadata for custom Shader properties
290
291   IntrusivePtr<Dali::ShaderEffect::Extension> mExtension;
292
293   std::vector<ResourceTicketPtr>  mTickets;             ///< Collection of shader program tickets
294
295   unsigned int  mConnectionCount;                       ///< number of on-stage ImageActors using this shader effect
296
297   // Default properties
298   typedef std::map<std::string, Property::Index> DefaultPropertyLookup;
299   static DefaultPropertyLookup* mDefaultPropertyLookup;
300 };
301
302 } // namespace Internal
303
304 // Helpers for public-api forwarding methods
305
306 inline Internal::ShaderEffect& GetImplementation(Dali::ShaderEffect& effect)
307 {
308   DALI_ASSERT_ALWAYS(effect && "ShaderEffect handle is empty");
309
310   BaseObject& handle = effect.GetBaseObject();
311
312   return static_cast<Internal::ShaderEffect&>(handle);
313 }
314
315 inline const Internal::ShaderEffect& GetImplementation(const Dali::ShaderEffect& effect)
316 {
317   DALI_ASSERT_ALWAYS(effect && "ShaderEffect handle is empty");
318
319   const BaseObject& handle = effect.GetBaseObject();
320
321   return static_cast<const Internal::ShaderEffect&>(handle);
322 }
323
324 } // namespace Dali
325
326 #endif // __DALI_INTERNAL_SHADER_EFFECT_H__