1 #ifndef __DALI_SHADER_EFFECT_H__
2 #define __DALI_SHADER_EFFECT_H__
5 * Copyright (c) 2015 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/handle.h>
23 #include <dali/public-api/object/property-index-ranges.h>
29 * @brief DALI_COMPOSE_SHADER macro provides a convenient way to write shader source code.
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.
35 * [An example of double quotation marks usage]
36 * const string FRAGMENT_SHADER_SOURCE = \
39 * " gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
42 * [An example of DALI_COMPOSE_SHADER usage]
43 * const string VERTEX_SHADER_SOURCE = DALI_COMPOSE_SHADER (
46 * gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
47 * vTexCoord = aTexCoord;
51 #define DALI_COMPOSE_SHADER(STR) #STR
58 namespace Internal DALI_INTERNAL
64 * @brief GeometryType determines how geometry is shaped.
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 // @todo MESH_REWORK - Remove these geometry types.
76 * @brief Shader effects provide a visual effect for actors.
78 * @deprecated Use classes Dali::Shader, Dali::Material, and Dali::Sampler to implement
79 * any new programmable shading effects.
81 * For a Custom shader you can provide the vertex and fragment shader code as strings.
82 * These shader snippets get concatenated with the default attributes and uniforms.
83 * For a vertex shader this part contains the following code:
85 * precision highp float;
86 * attribute vec3 aPosition;
87 * attribute vec2 aTexCoord;
88 * uniform mat4 uMvpMatrix;
89 * uniform mat4 uModelMatrix;
90 * uniform mat4 uViewMatrix;
91 * uniform mat4 uModelView;
92 * uniform mat3 uNormalMatrix;
93 * uniform mat4 uProjection;
94 * uniform vec4 uColor;
95 * varying vec2 vTexCoord;
97 * The custom shader part is expected to output the vertex position and texture coordinate.
98 * A basic custom vertex shader would contain the following code:
102 * gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
103 * vTexCoord = aTexCoord;
106 * For fragment shader the default part for images contains the following code:
108 * precision mediump float;
109 * uniform sampler2D sTexture;
110 * uniform sampler2D sEffect;
111 * uniform vec4 uColor;
112 * varying vec2 vTexCoord;
116 * Note: In order for fade and color animations to work, the fragment shader needs to multiply the fragment color
117 * with the uniform color "uColor" of the node
120 class DALI_IMPORT_API ShaderEffect : public Handle
124 // Default Properties
126 * @brief An enumeration of properties belonging to the Path class.
127 * Grid Density defines the spacing of vertex coordinates in world units.
128 * ie a larger actor will have more grids at the same spacing.
130 * +---+---+ +---+---+---+
132 * +---+---+ +---+---+---+
134 * +---+---+ +---+---+---+
142 GRID_DENSITY = DEFAULT_ACTOR_PROPERTY_START_INDEX, ///< name "grid-density", type float
143 IMAGE, ///< name "image", type Map {"filename":"", "load-policy":...}
144 PROGRAM, ///< name "program", type Map {"vertex-prefix":"","fragment-prefix":"","vertex":"","fragment":""}
145 GEOMETRY_HINTS ///< name "geometry-hints", type int (bitfield) values from enum GeometryHints
149 static const float DEFAULT_GRID_DENSITY; ///< The default density is 40 pixels
152 * @brief Hints for rendering/subdividing geometry.
156 HINT_NONE = 0x00, ///< no hints
157 HINT_GRID_X = 0x01, ///< Geometry must be subdivided in X
158 HINT_GRID_Y = 0x02, ///< Geometry must be subdivided in Y
159 HINT_GRID = (HINT_GRID_X | HINT_GRID_Y),
160 HINT_DEPTH_BUFFER = 0x04, ///< Needs depth buffering turned on
161 HINT_BLENDING = 0x08, ///< Notifies the actor to use blending even if it's fully opaque. Needs actor's blending set to BlendingMode::AUTO
162 HINT_DOESNT_MODIFY_GEOMETRY = 0x10 ///< Notifies that the vertex shader will not change geometry (enables bounding box culling)
166 * @brief Coordinate type of the shader uniform.
168 * Viewport coordinate types will convert from viewport to view space.
169 * Use this coordinate type if your are doing a transformation in view space.
170 * The texture coordinate type converts a value in actor local space to texture coodinates.
171 * This is useful for pixel shaders and accounts for texture atlas.
173 enum UniformCoordinateType
175 COORDINATE_TYPE_DEFAULT, ///< Default, No transformation to be applied
176 COORDINATE_TYPE_VIEWPORT_POSITION, ///< The uniform is a position vector in viewport coordinates that needs to be converted to GL view space coordinates.
177 COORDINATE_TYPE_VIEWPORT_DIRECTION ///< The uniform is a directional vector in viewport coordinates that needs to be converted to GL view space coordinates.
181 * @brief Create an empty ShaderEffect.
183 * This can be initialised with ShaderEffect::New(...)
188 * @brief Create ShaderEffect.
190 * @param vertexShader code for the effect. If you pass in an empty string, the default version will be used
191 * @param fragmentShader code for the effect. If you pass in an empty string, the default version will be used
192 * @param type GeometryType to define the shape of the geometry. Only GEOMETRY_TYPE_IMAGE is accepted for this
193 * parameter. Any other value will lead to an error.
194 * @param hints GeometryHints to define the geometry of the rendered object
195 * @return A handle to a shader effect
197 static ShaderEffect New( const std::string& vertexShader,
198 const std::string& fragmentShader,
199 GeometryType type = GeometryType(GEOMETRY_TYPE_IMAGE),
200 GeometryHints hints = GeometryHints(HINT_NONE) );
203 * @brief Create ShaderEffect.
204 * @param vertexShaderPrefix code for the effect. It will be inserted before the default uniforms (ideal for \#defines)
205 * @param vertexShader code for the effect. If you pass in an empty string, the default version will be used
206 * @param fragmentShaderPrefix code for the effect. It will be inserted before the default uniforms (ideal for \#defines)
207 * @param fragmentShader code for the effect. If you pass in an empty string, the default version will be used
208 * @param type GeometryType to define the shape of the geometry. Only GEOMETRY_TYPE_IMAGE is accepted for this
209 * parameter. Any other value will lead to an error.
210 * @param hints GeometryHints to define the geometry of the rendered object
211 * @return A handle to a shader effect
213 static ShaderEffect NewWithPrefix(const std::string& vertexShaderPrefix,
214 const std::string& vertexShader,
215 const std::string& fragmentShaderPrefix,
216 const std::string& fragmentShader,
217 GeometryType type = GeometryType(GEOMETRY_TYPE_IMAGE),
218 GeometryHints hints = GeometryHints(HINT_NONE) );
221 * @brief Downcast an Object handle to ShaderEffect.
223 * If handle points to a ShaderEffect the downcast produces valid
224 * handle. If not the returned handle is left uninitialized.
226 * @param[in] handle to An object
227 * @return handle to a ShaderEffect object or an uninitialized handle
229 static ShaderEffect DownCast( BaseHandle handle );
234 * This is non-virtual since derived Handle types must not contain data or virtual methods.
239 * @brief Copy constructor
241 * @param object A reference to a ShaderEffect object
243 ShaderEffect(const ShaderEffect& object);
246 * @brief This assignment operator is required for (smart) pointer semantics.
248 * @param [in] rhs A reference to the copied handle
249 * @return A reference to this
251 ShaderEffect& operator=(const ShaderEffect& rhs);
254 * @brief Sets image for using as effect texture.
256 * This image texture will be bound to the "sEffect" sampler
257 * so it can be used in fragment shader for effects
259 * @param[in] image to use as effect texture
261 void SetEffectImage( Image image );
264 * @brief Set a uniform value.
265 * This will register a property of type Property::FLOAT; see Object::RegisterProperty() for more details.
266 * If name matches a uniform in the shader source, this value will be uploaded when rendering.
267 * @pre Either the property name is not in use, or a property exists with the correct name & type.
268 * @param name The name of the uniform.
269 * @param value The value to to set.
270 * @param uniformCoordinateType The coordinate type of the uniform.
272 void SetUniform( const std::string& name,
274 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
277 * @brief Set a uniform value.
279 * This will register a property of type Property::VECTOR2; see Object::RegisterProperty() for more details.
280 * If name matches a uniform in the shader source, this value will be uploaded when rendering.
281 * @pre Either the property name is not in use, or a property exists with the correct name & type.
282 * @param name The name of the uniform.
283 * @param value The value to to set.
284 * @param uniformCoordinateType The coordinate type of the uniform.
286 void SetUniform( const std::string& name,
288 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
291 * @brief Set a uniform value.
293 * This will register a property of type Property::VECTOR3; see Object::RegisterProperty() for more details.
294 * If name matches a uniform in the shader source, this value will be uploaded when rendering.
295 * @pre Either the property name is not in use, or a property exists with the correct name & type.
296 * @param name The name of the uniform.
297 * @param value The value to to set.
298 * @param uniformCoordinateType The coordinate type of the uniform.
300 void SetUniform( const std::string& name,
302 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
305 * @brief Set a uniform value.
307 * This will register a property of type Property::VECTOR4; see Object::RegisterProperty() for more details.
308 * If name matches a uniform in the shader source, this value will be uploaded when rendering.
309 * @pre Either the property name is not in use, or a property exists with the correct name & type.
310 * @param name The name of the uniform.
311 * @param value The value to to set.
312 * @param uniformCoordinateType The coordinate type of the uniform.
314 void SetUniform( const std::string& name,
316 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
319 * @brief Set a uniform value.
321 * This will register a property of type Property::MATRIX; see Object::RegisterProperty() for more details.
322 * If name matches a uniform in the shader source, this value will be uploaded when rendering.
323 * @pre Either the property name is not in use, or a property exists with the correct name & type.
324 * @param name The name of the uniform.
325 * @param value The value to to set.
326 * @param uniformCoordinateType The coordinate type of the uniform.
328 void SetUniform( const std::string& name,
330 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
333 * @brief Set a uniform value.
335 * This will register a property of type Property::MATRIX3; see Object::RegisterProperty() for more details.
336 * If name matches a uniform in the shader source, this value will be uploaded when rendering.
337 * @pre Either the property name is not in use, or a property exists with the correct name & type.
338 * @param name The name of the uniform.
339 * @param value The value to to set.
340 * @param uniformCoordinateType The coordinate type of the uniform.
342 void SetUniform( const std::string& name,
343 const Matrix3& value,
344 UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
346 public: // Not intended for application developers
349 * @brief This constructor is used by Dali New() methods.
350 * @param [in] effect A pointer to a newly allocated Dali resource.
352 explicit DALI_INTERNAL ShaderEffect(Internal::ShaderEffect* effect);
357 #endif // __DALI_SHADER_EFFECT_H__