Merge branch 'v3.1hotfix' into v3

Conflicts:
	tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp
	tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h
This commit is contained in:
Huabing.Xu 2014-05-30 10:19:56 +08:00
commit 41d19007eb
6 changed files with 103 additions and 13 deletions

View File

@ -33,6 +33,10 @@ THE SOFTWARE.
#include "renderer/CCGLProgramCache.h" #include "renderer/CCGLProgramCache.h"
#include "renderer/ccGLStateCache.h" #include "renderer/ccGLStateCache.h"
#include "renderer/CCTexture2D.h" #include "renderer/CCTexture2D.h"
#include "base/CCEventCustom.h"
#include "base/CCEventListenerCustom.h"
#include "base/CCEventType.h"
#include "base/CCDirector.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -272,11 +276,22 @@ GLProgramState::GLProgramState()
: _vertexAttribsFlags(0) : _vertexAttribsFlags(0)
, _glprogram(nullptr) , _glprogram(nullptr)
, _textureUnitIndex(1) , _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() GLProgramState::~GLProgramState()
{ {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundlistener);
#endif
CC_SAFE_RELEASE(_glprogram); CC_SAFE_RELEASE(_glprogram);
} }
@ -312,7 +327,24 @@ void GLProgramState::resetGLProgram()
void GLProgramState::apply(const Mat4& modelView) void GLProgramState::apply(const Mat4& modelView)
{ {
CCASSERT(_glprogram, "invalid glprogram"); 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 // set shader
_glprogram->use(); _glprogram->use();
_glprogram->setUniformsForBuiltins(modelView); _glprogram->setUniformsForBuiltins(modelView);
@ -322,9 +354,9 @@ void GLProgramState::apply(const Mat4& modelView)
if(_vertexAttribsFlags) { if(_vertexAttribsFlags) {
// enable/disable vertex attribs // enable/disable vertex attribs
GL::enableVertexAttribs(_vertexAttribsFlags); GL::enableVertexAttribs(_vertexAttribsFlags);
// set attributes // set attributes
for(auto &attribute : _attributes) { for(auto &attribute : _attributes)
{
attribute.second.apply(); attribute.second.apply();
} }
} }

View File

@ -39,6 +39,8 @@ class GLProgram;
class Texture2D; class Texture2D;
struct Uniform; struct Uniform;
struct VertexAttrib; struct VertexAttrib;
class EventListenerCustom;
class EventCustom;
// //
// //
@ -48,7 +50,7 @@ struct VertexAttrib;
class UniformValue class UniformValue
{ {
friend class GLProgram; friend class GLProgram;
friend class GLProgramState;
public: public:
UniformValue(); UniformValue();
UniformValue(Uniform *uniform, GLProgram* glprogram); UniformValue(Uniform *uniform, GLProgram* glprogram);
@ -186,12 +188,17 @@ protected:
VertexAttribValue* getVertexAttribValue(const std::string &attributeName); VertexAttribValue* getVertexAttribValue(const std::string &attributeName);
UniformValue* getUniformValue(const std::string &uniformName); UniformValue* getUniformValue(const std::string &uniformName);
bool _uniformAttributeValueDirty;
std::unordered_map<std::string, UniformValue> _uniforms; std::unordered_map<std::string, UniformValue> _uniforms;
std::unordered_map<std::string, VertexAttribValue> _attributes; std::unordered_map<std::string, VertexAttribValue> _attributes;
int _textureUnitIndex; int _textureUnitIndex;
uint32_t _vertexAttribsFlags; uint32_t _vertexAttribsFlags;
GLProgram *_glprogram; GLProgram *_glprogram;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
EventListenerCustom* _backToForegroundlistener;
#endif
}; };
NS_CC_END NS_CC_END

View File

@ -208,12 +208,42 @@ bool Effect::initGLProgramState(const std::string &fragmentFilename)
auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath); auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath);
auto glprogram = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource.c_str()); 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 = GLProgramState::getOrCreateWithGLProgram(glprogram);
_glprogramstate->retain(); _glprogramstate->retain();
return _glprogramstate != nullptr; 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 // Blur
class EffectBlur : public Effect class EffectBlur : public Effect
{ {

View File

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

View File

@ -297,7 +297,7 @@ Effect3DOutline* Effect3DOutline::create()
bool Effect3DOutline::init() bool Effect3DOutline::init()
{ {
GLProgram* glprogram = Effect3DOutline::getOrCreateProgram(); GLProgram* glprogram = GLProgram::createWithFilenames(_vertShaderFile, _fragShaderFile);
if(nullptr == glprogram) if(nullptr == glprogram)
{ {
CC_SAFE_DELETE(glprogram); CC_SAFE_DELETE(glprogram);
@ -320,12 +320,27 @@ Effect3DOutline::Effect3DOutline()
, _outlineColor(1, 1, 1) , _outlineColor(1, 1, 1)
, _sprite(nullptr) , _sprite(nullptr)
{ {
#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() Effect3DOutline::~Effect3DOutline()
{ {
CC_SAFE_RELEASE_NULL(_sprite); CC_SAFE_RELEASE_NULL(_sprite);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForeGroundLister);
#endif
} }
void Effect3DOutline::setOutlineColor(const Vec3& color) void Effect3DOutline::setOutlineColor(const Vec3& color)

View File

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