2012-03-16 13:42:53 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-03-16 13:42:53 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-03-16 13:42:53 +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
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "renderer/CCGLProgramCache.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-09-25 15:29:32 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-04-17 17:55:26 +08:00
|
|
|
enum {
|
2013-06-20 14:13:12 +08:00
|
|
|
kShaderType_PositionTextureColor,
|
2013-12-06 13:24:59 +08:00
|
|
|
kShaderType_PositionTextureColor_noMVP,
|
2013-06-20 14:13:12 +08:00
|
|
|
kShaderType_PositionTextureColorAlphaTest,
|
2014-03-10 15:12:44 +08:00
|
|
|
kShaderType_PositionTextureColorAlphaTestNoMV,
|
2013-06-20 14:13:12 +08:00
|
|
|
kShaderType_PositionColor,
|
2014-01-07 22:08:00 +08:00
|
|
|
kShaderType_PositionColor_noMVP,
|
2013-06-20 14:13:12 +08:00
|
|
|
kShaderType_PositionTexture,
|
|
|
|
kShaderType_PositionTexture_uColor,
|
|
|
|
kShaderType_PositionTextureA8Color,
|
|
|
|
kShaderType_Position_uColor,
|
|
|
|
kShaderType_PositionLengthTexureColor,
|
2013-12-13 12:42:15 +08:00
|
|
|
kShaderType_LabelDistanceFieldNormal,
|
|
|
|
kShaderType_LabelDistanceFieldGlow,
|
2014-03-20 20:56:10 +08:00
|
|
|
kShaderType_LabelNormal,
|
2014-03-05 16:51:16 +08:00
|
|
|
kShaderType_LabelOutline,
|
2014-05-19 05:49:16 +08:00
|
|
|
kShaderType_3DPosition,
|
|
|
|
kShaderType_3DPositionTex,
|
2014-06-05 16:36:01 +08:00
|
|
|
kShaderType_3DSkinPositionTex,
|
2014-08-21 11:33:00 +08:00
|
|
|
kShaderType_3DPositionNormal,
|
|
|
|
kShaderType_3DPositionNormalTex,
|
|
|
|
kShaderType_3DSkinPositionNormalTex,
|
2013-06-20 14:13:12 +08:00
|
|
|
kShaderType_MAX,
|
2012-04-17 17:55:26 +08:00
|
|
|
};
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
static GLProgramCache *_sharedGLProgramCache = 0;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
GLProgramCache* GLProgramCache::getInstance()
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2014-05-10 09:39:25 +08:00
|
|
|
if (!_sharedGLProgramCache) {
|
2014-08-28 07:31:57 +08:00
|
|
|
_sharedGLProgramCache = new (std::nothrow) GLProgramCache();
|
2014-05-10 09:39:25 +08:00
|
|
|
if (!_sharedGLProgramCache->init())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-10 09:39:25 +08:00
|
|
|
CC_SAFE_DELETE(_sharedGLProgramCache);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-10 09:39:25 +08:00
|
|
|
return _sharedGLProgramCache;
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
void GLProgramCache::destroyInstance()
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2014-05-10 09:39:25 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_sharedGLProgramCache);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME: deprecated
|
2014-05-10 09:39:25 +08:00
|
|
|
GLProgramCache* GLProgramCache::sharedShaderCache()
|
2013-07-12 06:24:23 +08:00
|
|
|
{
|
2014-05-10 09:39:25 +08:00
|
|
|
return GLProgramCache::getInstance();
|
2013-07-12 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME: deprecated
|
2014-05-10 09:39:25 +08:00
|
|
|
void GLProgramCache::purgeSharedShaderCache()
|
2013-07-12 06:24:23 +08:00
|
|
|
{
|
2014-05-10 09:39:25 +08:00
|
|
|
GLProgramCache::destroyInstance();
|
2013-07-12 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
GLProgramCache::GLProgramCache()
|
2013-09-07 13:55:11 +08:00
|
|
|
: _programs()
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
GLProgramCache::~GLProgramCache()
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2013-09-07 13:55:11 +08:00
|
|
|
for( auto it = _programs.begin(); it != _programs.end(); ++it ) {
|
|
|
|
(it->second)->release();
|
|
|
|
}
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
CCLOGINFO("deallocing GLProgramCache: %p", this);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
bool GLProgramCache::init()
|
2013-09-07 13:55:11 +08:00
|
|
|
{
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLPrograms();
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
void GLProgramCache::loadDefaultGLPrograms()
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// Position Texture Color shader
|
2014-08-28 07:31:57 +08:00
|
|
|
GLProgram *p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTextureColor);
|
2013-09-07 13:55:11 +08:00
|
|
|
_programs.insert( std::make_pair( GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, p ) );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-12-06 13:24:59 +08:00
|
|
|
// Position Texture Color without MVP shader
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTextureColor_noMVP);
|
2013-12-06 13:24:59 +08:00
|
|
|
_programs.insert( std::make_pair( GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP, p ) );
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Position Texture Color alpha test
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTextureColorAlphaTest);
|
2013-09-07 13:55:11 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST, p) );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2014-03-10 15:12:44 +08:00
|
|
|
// Position Texture Color alpha test
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTextureColorAlphaTestNoMV);
|
2014-03-10 15:12:44 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV, p) );
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position, Color shader
|
|
|
|
//
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionColor);
|
2013-09-07 13:55:11 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_COLOR, p) );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2014-01-07 22:08:00 +08:00
|
|
|
//
|
|
|
|
// Position, Color shader no MVP
|
|
|
|
//
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionColor_noMVP);
|
2014-01-07 22:08:00 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP, p) );
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture shader
|
|
|
|
//
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTexture);
|
2013-09-07 13:55:11 +08:00
|
|
|
_programs.insert( std::make_pair( GLProgram::SHADER_NAME_POSITION_TEXTURE, p) );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position, Texture attribs, 1 Color as uniform shader
|
|
|
|
//
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTexture_uColor);
|
2013-09-07 13:55:11 +08:00
|
|
|
_programs.insert( std::make_pair( GLProgram::SHADER_NAME_POSITION_TEXTURE_U_COLOR, p) );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture A8 Color shader
|
|
|
|
//
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTextureA8Color);
|
2013-09-07 13:55:11 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR, p) );
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
2012-09-17 15:02:24 +08:00
|
|
|
// Position and 1 color passed as a uniform (to simulate glColor4ub )
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_Position_uColor);
|
2013-09-07 13:55:11 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_U_COLOR, p) );
|
2012-11-13 11:06:32 +08:00
|
|
|
|
|
|
|
//
|
2014-09-23 17:08:28 +08:00
|
|
|
// Position, Legth(TexCoords, Color (used by Draw Node basically )
|
|
|
|
//
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionLengthTexureColor);
|
2013-09-07 13:55:11 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR, p) );
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-10-29 11:57:40 +08:00
|
|
|
#if CC_TARGET_PLATFORM != CC_PLATFORM_WP8
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_LabelDistanceFieldNormal);
|
2013-12-13 12:42:15 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL, p) );
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_LabelDistanceFieldGlow);
|
2013-12-13 12:42:15 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW, p) );
|
2014-10-29 11:57:40 +08:00
|
|
|
#endif
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_LabelNormal);
|
2014-03-20 20:56:10 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_LABEL_NORMAL, p) );
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_LabelOutline);
|
2014-03-05 16:51:16 +08:00
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_LABEL_OUTLINE, p) );
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-19 05:49:16 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_3DPosition);
|
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_3D_POSITION, p) );
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-05-19 05:49:16 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_3DPositionTex);
|
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_3D_POSITION_TEXTURE, p) );
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
p = new (std::nothrow) GLProgram();
|
2014-06-05 16:36:01 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_3DSkinPositionTex);
|
|
|
|
_programs.insert(std::make_pair(GLProgram::SHADER_3D_SKINPOSITION_TEXTURE, p));
|
2014-08-21 10:33:16 +08:00
|
|
|
|
|
|
|
p = new GLProgram();
|
2014-08-21 11:33:00 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_3DPositionNormal);
|
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_3D_POSITION_NORMAL, p) );
|
2014-08-21 10:33:16 +08:00
|
|
|
|
|
|
|
p = new GLProgram();
|
2014-08-21 11:33:00 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_3DPositionNormalTex);
|
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_3D_POSITION_NORMAL_TEXTURE, p) );
|
2014-08-21 10:33:16 +08:00
|
|
|
|
|
|
|
p = new GLProgram();
|
2014-08-21 11:33:00 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_3DSkinPositionNormalTex);
|
|
|
|
_programs.insert(std::make_pair(GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE, p));
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
void GLProgramCache::reloadDefaultGLPrograms()
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
|
|
|
// reset all programs and reload them
|
|
|
|
|
|
|
|
// Position Texture Color shader
|
2014-05-10 09:39:25 +08:00
|
|
|
GLProgram *p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTextureColor);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-12-27 11:56:56 +08:00
|
|
|
// Position Texture Color without MVP shader
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP);
|
2013-12-27 11:56:56 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTextureColor_noMVP);
|
2013-12-27 11:56:56 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Position Texture Color alpha test
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTextureColorAlphaTest);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2014-03-10 15:12:44 +08:00
|
|
|
// Position Texture Color alpha test
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
|
2014-03-10 15:12:44 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTextureColorAlphaTestNoMV);
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position, Color shader
|
|
|
|
//
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionColor);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2014-01-07 22:08:00 +08:00
|
|
|
//
|
|
|
|
// Position, Color shader no MVP
|
|
|
|
//
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP);
|
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionColor_noMVP);
|
2014-01-07 22:08:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture shader
|
|
|
|
//
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTexture);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position, Texture attribs, 1 Color as uniform shader
|
|
|
|
//
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_U_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTexture_uColor);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture A8 Color shader
|
|
|
|
//
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionTextureA8Color);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
2012-09-17 15:02:24 +08:00
|
|
|
// Position and 1 color passed as a uniform (to simulate glColor4ub )
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_Position_uColor);
|
2012-11-13 11:06:32 +08:00
|
|
|
|
|
|
|
//
|
2014-08-21 10:33:16 +08:00
|
|
|
// Position, Legth(TexCoords, Color (used by Draw Node basically )
|
|
|
|
//
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR);
|
2012-11-13 11:06:32 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_PositionLengthTexureColor);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-10-29 11:57:40 +08:00
|
|
|
#if CC_TARGET_PLATFORM != CC_PLATFORM_WP8
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL);
|
2013-12-13 12:42:15 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_LabelDistanceFieldNormal);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW);
|
2013-12-13 12:42:15 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_LabelDistanceFieldGlow);
|
2014-10-29 11:57:40 +08:00
|
|
|
#endif
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_LABEL_NORMAL);
|
2014-03-20 20:56:10 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_LabelNormal);
|
2014-03-20 20:56:10 +08:00
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_NAME_LABEL_OUTLINE);
|
2013-12-13 12:42:15 +08:00
|
|
|
p->reset();
|
2014-05-10 09:39:25 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_LabelOutline);
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
p = getGLProgram(GLProgram::SHADER_3D_POSITION);
|
|
|
|
p->reset();
|
|
|
|
loadDefaultGLProgram(p, kShaderType_3DPosition);
|
|
|
|
|
|
|
|
p = getGLProgram(GLProgram::SHADER_3D_POSITION_TEXTURE);
|
|
|
|
p->reset();
|
|
|
|
loadDefaultGLProgram(p, kShaderType_3DPositionTex);
|
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_TEXTURE);
|
|
|
|
p->reset();
|
|
|
|
loadDefaultGLProgram(p, kShaderType_3DSkinPositionTex);
|
2014-08-21 10:33:16 +08:00
|
|
|
|
2014-08-21 11:33:00 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_3D_POSITION_NORMAL);
|
2014-08-21 10:33:16 +08:00
|
|
|
p->reset();
|
2014-08-21 11:33:00 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_3DPositionNormal);
|
2014-08-21 10:33:16 +08:00
|
|
|
|
2014-08-21 11:33:00 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_3D_POSITION_NORMAL_TEXTURE);
|
2014-08-21 10:33:16 +08:00
|
|
|
p->reset();
|
2014-08-21 11:33:00 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_3DPositionNormalTex);
|
2014-08-21 10:33:16 +08:00
|
|
|
|
2014-08-21 11:33:00 +08:00
|
|
|
p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE);
|
2014-08-21 10:33:16 +08:00
|
|
|
p->reset();
|
2014-08-21 11:33:00 +08:00
|
|
|
loadDefaultGLProgram(p, kShaderType_3DSkinPositionNormalTex);
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type)
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
|
|
|
switch (type) {
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionTextureColor:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColor_frag);
|
2012-04-17 17:55:26 +08:00
|
|
|
break;
|
2013-12-06 13:24:59 +08:00
|
|
|
case kShaderType_PositionTextureColor_noMVP:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColor_noMVP_frag);
|
2013-12-06 13:24:59 +08:00
|
|
|
break;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionTextureColorAlphaTest:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag);
|
2012-04-17 17:55:26 +08:00
|
|
|
break;
|
2014-03-10 15:12:44 +08:00
|
|
|
case kShaderType_PositionTextureColorAlphaTestNoMV:
|
|
|
|
p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColorAlphaTest_frag);
|
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionColor:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccPositionColor_vert ,ccPositionColor_frag);
|
2014-01-07 22:08:00 +08:00
|
|
|
break;
|
|
|
|
case kShaderType_PositionColor_noMVP:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag);
|
2012-04-17 17:55:26 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionTexture:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccPositionTexture_vert ,ccPositionTexture_frag);
|
2012-04-17 17:55:26 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionTexture_uColor:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag);
|
2012-04-17 17:55:26 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionTextureA8Color:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag);
|
2012-04-17 17:55:26 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_Position_uColor:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccPosition_uColor_vert, ccPosition_uColor_frag);
|
|
|
|
p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION);
|
2012-11-13 11:06:32 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionLengthTexureColor:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag);
|
2013-12-13 12:42:15 +08:00
|
|
|
break;
|
2014-10-29 11:57:40 +08:00
|
|
|
#if CC_TARGET_PLATFORM != CC_PLATFORM_WP8
|
2013-12-13 12:42:15 +08:00
|
|
|
case kShaderType_LabelDistanceFieldNormal:
|
2014-03-20 20:56:10 +08:00
|
|
|
p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldNormal_frag);
|
2013-12-13 12:42:15 +08:00
|
|
|
break;
|
|
|
|
case kShaderType_LabelDistanceFieldGlow:
|
2014-03-20 20:56:10 +08:00
|
|
|
p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldGlow_frag);
|
|
|
|
break;
|
2014-10-29 11:57:40 +08:00
|
|
|
#endif
|
2014-03-20 20:56:10 +08:00
|
|
|
case kShaderType_LabelNormal:
|
|
|
|
p->initWithByteArrays(ccLabel_vert, ccLabelNormal_frag);
|
2013-12-13 12:42:15 +08:00
|
|
|
break;
|
2014-03-05 16:51:16 +08:00
|
|
|
case kShaderType_LabelOutline:
|
2014-03-20 20:56:10 +08:00
|
|
|
p->initWithByteArrays(ccLabel_vert, ccLabelOutline_frag);
|
2012-04-17 17:55:26 +08:00
|
|
|
break;
|
2014-05-19 05:49:16 +08:00
|
|
|
case kShaderType_3DPosition:
|
|
|
|
p->initWithByteArrays(cc3D_PositionTex_vert, cc3D_Color_frag);
|
|
|
|
break;
|
|
|
|
case kShaderType_3DPositionTex:
|
|
|
|
p->initWithByteArrays(cc3D_PositionTex_vert, cc3D_ColorTex_frag);
|
|
|
|
break;
|
2014-06-05 16:36:01 +08:00
|
|
|
case kShaderType_3DSkinPositionTex:
|
|
|
|
p->initWithByteArrays(cc3D_SkinPositionTex_vert, cc3D_ColorTex_frag);
|
|
|
|
break;
|
2014-08-21 11:33:00 +08:00
|
|
|
case kShaderType_3DPositionNormal:
|
2014-09-23 17:08:28 +08:00
|
|
|
{
|
2014-09-25 15:29:32 +08:00
|
|
|
std::string def = getShaderMacrosForLight();
|
|
|
|
p->initWithByteArrays((def + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormal_frag)).c_str());
|
2014-09-23 17:08:28 +08:00
|
|
|
}
|
2014-08-21 10:33:16 +08:00
|
|
|
break;
|
2014-08-21 11:33:00 +08:00
|
|
|
case kShaderType_3DPositionNormalTex:
|
2014-09-23 17:08:28 +08:00
|
|
|
{
|
2014-09-25 15:29:32 +08:00
|
|
|
std::string def = getShaderMacrosForLight();
|
|
|
|
p->initWithByteArrays((def + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str());
|
2014-09-23 17:08:28 +08:00
|
|
|
}
|
2014-08-21 10:33:16 +08:00
|
|
|
break;
|
2014-08-21 11:33:00 +08:00
|
|
|
case kShaderType_3DSkinPositionNormalTex:
|
2014-09-23 17:08:28 +08:00
|
|
|
{
|
2014-09-25 15:29:32 +08:00
|
|
|
std::string def = getShaderMacrosForLight();
|
|
|
|
p->initWithByteArrays((def + std::string(cc3D_SkinPositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str());
|
2014-09-23 17:08:28 +08:00
|
|
|
}
|
2014-08-21 10:33:16 +08:00
|
|
|
break;
|
2012-04-17 17:55:26 +08:00
|
|
|
default:
|
|
|
|
CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
GLProgram* GLProgramCache::getGLProgram(const std::string &key)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2013-09-07 13:55:11 +08:00
|
|
|
auto it = _programs.find(key);
|
|
|
|
if( it != _programs.end() )
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
void GLProgramCache::addGLProgram(GLProgram* program, const std::string &key)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2014-10-27 09:59:47 +08:00
|
|
|
// release old one
|
2014-11-07 04:25:55 +08:00
|
|
|
auto prev = getGLProgram(key);
|
2014-10-27 09:59:47 +08:00
|
|
|
if( prev == program )
|
2014-10-27 10:02:28 +08:00
|
|
|
return;
|
2014-10-24 18:31:00 +08:00
|
|
|
|
2014-10-27 09:59:47 +08:00
|
|
|
_programs.erase(key);
|
|
|
|
CC_SAFE_RELEASE_NULL(prev);
|
2014-10-24 18:31:00 +08:00
|
|
|
|
2014-06-23 01:53:01 +08:00
|
|
|
if (program)
|
2014-11-07 04:25:55 +08:00
|
|
|
program->retain();
|
2014-06-23 01:53:01 +08:00
|
|
|
_programs[key] = program;
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2014-09-25 15:29:32 +08:00
|
|
|
std::string GLProgramCache::getShaderMacrosForLight() const
|
|
|
|
{
|
|
|
|
GLchar def[256];
|
2014-11-07 04:25:55 +08:00
|
|
|
auto conf = Configuration::getInstance();
|
|
|
|
|
|
|
|
snprintf(def, sizeof(def)-1, "\n#define MAX_DIRECTIONAL_LIGHT_NUM %d \n"
|
2014-09-25 15:29:32 +08:00
|
|
|
"\n#define MAX_POINT_LIGHT_NUM %d \n"
|
2014-11-07 04:25:55 +08:00
|
|
|
"\n#define MAX_SPOT_LIGHT_NUM %d \n",
|
|
|
|
conf->getMaxSupportDirLightInShader(),
|
|
|
|
conf->getMaxSupportPointLightInShader(),
|
|
|
|
conf->getMaxSupportSpotLightInShader());
|
2014-09-25 15:29:32 +08:00
|
|
|
return std::string(def);
|
|
|
|
}
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
NS_CC_END
|