axmol/cocos/renderer/CCGLProgram.h

344 lines
13 KiB
C
Raw Normal View History

/****************************************************************************
Copyright 2011 Jeff Lamarche
Copyright 2012 Goffredo Marocchi
Copyright 2012 Ricardo Quesada
Copyright 2012 cocos2d-x.org
Copyright 2013-2014 Chukong Technologies Inc.
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.
****************************************************************************/
#ifndef __CCGLPROGRAM_H__
#define __CCGLPROGRAM_H__
2014-04-30 08:37:36 +08:00
#include "base/ccMacros.h"
#include "base/CCRef.h"
2014-05-08 11:20:19 +08:00
#include "base/ccTypes.h"
2012-06-08 13:55:28 +08:00
#include "CCGL.h"
2014-04-26 13:03:25 +08:00
#include "math/CCMath.h"
2014-05-07 03:46:35 +08:00
#include <unordered_map>
2012-06-08 13:55:28 +08:00
NS_CC_BEGIN
2012-06-20 18:09:11 +08:00
/**
* @addtogroup shaders
* @{
*/
struct _hashUniformEntry;
class GLProgram;
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);
2014-05-07 02:19:51 +08:00
2014-05-09 05:01:03 +08:00
struct VertexAttrib
{
2014-05-09 05:01:03 +08:00
GLuint index;
GLint size;
GLenum type;
std::string name;
2014-05-08 01:44:45 +08:00
};
2014-05-08 11:38:15 +08:00
struct Uniform
2014-05-08 01:44:45 +08:00
{
2014-05-08 11:38:15 +08:00
GLint location;
GLint size;
GLenum type;
2014-05-09 05:01:03 +08:00
std::string name;
2014-05-07 02:19:51 +08:00
};
/** GLProgram
Class that implements a glProgram
@since v2.0.0
*/
class CC_DLL GLProgram : public Ref
{
2014-05-08 11:20:19 +08:00
friend class GLProgramState;
public:
enum
{
VERTEX_ATTRIB_POSITION,
VERTEX_ATTRIB_COLOR,
2014-05-09 03:34:26 +08:00
VERTEX_ATTRIB_TEX_COORD,
VERTEX_ATTRIB_NORMAL,
VERTEX_ATTRIB_MAX,
2014-05-09 03:34:26 +08:00
// backward compatibility
VERTEX_ATTRIB_TEX_COORDS = VERTEX_ATTRIB_TEX_COORD,
};
enum
{
UNIFORM_P_MATRIX,
UNIFORM_MV_MATRIX,
UNIFORM_MVP_MATRIX,
UNIFORM_TIME,
UNIFORM_SIN_TIME,
UNIFORM_COS_TIME,
UNIFORM_RANDOM01,
2014-05-14 09:12:58 +08:00
UNIFORM_SAMPLER0,
UNIFORM_SAMPLER1,
UNIFORM_SAMPLER2,
UNIFORM_SAMPLER3,
UNIFORM_MAX,
};
static const char* SHADER_NAME_POSITION_TEXTURE_COLOR;
static const char* SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP;
static const char* SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST;
static const char* SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV;
static const char* SHADER_NAME_POSITION_COLOR;
static const char* SHADER_NAME_POSITION_COLOR_NO_MVP;
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;
static const char* SHADER_NAME_LABEL_NORMAL;
static const char* SHADER_NAME_LABEL_OUTLINE;
static const char* SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL;
static const char* SHADER_NAME_LABEL_DISTANCEFIELD_GLOW;
// 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;
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;
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;
2014-05-09 03:34:26 +08:00
static const char* ATTRIBUTE_NAME_NORMAL;
2014-05-08 05:38:41 +08:00
GLProgram();
virtual ~GLProgram();
/** Initializes the GLProgram with a vertex and fragment with bytes array
* @js initWithString
* @lua initWithString
*/
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
/** Initializes the CCGLProgram with precompiled shader program */
2014-05-08 05:38:41 +08:00
static GLProgram* createWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
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-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);
/** Initializes the GLProgram with a vertex and fragment with contents of filenames
* @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);
2014-05-08 01:44:45 +08:00
//void bindUniform(std::string uniformName, int value);
Uniform* getUniform(const std::string& name);
VertexAttrib* getVertexAttrib(const std::string& name);
2014-03-05 05:51:43 +08:00
/** It will add a new attribute to the shader by calling glBindAttribLocation */
void bindAttribLocation(const std::string& attributeName, GLuint index) const;
2014-03-05 05:51:43 +08:00
/** calls glGetAttribLocation */
GLint getAttribLocation(const std::string& attributeName) const;
2014-03-05 05:51:43 +08:00
/** calls glGetUniformLocation() */
GLint getUniformLocation(const std::string& attributeName) const;
2014-03-05 05:51:43 +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:
- kUniformPMatrix
- kUniformMVMatrix
- kUniformMVPMatrix
- GLProgram::UNIFORM_SAMPLER
And it will bind "GLProgram::UNIFORM_SAMPLER" to 0
*/
void updateUniforms();
2013-02-27 09:38:30 +08:00
/** calls retrieves the named uniform location for this shader program. */
GLint getUniformLocationForName(const char* name) const;
2013-02-27 09:38:30 +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);
/** 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);
/** 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);
/** 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);
/** 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);
/** 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);
/** calls glUniform2fv only if the values are different than the previous call for this same shader program. */
void setUniformLocationWith2fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays);
/** calls glUniform3fv only if the values are different than the previous call for this same shader program. */
void setUniformLocationWith3fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays);
/** calls glUniform4fv only if the values are different than the previous call for this same shader program. */
void setUniformLocationWith4fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays);
/** calls glUniformMatrix2fv only if the values are different than the previous call for this same shader program. */
void setUniformLocationWithMatrix2fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices);
/** calls glUniformMatrix3fv only if the values are different than the previous call for this same shader program. */
void setUniformLocationWithMatrix3fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices);
/** calls glUniformMatrix4fv only if the values are different than the previous call for this same shader program. */
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();
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
void setUniformsForBuiltins(const Mat4 &modelView);
// Attribute
/** returns the vertexShader error log */
std::string getVertexShaderLog() const;
/** returns the fragmentShader error log */
std::string getFragmentShaderLog() const;
/** returns the program error log */
std::string getProgramLog() const;
// reload all shaders, this function is designed for android
// when opengl context lost, so don't call it.
void reset();
inline const GLuint getProgram() const { return _program; }
2014-03-31 13:47:07 +08:00
2014-03-05 05:51:43 +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
protected:
bool updateUniformLocation(GLint location, const GLvoid* data, unsigned int bytes);
virtual std::string getDescription() const;
2014-05-09 03:34:26 +08:00
void bindPredefinedVertexAttribs();
void parseVertexAttribs();
void parseUniforms();
bool compileShader(GLuint * shader, GLenum type, const GLchar* source);
std::string logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc) const;
GLuint _program;
GLuint _vertShader;
GLuint _fragShader;
2014-05-14 09:12:58 +08:00
GLint _builtInUniforms[UNIFORM_MAX];
struct _hashUniformEntry* _hashForUniforms;
bool _hasShaderCompiler;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
std::string _shaderId;
#endif
2013-11-23 01:42:21 +08:00
struct flag_struct {
unsigned int usesTime:1;
unsigned int usesMVP:1;
unsigned int usesMV:1;
2013-12-06 11:04:01 +08:00
unsigned int usesP:1;
unsigned int usesRandom:1;
// handy way to initialize the bitfield
flag_struct() { memset(this, 0, sizeof(*this)); }
} _flags;
2014-05-14 09:12:58 +08:00
std::unordered_map<std::string, Uniform> _userUniforms;
std::unordered_map<std::string, VertexAttrib> _vertexAttribs;
};
NS_CC_END
#endif /* __CCGLPROGRAM_H__ */