4 * An object oriented GL/GLES Abstraction/Utility Layer
6 * Copyright (C) 2008,2009 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
24 #if !defined(__COGL_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
25 #error "Only <cogl/cogl.h> can be included directly."
28 #ifndef __COGL_SHADER_H__
29 #define __COGL_SHADER_H__
31 #include <cogl/cogl-types.h>
32 #include <cogl/cogl-defines.h>
37 * SECTION:cogl-shaders
38 * @short_description: Fuctions for accessing the programmable GL pipeline
40 * Cogl allows accessing the GL programmable pipeline in order to create
41 * vertex and fragment shaders.
43 * The shader source code can either be GLSL or ARBfp. If the source
44 * code is ARBfp, it must begin with the string “!!ARBfp1.0”. The
45 * application should check for the %COGL_FEATURE_SHADERS_GLSL or
46 * %COGL_FEATURE_SHADERS_ARBFP features before using shaders.
48 * When using GLSL Cogl provides replacement names for most of the
49 * builtin varyings and uniforms. It is recommended to use these names
50 * wherever possible to increase portability between OpenGL 2.0 and
51 * GLES 2.0. GLES 2.0 does not have most of the builtins under their
52 * original names so they will only work with the Cogl names.
54 * For use in all GLSL shaders, the Cogl builtins are as follows:
59 * <glossterm>uniform mat4
60 * <emphasis>cogl_modelview_matrix</emphasis></glossterm>
62 * The current modelview matrix. This is equivalent to
63 * #gl_ModelViewMatrix.
67 * <glossterm>uniform mat4
68 * <emphasis>cogl_projection_matrix</emphasis></glossterm>
70 * The current projection matrix. This is equivalent to
71 * #gl_ProjectionMatrix.
75 * <glossterm>uniform mat4
76 * <emphasis>cogl_modelview_projection_matrix</emphasis></glossterm>
78 * The combined modelview and projection matrix. A vertex shader
79 * would typically use this to transform the incoming vertex
80 * position. The separate modelview and projection matrices are
81 * usually only needed for lighting calculations. This is
82 * equivalent to #gl_ModelViewProjectionMatrix.
86 * <glossterm>uniform mat4
87 * <emphasis>cogl_texture_matrix</emphasis>[]</glossterm>
89 * An array of matrices for transforming the texture
90 * coordinates. This is equivalent to #gl_TextureMatrix.
96 * In a vertex shader, the following are also available:
101 * <glossterm>attribute vec4
102 * <emphasis>cogl_position_in</emphasis></glossterm>
104 * The incoming vertex position. This is equivalent to #gl_Vertex.
108 * <glossterm>attribute vec4
109 * <emphasis>cogl_color_in</emphasis></glossterm>
111 * The incoming vertex color. This is equivalent to #gl_Color.
115 * <glossterm>attribute vec4
116 * <emphasis>cogl_tex_coord_in</emphasis></glossterm>
118 * The texture coordinate for the first texture unit. This is
119 * equivalent to #gl_MultiTexCoord0.
123 * <glossterm>attribute vec4
124 * <emphasis>cogl_tex_coord0_in</emphasis></glossterm>
126 * The texture coordinate for the first texture unit. This is
127 * equivalent to #gl_MultiTexCoord0. There is also
128 * #cogl_tex_coord1_in and so on.
132 * <glossterm>attribute vec3
133 * <emphasis>cogl_normal_in</emphasis></glossterm>
135 * The normal of the vertex. This is equivalent to #gl_Normal.
140 * <emphasis>cogl_position_out</emphasis></glossterm>
142 * The calculated position of the vertex. This must be written to
143 * in all vertex shaders. This is equivalent to #gl_Position.
148 * <emphasis>cogl_point_size_out</emphasis></glossterm>
150 * The calculated size of a point. This is equivalent to #gl_PointSize.
154 * <glossterm>varying vec4
155 * <emphasis>cogl_color_out</emphasis></glossterm>
157 * The calculated color of a vertex. This is equivalent to #gl_FrontColor.
161 * <glossterm>varying vec4
162 * <emphasis>cogl_tex_coord_out</emphasis>[]</glossterm>
164 * An array of calculated texture coordinates for a vertex. This is
165 * equivalent to #gl_TexCoord.
171 * In a fragment shader, the following are also available:
176 * <glossterm>varying vec4 <emphasis>cogl_color_in</emphasis></glossterm>
178 * The calculated color of a vertex. This is equivalent to #gl_FrontColor.
182 * <glossterm>varying vec4
183 * <emphasis>cogl_tex_coord_in</emphasis>[]</glossterm>
185 * An array of calculated texture coordinates for a vertex. This is
186 * equivalent to #gl_TexCoord.
190 * <glossterm>vec4 <emphasis>cogl_color_out</emphasis></glossterm>
192 * The final calculated color of the fragment. All fragment shaders
193 * must write to this variable. This is equivalent to
198 * <glossterm>float <emphasis>cogl_depth_out</emphasis></glossterm>
200 * An optional output variable specifying the depth value to use
201 * for this fragment. This is equivalent to #gl_FragDepth.
205 * <glossterm>bool <emphasis>cogl_front_facing</emphasis></glossterm>
207 * A readonly variable that will be true if the current primitive
208 * is front facing. This can be used to implement two-sided
209 * coloring algorithms. This is equivalent to #gl_FrontFacing.
215 * It's worth nothing that this API isn't what Cogl would like to have
216 * in the long term and it may be removed in Cogl 2.0. The
217 * experimental #CoglShader API is the proposed replacement.
222 * @COGL_SHADER_TYPE_VERTEX: A program for proccessing vertices
223 * @COGL_SHADER_TYPE_FRAGMENT: A program for processing fragments
230 COGL_SHADER_TYPE_VERTEX,
231 COGL_SHADER_TYPE_FRAGMENT
235 * cogl_create_shader:
236 * @shader_type: COGL_SHADER_TYPE_VERTEX or COGL_SHADER_TYPE_FRAGMENT.
238 * Create a new shader handle, use cogl_shader_source() to set the
239 * source code to be used on it.
241 * Returns: a new shader handle.
244 cogl_create_shader (CoglShaderType shader_type);
246 #ifndef COGL_DISABLE_DEPRECATED
250 * @handle: A #CoglHandle to a shader.
252 * Add an extra reference to a shader.
254 * Deprecated: 1.0: Please use cogl_handle_ref() instead.
259 cogl_shader_ref (CoglHandle handle) G_GNUC_DEPRECATED;
263 * @handle: A #CoglHandle to a shader.
265 * Removes a reference to a shader. If it was the last reference the
266 * shader object will be destroyed.
268 * Deprecated: 1.0: Please use cogl_handle_unref() instead.
271 cogl_shader_unref (CoglHandle handle) G_GNUC_DEPRECATED;
273 #endif /* COGL_DISABLE_DEPRECATED */
277 * @handle: A CoglHandle
279 * Gets whether the given handle references an existing shader object.
281 * Returns: %TRUE if the handle references a shader,
285 cogl_is_shader (CoglHandle handle);
288 * cogl_shader_source:
289 * @shader: #CoglHandle for a shader.
290 * @source: Shader source.
292 * Replaces the current source associated with a shader with a new
296 * linkend="cogl-Shaders-and-Programmable-Pipeline.description">above</link>
297 * for a description of the recommended format for the shader code.
300 cogl_shader_source (CoglHandle shader,
303 * cogl_shader_compile:
304 * @handle: #CoglHandle for a shader.
306 * Compiles the shader, no return value, but the shader is now ready
307 * for linking into a program. Note that calling this function is
308 * optional. If it is not called then the shader will be automatically
309 * compiled when it is linked.
312 cogl_shader_compile (CoglHandle handle);
315 * cogl_shader_get_info_log:
316 * @handle: #CoglHandle for a shader.
318 * Retrieves the information log for a coglobject, can be used in conjunction
319 * with cogl_shader_get_parameteriv() to retrieve the compiler warnings/error
320 * messages that caused a shader to not compile correctly, mainly useful for
321 * debugging purposes.
323 * Return value: a newly allocated string containing the info log. Use
324 * g_free() to free it
327 cogl_shader_get_info_log (CoglHandle handle);
330 * cogl_shader_get_type:
331 * @handle: #CoglHandle for a shader.
333 * Retrieves the type of a shader #CoglHandle
335 * Return value: %COGL_SHADER_TYPE_VERTEX if the shader is a vertex processor
336 * or %COGL_SHADER_TYPE_FRAGMENT if the shader is a frament processor
339 cogl_shader_get_type (CoglHandle handle);
342 * cogl_shader_is_compiled:
343 * @handle: #CoglHandle for a shader.
345 * Retrieves whether a shader #CoglHandle has been compiled
347 * Return value: %TRUE if the shader object has sucessfully be compiled
350 cogl_shader_is_compiled (CoglHandle handle);
353 * cogl_create_program:
355 * Create a new cogl program object that can be used to replace parts of the GL
356 * rendering pipeline with custom code.
358 * Returns: a new cogl program.
361 cogl_create_program (void);
363 #ifndef COGL_DISABLE_DEPRECATED
367 * @handle: A #CoglHandle to a program.
369 * Add an extra reference to a program.
371 * Deprecated: 1.0: Please use cogl_handle_ref() instead.
376 cogl_program_ref (CoglHandle handle) G_GNUC_DEPRECATED;
379 * cogl_program_unref:
380 * @handle: A #CoglHandle to a program.
382 * Removes a reference to a program. If it was the last reference the
383 * program object will be destroyed.
385 * Deprecated: 1.0: Please use cogl_handle_unref() instead.
388 cogl_program_unref (CoglHandle handle) G_GNUC_DEPRECATED;
390 #endif /* COGL_DISABLE_DEPRECATED */
394 * @handle: A CoglHandle
396 * Gets whether the given handle references an existing program object.
398 * Returns: %TRUE if the handle references a program,
402 cogl_is_program (CoglHandle handle);
405 * cogl_program_attach_shader:
406 * @program_handle: a #CoglHandle for a shdaer program.
407 * @shader_handle: a #CoglHandle for a vertex of fragment shader.
409 * Attaches a shader to a program object. A program can have multiple
410 * vertex or fragment shaders but only one of them may provide a
411 * main() function. It is allowed to use a program with only a vertex
412 * shader or only a fragment shader.
415 cogl_program_attach_shader (CoglHandle program_handle,
416 CoglHandle shader_handle);
420 * @handle: a #CoglHandle for a shader program.
422 * Links a program making it ready for use. Note that calling this
423 * function is optional. If it is not called the program will
424 * automatically be linked the first time it is used.
427 cogl_program_link (CoglHandle handle);
431 * @handle: a #CoglHandle for a shader program or %COGL_INVALID_HANDLE.
433 * Activate a specific shader program replacing that part of the GL
434 * rendering pipeline, if passed in %COGL_INVALID_HANDLE the default
435 * behavior of GL is reinstated.
437 * This function affects the global state of the current Cogl
438 * context. It is much more efficient to attach the shader to a
439 * specific material used for rendering instead by calling
440 * cogl_material_set_user_program().
443 cogl_program_use (CoglHandle handle);
446 * cogl_program_get_uniform_location:
447 * @handle: a #CoglHandle for a shader program.
448 * @uniform_name: the name of a uniform.
450 * Retrieve the location (offset) of a uniform variable in a shader program,
451 * a uniform is a variable that is constant for all vertices/fragments for a
452 * shader object and is possible to modify as an external parameter.
454 * Return value: the offset of a uniform in a specified program.
455 * This uniform can be set using cogl_program_uniform_1f() when the
459 cogl_program_get_uniform_location (CoglHandle handle,
460 const char *uniform_name);
463 * cogl_program_set_uniform_1f:
464 * @program: A #CoglHandle for a linked program
465 * @uniform_location: the uniform location retrieved from
466 * cogl_program_get_uniform_location().
467 * @value: the new value of the uniform.
469 * Changes the value of a floating point uniform for the given linked
475 cogl_program_set_uniform_1f (CoglHandle program,
476 int uniform_location,
480 * cogl_program_set_uniform_1i:
481 * @program: A #CoglHandle for a linked program
482 * @uniform_location: the uniform location retrieved from
483 * cogl_program_get_uniform_location().
484 * @value: the new value of the uniform.
486 * Changes the value of an integer uniform for the given linked
492 cogl_program_set_uniform_1i (CoglHandle program,
493 int uniform_location,
497 * cogl_program_set_uniform_float:
498 * @program: A #CoglHandle for a linked program
499 * @uniform_location: the uniform location retrieved from
500 * cogl_program_get_uniform_location().
501 * @n_components: The number of components for the uniform. For
502 * example with glsl you'd use 3 for a vec3 or 4 for a vec4.
503 * @count: For uniform arrays this is the array length otherwise just
505 * @value: (array length=count): the new value of the uniform[s].
507 * Changes the value of a float vector uniform, or uniform array for
508 * the given linked @program.
513 cogl_program_set_uniform_float (CoglHandle program,
514 int uniform_location,
520 * cogl_program_set_uniform_int:
521 * @program: A #CoglHandle for a linked program
522 * @uniform_location: the uniform location retrieved from
523 * cogl_program_get_uniform_location().
524 * @n_components: The number of components for the uniform. For
525 * example with glsl you'd use 3 for a vec3 or 4 for a vec4.
526 * @count: For uniform arrays this is the array length otherwise just
528 * @value: (array length=count): the new value of the uniform[s].
530 * Changes the value of a int vector uniform, or uniform array for
531 * the given linked @program.
536 cogl_program_set_uniform_int (CoglHandle program,
537 int uniform_location,
543 * cogl_program_set_uniform_matrix:
544 * @program: A #CoglHandle for a linked program
545 * @uniform_location: the uniform location retrieved from
546 * cogl_program_get_uniform_location().
547 * @dimensions: The dimensions of the matrix. So for for example pass
548 * 2 for a 2x2 matrix or 3 for 3x3.
549 * @count: For uniform arrays this is the array length otherwise just
551 * @transpose: Whether to transpose the matrix when setting the uniform.
552 * @value: (array length=count): the new value of the uniform.
554 * Changes the value of a matrix uniform, or uniform array in the
555 * given linked @program.
560 cogl_program_set_uniform_matrix (CoglHandle program,
561 int uniform_location,
567 #ifndef COGL_DISABLE_DEPRECATED
570 * cogl_program_uniform_1f:
571 * @uniform_no: the uniform to set.
572 * @value: the new value of the uniform.
574 * Changes the value of a floating point uniform in the currently
575 * used (see cogl_program_use()) shader program.
577 * Deprecated: 1.4: Use cogl_program_set_uniform_1f() instead.
580 cogl_program_uniform_1f (int uniform_no,
581 float value) G_GNUC_DEPRECATED;
584 * cogl_program_uniform_1i:
585 * @uniform_no: the uniform to set.
586 * @value: the new value of the uniform.
588 * Changes the value of an integer uniform in the currently
589 * used (see cogl_program_use()) shader program.
591 * Deprecated: 1.4: Use cogl_program_set_uniform_1i() instead.
594 cogl_program_uniform_1i (int uniform_no,
595 int value) G_GNUC_DEPRECATED;
598 * cogl_program_uniform_float:
599 * @uniform_no: the uniform to set.
600 * @size: Size of float vector.
601 * @count: Size of array of uniforms.
602 * @value: (array length=count): the new value of the uniform.
604 * Changes the value of a float vector uniform, or uniform array in the
605 * currently used (see cogl_program_use()) shader program.
607 * Deprecated: 1.4: Use cogl_program_set_uniform_float() instead.
610 cogl_program_uniform_float (int uniform_no,
613 const float *value) G_GNUC_DEPRECATED;
616 * cogl_program_uniform_int:
617 * @uniform_no: the uniform to set.
618 * @size: Size of int vector.
619 * @count: Size of array of uniforms.
620 * @value: (array length=count): the new value of the uniform.
622 * Changes the value of a int vector uniform, or uniform array in the
623 * currently used (see cogl_program_use()) shader program.
626 cogl_program_uniform_int (int uniform_no,
629 const int *value) G_GNUC_DEPRECATED;
632 * cogl_program_uniform_matrix:
633 * @uniform_no: the uniform to set.
634 * @size: Size of matrix.
635 * @count: Size of array of uniforms.
636 * @transpose: Whether to transpose the matrix when setting the uniform.
637 * @value: (array length=count): the new value of the uniform.
639 * Changes the value of a matrix uniform, or uniform array in the
640 * currently used (see cogl_program_use()) shader program. The @size
641 * parameter is used to determine the square size of the matrix.
644 cogl_program_uniform_matrix (int uniform_no,
648 const float *value) G_GNUC_DEPRECATED;
650 #endif /* COGL_DISABLE_DEPRECATED */
654 #endif /* __COGL_SHADER_H__ */