Merge pull request #6919 from dabingnn/v3.1hotfix

V3.1hotfix
This commit is contained in:
minggo 2014-05-29 14:32:24 +08:00
commit f4d8e396e2
6 changed files with 103 additions and 13 deletions

View File

@ -33,6 +33,10 @@ THE SOFTWARE.
#include "renderer/CCGLProgramCache.h"
#include "renderer/ccGLStateCache.h"
#include "renderer/CCTexture2D.h"
#include "base/CCEventCustom.h"
#include "base/CCEventListenerCustom.h"
#include "base/CCEventType.h"
#include "base/CCDirector.h"
NS_CC_BEGIN
@ -272,11 +276,22 @@ GLProgramState::GLProgramState()
: _vertexAttribsFlags(0)
, _glprogram(nullptr)
, _textureUnitIndex(1)
, _uniformAttributeValueDirty(true)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
// listen the event when app go to foreground
CCLOG("create _backToForegroundlistener for GLProgramState");
_backToForegroundlistener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom*) { _uniformAttributeValueDirty = true; });
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundlistener, -1);
#endif
}
GLProgramState::~GLProgramState()
{
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundlistener);
#endif
CC_SAFE_RELEASE(_glprogram);
}
@ -312,7 +327,24 @@ void GLProgramState::resetGLProgram()
void GLProgramState::apply(const Mat4& modelView)
{
CCASSERT(_glprogram, "invalid glprogram");
if(_uniformAttributeValueDirty)
{
for(auto& uniformValue : _uniforms)
{
uniformValue.second._uniform = _glprogram->getUniform(uniformValue.first);
}
_vertexAttribsFlags = 0;
for(auto& attributeValue : _attributes)
{
attributeValue.second._vertexAttrib = _glprogram->getVertexAttrib(attributeValue.first);;
if(attributeValue.second._enabled)
_vertexAttribsFlags |= 1 << attributeValue.second._vertexAttrib->index;
}
_uniformAttributeValueDirty = false;
}
// set shader
_glprogram->use();
_glprogram->setUniformsForBuiltins(modelView);
@ -322,9 +354,9 @@ void GLProgramState::apply(const Mat4& modelView)
if(_vertexAttribsFlags) {
// enable/disable vertex attribs
GL::enableVertexAttribs(_vertexAttribsFlags);
// set attributes
for(auto &attribute : _attributes) {
for(auto &attribute : _attributes)
{
attribute.second.apply();
}
}

View File

@ -39,6 +39,8 @@ class GLProgram;
class Texture2D;
struct Uniform;
struct VertexAttrib;
class EventListenerCustom;
class EventCustom;
//
//
@ -48,7 +50,7 @@ struct VertexAttrib;
class UniformValue
{
friend class GLProgram;
friend class GLProgramState;
public:
UniformValue();
UniformValue(Uniform *uniform, GLProgram* glprogram);
@ -177,7 +179,7 @@ public:
void setUniformCallback(const std::string &uniformName, const std::function<void(Uniform*)> &callback);
void setUniformTexture(const std::string &uniformName, Texture2D *texture);
void setUniformTexture(const std::string &uniformName, GLuint textureId);
protected:
GLProgramState();
~GLProgramState();
@ -185,13 +187,18 @@ protected:
void resetGLProgram();
VertexAttribValue* getVertexAttribValue(const std::string &attributeName);
UniformValue* getUniformValue(const std::string &uniformName);
bool _uniformAttributeValueDirty;
std::unordered_map<std::string, UniformValue> _uniforms;
std::unordered_map<std::string, VertexAttribValue> _attributes;
int _textureUnitIndex;
uint32_t _vertexAttribsFlags;
GLProgram *_glprogram;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
EventListenerCustom* _backToForegroundlistener;
#endif
};
NS_CC_END

View File

@ -207,13 +207,43 @@ bool Effect::initGLProgramState(const std::string &fragmentFilename)
auto fragmentFullPath = fileUtiles->fullPathForFilename(fragmentFilename);
auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath);
auto glprogram = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource.c_str());
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
_fragSource = fragSource;
#endif
_glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);
_glprogramstate->retain();
return _glprogramstate != nullptr;
}
Effect::Effect()
: _glprogramstate(nullptr)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
_backgroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND,
[this](EventCustom*)
{
auto glProgram = _glprogramstate->getGLProgram();
glProgram->reset();
glProgram->initWithByteArrays(ccPositionTextureColor_noMVP_vert, _fragSource.c_str());
glProgram->link();
glProgram->updateUniforms();
}
);
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backgroundListener, -1);
#endif
}
Effect::~Effect()
{
CC_SAFE_RELEASE_NULL(_glprogramstate);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backgroundListener);
#endif
}
// Blur
class EffectBlur : public Effect
{

View File

@ -37,10 +37,13 @@ public:
protected:
bool initGLProgramState(const std::string &fragmentFilename);
Effect() : _glprogramstate(nullptr)
{}
virtual ~Effect() {}
Effect();
virtual ~Effect();
GLProgramState *_glprogramstate;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
std::string _fragSource;
EventListenerCustom* _backgroundListener;
#endif
};
class EffectSpriteTest : public ShaderTestDemo2

View File

@ -296,7 +296,7 @@ Effect3DOutline* Effect3DOutline::create()
bool Effect3DOutline::init()
{
GLProgram* glprogram = Effect3DOutline::getOrCreateProgram();
GLProgram* glprogram = GLProgram::createWithFilenames(_vertShaderFile, _fragShaderFile);
if(nullptr == glprogram)
{
CC_SAFE_DELETE(glprogram);
@ -318,11 +318,26 @@ Effect3DOutline::Effect3DOutline()
: _outlineWidth(1.0f)
, _outlineColor(1, 1, 1)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
_backToForeGroundLister = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND,
[this](EventCustom*)
{
auto glProgram = _glProgramState->getGLProgram();
glProgram->reset();
glProgram->initWithFilenames(_vertShaderFile, _fragShaderFile);
glProgram->link();
glProgram->updateUniforms();
}
);
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForeGroundLister, -1);
#endif
}
Effect3DOutline::~Effect3DOutline()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForeGroundLister);
#endif
}
void Effect3DOutline::setOutlineColor(const Vec3& color)

View File

@ -96,6 +96,9 @@ protected:
Vec3 _outlineColor;
float _outlineWidth;
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
EventListenerCustom* _backToForeGroundLister;
//#endif
protected:
static const std::string _vertShaderFile;