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