Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-core.git] / dali / public-api / shader-effects / shader-effect.h
1 #ifndef __DALI_SHADER_EFFECT_H__
2 #define __DALI_SHADER_EFFECT_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
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
21 // INTERNAL INCLUDES
22 #include <dali/public-api/animation/active-constraint-declarations.h>
23 #include <dali/public-api/object/handle.h>
24 #include <dali/public-api/object/property-index-ranges.h>
25
26 namespace Dali
27 {
28
29 /**
30  * @brief DALI_COMPOSE_SHADER macro provides a convenient way to write shader source code.
31  *
32  * We normally use double quotation marks to write a string such as "Hello World".
33  * However many symbols are needed to add multiple lines of string.
34  * We don't need to write quotation marks using this macro at every line.
35  *
36  * [An example of double quotation marks usage]
37  * const string FRAGMENT_SHADER_SOURCE = \
38  * "  void main()\n"
39  * "  {\n"
40  * "    gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
41  * "  }\n";
42  *
43  * [An example of DALI_COMPOSE_SHADER usage]
44  * const string VERTEX_SHADER_SOURCE = DALI_COMPOSE_SHADER (
45  *   void main()
46  *   {
47  *     gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
48  *     vTexCoord = aTexCoord;
49  *   }
50  * );
51  */
52 #define DALI_COMPOSE_SHADER(STR) #STR
53
54 class Constraint;
55 class Image;
56 struct Vector2;
57 struct Vector3;
58 struct Vector4;
59
60 namespace Internal DALI_INTERNAL
61 {
62 class ShaderEffect;
63 }
64
65 /**
66  * @brief GeometryType determines how geometry is shaped.
67  */
68 enum GeometryType
69 {
70   GEOMETRY_TYPE_IMAGE = 0x01,           ///< image, with flat color or texture
71   GEOMETRY_TYPE_UNTEXTURED_MESH = 0x02, ///< Complex meshes, with flat color
72   GEOMETRY_TYPE_TEXTURED_MESH = 0x04,   ///< Complex meshes, with texture
73   GEOMETRY_TYPE_LAST = 0x08
74 };
75
76 /**
77  * @brief Shader effects provide a visual effect for actors.
78  *
79  * For a Custom shader you can provide the vertex and fragment shader code as strings.
80  * These shader snippets get concatenated with the default attributes and uniforms.
81  * For a vertex shader this part contains the following code:
82  * <pre>
83  * precision highp float;
84  * attribute vec3  aPosition;
85  * attribute vec2  aTexCoord;
86  * uniform   mat4  uMvpMatrix;
87  * uniform   mat4  uModelMatrix;
88  * uniform   mat4  uViewMatrix;
89  * uniform   mat4  uModelView;
90  * uniform   mat3  uNormalMatrix;
91  * uniform   mat4  uProjection;
92  * uniform   vec4  uColor;
93  * varying   vec2  vTexCoord;
94  * </pre>
95  * The custom shader part is expected to output the vertex position and texture coordinate.
96  * A basic custom vertex shader would contain the following code:
97  * <pre>
98  * void main()
99  * {
100  *   gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
101  *   vTexCoord = aTexCoord;
102  * }
103  * </pre>
104  * For fragment shader the default part for images contains the following code:
105  * <pre>
106  * precision mediump float;
107  * uniform   sampler2D sTexture;
108  * uniform   sampler2D sEffect;
109  * uniform   vec4      uColor;
110  * varying   vec2      vTexCoord;
111  * </pre>
112  * <BR>
113  * <B>
114  * Note: In order for fade and color animations to work, the fragment shader needs to multiply the fragment color
115  * with the uniform color "uColor" of the node
116  * </B>
117  */
118 class DALI_IMPORT_API ShaderEffect : public Handle
119 {
120 public:
121
122   // Default Properties
123   /**
124    * @brief An enumeration of properties belonging to the Path class.
125    * Grid Density defines the spacing of vertex coordinates in world units.
126    * ie a larger actor will have more grids at the same spacing.
127    *
128    *  +---+---+         +---+---+---+
129    *  |   |   |         |   |   |   |
130    *  +---+---+         +---+---+---+
131    *  |   |   |         |   |   |   |
132    *  +---+---+         +---+---+---+
133    *                    |   |   |   |
134    *                    +---+---+---+
135    */
136   struct Property
137   {
138     enum
139     {
140       GRID_DENSITY = DEFAULT_ACTOR_PROPERTY_START_INDEX, ///< name "grid-density",   type float
141       IMAGE,                                             ///< name "image",          type Map {"filename":"", "load-policy":...}
142       PROGRAM,                                           ///< name "program",        type Map {"vertex-prefix":"","fragment-prefix":"","vertex":"","fragment":""}
143       GEOMETRY_HINTS                                     ///< name "geometry-hints", type int (bitfield) values from enum GeometryHints
144     };
145   };
146
147   static const float DEFAULT_GRID_DENSITY;              ///< The default density is 40 pixels
148
149   /**
150    * @brief The Extension class is a base class for objects that can be attached to the
151    * ShaderEffects as extensions.
152    *
153    * Extensions are useful to create pimpled implementations of custom shaders.
154    * The shader effect will hold an intrusive pointer to the extension.
155    */
156   class Extension : public RefObject
157   {
158   protected:
159     /**
160      * @brief Disable default constructor. This a base class is not meant to be initialised on its own.
161      */
162     Extension();
163
164     /**
165      * @brief Virtual destructor.
166      */
167     virtual ~Extension();
168   };
169
170   /**
171    * @brief Hints for rendering/subdividing geometry.
172    */
173   enum GeometryHints
174   {
175     HINT_NONE           = 0x00,   ///< no hints
176     HINT_GRID_X         = 0x01,   ///< Geometry must be subdivided in X
177     HINT_GRID_Y         = 0x02,   ///< Geometry must be subdivided in Y
178     HINT_GRID           = (HINT_GRID_X | HINT_GRID_Y),
179     HINT_DEPTH_BUFFER   = 0x04,   ///< Needs depth buffering turned on
180     HINT_BLENDING       = 0x08,   ///< Notifies the actor to use blending even if it's fully opaque. Needs actor's blending set to BlendingMode::AUTO
181     HINT_DOESNT_MODIFY_GEOMETRY = 0x10 ///< Notifies that the vertex shader will not change geometry (enables bounding box culling)
182   };
183
184   /**
185    * @brief Coordinate type of the shader uniform.
186    *
187    * Viewport coordinate types will convert from viewport to view space.
188    * Use this coordinate type if your are doing a transformation in view space.
189    * The texture coordinate type converts a value in actor local space to texture coodinates.
190    * This is useful for pixel shaders and accounts for texture atlas.
191    */
192   enum UniformCoordinateType
193   {
194     COORDINATE_TYPE_DEFAULT,           ///< Default, No transformation to be applied
195     COORDINATE_TYPE_VIEWPORT_POSITION, ///< The uniform is a position vector in viewport coordinates that needs to be converted to GL view space coordinates.
196     COORDINATE_TYPE_VIEWPORT_DIRECTION ///< The uniform is a directional vector in viewport coordinates that needs to be converted to GL view space coordinates.
197   };
198
199   /**
200    * @brief Create an empty ShaderEffect.
201    *
202    * This can be initialised with ShaderEffect::New(...)
203    */
204   ShaderEffect();
205
206   /**
207    * @brief Create ShaderEffect.
208    *
209    * @param vertexShader code for the effect. If you pass in an empty string, the default version will be used
210    * @param fragmentShader code for the effect. If you pass in an empty string, the default version will be used
211    * @param type GeometryType to define the shape of the geometry
212    * @param hints GeometryHints to define the geometry of the rendered object
213    * @return A handle to a shader effect
214    */
215   static ShaderEffect New( const std::string& vertexShader,
216                            const std::string& fragmentShader,
217                            GeometryType type = GeometryType(GEOMETRY_TYPE_IMAGE),
218                            GeometryHints hints = GeometryHints(HINT_NONE) );
219
220   /**
221    * @brief Create ShaderEffect.
222    * @param vertexShaderPrefix code for the effect. It will be inserted before the default uniforms (ideal for \#defines)
223    * @param vertexShader code for the effect. If you pass in an empty string, the default version will be used
224    * @param fragmentShaderPrefix code for the effect. It will be inserted before the default uniforms (ideal for \#defines)
225    * @param fragmentShader code for the effect. If you pass in an empty string, the default version will be used
226    * @param type GeometryType to define the shape of the geometry
227    * @param hints GeometryHints to define the geometry of the rendered object
228    * @return A handle to a shader effect
229    */
230   static ShaderEffect NewWithPrefix(const std::string& vertexShaderPrefix,
231                                     const std::string& vertexShader,
232                                     const std::string& fragmentShaderPrefix,
233                                     const std::string& fragmentShader,
234                                     GeometryType type = GeometryType(GEOMETRY_TYPE_IMAGE),
235                                     GeometryHints hints = GeometryHints(HINT_NONE) );
236
237   /**
238    * @brief Downcast an Object handle to ShaderEffect.
239    *
240    * If handle points to a ShaderEffect the downcast produces valid
241    * handle. If not the returned handle is left uninitialized.
242    *
243    * @param[in] handle to An object
244    * @return handle to a ShaderEffect object or an uninitialized handle
245    */
246   static ShaderEffect DownCast( BaseHandle handle );
247
248   /**
249    * @brief Destructor
250    *
251    * This is non-virtual since derived Handle types must not contain data or virtual methods.
252    */
253   ~ShaderEffect();
254
255   /**
256    * @brief Copy constructor
257    *
258    * @param object A reference to a ShaderEffect object
259    */
260   ShaderEffect(const ShaderEffect& object);
261
262   /**
263    * @brief This assignment operator is required for (smart) pointer semantics.
264    *
265    * @param [in] rhs  A reference to the copied handle
266    * @return A reference to this
267    */
268   ShaderEffect& operator=(const ShaderEffect& rhs);
269
270   /**
271    * @brief Sets image for using as effect texture.
272    *
273    * This image texture will be bound to the "sEffect" sampler
274    * so it can be used in fragment shader for effects
275    *
276    * @param[in] image to use as effect texture
277    */
278   void SetEffectImage( Image image );
279
280   /**
281    * @brief Set a uniform value.
282    * This will register a property of type Property::FLOAT; see Object::RegisterProperty() for more details.
283    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
284    * @pre Either the property name is not in use, or a property exists with the correct name & type.
285    * @param name The name of the uniform.
286    * @param value The value to to set.
287    * @param uniformCoordinateType The coordinate type of the uniform.
288    */
289   void SetUniform( const std::string& name,
290                    float value,
291                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
292
293   /**
294    * @brief Set a uniform value.
295    *
296    * This will register a property of type Property::VECTOR2; see Object::RegisterProperty() for more details.
297    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
298    * @pre Either the property name is not in use, or a property exists with the correct name & type.
299    * @param name The name of the uniform.
300    * @param value The value to to set.
301    * @param uniformCoordinateType The coordinate type of the uniform.
302    */
303   void SetUniform( const std::string& name,
304                    Vector2 value,
305                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
306
307   /**
308    * @brief Set a uniform value.
309    *
310    * This will register a property of type Property::VECTOR3; see Object::RegisterProperty() for more details.
311    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
312    * @pre Either the property name is not in use, or a property exists with the correct name & type.
313    * @param name The name of the uniform.
314    * @param value The value to to set.
315    * @param uniformCoordinateType The coordinate type of the uniform.
316    */
317   void SetUniform( const std::string& name,
318                    Vector3 value,
319                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
320
321   /**
322    * @brief Set a uniform value.
323    *
324    * This will register a property of type Property::VECTOR4; see Object::RegisterProperty() for more details.
325    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
326    * @pre Either the property name is not in use, or a property exists with the correct name & type.
327    * @param name The name of the uniform.
328    * @param value The value to to set.
329    * @param uniformCoordinateType The coordinate type of the uniform.
330    */
331   void SetUniform( const std::string& name,
332                    Vector4 value,
333                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
334
335   /**
336    * @brief Set a uniform value.
337    *
338    * This will register a property of type Property::MATRIX; see Object::RegisterProperty() for more details.
339    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
340    * @pre Either the property name is not in use, or a property exists with the correct name & type.
341    * @param name The name of the uniform.
342    * @param value The value to to set.
343    * @param uniformCoordinateType The coordinate type of the uniform.
344    */
345   void SetUniform( const std::string& name,
346                    const Matrix& value,
347                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
348
349   /**
350    * @brief Set a uniform value.
351    *
352    * This will register a property of type Property::MATRIX3; see Object::RegisterProperty() for more details.
353    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
354    * @pre Either the property name is not in use, or a property exists with the correct name & type.
355    * @param name The name of the uniform.
356    * @param value The value to to set.
357    * @param uniformCoordinateType The coordinate type of the uniform.
358    */
359   void SetUniform( const std::string& name,
360                    const Matrix3& value,
361                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
362
363   /**
364    * @brief Attach an extension object.
365    *
366    * This object is reference counted and will be automatically deleted.
367    * This object can be retrieved back with the GetExtension function.
368    * @param object Pointer to a Extension.
369    * @pre extension is not NULL
370    */
371   void AttachExtension( Extension *object );
372
373   /**
374    * @brief Retrieve the attached extension object.
375    *
376    * This object can be set with the AttachExtension function.
377    * @return implementation Pointer to a Extension.
378    * @pre An extension needs to be attached previously.
379    */
380   Extension& GetExtension();
381
382   /**
383    * @brief Retrieve the attached extension object.
384    *
385    * This object can be set with the AttachExtension function.
386    * @return implementation Pointer to a Extension.
387    * @pre An extension needs to be attached previously.
388    */
389   const Extension& GetExtension() const;
390
391
392 public: // Not intended for application developers
393
394   /**
395    * @brief This constructor is used by Dali New() methods.
396    * @param [in] effect A pointer to a newly allocated Dali resource.
397    */
398   explicit DALI_INTERNAL ShaderEffect(Internal::ShaderEffect* effect);
399 };
400
401 } // namespace Dali
402
403 #endif // __DALI_SHADER_EFFECT_H__