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
|
|
|
|
|
|
|
#include "CCShaderCache.h"
|
|
|
|
#include "CCGLProgram.h"
|
|
|
|
#include "ccMacros.h"
|
2012-03-16 13:42:53 +08:00
|
|
|
#include "ccShaders.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-05 16:51:16 +08:00
|
|
|
kShaderType_LabelOutline,
|
2013-06-20 14:13:12 +08:00
|
|
|
kShaderType_MAX,
|
2012-04-17 17:55:26 +08:00
|
|
|
};
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static ShaderCache *_sharedShaderCache = 0;
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
ShaderCache* ShaderCache::getInstance()
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (!_sharedShaderCache) {
|
2013-06-20 14:13:12 +08:00
|
|
|
_sharedShaderCache = new ShaderCache();
|
2012-04-19 14:35:52 +08:00
|
|
|
if (!_sharedShaderCache->init())
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_sharedShaderCache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _sharedShaderCache;
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
void ShaderCache::destroyInstance()
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_sharedShaderCache);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
// XXX: deprecated
|
|
|
|
ShaderCache* ShaderCache::sharedShaderCache()
|
|
|
|
{
|
|
|
|
return ShaderCache::getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: deprecated
|
|
|
|
void ShaderCache::purgeSharedShaderCache()
|
|
|
|
{
|
|
|
|
ShaderCache::destroyInstance();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ShaderCache::ShaderCache()
|
2013-09-07 13:55:11 +08:00
|
|
|
: _programs()
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ShaderCache::~ShaderCache()
|
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();
|
|
|
|
}
|
|
|
|
|
2013-08-22 11:12:09 +08:00
|
|
|
CCLOGINFO("deallocing ShaderCache: %p", this);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ShaderCache::init()
|
2013-09-07 13:55:11 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
loadDefaultShaders();
|
|
|
|
return true;
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ShaderCache::loadDefaultShaders()
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// Position Texture Color shader
|
2013-06-20 14:13:12 +08:00
|
|
|
GLProgram *p = new GLProgram();
|
|
|
|
loadDefaultShader(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
|
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(p, kShaderType_PositionTextureColor_noMVP);
|
|
|
|
_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
|
2013-06-20 14:13:12 +08:00
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(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
|
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(p, kShaderType_PositionTextureColorAlphaTestNoMV);
|
|
|
|
_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
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(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
|
|
|
|
//
|
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(p, kShaderType_PositionColor_noMVP);
|
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP, p) );
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture shader
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(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
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(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
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(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
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(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
|
|
|
|
|
|
|
//
|
|
|
|
// Position, Legth(TexCoords, Color (used by Draw Node basically )
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(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
|
|
|
|
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(p, kShaderType_LabelDistanceFieldNormal);
|
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL, p) );
|
|
|
|
|
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(p, kShaderType_LabelDistanceFieldGlow);
|
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW, p) );
|
|
|
|
|
|
|
|
p = new GLProgram();
|
2014-03-05 16:51:16 +08:00
|
|
|
loadDefaultShader(p, kShaderType_LabelOutline);
|
|
|
|
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_LABEL_OUTLINE, p) );
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ShaderCache::reloadDefaultShaders()
|
2012-04-17 17:55:26 +08:00
|
|
|
{
|
|
|
|
// reset all programs and reload them
|
|
|
|
|
|
|
|
// Position Texture Color shader
|
2013-09-16 20:38:03 +08:00
|
|
|
GLProgram *p = getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2013-06-20 14:13:12 +08:00
|
|
|
loadDefaultShader(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
|
|
|
|
p = getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP);
|
|
|
|
p->reset();
|
|
|
|
loadDefaultShader(p, kShaderType_PositionTextureColor_noMVP);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Position Texture Color alpha test
|
2013-09-16 20:38:03 +08:00
|
|
|
p = getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2013-06-20 14:13:12 +08:00
|
|
|
loadDefaultShader(p, kShaderType_PositionTextureColorAlphaTest);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2014-03-10 15:12:44 +08:00
|
|
|
// Position Texture Color alpha test
|
|
|
|
p = getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
|
|
|
|
p->reset();
|
|
|
|
loadDefaultShader(p, kShaderType_PositionTextureColorAlphaTestNoMV);
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position, Color shader
|
|
|
|
//
|
2013-09-16 20:38:03 +08:00
|
|
|
p = getProgram(GLProgram::SHADER_NAME_POSITION_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2013-06-20 14:13:12 +08:00
|
|
|
loadDefaultShader(p, kShaderType_PositionColor);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2014-01-07 22:08:00 +08:00
|
|
|
//
|
|
|
|
// Position, Color shader no MVP
|
|
|
|
//
|
|
|
|
p = getProgram(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP);
|
|
|
|
loadDefaultShader(p, kShaderType_PositionColor_noMVP);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture shader
|
|
|
|
//
|
2013-09-16 20:38:03 +08:00
|
|
|
p = getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2013-06-20 14:13:12 +08:00
|
|
|
loadDefaultShader(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
|
|
|
|
//
|
2013-09-16 20:38:03 +08:00
|
|
|
p = getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_U_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2013-06-20 14:13:12 +08:00
|
|
|
loadDefaultShader(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
|
|
|
|
//
|
2013-09-16 20:38:03 +08:00
|
|
|
p = getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2013-06-20 14:13:12 +08:00
|
|
|
loadDefaultShader(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
|
|
|
//
|
2013-09-16 20:38:03 +08:00
|
|
|
p = getProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2013-06-20 14:13:12 +08:00
|
|
|
loadDefaultShader(p, kShaderType_Position_uColor);
|
2012-11-13 11:06:32 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Position, Legth(TexCoords, Color (used by Draw Node basically )
|
|
|
|
//
|
2013-09-16 20:38:03 +08:00
|
|
|
p = getProgram(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR);
|
2012-11-13 11:06:32 +08:00
|
|
|
p->reset();
|
2013-07-17 12:55:27 +08:00
|
|
|
loadDefaultShader(p, kShaderType_PositionLengthTexureColor);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
|
|
|
p = getProgram(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL);
|
|
|
|
p->reset();
|
|
|
|
loadDefaultShader(p, kShaderType_LabelDistanceFieldNormal);
|
|
|
|
|
|
|
|
p = getProgram(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW);
|
|
|
|
p->reset();
|
|
|
|
loadDefaultShader(p, kShaderType_LabelDistanceFieldGlow);
|
|
|
|
|
2014-03-05 16:51:16 +08:00
|
|
|
p = getProgram(GLProgram::SHADER_NAME_LABEL_OUTLINE);
|
2013-12-13 12:42:15 +08:00
|
|
|
p->reset();
|
2014-03-05 16:51:16 +08:00
|
|
|
loadDefaultShader(p, kShaderType_LabelOutline);
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ShaderCache::loadDefaultShader(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
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
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
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
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
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
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);
|
|
|
|
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
|
|
|
|
|
|
|
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);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
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);
|
2014-01-07 22:08:00 +08:00
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
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
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
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
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
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
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
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);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
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);
|
2012-11-13 11:06:32 +08:00
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
2012-11-13 11:06:32 +08:00
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
break;
|
|
|
|
case kShaderType_LabelDistanceFieldNormal:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccLabelDistanceFieldNormal_vert, ccLabelDistanceFieldNormal_frag);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
case kShaderType_LabelDistanceFieldGlow:
|
2014-03-05 05:51:43 +08:00
|
|
|
p->initWithByteArrays(ccLabelDistanceFieldGlow_vert, ccLabelDistanceFieldGlow_frag);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
|
|
|
break;
|
2014-03-05 16:51:16 +08:00
|
|
|
case kShaderType_LabelOutline:
|
|
|
|
p->initWithByteArrays(ccLabelOutline_vert, ccLabelOutline_frag);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2012-04-17 17:55:26 +08:00
|
|
|
break;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
GLProgram* ShaderCache::getProgram(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
|
|
|
}
|
|
|
|
|
2013-09-07 13:55:11 +08:00
|
|
|
void ShaderCache::addProgram(GLProgram* program, const std::string &key)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2013-09-07 13:55:11 +08:00
|
|
|
program->retain();
|
|
|
|
_programs.insert( std::make_pair( key, program) );
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|