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
|
|
|
|
Copyright 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
|
2012-03-15 10:42:22 +08:00
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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__
|
|
|
|
|
|
|
|
#include "ccMacros.h"
|
2014-02-20 10:53:49 +08:00
|
|
|
#include "CCRef.h"
|
2012-06-08 13:55:28 +08:00
|
|
|
#include "CCGL.h"
|
2014-04-09 11:53:01 +08:00
|
|
|
#include "CCMath.h"
|
2014-03-25 16:54:29 +08:00
|
|
|
#include <set>
|
2012-06-08 13:55:28 +08:00
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-04-09 11:53:01 +08:00
|
|
|
USING_NS_CC_MATH;
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup shaders
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-03-15 10:42:22 +08:00
|
|
|
struct _hashUniformEntry;
|
|
|
|
|
2012-03-15 10:54:07 +08:00
|
|
|
typedef void (*GLInfoFunction)(GLuint program, GLenum pname, GLint* params);
|
|
|
|
typedef void (*GLLogFunction) (GLuint program, GLsizei bufsize, GLsizei* length, GLchar* infolog);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** GLProgram
|
2012-04-17 17:55:26 +08:00
|
|
|
Class that implements a glProgram
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
{
|
|
|
|
public:
|
2013-07-25 17:48:22 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
VERTEX_ATTRIB_POSITION,
|
|
|
|
VERTEX_ATTRIB_COLOR,
|
|
|
|
VERTEX_ATTRIB_TEX_COORDS,
|
|
|
|
|
|
|
|
VERTEX_ATTRIB_MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
UNIFORM_P_MATRIX,
|
|
|
|
UNIFORM_MV_MATRIX,
|
|
|
|
UNIFORM_MVP_MATRIX,
|
|
|
|
UNIFORM_TIME,
|
|
|
|
UNIFORM_SIN_TIME,
|
|
|
|
UNIFORM_COS_TIME,
|
|
|
|
UNIFORM_RANDOM01,
|
|
|
|
UNIFORM_SAMPLER,
|
|
|
|
|
|
|
|
UNIFORM_MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* SHADER_NAME_POSITION_TEXTURE_COLOR;
|
2013-12-06 13:24:59 +08:00
|
|
|
static const char* SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP;
|
2013-07-25 17:48:22 +08:00
|
|
|
static const char* SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST;
|
2014-03-10 15:12:44 +08:00
|
|
|
static const char* SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV;
|
2013-07-25 17:48:22 +08:00
|
|
|
static const char* SHADER_NAME_POSITION_COLOR;
|
2014-01-07 22:08:00 +08:00
|
|
|
static const char* SHADER_NAME_POSITION_COLOR_NO_MVP;
|
2013-07-25 17:48:22 +08:00
|
|
|
static const char* SHADER_NAME_POSITION_TEXTURE;
|
|
|
|
static const char* SHADER_NAME_POSITION_TEXTURE_U_COLOR;
|
|
|
|
static const char* SHADER_NAME_POSITION_TEXTURE_A8_COLOR;
|
|
|
|
static const char* SHADER_NAME_POSITION_U_COLOR;
|
|
|
|
static const char* SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR;
|
2013-12-13 12:42:15 +08:00
|
|
|
|
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;
|
2014-03-20 20:56:10 +08:00
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
|
|
|
|
// uniform names
|
|
|
|
static const char* UNIFORM_NAME_P_MATRIX;
|
|
|
|
static const char* UNIFORM_NAME_MV_MATRIX;
|
|
|
|
static const char* UNIFORM_NAME_MVP_MATRIX;
|
|
|
|
static const char* UNIFORM_NAME_TIME;
|
|
|
|
static const char* UNIFORM_NAME_SIN_TIME;
|
|
|
|
static const char* UNIFORM_NAME_COS_TIME;
|
|
|
|
static const char* UNIFORM_NAME_RANDOM01;
|
|
|
|
static const char* UNIFORM_NAME_SAMPLER;
|
|
|
|
static const char* UNIFORM_NAME_ALPHA_TEST_VALUE;
|
|
|
|
|
|
|
|
// Attribute names
|
|
|
|
static const char* ATTRIBUTE_NAME_COLOR;
|
|
|
|
static const char* ATTRIBUTE_NAME_POSITION;
|
|
|
|
static const char* ATTRIBUTE_NAME_TEX_COORD;
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
GLProgram();
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual ~GLProgram();
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Initializes the GLProgram with a vertex and fragment with bytes array
|
|
|
|
* @js initWithString
|
|
|
|
* @lua initWithString
|
|
|
|
*/
|
2014-03-22 20:58:07 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
|
|
|
/** Initializes the CCGLProgram with precompiled shader program */
|
|
|
|
bool initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Initializes the GLProgram with a vertex and fragment with bytes array
|
|
|
|
* @js initWithString
|
|
|
|
* @lua initWithString
|
|
|
|
*/
|
2014-03-05 05:51:43 +08:00
|
|
|
bool initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
|
2014-03-22 20:58:07 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Initializes the GLProgram with a vertex and fragment with contents of filenames
|
|
|
|
* @js init
|
|
|
|
* @lua init
|
|
|
|
*/
|
2014-03-05 05:51:43 +08:00
|
|
|
bool initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename);
|
|
|
|
|
|
|
|
/** It will add a new attribute to the shader by calling glBindAttribLocation */
|
|
|
|
void bindAttribLocation(const char* attributeName, GLuint index) const;
|
|
|
|
|
|
|
|
/** calls glGetAttribLocation */
|
|
|
|
GLint getAttribLocation(const char* attributeName) const;
|
|
|
|
|
|
|
|
/** calls glGetUniformLocation() */
|
|
|
|
GLint getUniformLocation(const char* attributeName) const;
|
|
|
|
|
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();
|
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;
|
2013-02-27 09:38:30 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** calls glUniform1i only if the values are different than the previous call for this same shader program.
|
|
|
|
* @js setUniformLocationI32
|
|
|
|
* @lua setUniformLocationI32
|
|
|
|
*/
|
2012-11-09 12:08:18 +08:00
|
|
|
void setUniformLocationWith1i(GLint location, GLint i1);
|
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);
|
|
|
|
|
|
|
|
/** 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);
|
|
|
|
|
|
|
|
/** 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);
|
|
|
|
|
|
|
|
/** 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);
|
|
|
|
|
|
|
|
/** 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);
|
|
|
|
|
|
|
|
/** calls glUniform4iv only if the values are different than the previous call for this same shader program. */
|
|
|
|
|
|
|
|
void setUniformLocationWith4iv(GLint location, GLint* ints, unsigned int numberOfArrays);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** calls glUniform1f only if the values are different than the previous call for this same shader program.
|
|
|
|
* 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
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** calls glUniform2f only if the values are different than the previous call for this same shader program.
|
|
|
|
* 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
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** calls glUniform3f only if the values are different than the previous call for this same shader program.
|
|
|
|
* 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
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** calls glUniform4f only if the values are different than the previous call for this same shader program.
|
|
|
|
* 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
|
|
|
|
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);
|
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);
|
2013-10-28 00:58:57 +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);
|
2012-11-09 12:08:18 +08:00
|
|
|
|
|
|
|
/** will update the builtin uniforms if they are different than the previous call for this same shader program. */
|
|
|
|
void setUniformsForBuiltins();
|
2014-04-09 11:53:01 +08:00
|
|
|
void setUniformsForBuiltins(const Matrix &modelView);
|
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;
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
// reload all shaders, this function is designed for android
|
|
|
|
// when opengl context lost, so don't call it.
|
|
|
|
void reset();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
inline const GLuint getProgram() const { return _program; }
|
2014-03-31 13:47:07 +08:00
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
// DEPRECATED
|
|
|
|
CC_DEPRECATED_ATTRIBUTE bool initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
|
|
|
|
{ return initWithByteArrays(vShaderByteArray, fShaderByteArray); }
|
|
|
|
CC_DEPRECATED_ATTRIBUTE bool initWithVertexShaderFilename(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
|
|
|
|
{ return initWithFilenames(vShaderByteArray, fShaderByteArray); }
|
|
|
|
CC_DEPRECATED_ATTRIBUTE void addAttribute(const char* attributeName, GLuint index) const { return bindAttribLocation(attributeName, index); }
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
private:
|
2013-12-07 09:42:16 +08:00
|
|
|
bool updateUniformLocation(GLint location, const GLvoid* data, unsigned int bytes);
|
2013-12-13 06:38:12 +08:00
|
|
|
virtual std::string getDescription() const;
|
2013-12-13 06:41:42 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
bool compileShader(GLuint * shader, GLenum type, const GLchar* source);
|
2013-12-11 22:35:37 +08:00
|
|
|
std::string logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc) const;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-03-15 10:42:22 +08:00
|
|
|
private:
|
2013-06-15 14:03:30 +08:00
|
|
|
GLuint _program;
|
|
|
|
GLuint _vertShader;
|
|
|
|
GLuint _fragShader;
|
2013-07-25 17:48:22 +08:00
|
|
|
GLint _uniforms[UNIFORM_MAX];
|
2013-06-15 14:03:30 +08:00
|
|
|
struct _hashUniformEntry* _hashForUniforms;
|
2014-03-22 20:58:07 +08:00
|
|
|
bool _hasShaderCompiler;
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
|
|
|
std::string _shaderId;
|
|
|
|
#endif
|
2013-11-01 08:56:15 +08:00
|
|
|
|
2013-11-23 01:42:21 +08:00
|
|
|
struct flag_struct {
|
2013-11-01 08:56:15 +08:00
|
|
|
unsigned int usesTime:1;
|
|
|
|
unsigned int usesMVP:1;
|
|
|
|
unsigned int usesMV:1;
|
2013-12-06 11:04:01 +08:00
|
|
|
unsigned int usesP:1;
|
2013-11-01 08:56:15 +08:00
|
|
|
unsigned int usesRandom:1;
|
|
|
|
|
|
|
|
// handy way to initialize the bitfield
|
|
|
|
flag_struct() { memset(this, 0, sizeof(*this)); }
|
|
|
|
} _flags;
|
2014-03-25 16:54:29 +08:00
|
|
|
public:
|
|
|
|
static const GLuint _maxMaterialIDNumber;
|
2012-03-12 15:22:03 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of shaders group
|
|
|
|
/// @}
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* __CCGLPROGRAM_H__ */
|