2012-03-16 13:42:53 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-03-16 13:42:53 +08:00
|
|
|
Copyright (c) 2011 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga 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.
|
|
|
|
****************************************************************************/
|
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,
|
|
|
|
kShaderType_PositionTextureColorAlphaTest,
|
|
|
|
kShaderType_PositionColor,
|
|
|
|
kShaderType_PositionTexture,
|
|
|
|
kShaderType_PositionTexture_uColor,
|
|
|
|
kShaderType_PositionTextureA8Color,
|
|
|
|
kShaderType_Position_uColor,
|
|
|
|
kShaderType_PositionLengthTexureColor,
|
2012-04-17 17:55:26 +08:00
|
|
|
|
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-06-15 14:03:30 +08:00
|
|
|
: _programs(0)
|
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-06-06 09:21:05 +08:00
|
|
|
CCLOGINFO("cocos2d deallocing %p", this);
|
2013-06-15 14:03:30 +08:00
|
|
|
_programs->release();
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ShaderCache::init()
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_programs = new Dictionary();
|
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);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_programs->setObject(p, kShader_PositionTextureColor);
|
2012-04-19 14:35:52 +08:00
|
|
|
p->release();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
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);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_programs->setObject(p, kShader_PositionTextureColorAlphaTest);
|
2012-04-19 14:35:52 +08:00
|
|
|
p->release();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
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);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_programs->setObject(p, kShader_PositionColor);
|
2012-04-19 14:35:52 +08:00
|
|
|
p->release();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
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);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_programs->setObject(p, kShader_PositionTexture);
|
2012-04-19 14:35:52 +08:00
|
|
|
p->release();
|
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);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_programs->setObject(p ,kShader_PositionTexture_uColor);
|
2012-04-19 14:35:52 +08:00
|
|
|
p->release();
|
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);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_programs->setObject(p, kShader_PositionTextureA8Color);
|
2012-04-19 14:35:52 +08:00
|
|
|
p->release();
|
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);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_programs->setObject(p, kShader_Position_uColor);
|
2012-11-13 11:06:32 +08:00
|
|
|
p->release();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Position, Legth(TexCoords, Color (used by Draw Node basically )
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
p = new GLProgram();
|
|
|
|
loadDefaultShader(p, kShaderType_PositionLengthTexureColor);
|
2012-11-13 11:06:32 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_programs->setObject(p, kShader_PositionLengthTexureColor);
|
2012-11-13 11:06:32 +08:00
|
|
|
p->release();
|
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-06-20 14:13:12 +08:00
|
|
|
GLProgram *p = programForKey(kShader_PositionTextureColor);
|
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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Position Texture Color alpha test
|
2013-06-20 14:13:12 +08:00
|
|
|
p = programForKey(kShader_PositionTextureColorAlphaTest);
|
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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position, Color shader
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
p = programForKey(kShader_PositionColor);
|
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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture shader
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
p = programForKey(kShader_PositionTexture);
|
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-06-20 14:13:12 +08:00
|
|
|
p = programForKey(kShader_PositionTexture_uColor);
|
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-06-20 14:13:12 +08:00
|
|
|
p = programForKey(kShader_PositionTextureA8Color);
|
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-06-20 14:13:12 +08:00
|
|
|
p = programForKey(kShader_Position_uColor);
|
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-06-20 14:13:12 +08:00
|
|
|
p = programForKey(kShader_PositionLengthTexureColor);
|
2012-11-13 11:06:32 +08:00
|
|
|
p->reset();
|
2013-06-20 14:13:12 +08:00
|
|
|
loadDefaultShader(p, kShaderType_Position_uColor);
|
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:
|
2012-04-17 17:55:26 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColor_frag);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position);
|
|
|
|
p->addAttribute(kAttributeNameColor, kVertexAttrib_Color);
|
|
|
|
p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionTextureColorAlphaTest:
|
2012-04-17 17:55:26 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position);
|
|
|
|
p->addAttribute(kAttributeNameColor, kVertexAttrib_Color);
|
|
|
|
p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionColor:
|
2012-04-17 17:55:26 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionColor_vert ,ccPositionColor_frag);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position);
|
|
|
|
p->addAttribute(kAttributeNameColor, kVertexAttrib_Color);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionTexture:
|
2012-04-17 17:55:26 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTexture_vert ,ccPositionTexture_frag);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position);
|
|
|
|
p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionTexture_uColor:
|
2012-04-17 17:55:26 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position);
|
|
|
|
p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_PositionTextureA8Color:
|
2012-04-17 17:55:26 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position);
|
|
|
|
p->addAttribute(kAttributeNameColor, kVertexAttrib_Color);
|
|
|
|
p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case kShaderType_Position_uColor:
|
2012-04-19 14:35:52 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPosition_uColor_vert, ccPosition_uColor_frag);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
p->addAttribute("aVertex", kVertexAttrib_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:
|
2012-11-13 11:06:32 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
p->addAttribute(kAttributeNamePosition, kVertexAttrib_Position);
|
|
|
|
p->addAttribute(kAttributeNameTexCoord, kVertexAttrib_TexCoords);
|
|
|
|
p->addAttribute(kAttributeNameColor, kVertexAttrib_Color);
|
2012-11-13 11:06:32 +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-06-20 14:13:12 +08:00
|
|
|
GLProgram* ShaderCache::programForKey(const char* key)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return (GLProgram*)_programs->objectForKey(key);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ShaderCache::addProgram(GLProgram* program, const char* key)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_programs->setObject(program, key);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|