1 #ifndef __DALI_SHADER_EFFECT_H__
2 #define __DALI_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/animation/active-constraint-declarations.h>
23 #include <dali/public-api/object/handle.h>
24 #include <dali/public-api/object/property-index-ranges.h>
30 * @brief DALI_COMPOSE_SHADER macro provides a convenient way to write shader source code.
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.
36 * [An example of double quotation marks usage]
37 * const string FRAGMENT_SHADER_SOURCE = \
40 * " gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
43 * [An example of DALI_COMPOSE_SHADER usage]
44 * const string VERTEX_SHADER_SOURCE = DALI_COMPOSE_SHADER (
47 * gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
48 * vTexCoord = aTexCoord;
52 #define DALI_COMPOSE_SHADER(STR) #STR
60 namespace Internal DALI_INTERNAL
66 * @brief GeometryType determines how geometry is shaped.
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
77 * @brief Shader effects provide a visual effect for actors.
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:
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;
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:
100 * gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
101 * vTexCoord = aTexCoord;
104 * For fragment shader the default part for images contains the following code:
106 * precision mediump float;
107 * uniform sampler2D sTexture;
108 * uniform sampler2D sEffect;
109 * uniform vec4 uColor;
110 * varying vec2 vTexCoord;
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
118 class DALI_IMPORT_API ShaderEffect : public Handle
122 // Default Properties
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.
128 * +---+---+ +---+---+---+
130 * +---+---+ +---+---+---+
132 * +---+---+ +---+---+---+
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
147 static const float DEFAULT_GRID_DENSITY; ///< The default density is 40 pixels
150 * @brief The Extension class is a base class for objects that can be attached to the
151 * ShaderEffects as extensions.
153 * Extensions are useful to create pimpled implementations of custom shaders.
154 * The shader effect will hold an intrusive pointer to the extension.
156 class Extension : public RefObject
160 * @brief Disable default constructor. This a base class is not meant to be initialised on its own.
165 * @brief Virtual destructor.
167 virtual ~Extension();
171 * @brief Hints for rendering/subdividing geometry.
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)
185 * @brief Coordinate type of the shader uniform.
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.
192 enum UniformCoordinateType
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.
200 * @brief Create an empty ShaderEffect.
202 * This can be initialised with ShaderEffect::New(...)
207 * @brief Create ShaderEffect.
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
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) );
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
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) );
238 * @brief Downcast an Object handle to ShaderEffect.
240 * If handle points to a ShaderEffect the downcast produces valid
241 * handle. If not the returned handle is left uninitialized.
243 * @param[in] handle to An object
244 * @return handle to a ShaderEffect object or an uninitialized handle
246 static ShaderEffect DownCast( BaseHandle handle );
251 * This is non-virtual since derived Handle types must not contain data or virtual methods.
256 * @brief Copy constructor
258 * @param object A reference to a ShaderEffect object
260 ShaderEffect(const ShaderEffect& object);
263 * @brief This assignment operator is required for (smart) pointer semantics.
265 * @param [in] rhs A reference to the copied handle
266 * @return A reference to this
268 ShaderEffect& operator=(const ShaderEffect& rhs);
271 * @brief Sets image for using as effect texture.
273 * This image texture will be bound to the "sEffect" sampler
274 * so it can be used in fragment shader for effects
276 * @param[in] image to use as effect texture
278 void SetEffectImage( Image image );
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.
289 void SetUniform( const std::string& name,
291 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
294 * @brief Set a uniform value.
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.
303 void SetUniform( const std::string& name,
305 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
308 * @brief Set a uniform value.
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.
317 void SetUniform( const std::string& name,
319 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
322 * @brief Set a uniform value.
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.
331 void SetUniform( const std::string& name,
333 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
336 * @brief Set a uniform value.
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.
345 void SetUniform( const std::string& name,
347 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
350 * @brief Set a uniform value.
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.
359 void SetUniform( const std::string& name,
360 const Matrix3& value,
361 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
364 * @brief Attach an extension object.
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
371 void AttachExtension( Extension *object );
374 * @brief Retrieve the attached extension object.
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.
380 Extension& GetExtension();
383 * @brief Retrieve the attached extension object.
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.
389 const Extension& GetExtension() const;
392 public: // Not intended for application developers
395 * @brief This constructor is used by Dali New() methods.
396 * @param [in] effect A pointer to a newly allocated Dali resource.
398 explicit DALI_INTERNAL ShaderEffect(Internal::ShaderEffect* effect);
403 #endif // __DALI_SHADER_EFFECT_H__