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 {
|
2012-04-19 14:35:52 +08:00
|
|
|
kCCShaderType_PositionTextureColor,
|
2012-04-17 17:55:26 +08:00
|
|
|
kCCShaderType_PositionTextureColorAlphaTest,
|
|
|
|
kCCShaderType_PositionColor,
|
|
|
|
kCCShaderType_PositionTexture,
|
|
|
|
kCCShaderType_PositionTexture_uColor,
|
|
|
|
kCCShaderType_PositionTextureA8Color,
|
|
|
|
kCCShaderType_Position_uColor,
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
kCCShaderType_MAX,
|
2012-04-17 17:55:26 +08:00
|
|
|
};
|
|
|
|
|
2012-03-12 15:22:03 +08:00
|
|
|
static CCShaderCache *_sharedShaderCache = 0;
|
|
|
|
|
|
|
|
CCShaderCache* CCShaderCache::sharedShaderCache()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (!_sharedShaderCache) {
|
|
|
|
_sharedShaderCache = new CCShaderCache();
|
|
|
|
if (!_sharedShaderCache->init())
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_sharedShaderCache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _sharedShaderCache;
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCShaderCache::purgeSharedShaderCache()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_sharedShaderCache);
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCShaderCache::CCShaderCache()
|
2012-03-16 17:56:19 +08:00
|
|
|
: m_pPrograms(0)
|
2012-03-12 15:22:03 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CCShaderCache::~CCShaderCache()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOGINFO("cocos2d deallocing 0x%X", this);
|
|
|
|
m_pPrograms->release();
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCShaderCache::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pPrograms = new CCDictionary();
|
|
|
|
loadDefaultShaders();
|
|
|
|
return true;
|
2012-03-12 15:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCShaderCache::loadDefaultShaders()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// Position Texture Color shader
|
|
|
|
CCGLProgram *p = new CCGLProgram();
|
2012-04-17 17:55:26 +08:00
|
|
|
loadDefaultShader(p, kCCShaderType_PositionTextureColor);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pPrograms->setObject(p, kCCShader_PositionTextureColor);
|
|
|
|
p->release();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Position Texture Color alpha test
|
|
|
|
p = new CCGLProgram();
|
2012-04-17 17:55:26 +08:00
|
|
|
loadDefaultShader(p, kCCShaderType_PositionTextureColorAlphaTest);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pPrograms->setObject(p, kCCShader_PositionTextureColorAlphaTest);
|
|
|
|
p->release();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position, Color shader
|
|
|
|
//
|
|
|
|
p = new CCGLProgram();
|
2012-04-17 17:55:26 +08:00
|
|
|
loadDefaultShader(p, kCCShaderType_PositionColor);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pPrograms->setObject(p, kCCShader_PositionColor);
|
|
|
|
p->release();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture shader
|
|
|
|
//
|
|
|
|
p = new CCGLProgram();
|
2012-04-17 17:55:26 +08:00
|
|
|
loadDefaultShader(p, kCCShaderType_PositionTexture);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pPrograms->setObject(p, kCCShader_PositionTexture);
|
|
|
|
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
|
|
|
|
//
|
|
|
|
p = new CCGLProgram();
|
2012-04-17 17:55:26 +08:00
|
|
|
loadDefaultShader(p, kCCShaderType_PositionTexture_uColor);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pPrograms->setObject(p ,kCCShader_PositionTexture_uColor);
|
|
|
|
p->release();
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture A8 Color shader
|
|
|
|
//
|
|
|
|
p = new CCGLProgram();
|
2012-04-17 17:55:26 +08:00
|
|
|
loadDefaultShader(p, kCCShaderType_PositionTextureA8Color);
|
|
|
|
|
|
|
|
m_pPrograms->setObject(p, kCCShader_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
|
|
|
//
|
|
|
|
p = new CCGLProgram();
|
2012-04-17 17:55:26 +08:00
|
|
|
loadDefaultShader(p, kCCShaderType_Position_uColor);
|
|
|
|
|
|
|
|
m_pPrograms->setObject(p, kCCShader_Position_uColor);
|
2012-04-19 14:35:52 +08:00
|
|
|
p->release();
|
2012-04-17 17:55:26 +08:00
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-17 17:55:26 +08:00
|
|
|
void CCShaderCache::reloadDefaultShaders()
|
|
|
|
{
|
|
|
|
// reset all programs and reload them
|
|
|
|
|
|
|
|
// Position Texture Color shader
|
|
|
|
CCGLProgram *p = programForKey(kCCShader_PositionTextureColor);
|
|
|
|
p->reset();
|
2012-04-19 14:35:52 +08:00
|
|
|
loadDefaultShader(p, kCCShaderType_PositionTextureColor);
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Position Texture Color alpha test
|
|
|
|
p = programForKey(kCCShader_PositionTextureColorAlphaTest);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
2012-04-19 14:35:52 +08:00
|
|
|
loadDefaultShader(p, kCCShaderType_PositionTextureColorAlphaTest);
|
2012-04-17 17:55:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position, Color shader
|
|
|
|
//
|
|
|
|
p = programForKey(kCCShader_PositionColor);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
|
|
|
loadDefaultShader(p, kCCShaderType_PositionColor);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture shader
|
|
|
|
//
|
|
|
|
p = programForKey(kCCShader_PositionTexture);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
|
|
|
loadDefaultShader(p, kCCShaderType_PositionTexture);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position, Texture attribs, 1 Color as uniform shader
|
|
|
|
//
|
|
|
|
p = programForKey(kCCShader_PositionTexture_uColor);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
|
|
|
loadDefaultShader(p, kCCShaderType_PositionTexture_uColor);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Position Texture A8 Color shader
|
|
|
|
//
|
|
|
|
p = programForKey(kCCShader_PositionTextureA8Color);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
|
|
|
loadDefaultShader(p, kCCShaderType_PositionTextureA8Color);
|
|
|
|
|
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
|
|
|
//
|
|
|
|
p = programForKey(kCCShader_Position_uColor);
|
2012-04-17 17:55:26 +08:00
|
|
|
p->reset();
|
|
|
|
loadDefaultShader(p, kCCShaderType_Position_uColor);
|
|
|
|
}
|
2012-03-12 15:22:03 +08:00
|
|
|
|
2012-04-17 17:55:26 +08:00
|
|
|
void CCShaderCache::loadDefaultShader(CCGLProgram *p, int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case kCCShaderType_PositionTextureColor:
|
|
|
|
p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColor_frag);
|
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case kCCShaderType_PositionTextureColorAlphaTest:
|
|
|
|
p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag);
|
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case kCCShaderType_PositionColor:
|
|
|
|
p->initWithVertexShaderByteArray(ccPositionColor_vert ,ccPositionColor_frag);
|
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case kCCShaderType_PositionTexture:
|
|
|
|
p->initWithVertexShaderByteArray(ccPositionTexture_vert ,ccPositionTexture_frag);
|
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case kCCShaderType_PositionTexture_uColor:
|
|
|
|
p->initWithVertexShaderByteArray(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag);
|
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case kCCShaderType_PositionTextureA8Color:
|
|
|
|
p->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag);
|
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case kCCShaderType_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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
p->addAttribute("aVertex", kCCVertexAttrib_Position);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
CCGLProgram* CCShaderCache::programForKey(const char* key)
|
|
|
|
{
|
2012-04-19 14:35:52 +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
|