2012-03-15 10:42:22 +08:00
/****************************************************************************
Copyright 2011 Jeff Lamarche
Copyright 2012 Goffredo Marocchi
Copyright 2012 Ricardo Quesada
2014-01-07 11:25:07 +08:00
Copyright 2012 cocos2d - x . org
2017-02-14 14:59:12 +08:00
Copyright ( c ) 2013 - 2017 Chukong Technologies Inc .
2015-04-20 17:00:40 +08:00
2012-03-15 10:42:22 +08:00
http : //www.cocos2d-x.org
2015-04-20 17:00:40 +08:00
2012-03-15 10:42:22 +08:00
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-03-12 15:22:03 +08:00
# ifndef __CCGLPROGRAM_H__
# define __CCGLPROGRAM_H__
2014-05-17 05:36:00 +08:00
# include <unordered_map>
2015-05-06 04:07:32 +08:00
# include <string>
2014-05-17 05:36:00 +08:00
2014-04-30 08:37:36 +08:00
# include "base/ccMacros.h"
2014-04-27 01:11:22 +08:00
# include "base/CCRef.h"
2014-05-08 11:20:19 +08:00
# include "base/ccTypes.h"
2014-09-10 07:50:02 +08:00
# include "platform/CCGL.h"
2014-04-26 13:03:25 +08:00
# include "math/CCMath.h"
2012-06-08 13:55:28 +08:00
2015-03-27 11:54:40 +08:00
/**
2015-06-01 13:43:56 +08:00
* @ addtogroup renderer
2015-03-27 11:54:40 +08:00
* @ {
*/
2012-03-12 15:22:03 +08:00
NS_CC_BEGIN
2014-05-07 08:58:14 +08:00
class GLProgram ;
2014-12-04 18:37:57 +08:00
class Director ;
2015-03-20 09:46:08 +08:00
//FIXME: these two typedefs would be deprecated or removed in version 4.0.
2015-01-05 13:52:22 +08:00
typedef void ( * GLInfoFunction ) ( GLuint program , GLenum pname , GLint * params ) ;
typedef void ( * GLLogFunction ) ( GLuint program , GLsizei bufsize , GLsizei * length , GLchar * infolog ) ;
2014-05-07 02:19:51 +08:00
2015-03-20 09:46:08 +08:00
/**VertexAttrib is a structure to encapsulate data got from glGetActiveAttrib.*/
2014-05-09 05:01:03 +08:00
struct VertexAttrib
2014-05-07 08:58:14 +08:00
{
2015-03-20 09:46:08 +08:00
/**Index of attribute, start from 0.*/
2014-05-09 05:01:03 +08:00
GLuint index ;
2015-03-20 09:46:08 +08:00
/**Number of Data type in the attribute, could range from 0-4.*/
2014-05-09 05:01:03 +08:00
GLint size ;
2015-03-20 09:46:08 +08:00
/**Data type of the attribute, could be GL_FLOAT, GL_UNSIGNED_BYTE etc.*/
2014-05-09 05:01:03 +08:00
GLenum type ;
2015-03-20 09:46:08 +08:00
/**The string name in vertex shader.*/
2014-05-09 05:01:03 +08:00
std : : string name ;
2014-05-08 01:44:45 +08:00
} ;
2015-03-20 09:46:08 +08:00
/**Uniform is a structure to encapsulate data got from glGetActiveUniform and glGetUniformLocation.*/
2014-05-08 11:38:15 +08:00
struct Uniform
2014-05-08 01:44:45 +08:00
{
2015-03-20 09:46:08 +08:00
/**The place where the uniform placed, starts from 0.*/
2014-05-08 11:38:15 +08:00
GLint location ;
2015-03-20 09:46:08 +08:00
/**Number of data type in attribute.*/
2014-05-08 11:38:15 +08:00
GLint size ;
2015-03-20 09:46:08 +08:00
/**Data type of the attribute.*/
2014-05-08 11:38:15 +08:00
GLenum type ;
2015-03-20 09:46:08 +08:00
/**String of the uniform name.*/
2014-05-09 05:01:03 +08:00
std : : string name ;
2014-05-07 02:19:51 +08:00
} ;
2013-06-20 14:13:12 +08:00
/** GLProgram
2012-04-17 17:55:26 +08:00
Class that implements a glProgram
2015-04-20 17:00:40 +08:00
2012-04-17 17:55:26 +08:00
@ since v2 .0 .0
2012-03-12 15:22:03 +08:00
*/
2014-02-20 10:53:49 +08:00
class CC_DLL GLProgram : public Ref
2012-03-12 15:22:03 +08:00
{
2014-05-08 11:20:19 +08:00
friend class GLProgramState ;
2015-05-13 10:58:09 +08:00
friend class VertexAttribBinding ;
2014-05-08 11:20:19 +08:00
2012-03-12 15:22:03 +08:00
public :
2015-03-20 09:46:08 +08:00
/**Enum the preallocated vertex attribute. */
2013-07-25 17:48:22 +08:00
enum
{
2015-03-20 09:46:08 +08:00
/**Index 0 will be used as Position.*/
2013-07-25 17:48:22 +08:00
VERTEX_ATTRIB_POSITION ,
2015-03-20 09:46:08 +08:00
/**Index 1 will be used as Color.*/
2013-07-25 17:48:22 +08:00
VERTEX_ATTRIB_COLOR ,
2015-03-20 09:46:08 +08:00
/**Index 2 will be used as Tex coord unit 0.*/
2014-05-09 03:34:26 +08:00
VERTEX_ATTRIB_TEX_COORD ,
2015-03-20 09:46:08 +08:00
/**Index 3 will be used as Tex coord unit 1.*/
2014-08-18 18:02:58 +08:00
VERTEX_ATTRIB_TEX_COORD1 ,
2015-03-20 09:46:08 +08:00
/**Index 4 will be used as Tex coord unit 2.*/
2014-08-21 18:14:24 +08:00
VERTEX_ATTRIB_TEX_COORD2 ,
2015-03-20 09:46:08 +08:00
/**Index 5 will be used as Tex coord unit 3.*/
2014-08-21 18:14:24 +08:00
VERTEX_ATTRIB_TEX_COORD3 ,
2015-03-20 09:46:08 +08:00
/**Index 6 will be used as Normal.*/
2014-05-09 03:34:26 +08:00
VERTEX_ATTRIB_NORMAL ,
2015-03-20 09:46:08 +08:00
/**Index 7 will be used as Blend weight for hardware skin.*/
2014-06-04 18:17:09 +08:00
VERTEX_ATTRIB_BLEND_WEIGHT ,
2015-03-20 09:46:08 +08:00
/**Index 8 will be used as Blend index.*/
2014-06-04 18:17:09 +08:00
VERTEX_ATTRIB_BLEND_INDEX ,
2015-11-25 10:45:03 +08:00
/**Index 9 will be used as tangent.*/
VERTEX_ATTRIB_TANGENT ,
/**Index 10 will be used as Binormal.*/
VERTEX_ATTRIB_BINORMAL ,
2013-07-25 17:48:22 +08:00
VERTEX_ATTRIB_MAX ,
2014-05-09 03:34:26 +08:00
// backward compatibility
VERTEX_ATTRIB_TEX_COORDS = VERTEX_ATTRIB_TEX_COORD ,
2013-07-25 17:48:22 +08:00
} ;
2015-04-20 17:00:40 +08:00
2015-03-20 09:46:08 +08:00
/**Preallocated uniform handle.*/
2013-07-25 17:48:22 +08:00
enum
{
2015-03-20 09:46:08 +08:00
/**Ambient color.*/
2014-08-18 17:10:07 +08:00
UNIFORM_AMBIENT_COLOR ,
2015-03-20 09:46:08 +08:00
/**Projection matrix.*/
2013-07-25 17:48:22 +08:00
UNIFORM_P_MATRIX ,
2016-10-28 09:33:31 +08:00
/**MultiView Projection matrix.*/
UNIFORM_MULTIVIEW_P_MATRIX ,
2015-03-20 09:46:08 +08:00
/**Model view matrix.*/
2013-07-25 17:48:22 +08:00
UNIFORM_MV_MATRIX ,
2015-03-20 09:46:08 +08:00
/**Model view projection matrix.*/
2013-07-25 17:48:22 +08:00
UNIFORM_MVP_MATRIX ,
2016-10-28 09:33:31 +08:00
/**MultiView Model view projection matrix.*/
UNIFORM_MULTIVIEW_MVP_MATRIX ,
2015-03-20 09:46:08 +08:00
/**Normal matrix.*/
2014-08-15 19:15:14 +08:00
UNIFORM_NORMAL_MATRIX ,
2015-03-20 09:46:08 +08:00
/**Time.*/
2013-07-25 17:48:22 +08:00
UNIFORM_TIME ,
2015-03-20 09:46:08 +08:00
/**sin(Time).*/
2013-07-25 17:48:22 +08:00
UNIFORM_SIN_TIME ,
2015-03-20 09:46:08 +08:00
/**cos(Time).*/
2013-07-25 17:48:22 +08:00
UNIFORM_COS_TIME ,
2015-03-20 09:46:08 +08:00
/**Random number.*/
2013-07-25 17:48:22 +08:00
UNIFORM_RANDOM01 ,
2015-03-20 09:46:08 +08:00
/** @{
* Sampler 0 - 3 , used for texture .
*/
2014-05-14 09:12:58 +08:00
UNIFORM_SAMPLER0 ,
UNIFORM_SAMPLER1 ,
UNIFORM_SAMPLER2 ,
UNIFORM_SAMPLER3 ,
2015-03-20 09:46:08 +08:00
/**@}*/
2013-07-25 17:48:22 +08:00
UNIFORM_MAX ,
} ;
2015-03-20 09:46:08 +08:00
2016-02-09 03:25:37 +08:00
/** Flags used by the uniforms */
struct UniformFlags {
unsigned int usesTime : 1 ;
unsigned int usesNormal : 1 ;
unsigned int usesMVP : 1 ;
2016-10-28 09:33:31 +08:00
unsigned int usesMultiViewMVP : 1 ;
2016-02-09 03:25:37 +08:00
unsigned int usesMV : 1 ;
unsigned int usesP : 1 ;
2016-10-28 09:33:31 +08:00
unsigned int usesMultiViewP : 1 ;
2016-02-09 03:25:37 +08:00
unsigned int usesRandom : 1 ;
// handy way to initialize the bitfield
UniformFlags ( ) { memset ( this , 0 , sizeof ( * this ) ) ; }
} ;
2015-03-20 09:46:08 +08:00
/**
@ name Built Shader types
@ {
*/
2016-07-25 17:31:54 +08:00
/** ETC1 ALPHA supports for 2d */
static const char * SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR ;
static const char * SHADER_NAME_ETC1AS_POSITION_TEXTURE_COLOR_NO_MVP ;
static const char * SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY ;
static const char * SHADER_NAME_ETC1AS_POSITION_TEXTURE_GRAY_NO_MVP ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, Texture and Color vertex attribute.*/
2013-07-25 17:48:22 +08:00
static const char * SHADER_NAME_POSITION_TEXTURE_COLOR ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, Texture and Color vertex attribute, but without multiply vertex by MVP matrix.*/
2013-12-06 13:24:59 +08:00
static const char * SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, Texture vertex attribute, but include alpha test.*/
2013-07-25 17:48:22 +08:00
static const char * SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, Texture and Color vertex attribute, include alpha test and without multiply vertex by MVP matrix.*/
2014-03-10 15:12:44 +08:00
static const char * SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, Color vertex attribute.*/
2013-07-25 17:48:22 +08:00
static const char * SHADER_NAME_POSITION_COLOR ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, Color, Texture vertex attribute. texture coordinate will used as point size.*/
2015-01-20 16:50:51 +08:00
static const char * SHADER_NAME_POSITION_COLOR_TEXASPOINTSIZE ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, Color vertex attribute, without multiply vertex by MVP matrix.*/
2014-01-07 22:08:00 +08:00
static const char * SHADER_NAME_POSITION_COLOR_NO_MVP ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, Texture vertex attribute.*/
2013-07-25 17:48:22 +08:00
static const char * SHADER_NAME_POSITION_TEXTURE ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, Texture vertex attribute. with a specified uniform as color*/
2013-07-25 17:48:22 +08:00
static const char * SHADER_NAME_POSITION_TEXTURE_U_COLOR ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, Texture and Color vertex attribute. but alpha will be the multiplication of color attribute and texture.*/
2013-07-25 17:48:22 +08:00
static const char * SHADER_NAME_POSITION_TEXTURE_A8_COLOR ;
2015-03-20 09:46:08 +08:00
/**Built in shader for 2d. Support Position, with color specified by a uniform.*/
2013-07-25 17:48:22 +08:00
static const char * SHADER_NAME_POSITION_U_COLOR ;
2015-03-20 09:46:08 +08:00
/**Built in shader for draw a sector with 90 degrees with center at bottom left point.*/
2013-07-25 17:48:22 +08:00
static const char * SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR ;
2015-04-20 17:00:40 +08:00
/**Built in shader for ui effects */
static const char * SHADER_NAME_POSITION_GRAYSCALE ;
2015-03-20 09:46:08 +08:00
/** @{
Built in shader for label and label with effects .
*/
2014-03-20 20:56:10 +08:00
static const char * SHADER_NAME_LABEL_NORMAL ;
static const char * SHADER_NAME_LABEL_OUTLINE ;
2013-12-13 12:42:15 +08:00
static const char * SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL ;
static const char * SHADER_NAME_LABEL_DISTANCEFIELD_GLOW ;
2015-03-20 09:46:08 +08:00
/**Built in shader used for 3D, support Position vertex attribute, with color specified by a uniform.*/
2014-05-19 05:49:16 +08:00
static const char * SHADER_3D_POSITION ;
2015-03-20 09:46:08 +08:00
/**Built in shader used for 3D, support Position and Texture vertex attribute, with color specified by a uniform.*/
2014-05-19 05:49:16 +08:00
static const char * SHADER_3D_POSITION_TEXTURE ;
2015-03-20 09:46:08 +08:00
/**
2015-04-20 17:00:40 +08:00
Built in shader used for 3 D , support Position ( Skeletal animation by hardware skin ) and Texture vertex attribute ,
2015-03-20 09:46:08 +08:00
with color specified by a uniform .
*/
2014-06-05 16:36:01 +08:00
static const char * SHADER_3D_SKINPOSITION_TEXTURE ;
2015-03-20 09:46:08 +08:00
/**
Built in shader used for 3 D , support Position and Normal vertex attribute , used in lighting . with color specified by a uniform .
*/
2014-08-21 11:33:00 +08:00
static const char * SHADER_3D_POSITION_NORMAL ;
2015-03-20 09:46:08 +08:00
/**
Built in shader used for 3 D , support Position , Normal , Texture vertex attribute , used in lighting . with color specified by a uniform .
*/
2014-08-21 11:33:00 +08:00
static const char * SHADER_3D_POSITION_NORMAL_TEXTURE ;
2015-03-20 09:46:08 +08:00
/**
2015-04-20 17:00:40 +08:00
Built in shader used for 3 D , support Position ( skeletal animation by hardware skin ) , Normal , Texture vertex attribute ,
2015-03-20 09:46:08 +08:00
used in lighting . with color specified by a uniform .
*/
2014-08-21 11:33:00 +08:00
static const char * SHADER_3D_SKINPOSITION_NORMAL_TEXTURE ;
2015-03-20 09:46:08 +08:00
/**
2015-11-25 10:45:03 +08:00
Built in shader used for 3 D , support Position , Bumped Normal , Texture vertex attribute , used in lighting . with color specified by a uniform .
*/
static const char * SHADER_3D_POSITION_BUMPEDNORMAL_TEXTURE ;
/**
Built in shader used for 3 D , support Position ( skeletal animation by hardware skin ) , Bumped Normal , Texture vertex attribute ,
used in lighting . with color specified by a uniform .
*/
static const char * SHADER_3D_SKINPOSITION_BUMPEDNORMAL_TEXTURE ;
/**
2015-03-20 09:46:08 +08:00
Built in shader for particles , support Position and Texture , with a color specified by a uniform .
*/
2015-02-11 18:14:22 +08:00
static const char * SHADER_3D_PARTICLE_TEXTURE ;
2015-03-20 09:46:08 +08:00
/**
Built in shader for particles , support Position , with a color specified by a uniform .
*/
2015-02-11 18:14:22 +08:00
static const char * SHADER_3D_PARTICLE_COLOR ;
2015-04-20 17:00:40 +08:00
2015-03-26 11:59:26 +08:00
/**
Built in shader for skybox
*/
static const char * SHADER_3D_SKYBOX ;
2015-04-20 17:00:40 +08:00
2015-03-30 13:45:50 +08:00
/**
Built in shader for terrain
*/
static const char * SHADER_3D_TERRAIN ;
2015-05-07 15:09:06 +08:00
2017-05-22 13:42:00 +08:00
/**
Built in shader for LayerRadialGradient
*/
static const char * SHADER_LAYER_RADIAL_GRADIENT ;
2015-05-07 15:09:06 +08:00
/**
Built in shader for camera clear
*/
static const char * SHADER_CAMERA_CLEAR ;
2015-03-20 09:46:08 +08:00
/**
end of built shader types .
@ }
*/
2015-04-20 17:00:40 +08:00
2015-03-20 09:46:08 +08:00
/**
@ name Built uniform names
@ {
*/
/**Ambient Color uniform.*/
2014-08-18 17:10:07 +08:00
static const char * UNIFORM_NAME_AMBIENT_COLOR ;
2015-03-20 09:46:08 +08:00
/**Projection Matrix uniform.*/
2013-07-25 17:48:22 +08:00
static const char * UNIFORM_NAME_P_MATRIX ;
2016-10-28 09:33:31 +08:00
/**MultiView Projection Matrix uniform.*/
static const char * UNIFORM_NAME_MULTIVIEW_P_MATRIX ;
2015-03-20 09:46:08 +08:00
/**Model view matrix uniform.*/
2013-07-25 17:48:22 +08:00
static const char * UNIFORM_NAME_MV_MATRIX ;
2015-03-20 09:46:08 +08:00
/**Model view projection uniform.*/
2013-07-25 17:48:22 +08:00
static const char * UNIFORM_NAME_MVP_MATRIX ;
2016-10-28 09:33:31 +08:00
/**MultiView Model view projection uniform.*/
static const char * UNIFORM_NAME_MULTIVIEW_MVP_MATRIX ;
2015-03-20 09:46:08 +08:00
/**Normal matrix uniform.*/
2014-08-15 19:15:14 +08:00
static const char * UNIFORM_NAME_NORMAL_MATRIX ;
2015-03-20 09:46:08 +08:00
/**Time uniform.*/
2013-07-25 17:48:22 +08:00
static const char * UNIFORM_NAME_TIME ;
2015-03-20 09:46:08 +08:00
/**Sin time uniform.*/
2013-07-25 17:48:22 +08:00
static const char * UNIFORM_NAME_SIN_TIME ;
2015-03-20 09:46:08 +08:00
/**Cos time uniform.*/
2013-07-25 17:48:22 +08:00
static const char * UNIFORM_NAME_COS_TIME ;
2015-03-20 09:46:08 +08:00
/**Random number uniform.*/
2013-07-25 17:48:22 +08:00
static const char * UNIFORM_NAME_RANDOM01 ;
2015-03-20 09:46:08 +08:00
/**
@ { Sampler uniform 0 - 3 , used for textures .
*/
2014-05-14 09:12:58 +08:00
static const char * UNIFORM_NAME_SAMPLER0 ;
static const char * UNIFORM_NAME_SAMPLER1 ;
static const char * UNIFORM_NAME_SAMPLER2 ;
static const char * UNIFORM_NAME_SAMPLER3 ;
2015-03-20 09:46:08 +08:00
/**
@ }
*/
/**Alpha test value uniform.*/
2013-07-25 17:48:22 +08:00
static const char * UNIFORM_NAME_ALPHA_TEST_VALUE ;
2015-03-20 09:46:08 +08:00
/**
end of Built uniform names
@ }
*/
/**
@ name Built Attribute names
@ {
*/
/**Attribute color.*/
2013-07-25 17:48:22 +08:00
static const char * ATTRIBUTE_NAME_COLOR ;
2015-03-20 09:46:08 +08:00
/**Attribute position.*/
2013-07-25 17:48:22 +08:00
static const char * ATTRIBUTE_NAME_POSITION ;
2015-03-20 09:46:08 +08:00
/**@{ Attribute Texcoord 0-3.*/
2013-07-25 17:48:22 +08:00
static const char * ATTRIBUTE_NAME_TEX_COORD ;
2014-08-18 18:02:58 +08:00
static const char * ATTRIBUTE_NAME_TEX_COORD1 ;
2014-08-21 18:14:24 +08:00
static const char * ATTRIBUTE_NAME_TEX_COORD2 ;
static const char * ATTRIBUTE_NAME_TEX_COORD3 ;
2015-03-20 09:46:08 +08:00
/**@}*/
/**Attribute normal.*/
2014-05-09 03:34:26 +08:00
static const char * ATTRIBUTE_NAME_NORMAL ;
2015-03-20 09:46:08 +08:00
/**Attribute blend weight.*/
2014-06-04 18:17:09 +08:00
static const char * ATTRIBUTE_NAME_BLEND_WEIGHT ;
2015-03-20 09:46:08 +08:00
/**Attribute blend index.*/
2014-06-04 18:17:09 +08:00
static const char * ATTRIBUTE_NAME_BLEND_INDEX ;
2015-11-25 10:45:03 +08:00
/**Attribute blend tangent.*/
static const char * ATTRIBUTE_NAME_TANGENT ;
/**Attribute blend binormal.*/
static const char * ATTRIBUTE_NAME_BINORMAL ;
2015-03-20 09:46:08 +08:00
/**
end of Built Attribute names
@ }
*/
2014-05-08 05:38:41 +08:00
2015-03-20 09:46:08 +08:00
/**Constructor.*/
2013-06-20 14:13:12 +08:00
GLProgram ( ) ;
2015-03-20 09:46:08 +08:00
/**Destructor.*/
2013-06-20 14:13:12 +08:00
virtual ~ GLProgram ( ) ;
2014-03-22 20:58:07 +08:00
2015-03-20 09:46:08 +08:00
/** @{
Create or Initializes the GLProgram with a vertex and fragment with bytes array .
* @ js initWithString .
* @ lua initWithString .
2014-03-22 20:58:07 +08:00
*/
2014-05-08 05:38:41 +08:00
static GLProgram * createWithByteArrays ( const GLchar * vShaderByteArray , const GLchar * fShaderByteArray ) ;
2014-03-05 05:51:43 +08:00
bool initWithByteArrays ( const GLchar * vShaderByteArray , const GLchar * fShaderByteArray ) ;
2015-05-06 04:07:32 +08:00
static GLProgram * createWithByteArrays ( const GLchar * vShaderByteArray , const GLchar * fShaderByteArray , const std : : string & compileTimeDefines ) ;
bool initWithByteArrays ( const GLchar * vShaderByteArray , const GLchar * fShaderByteArray , const std : : string & compileTimeDefines ) ;
2015-03-20 09:46:08 +08:00
/**
@ }
*/
2016-10-28 09:33:31 +08:00
/** @{
2017-02-07 09:41:52 +08:00
Create or Initializes the GLProgram with a vertex and fragment with bytes array , with shader headers definition ( eg . # version . . . or # extension . . . ) , with compileTimeDefines ( eg . # define . . . ) .
2016-10-28 09:33:31 +08:00
* @ js initWithString .
* @ lua initWithString .
*/
static GLProgram * createWithByteArrays ( const GLchar * vShaderByteArray , const GLchar * fShaderByteArray , const std : : string & compileTimeHeaders , const std : : string & compileTimeDefines ) ;
bool initWithByteArrays ( const GLchar * vShaderByteArray , const GLchar * fShaderByteArray , const std : : string & compileTimeHeaders , const std : : string & compileTimeDefines ) ;
/**
@ }
*/
2015-03-20 09:46:08 +08:00
/** @{
Create or Initializes the GLProgram with a vertex and fragment with contents of filenames .
2013-09-13 11:41:20 +08:00
* @ js init
* @ lua init
*/
2014-05-08 05:38:41 +08:00
static GLProgram * createWithFilenames ( const std : : string & vShaderFilename , const std : : string & fShaderFilename ) ;
2014-03-05 05:51:43 +08:00
bool initWithFilenames ( const std : : string & vShaderFilename , const std : : string & fShaderFilename ) ;
2015-05-06 04:07:32 +08:00
static GLProgram * createWithFilenames ( const std : : string & vShaderFilename , const std : : string & fShaderFilename , const std : : string & compileTimeDefines ) ;
bool initWithFilenames ( const std : : string & vShaderFilename , const std : : string & fShaderFilename , const std : : string & compileTimeDefines ) ;
2015-03-20 09:46:08 +08:00
/**
@ }
*/
2015-04-20 17:00:40 +08:00
2016-10-28 09:33:31 +08:00
/** @{
2017-02-07 09:41:52 +08:00
Create or Initializes the GLProgram with a vertex and fragment with contents of filenames , with shader headers definition ( eg . # version . . . or # extension . . . ) , with compileTimeDefines ( eg . # define . . . ) .
2016-10-28 09:33:31 +08:00
* @ js init
* @ lua init
*/
static GLProgram * createWithFilenames ( const std : : string & vShaderFilename , const std : : string & fShaderFilename , const std : : string & compileTimeHeaders , const std : : string & compileTimeDefines ) ;
bool initWithFilenames ( const std : : string & vShaderFilename , const std : : string & fShaderFilename , const std : : string & compileTimeHeaders , const std : : string & compileTimeDefines ) ;
/**
@ }
*/
2015-03-20 09:46:08 +08:00
/**@{ Get the uniform or vertex attribute by string name in shader, return null if it does not exist.*/
2014-08-15 19:15:14 +08:00
Uniform * getUniform ( const std : : string & name ) ;
2014-05-07 08:58:14 +08:00
VertexAttrib * getVertexAttrib ( const std : : string & name ) ;
2015-03-20 09:46:08 +08:00
/**@}*/
2014-03-05 05:51:43 +08:00
2015-03-20 09:46:08 +08:00
/** It will add a new attribute to the shader by calling glBindAttribLocation. */
2014-05-07 08:58:14 +08:00
void bindAttribLocation ( const std : : string & attributeName , GLuint index ) const ;
2014-03-05 05:51:43 +08:00
2015-03-20 09:46:08 +08:00
/** Calls glGetAttribLocation. */
2014-05-07 08:58:14 +08:00
GLint getAttribLocation ( const std : : string & attributeName ) const ;
2014-03-05 05:51:43 +08:00
2015-03-20 09:46:08 +08:00
/** Calls glGetUniformLocation(). */
2014-05-07 08:58:14 +08:00
GLint getUniformLocation ( const std : : string & attributeName ) const ;
2014-03-05 05:51:43 +08:00
2012-04-19 14:35:52 +08:00
/** links the glProgram */
bool link ( ) ;
/** it will call glUseProgram() */
void use ( ) ;
2012-11-09 12:08:18 +08:00
/** It will create 4 uniforms:
2013-06-20 14:13:12 +08:00
- kUniformPMatrix
- kUniformMVMatrix
- kUniformMVPMatrix
2013-07-25 17:48:22 +08:00
- GLProgram : : UNIFORM_SAMPLER
2012-03-12 15:22:03 +08:00
2013-07-25 17:48:22 +08:00
And it will bind " GLProgram::UNIFORM_SAMPLER " to 0
2012-03-12 15:22:03 +08:00
*/
2012-04-19 14:35:52 +08:00
void updateUniforms ( ) ;
2015-04-20 17:00:40 +08:00
2013-02-27 09:38:30 +08:00
/** calls retrieves the named uniform location for this shader program. */
2013-07-07 13:01:21 +08:00
GLint getUniformLocationForName ( const char * name ) const ;
2015-04-20 17:00:40 +08:00
/** calls glUniform1i only if the values are different than the previous call for this same shader program.
2013-09-13 11:41:20 +08:00
* @ js setUniformLocationI32
* @ lua setUniformLocationI32
*/
2012-11-09 12:08:18 +08:00
void setUniformLocationWith1i ( GLint location , GLint i1 ) ;
2015-04-20 17:00:40 +08:00
2013-03-27 02:20:38 +08:00
/** calls glUniform2i only if the values are different than the previous call for this same shader program. */
void setUniformLocationWith2i ( GLint location , GLint i1 , GLint i2 ) ;
2015-04-20 17:00:40 +08:00
2013-03-27 02:20:38 +08:00
/** calls glUniform3i only if the values are different than the previous call for this same shader program. */
void setUniformLocationWith3i ( GLint location , GLint i1 , GLint i2 , GLint i3 ) ;
2015-04-20 17:00:40 +08:00
2013-03-27 02:20:38 +08:00
/** calls glUniform4i only if the values are different than the previous call for this same shader program. */
void setUniformLocationWith4i ( GLint location , GLint i1 , GLint i2 , GLint i3 , GLint i4 ) ;
2015-04-20 17:00:40 +08:00
2013-03-27 02:20:38 +08:00
/** calls glUniform2iv only if the values are different than the previous call for this same shader program. */
void setUniformLocationWith2iv ( GLint location , GLint * ints , unsigned int numberOfArrays ) ;
2015-04-20 17:00:40 +08:00
2013-03-27 02:20:38 +08:00
/** calls glUniform3iv only if the values are different than the previous call for this same shader program. */
void setUniformLocationWith3iv ( GLint location , GLint * ints , unsigned int numberOfArrays ) ;
2015-04-20 17:00:40 +08:00
2013-03-27 02:20:38 +08:00
/** calls glUniform4iv only if the values are different than the previous call for this same shader program. */
2015-04-20 17:00:40 +08:00
2013-03-27 02:20:38 +08:00
void setUniformLocationWith4iv ( GLint location , GLint * ints , unsigned int numberOfArrays ) ;
2012-04-17 17:55:26 +08:00
2015-04-20 17:00:40 +08:00
/** calls glUniform1f only if the values are different than the previous call for this same shader program.
2013-09-13 11:41:20 +08:00
* In js or lua , please use setUniformLocationF32
* @ js NA
*/
2012-11-09 12:08:18 +08:00
void setUniformLocationWith1f ( GLint location , GLfloat f1 ) ;
2012-04-17 17:55:26 +08:00
2015-04-20 17:00:40 +08:00
/** calls glUniform2f only if the values are different than the previous call for this same shader program.
2013-09-13 11:41:20 +08:00
* In js or lua , please use setUniformLocationF32
* @ js NA
*/
2012-11-09 12:08:18 +08:00
void setUniformLocationWith2f ( GLint location , GLfloat f1 , GLfloat f2 ) ;
2012-04-17 17:55:26 +08:00
2015-04-20 17:00:40 +08:00
/** calls glUniform3f only if the values are different than the previous call for this same shader program.
2013-09-13 11:41:20 +08:00
* In js or lua , please use setUniformLocationF32
* @ js NA
*/
2012-11-09 12:08:18 +08:00
void setUniformLocationWith3f ( GLint location , GLfloat f1 , GLfloat f2 , GLfloat f3 ) ;
2012-04-17 17:55:26 +08:00
2015-04-20 17:00:40 +08:00
/** calls glUniform4f only if the values are different than the previous call for this same shader program.
2013-09-13 11:41:20 +08:00
* In js or lua , please use setUniformLocationF32
* @ js NA
*/
2012-11-09 12:08:18 +08:00
void setUniformLocationWith4f ( GLint location , GLfloat f1 , GLfloat f2 , GLfloat f3 , GLfloat f4 ) ;
2012-04-17 17:55:26 +08:00
2014-08-15 19:15:14 +08:00
/** calls glUniformfv only if the values are different than the previous call for this same shader program. */
void setUniformLocationWith1fv ( GLint location , const GLfloat * floats , unsigned int numberOfArrays ) ;
2012-04-19 14:35:52 +08:00
/** calls glUniform2fv only if the values are different than the previous call for this same shader program. */
2013-12-07 09:42:16 +08:00
void setUniformLocationWith2fv ( GLint location , const GLfloat * floats , unsigned int numberOfArrays ) ;
2012-04-17 17:55:26 +08:00
2012-04-19 14:35:52 +08:00
/** calls glUniform3fv only if the values are different than the previous call for this same shader program. */
2013-12-07 09:42:16 +08:00
void setUniformLocationWith3fv ( GLint location , const GLfloat * floats , unsigned int numberOfArrays ) ;
2012-04-17 17:55:26 +08:00
2012-04-19 14:35:52 +08:00
/** calls glUniform4fv only if the values are different than the previous call for this same shader program. */
2013-12-07 09:42:16 +08:00
void setUniformLocationWith4fv ( GLint location , const GLfloat * floats , unsigned int numberOfArrays ) ;
2012-04-17 17:55:26 +08:00
2013-10-28 00:58:57 +08:00
/** calls glUniformMatrix2fv only if the values are different than the previous call for this same shader program. */
2013-12-07 09:42:16 +08:00
void setUniformLocationWithMatrix2fv ( GLint location , const GLfloat * matrixArray , unsigned int numberOfMatrices ) ;
2015-04-20 17:00:40 +08:00
2013-10-28 00:58:57 +08:00
/** calls glUniformMatrix3fv only if the values are different than the previous call for this same shader program. */
2013-12-07 09:42:16 +08:00
void setUniformLocationWithMatrix3fv ( GLint location , const GLfloat * matrixArray , unsigned int numberOfMatrices ) ;
2015-04-20 17:00:40 +08:00
2012-04-19 14:35:52 +08:00
/** calls glUniformMatrix4fv only if the values are different than the previous call for this same shader program. */
2013-12-07 09:42:16 +08:00
void setUniformLocationWithMatrix4fv ( GLint location , const GLfloat * matrixArray , unsigned int numberOfMatrices ) ;
2015-04-20 17:00:40 +08:00
2015-03-27 11:03:21 +08:00
/**
Update the builtin uniforms if they are different than the previous call for this same shader program .
2016-02-09 03:25:37 +08:00
@ param modelView modelView matrix applied to the built in uniform of the shader .
2015-03-27 11:03:21 +08:00
*/
2016-02-09 03:25:37 +08:00
void setUniformsForBuiltins ( const Mat4 & modelView ) ;
2015-03-27 11:03:21 +08:00
/**
Update the builtin uniforms if they are different than the previous call for this same shader program .
*/
2016-02-09 03:25:37 +08:00
void setUniformsForBuiltins ( ) ;
2012-04-17 17:55:26 +08:00
2012-04-19 14:35:52 +08:00
/** returns the vertexShader error log */
2013-12-11 22:35:37 +08:00
std : : string getVertexShaderLog ( ) const ;
2012-04-19 14:35:52 +08:00
/** returns the fragmentShader error log */
2013-12-11 22:35:37 +08:00
std : : string getFragmentShaderLog ( ) const ;
2012-04-19 14:35:52 +08:00
/** returns the program error log */
2013-12-11 22:35:37 +08:00
std : : string getProgramLog ( ) const ;
2015-04-20 17:00:40 +08:00
2015-03-20 09:46:08 +08:00
/** Reload all shaders, this function is designed for android
when opengl context lost , so don ' t call it .
*/
2012-04-17 17:55:26 +08:00
void reset ( ) ;
2016-02-09 03:25:37 +08:00
/** returns the OpenGL Program object */
2016-09-12 09:44:21 +08:00
GLuint getProgram ( ) const { return _program ; }
2014-03-31 13:47:07 +08:00
2016-02-09 03:25:37 +08:00
/** returns the Uniform flags */
2016-09-12 09:44:21 +08:00
const UniformFlags & getUniformFlags ( ) const { return _flags ; }
2016-02-09 03:25:37 +08:00
2015-03-20 09:46:08 +08:00
//DEPRECATED
2014-05-14 09:12:58 +08:00
CC_DEPRECATED_ATTRIBUTE bool initWithVertexShaderByteArray ( const GLchar * vertexByteArray , const GLchar * fragByteArray )
{ return initWithByteArrays ( vertexByteArray , fragByteArray ) ; }
CC_DEPRECATED_ATTRIBUTE bool initWithVertexShaderFilename ( const std : : string & vertexFilename , const std : : string & fragFilename )
{ return initWithFilenames ( vertexFilename , fragFilename ) ; }
CC_DEPRECATED_ATTRIBUTE void addAttribute ( const std : : string & attributeName , GLuint index ) const { return bindAttribLocation ( attributeName , index ) ; }
2014-03-05 05:51:43 +08:00
2014-05-07 08:58:14 +08:00
protected :
2015-03-20 09:46:08 +08:00
/**
Update the uniform data in location .
@ param location The location of the uniform .
@ param data Updated data .
2016-07-22 01:05:19 +08:00
@ param bytes Data length in bytes to update .
2015-03-20 09:46:08 +08:00
*/
2013-12-07 09:42:16 +08:00
bool updateUniformLocation ( GLint location , const GLvoid * data , unsigned int bytes ) ;
2015-03-20 09:46:08 +08:00
/**Get a general description of the shader.*/
2013-12-13 06:38:12 +08:00
virtual std : : string getDescription ( ) const ;
2014-05-09 03:34:26 +08:00
2015-03-20 09:46:08 +08:00
/**Bind the predefined vertex attributes to their specific slot.*/
2014-05-09 03:34:26 +08:00
void bindPredefinedVertexAttribs ( ) ;
2015-03-20 09:46:08 +08:00
/**Parse user defined Vertex Attributes automatically.*/
2014-05-07 08:58:14 +08:00
void parseVertexAttribs ( ) ;
2015-03-20 09:46:08 +08:00
/**Parse user defined uniform automatically.*/
2014-05-07 08:58:14 +08:00
void parseUniforms ( ) ;
2015-03-20 09:46:08 +08:00
/**Compile the shader sources.*/
2016-10-28 09:33:31 +08:00
bool compileShader ( GLuint * shader , GLenum type , const GLchar * source , const std : : string & compileTimeHeaders , const std : : string & convertedDefines ) ;
2015-05-06 04:07:32 +08:00
bool compileShader ( GLuint * shader , GLenum type , const GLchar * source , const std : : string & convertedDefines ) ;
2012-04-19 14:35:52 +08:00
bool compileShader ( GLuint * shader , GLenum type , const GLchar * source ) ;
2016-02-09 03:25:37 +08:00
void clearShader ( ) ;
2015-04-20 17:00:40 +08:00
2017-02-06 15:11:42 +08:00
void clearHashUniforms ( ) ;
2015-03-20 09:46:08 +08:00
/**OpenGL handle for program.*/
2013-06-15 14:03:30 +08:00
GLuint _program ;
2015-03-20 09:46:08 +08:00
/**OpenGL handle for vertex shader.*/
2013-06-15 14:03:30 +08:00
GLuint _vertShader ;
2015-03-20 09:46:08 +08:00
/**OpenGL handle for fragment shader.*/
2013-06-15 14:03:30 +08:00
GLuint _fragShader ;
2015-03-20 09:46:08 +08:00
/**Built in uniforms.*/
2014-05-14 09:12:58 +08:00
GLint _builtInUniforms [ UNIFORM_MAX ] ;
2015-03-20 09:46:08 +08:00
/**Indicate whether it has a offline shader compiler or not.*/
2014-08-15 19:15:14 +08:00
bool _hasShaderCompiler ;
2015-04-20 17:00:40 +08:00
2015-03-20 09:46:08 +08:00
/**User defined Uniforms.*/
2014-05-14 09:12:58 +08:00
std : : unordered_map < std : : string , Uniform > _userUniforms ;
2015-03-20 09:46:08 +08:00
/**User defined vertex attributes.*/
2014-05-14 09:12:58 +08:00
std : : unordered_map < std : : string , VertexAttrib > _vertexAttribs ;
2015-03-20 09:46:08 +08:00
/**Hash value of uniforms for quick access.*/
2015-01-26 18:40:00 +08:00
std : : unordered_map < GLint , std : : pair < GLvoid * , unsigned int > > _hashForUniforms ;
2014-12-04 18:37:57 +08:00
//cached director pointer for calling
Director * _director ;
2016-02-09 03:25:37 +08:00
/*needed uniforms*/
UniformFlags _flags ;
2012-03-12 15:22:03 +08:00
} ;
NS_CC_END
2015-03-27 11:54:40 +08:00
/**
end of support group
@ }
*/
2012-03-12 15:22:03 +08:00
# endif /* __CCGLPROGRAM_H__ */