2012-03-16 13:42:53 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
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
|
|
|
|
|
|
|
|
static CCShaderCache *_sharedShaderCache = 0;
|
|
|
|
|
|
|
|
CCShaderCache* CCShaderCache::sharedShaderCache()
|
|
|
|
{
|
|
|
|
if (!_sharedShaderCache) {
|
|
|
|
_sharedShaderCache = new CCShaderCache();
|
|
|
|
if (!_sharedShaderCache->init())
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_sharedShaderCache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _sharedShaderCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCShaderCache::purgeSharedShaderCache()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_sharedShaderCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCShaderCache::CCShaderCache()
|
2012-03-16 17:56:19 +08:00
|
|
|
: m_pPrograms(0)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CCShaderCache::~CCShaderCache()
|
|
|
|
{
|
|
|
|
CCLOGINFO("cocos2d deallocing 0x%X", this);
|
2012-03-16 17:56:19 +08:00
|
|
|
m_pPrograms->release();
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCShaderCache::init()
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
m_pPrograms = new CCDictionary();
|
2012-03-12 15:22:03 +08:00
|
|
|
loadDefaultShaders();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCShaderCache::loadDefaultShaders()
|
|
|
|
{
|
|
|
|
// Position Texture Color shader
|
|
|
|
CCGLProgram *p = new CCGLProgram();
|
2012-03-16 13:42:53 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColor_frag);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
|
2012-03-16 17:56:19 +08:00
|
|
|
m_pPrograms->setObject(p, kCCShader_PositionTextureColor);
|
2012-03-12 15:22:03 +08:00
|
|
|
p->release();
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
// Position Texture Color alpha test
|
|
|
|
p = new CCGLProgram();
|
2012-03-16 13:42:53 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
|
2012-03-16 17:56:19 +08:00
|
|
|
m_pPrograms->setObject(p, kCCShader_PositionTextureColorAlphaTest);
|
2012-03-12 15:22:03 +08:00
|
|
|
p->release();
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
//
|
|
|
|
// Position, Color shader
|
|
|
|
//
|
|
|
|
p = new CCGLProgram();
|
2012-03-16 13:42:53 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionColor_vert ,ccPositionColor_frag);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
|
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
|
2012-03-16 17:56:19 +08:00
|
|
|
m_pPrograms->setObject(p, kCCShader_PositionColor);
|
2012-03-12 15:22:03 +08:00
|
|
|
p->release();
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-03-12 15:22:03 +08:00
|
|
|
//
|
|
|
|
// Position Texture shader
|
|
|
|
//
|
|
|
|
p = new CCGLProgram();
|
2012-03-16 13:42:53 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTexture_vert ,ccPositionTexture_frag);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
|
2012-03-16 17:56:19 +08:00
|
|
|
m_pPrograms->setObject(p, kCCShader_PositionTexture);
|
2012-03-12 15:22:03 +08:00
|
|
|
p->release();
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
//
|
|
|
|
// Position, Texture attribs, 1 Color as uniform shader
|
|
|
|
//
|
|
|
|
p = new CCGLProgram();
|
2012-03-16 13:42:53 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
|
2012-03-16 17:56:19 +08:00
|
|
|
m_pPrograms->setObject(p ,kCCShader_PositionTexture_uColor);
|
2012-03-12 15:22:03 +08:00
|
|
|
p->release();
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
//
|
|
|
|
// Position Texture A8 Color shader
|
|
|
|
//
|
|
|
|
p = new CCGLProgram();
|
2012-03-16 13:42:53 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
|
2012-03-16 17:56:19 +08:00
|
|
|
m_pPrograms->setObject(p, kCCShader_PositionTextureA8Color);
|
2012-03-12 15:22:03 +08:00
|
|
|
p->release();
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Position and 1 color passed as a uniform (to similate glColor4ub )
|
|
|
|
//
|
|
|
|
p = new CCGLProgram();
|
|
|
|
p->initWithVertexShaderByteArray(ccPosition_uColor_vert, ccPosition_uColor_frag);
|
|
|
|
|
|
|
|
p->addAttribute("aVertex", kCCVertexAttrib_Position);
|
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
|
2012-03-16 17:56:19 +08:00
|
|
|
m_pPrograms->setObject(p, kCCShader_Position_uColor);
|
2012-03-16 13:42:53 +08:00
|
|
|
p->release();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCGLProgram* CCShaderCache::programForKey(const char* key)
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
return (CCGLProgram*)m_pPrograms->objectForKey(key);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCShaderCache::addProgram(CCGLProgram* program, const char* key)
|
|
|
|
{
|
2012-03-16 17:56:19 +08:00
|
|
|
m_pPrograms->setObject(program, key);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|