axmol/cocos/renderer/CCGLProgram.cpp

872 lines
27 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 false 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.
****************************************************************************/
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
#include "renderer/CCGLProgram.h"
#include "renderer/ccGLStateCache.h"
2014-04-30 08:37:36 +08:00
#include "base/ccMacros.h"
#include "2d/platform/CCFileUtils.h"
#include "2d/uthash.h"
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h"
2014-05-07 02:19:51 +08:00
#include "CCGL.h"
2014-05-10 04:24:56 +08:00
#ifndef WIN32
#include <alloca.h>
2014-05-10 04:24:56 +08:00
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
#include "CCPrecompiledShaders.h"
#endif
NS_CC_BEGIN
typedef struct _hashUniformEntry
{
GLvoid* value; // value
unsigned int location; // Key
UT_hash_handle hh; // hash entry
} tHashUniformEntry;
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR = "ShaderPositionTextureColor";
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP = "ShaderPositionTextureColor_noMVP";
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST = "ShaderPositionTextureColorAlphaTest";
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV = "ShaderPositionTextureColorAlphaTest_NoMV";
const char* GLProgram::SHADER_NAME_POSITION_COLOR = "ShaderPositionColor";
const char* GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP = "ShaderPositionColor_noMVP";
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE = "ShaderPositionTexture";
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_U_COLOR = "ShaderPositionTexture_uColor";
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR = "ShaderPositionTextureA8Color";
const char* GLProgram::SHADER_NAME_POSITION_U_COLOR = "ShaderPosition_uColor";
const char* GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR = "ShaderPositionLengthTextureColor";
const char* GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL = "ShaderLabelDFNormal";
const char* GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW = "ShaderLabelDFGlow";
const char* GLProgram::SHADER_NAME_LABEL_NORMAL = "ShaderLabelNormal";
const char* GLProgram::SHADER_NAME_LABEL_OUTLINE = "ShaderLabelOutline";
// uniform names
const char* GLProgram::UNIFORM_NAME_P_MATRIX = "CC_PMatrix";
const char* GLProgram::UNIFORM_NAME_MV_MATRIX = "CC_MVMatrix";
const char* GLProgram::UNIFORM_NAME_MVP_MATRIX = "CC_MVPMatrix";
const char* GLProgram::UNIFORM_NAME_TIME = "CC_Time";
const char* GLProgram::UNIFORM_NAME_SIN_TIME = "CC_SinTime";
const char* GLProgram::UNIFORM_NAME_COS_TIME = "CC_CosTime";
const char* GLProgram::UNIFORM_NAME_RANDOM01 = "CC_Random01";
2014-05-14 09:12:58 +08:00
const char* GLProgram::UNIFORM_NAME_SAMPLER0 = "CC_Texture0";
const char* GLProgram::UNIFORM_NAME_SAMPLER1 = "CC_Texture1";
const char* GLProgram::UNIFORM_NAME_SAMPLER2 = "CC_Texture2";
const char* GLProgram::UNIFORM_NAME_SAMPLER3 = "CC_Texture3";
const char* GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE = "CC_alpha_value";
// Attribute names
const char* GLProgram::ATTRIBUTE_NAME_COLOR = "a_color";
const char* GLProgram::ATTRIBUTE_NAME_POSITION = "a_position";
const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD = "a_texCoord";
2014-05-09 03:34:26 +08:00
const char* GLProgram::ATTRIBUTE_NAME_NORMAL = "a_normal";
2014-05-08 05:38:41 +08:00
GLProgram* GLProgram::createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
{
auto ret = new (std::nothrow) GLProgram();
if(ret && ret->initWithByteArrays(vShaderByteArray, fShaderByteArray)) {
ret->link();
ret->updateUniforms();
2014-05-08 05:38:41 +08:00
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
GLProgram* GLProgram::createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename)
{
auto ret = new (std::nothrow) GLProgram();
if(ret && ret->initWithFilenames(vShaderFilename, fShaderFilename)) {
ret->link();
ret->updateUniforms();
2014-05-08 05:38:41 +08:00
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
GLProgram::GLProgram()
: _program(0)
, _vertShader(0)
, _fragShader(0)
, _hashForUniforms(nullptr)
, _flags()
{
2014-05-14 09:12:58 +08:00
memset(_builtInUniforms, 0, sizeof(_builtInUniforms));
}
GLProgram::~GLProgram()
{
CCLOGINFO("%s %d deallocing GLProgram: %p", __FUNCTION__, __LINE__, this);
// there is no need to delete the shaders. They should have been already deleted.
CCASSERT(_vertShader == 0, "Vertex Shaders should have been already deleted");
CCASSERT(_fragShader == 0, "Fragment Shaders should have been already deleted");
if (_program)
{
GL::deleteProgram(_program);
}
tHashUniformEntry *current_element, *tmp;
// Purge uniform hash
HASH_ITER(hh, _hashForUniforms, current_element, tmp)
2012-11-09 12:08:18 +08:00
{
HASH_DEL(_hashForUniforms, current_element);
free(current_element->value);
free(current_element);
}
}
2014-03-05 05:51:43 +08:00
bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
GLboolean hasCompiler = false;
glGetBooleanv(GL_SHADER_COMPILER, &hasCompiler);
_hasShaderCompiler = (hasCompiler == GL_TRUE);
if(!_hasShaderCompiler)
{
return initWithPrecompiledProgramByteArray(vShaderByteArray,fShaderByteArray);
}
#endif
_program = glCreateProgram();
CHECK_GL_ERROR_DEBUG();
_vertShader = _fragShader = 0;
2012-11-09 12:08:18 +08:00
if (vShaderByteArray)
{
if (!compileShader(&_vertShader, GL_VERTEX_SHADER, vShaderByteArray))
2012-11-09 12:08:18 +08:00
{
CCLOG("cocos2d: ERROR: Failed to compile vertex shader");
2014-04-28 09:51:10 +08:00
return false;
}
}
// Create and compile fragment shader
2012-11-09 12:08:18 +08:00
if (fShaderByteArray)
{
if (!compileShader(&_fragShader, GL_FRAGMENT_SHADER, fShaderByteArray))
2012-11-09 12:08:18 +08:00
{
CCLOG("cocos2d: ERROR: Failed to compile fragment shader");
2014-04-28 09:51:10 +08:00
return false;
}
}
if (_vertShader)
2012-11-09 12:08:18 +08:00
{
glAttachShader(_program, _vertShader);
}
CHECK_GL_ERROR_DEBUG();
if (_fragShader)
2012-11-09 12:08:18 +08:00
{
glAttachShader(_program, _fragShader);
}
_hashForUniforms = nullptr;
CHECK_GL_ERROR_DEBUG();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
_shaderId = CCPrecompiledShaders::getInstance()->addShaders(vShaderByteArray, fShaderByteArray);
#endif
return true;
}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
2014-05-08 05:38:41 +08:00
GLProgram* GLProgram::createWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
{
auto ret = new (std::nothrow) GLProgram();
if(ret && ret->initWithPrecompiledProgramByteArray(vShaderByteArray, fShaderByteArray)) {
ret->link();
ret->updateUniforms();
2014-05-08 05:38:41 +08:00
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
bool GLProgram::initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
{
bool haveProgram = false;
_program = glCreateProgram();
CHECK_GL_ERROR_DEBUG();
_vertShader = _fragShader = 0;
haveProgram = CCPrecompiledShaders::getInstance()->loadProgram(_program, vShaderByteArray, fShaderByteArray);
CHECK_GL_ERROR_DEBUG();
_hashForUniforms = NULL;
CHECK_GL_ERROR_DEBUG();
return haveProgram;
}
#endif
2014-03-05 05:51:43 +08:00
bool GLProgram::initWithFilenames(const std::string &vShaderFilename, const std::string &fShaderFilename)
{
2014-03-05 05:51:43 +08:00
auto fileUtils = FileUtils::getInstance();
std::string vertexSource = fileUtils->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(vShaderFilename));
std::string fragmentSource = fileUtils->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(fShaderFilename));
2014-03-05 05:51:43 +08:00
return initWithByteArrays(vertexSource.c_str(), fragmentSource.c_str());
}
2014-05-09 03:34:26 +08:00
void GLProgram::bindPredefinedVertexAttribs()
{
static const struct {
const char *attributeName;
int location;
} attribute_locations[] =
{
{GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION},
{GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR},
{GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD},
{GLProgram::ATTRIBUTE_NAME_NORMAL, GLProgram::VERTEX_ATTRIB_NORMAL},
};
const int size = sizeof(attribute_locations) / sizeof(attribute_locations[0]);
for(int i=0; i<size;i++) {
glBindAttribLocation(_program, attribute_locations[i].location, attribute_locations[i].attributeName);
}
}
void GLProgram::parseVertexAttribs()
{
2014-05-14 09:12:58 +08:00
_vertexAttribs.clear();
// Query and store vertex attribute meta-data from the program.
GLint activeAttributes;
GLint length;
glGetProgramiv(_program, GL_ACTIVE_ATTRIBUTES, &activeAttributes);
if(activeAttributes > 0)
{
VertexAttrib attribute;
glGetProgramiv(_program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &length);
if(length > 0)
{
GLchar* attribName = (GLchar*) alloca(length + 1);
for(int i = 0; i < activeAttributes; ++i)
{
// Query attribute info.
2014-05-09 05:01:03 +08:00
glGetActiveAttrib(_program, i, length, NULL, &attribute.size, &attribute.type, attribName);
attribName[length] = '\0';
2014-05-09 05:01:03 +08:00
attribute.name = std::string(attribName);
// Query the pre-assigned attribute location
2014-05-09 05:01:03 +08:00
attribute.index = glGetAttribLocation(_program, attribName);
2014-05-14 09:12:58 +08:00
_vertexAttribs[attribute.name] = attribute;
}
}
}
}
void GLProgram::parseUniforms()
{
2014-05-14 09:12:58 +08:00
_userUniforms.clear();
2014-05-07 02:19:51 +08:00
// Query and store uniforms from the program.
GLint activeUniforms;
glGetProgramiv(_program, GL_ACTIVE_UNIFORMS, &activeUniforms);
if(activeUniforms > 0)
{
GLint length;
glGetProgramiv(_program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &length);
if(length > 0)
{
Uniform uniform;
GLchar* uniformName = (GLchar*)alloca(length + 1);
for(int i = 0; i < activeUniforms; ++i)
{
// Query uniform info.
2014-05-08 11:38:15 +08:00
glGetActiveUniform(_program, i, length, NULL, &uniform.size, &uniform.type, uniformName);
uniformName[length] = '\0';
2014-05-08 11:20:19 +08:00
2014-05-13 14:51:37 +08:00
// Only add uniforms that are not built-in.
2014-05-08 11:20:19 +08:00
// The ones that start with 'CC_' are built-ins
if(strncmp("CC_", uniformName, 3) != 0) {
// remove possible array '[]' from uniform name
2014-05-08 11:38:15 +08:00
if(uniform.size > 1 && length > 3)
2014-05-08 11:20:19 +08:00
{
char* c = strrchr(uniformName, '[');
if(c)
{
*c = '\0';
}
}
2014-05-08 11:38:15 +08:00
uniform.name = std::string(uniformName);
uniform.location = glGetUniformLocation(_program, uniformName);
2014-05-08 11:20:19 +08:00
2014-05-14 09:12:58 +08:00
_userUniforms[uniform.name] = uniform;
2014-05-08 11:20:19 +08:00
}
}
}
}
}
2014-05-08 01:44:45 +08:00
Uniform* GLProgram::getUniform(const std::string &name)
{
2014-05-14 09:12:58 +08:00
const auto itr = _userUniforms.find(name);
if( itr != _userUniforms.end())
2014-05-08 01:44:45 +08:00
return &itr->second;
return nullptr;
}
VertexAttrib* GLProgram::getVertexAttrib(const std::string &name)
2014-05-07 02:19:51 +08:00
{
2014-05-14 09:12:58 +08:00
const auto itr = _vertexAttribs.find(name);
if( itr != _vertexAttribs.end())
return &itr->second;
return nullptr;
2014-05-07 02:19:51 +08:00
}
std::string GLProgram::getDescription() const
{
return StringUtils::format("<GLProgram = "
CC_FORMAT_PRINTF_SIZE_T
" | Program = %i, VertexShader = %i, FragmentShader = %i>",
(size_t)this, _program, _vertShader, _fragShader);
}
bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source)
{
GLint status;
if (!source)
2012-11-09 12:08:18 +08:00
{
return false;
2012-11-09 12:08:18 +08:00
}
const GLchar *sources[] = {
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_LINUX && CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
2012-11-09 12:08:18 +08:00
(type == GL_VERTEX_SHADER ? "precision highp float;\n" : "precision mediump float;\n"),
#endif
"uniform mat4 CC_PMatrix;\n"
"uniform mat4 CC_MVMatrix;\n"
"uniform mat4 CC_MVPMatrix;\n"
"uniform vec4 CC_Time;\n"
"uniform vec4 CC_SinTime;\n"
"uniform vec4 CC_CosTime;\n"
"uniform vec4 CC_Random01;\n"
"//CC INCLUDES END\n\n",
source,
};
*shader = glCreateShader(type);
glShaderSource(*shader, sizeof(sources)/sizeof(*sources), sources, nullptr);
glCompileShader(*shader);
glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
2012-11-09 12:08:18 +08:00
if (! status)
{
GLsizei length;
2014-04-28 09:51:10 +08:00
glGetShaderiv(*shader, GL_SHADER_SOURCE_LENGTH, &length);
GLchar* src = (GLchar *)malloc(sizeof(GLchar) * length);
glGetShaderSource(*shader, length, nullptr, src);
CCLOG("cocos2d: ERROR: Failed to compile shader:\n%s", src);
2012-11-09 12:08:18 +08:00
if (type == GL_VERTEX_SHADER)
{
CCLOG("cocos2d: %s", getVertexShaderLog().c_str());
2012-11-09 12:08:18 +08:00
}
else
2012-11-09 12:08:18 +08:00
{
CCLOG("cocos2d: %s", getFragmentShaderLog().c_str());
2012-11-09 12:08:18 +08:00
}
2012-11-16 17:08:34 +08:00
free(src);
2012-11-09 12:08:18 +08:00
abort();
}
return (status == GL_TRUE);
}
GLint GLProgram::getAttribLocation(const std::string &attributeName) const
2014-03-05 05:51:43 +08:00
{
return glGetAttribLocation(_program, attributeName.c_str());
2014-03-05 05:51:43 +08:00
}
GLint GLProgram::getUniformLocation(const std::string &attributeName) const
2014-03-05 05:51:43 +08:00
{
return glGetUniformLocation(_program, attributeName.c_str());
2014-03-05 05:51:43 +08:00
}
void GLProgram::bindAttribLocation(const std::string &attributeName, GLuint index) const
{
glBindAttribLocation(_program, index, attributeName.c_str());
}
void GLProgram::updateUniforms()
{
2014-05-14 09:12:58 +08:00
_builtInUniforms[UNIFORM_P_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_P_MATRIX);
_builtInUniforms[UNIFORM_MV_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MV_MATRIX);
_builtInUniforms[UNIFORM_MVP_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MVP_MATRIX);
2014-04-28 09:51:10 +08:00
2014-05-14 09:12:58 +08:00
_builtInUniforms[UNIFORM_TIME] = glGetUniformLocation(_program, UNIFORM_NAME_TIME);
_builtInUniforms[UNIFORM_SIN_TIME] = glGetUniformLocation(_program, UNIFORM_NAME_SIN_TIME);
_builtInUniforms[UNIFORM_COS_TIME] = glGetUniformLocation(_program, UNIFORM_NAME_COS_TIME);
2014-05-14 09:12:58 +08:00
_builtInUniforms[UNIFORM_RANDOM01] = glGetUniformLocation(_program, UNIFORM_NAME_RANDOM01);
2014-05-14 09:12:58 +08:00
_builtInUniforms[UNIFORM_SAMPLER0] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER0);
_builtInUniforms[UNIFORM_SAMPLER1] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER1);
_builtInUniforms[UNIFORM_SAMPLER2] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER2);
_builtInUniforms[UNIFORM_SAMPLER3] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER3);
2014-05-14 09:12:58 +08:00
_flags.usesP = _builtInUniforms[UNIFORM_P_MATRIX] != -1;
_flags.usesMV = _builtInUniforms[UNIFORM_MV_MATRIX] != -1;
_flags.usesMVP = _builtInUniforms[UNIFORM_MVP_MATRIX] != -1;
2014-04-28 09:51:10 +08:00
_flags.usesTime = (
2014-05-14 09:12:58 +08:00
_builtInUniforms[UNIFORM_TIME] != -1 ||
_builtInUniforms[UNIFORM_SIN_TIME] != -1 ||
_builtInUniforms[UNIFORM_COS_TIME] != -1
);
2014-05-14 09:12:58 +08:00
_flags.usesRandom = _builtInUniforms[UNIFORM_RANDOM01] != -1;
this->use();
2012-11-09 12:08:18 +08:00
2014-05-14 09:12:58 +08:00
// Since sample most probably won't change, set it to 0,1,2,3 now.
if(_builtInUniforms[UNIFORM_SAMPLER0] != -1)
setUniformLocationWith1i(_builtInUniforms[UNIFORM_SAMPLER0], 0);
if(_builtInUniforms[UNIFORM_SAMPLER1] != -1)
setUniformLocationWith1i(_builtInUniforms[UNIFORM_SAMPLER1], 1);
if(_builtInUniforms[UNIFORM_SAMPLER2] != -1)
setUniformLocationWith1i(_builtInUniforms[UNIFORM_SAMPLER2], 2);
if(_builtInUniforms[UNIFORM_SAMPLER3] != -1)
setUniformLocationWith1i(_builtInUniforms[UNIFORM_SAMPLER3], 3);
}
bool GLProgram::link()
{
CCASSERT(_program != 0, "Cannot link invalid program");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
if(!_hasShaderCompiler)
{
// precompiled shader program is already linked
return true;
}
#endif
2012-11-09 12:08:18 +08:00
GLint status = GL_TRUE;
2014-05-09 03:34:26 +08:00
bindPredefinedVertexAttribs();
glLinkProgram(_program);
2014-05-08 03:41:04 +08:00
parseVertexAttribs();
parseUniforms();
if (_vertShader)
2012-11-09 12:08:18 +08:00
{
glDeleteShader(_vertShader);
2012-11-09 12:08:18 +08:00
}
if (_fragShader)
2012-11-09 12:08:18 +08:00
{
glDeleteShader(_fragShader);
2012-11-09 12:08:18 +08:00
}
_vertShader = _fragShader = 0;
2014-04-28 09:51:10 +08:00
#if DEBUG || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
glGetProgramiv(_program, GL_LINK_STATUS, &status);
2014-04-28 09:51:10 +08:00
2012-11-09 12:08:18 +08:00
if (status == GL_FALSE)
{
CCLOG("cocos2d: ERROR: Failed to link program: %i", _program);
GL::deleteProgram(_program);
_program = 0;
2012-11-09 12:08:18 +08:00
}
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
if (status == GL_TRUE)
{
CCPrecompiledShaders::getInstance()->addProgram(_program, _shaderId);
}
#endif
2012-11-09 12:08:18 +08:00
return (status == GL_TRUE);
}
void GLProgram::use()
{
GL::useProgram(_program);
}
std::string GLProgram::logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc) const
{
std::string ret;
GLint logLength = 0, charsWritten = 0;
infoFunc(object, GL_INFO_LOG_LENGTH, &logLength);
if (logLength < 1)
return "";
char *logBytes = (char*)malloc(logLength);
logFunc(object, logLength, &charsWritten, logBytes);
ret = logBytes;
free(logBytes);
return ret;
}
std::string GLProgram::getVertexShaderLog() const
{
return this->logForOpenGLObject(_vertShader, (GLInfoFunction)&glGetShaderiv, (GLLogFunction)&glGetShaderInfoLog);
}
std::string GLProgram::getFragmentShaderLog() const
{
return this->logForOpenGLObject(_fragShader, (GLInfoFunction)&glGetShaderiv, (GLLogFunction)&glGetShaderInfoLog);
}
std::string GLProgram::getProgramLog() const
{
return this->logForOpenGLObject(_program, (GLInfoFunction)&glGetProgramiv, (GLLogFunction)&glGetProgramInfoLog);
}
// Uniform cache
bool GLProgram::updateUniformLocation(GLint location, const GLvoid* data, unsigned int bytes)
{
2012-11-09 12:08:18 +08:00
if (location < 0)
{
return false;
}
bool updated = true;
tHashUniformEntry *element = nullptr;
HASH_FIND_INT(_hashForUniforms, &location, element);
2012-11-09 12:08:18 +08:00
if (! element)
{
element = (tHashUniformEntry*)malloc( sizeof(*element) );
// key
element->location = location;
// value
element->value = malloc( bytes );
memcpy(element->value, data, bytes );
HASH_ADD_INT(_hashForUniforms, location, element);
}
else
{
2012-11-09 12:08:18 +08:00
if (memcmp(element->value, data, bytes) == 0)
{
updated = false;
2012-11-09 12:08:18 +08:00
}
else
2012-11-09 12:08:18 +08:00
{
memcpy(element->value, data, bytes);
}
}
return updated;
}
GLint GLProgram::getUniformLocationForName(const char* name) const
2013-02-27 09:38:30 +08:00
{
CCASSERT(name != nullptr, "Invalid uniform name" );
CCASSERT(_program != 0, "Invalid operation. Cannot get uniform location when program is not initialized");
2013-02-27 09:38:30 +08:00
return glGetUniformLocation(_program, name);
2013-02-27 09:38:30 +08:00
}
void GLProgram::setUniformLocationWith1i(GLint location, GLint i1)
{
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, &i1, sizeof(i1)*1);
if( updated )
{
glUniform1i( (GLint)location, i1);
}
}
void GLProgram::setUniformLocationWith2i(GLint location, GLint i1, GLint i2)
{
GLint ints[2] = {i1,i2};
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, ints, sizeof(ints));
if( updated )
{
glUniform2i( (GLint)location, i1, i2);
}
}
void GLProgram::setUniformLocationWith3i(GLint location, GLint i1, GLint i2, GLint i3)
{
GLint ints[3] = {i1,i2,i3};
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, ints, sizeof(ints));
if( updated )
{
glUniform3i( (GLint)location, i1, i2, i3);
}
}
void GLProgram::setUniformLocationWith4i(GLint location, GLint i1, GLint i2, GLint i3, GLint i4)
{
GLint ints[4] = {i1,i2,i3,i4};
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, ints, sizeof(ints));
if( updated )
{
glUniform4i( (GLint)location, i1, i2, i3, i4);
}
}
void GLProgram::setUniformLocationWith2iv(GLint location, GLint* ints, unsigned int numberOfArrays)
{
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, ints, sizeof(int)*2*numberOfArrays);
if( updated )
{
glUniform2iv( (GLint)location, (GLsizei)numberOfArrays, ints );
}
}
void GLProgram::setUniformLocationWith3iv(GLint location, GLint* ints, unsigned int numberOfArrays)
{
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, ints, sizeof(int)*3*numberOfArrays);
if( updated )
{
glUniform3iv( (GLint)location, (GLsizei)numberOfArrays, ints );
}
}
void GLProgram::setUniformLocationWith4iv(GLint location, GLint* ints, unsigned int numberOfArrays)
{
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, ints, sizeof(int)*4*numberOfArrays);
if( updated )
{
glUniform4iv( (GLint)location, (GLsizei)numberOfArrays, ints );
}
}
void GLProgram::setUniformLocationWith1f(GLint location, GLfloat f1)
{
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, &f1, sizeof(f1)*1);
if( updated )
{
glUniform1f( (GLint)location, f1);
}
}
void GLProgram::setUniformLocationWith2f(GLint location, GLfloat f1, GLfloat f2)
{
GLfloat floats[2] = {f1,f2};
bool updated = updateUniformLocation(location, floats, sizeof(floats));
if( updated )
{
glUniform2f( (GLint)location, f1, f2);
}
}
void GLProgram::setUniformLocationWith3f(GLint location, GLfloat f1, GLfloat f2, GLfloat f3)
{
GLfloat floats[3] = {f1,f2,f3};
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, floats, sizeof(floats));
if( updated )
{
glUniform3f( (GLint)location, f1, f2, f3);
}
}
void GLProgram::setUniformLocationWith4f(GLint location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4)
{
GLfloat floats[4] = {f1,f2,f3,f4};
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, floats, sizeof(floats));
if( updated )
{
glUniform4f( (GLint)location, f1, f2, f3,f4);
}
}
void GLProgram::setUniformLocationWith2fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays)
{
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, floats, sizeof(float)*2*numberOfArrays);
if( updated )
{
glUniform2fv( (GLint)location, (GLsizei)numberOfArrays, floats );
}
}
void GLProgram::setUniformLocationWith3fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays)
{
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, floats, sizeof(float)*3*numberOfArrays);
if( updated )
{
glUniform3fv( (GLint)location, (GLsizei)numberOfArrays, floats );
}
}
void GLProgram::setUniformLocationWith4fv(GLint location, const GLfloat* floats, unsigned int numberOfArrays)
{
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, floats, sizeof(float)*4*numberOfArrays);
if( updated )
{
glUniform4fv( (GLint)location, (GLsizei)numberOfArrays, floats );
}
}
void GLProgram::setUniformLocationWithMatrix2fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices) {
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, matrixArray, sizeof(float)*4*numberOfMatrices);
if( updated )
{
glUniformMatrix2fv( (GLint)location, (GLsizei)numberOfMatrices, GL_FALSE, matrixArray);
}
}
void GLProgram::setUniformLocationWithMatrix3fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices) {
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, matrixArray, sizeof(float)*9*numberOfMatrices);
if( updated )
{
glUniformMatrix3fv( (GLint)location, (GLsizei)numberOfMatrices, GL_FALSE, matrixArray);
}
}
void GLProgram::setUniformLocationWithMatrix4fv(GLint location, const GLfloat* matrixArray, unsigned int numberOfMatrices)
{
2014-05-13 14:51:37 +08:00
bool updated = updateUniformLocation(location, matrixArray, sizeof(float)*16*numberOfMatrices);
if( updated )
{
glUniformMatrix4fv( (GLint)location, (GLsizei)numberOfMatrices, GL_FALSE, matrixArray);
}
}
void GLProgram::setUniformsForBuiltins()
{
Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
2014-04-28 09:51:10 +08:00
Matrix matrixMV;
2014-04-04 18:13:10 +08:00
matrixMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
setUniformsForBuiltins(matrixMV);
}
void GLProgram::setUniformsForBuiltins(const Matrix &matrixMV)
{
Matrix matrixP = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2013-12-06 11:04:01 +08:00
if(_flags.usesP)
2014-05-14 09:12:58 +08:00
setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_P_MATRIX], matrixP.m, 1);
2013-12-06 11:04:01 +08:00
if(_flags.usesMV)
2014-05-14 09:12:58 +08:00
setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_MV_MATRIX], matrixMV.m, 1);
2013-12-06 11:04:01 +08:00
if(_flags.usesMVP) {
Matrix matrixMVP = matrixP * matrixMV;
2014-05-14 09:12:58 +08:00
setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_MVP_MATRIX], matrixMVP.m, 1);
}
2014-04-28 09:51:10 +08:00
if(_flags.usesTime) {
Director *director = Director::getInstance();
// This doesn't give the most accurate global time value.
// Cocos2D doesn't store a high precision time value, so this will have to do.
// Getting Mach time per frame per shader using time could be extremely expensive.
2012-11-09 12:08:18 +08:00
float time = director->getTotalFrames() * director->getAnimationInterval();
2014-04-28 09:51:10 +08:00
2014-05-14 09:12:58 +08:00
setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_TIME], time/10.0, time, time*2, time*4);
setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_SIN_TIME], time/8.0, time/4.0, time/2.0, sinf(time));
setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_COS_TIME], time/8.0, time/4.0, time/2.0, cosf(time));
2014-04-28 09:51:10 +08:00
}
if(_flags.usesRandom)
2014-05-14 09:12:58 +08:00
setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_RANDOM01], CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());
2012-11-09 12:08:18 +08:00
}
void GLProgram::reset()
{
_vertShader = _fragShader = 0;
2014-05-14 09:12:58 +08:00
memset(_builtInUniforms, 0, sizeof(_builtInUniforms));
// it is already deallocated by android
//GL::deleteProgram(_program);
_program = 0;
tHashUniformEntry *current_element, *tmp;
// Purge uniform hash
HASH_ITER(hh, _hashForUniforms, current_element, tmp)
{
HASH_DEL(_hashForUniforms, current_element);
free(current_element->value);
free(current_element);
}
_hashForUniforms = nullptr;
}
NS_CC_END