This commit is contained in:
2youyou2 2014-06-13 18:00:01 +08:00
commit 369ed3e6d3
10 changed files with 136 additions and 203 deletions

View File

@ -29,6 +29,7 @@ cocos2d-x-3.2 ???
[FIX] Repeat: will run one more over in rare situations [FIX] Repeat: will run one more over in rare situations
[FIX] Scale9Sprite: support culling [FIX] Scale9Sprite: support culling
[FIX] Schedule: schedulePerFrame() can not be called twice [FIX] Schedule: schedulePerFrame() can not be called twice
[FIX] ShaderTest: 7 times performance improved of blur effect
[FIX] SpriteFrameCache: fix memory leak [FIX] SpriteFrameCache: fix memory leak
[FIX] Texture2D: use image's pixel format to create texture [FIX] Texture2D: use image's pixel format to create texture
[FIX] TextureCache: addImageAsync() may repeatedly generate Image for the same image file [FIX] TextureCache: addImageAsync() may repeatedly generate Image for the same image file

View File

@ -1529,7 +1529,7 @@ public:
virtual void setOpacityModifyRGB(bool bValue) override { return Node::setOpacityModifyRGB(bValue); } virtual void setOpacityModifyRGB(bool bValue) override { return Node::setOpacityModifyRGB(bValue); }
virtual bool isOpacityModifyRGB() const override { return Node::isOpacityModifyRGB(); } virtual bool isOpacityModifyRGB() const override { return Node::isOpacityModifyRGB(); }
protected: CC_CONSTRUCTOR_ACCESS:
__NodeRGBA(); __NodeRGBA();
virtual ~__NodeRGBA() {} virtual ~__NodeRGBA() {}

View File

@ -164,22 +164,25 @@ void bindTexture2DN(GLuint textureUnit, GLuint textureId)
void deleteTexture(GLuint textureId) void deleteTexture(GLuint textureId)
{
deleteTextureN(0, textureId);
}
void deleteTextureN(GLuint textureUnit, GLuint textureId)
{ {
#if CC_ENABLE_GL_STATE_CACHE #if CC_ENABLE_GL_STATE_CACHE
if (s_currentBoundTexture[textureUnit] == textureId) for (size_t i = 0; i < MAX_ACTIVE_TEXTURE; ++i)
{ {
s_currentBoundTexture[textureUnit] = -1; if (s_currentBoundTexture[i] == textureId)
{
s_currentBoundTexture[i] = -1;
}
} }
#endif // CC_ENABLE_GL_STATE_CACHE #endif // CC_ENABLE_GL_STATE_CACHE
glDeleteTextures(1, &textureId); glDeleteTextures(1, &textureId);
} }
void deleteTextureN(GLuint textureUnit, GLuint textureId)
{
deleteTexture(textureId);
}
void activeTexture(GLenum texture) void activeTexture(GLenum texture)
{ {
#if CC_ENABLE_GL_STATE_CACHE #if CC_ENABLE_GL_STATE_CACHE

View File

@ -129,7 +129,7 @@ void CC_DLL deleteTexture(GLuint textureId);
If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glDeleteTextures() directly. If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glDeleteTextures() directly.
@since v2.1.0 @since v2.1.0
*/ */
void CC_DLL deleteTextureN(GLuint textureUnit, GLuint textureId); CC_DEPRECATED_ATTRIBUTE void CC_DLL deleteTextureN(GLuint textureUnit, GLuint textureId);
/** Select active texture unit. /** Select active texture unit.
If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glActiveTexture() directly. If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glActiveTexture() directly.

View File

@ -255,27 +255,10 @@ int LuaStack::executeString(const char *codes)
int LuaStack::executeScriptFile(const char* filename) int LuaStack::executeScriptFile(const char* filename)
{ {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
std::string code("require \""); std::string code("require \"");
code.append(filename); code.append(filename);
code.append("\""); code.append("\"");
return executeString(code.c_str()); return executeString(code.c_str());
#else
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
++_callFromLua;
int nRet = luaL_dofile(_state, fullPath.c_str());
--_callFromLua;
CC_ASSERT(_callFromLua >= 0);
// lua_gc(_state, LUA_GCCOLLECT, 0);
if (nRet != 0)
{
CCLOG("[LUA ERROR] %s", lua_tostring(_state, -1));
lua_pop(_state, 1);
return nRet;
}
return 0;
#endif
} }
int LuaStack::executeGlobalFunction(const char* functionName) int LuaStack::executeGlobalFunction(const char* functionName)

View File

@ -421,22 +421,16 @@ class SpriteBlur : public Sprite
{ {
public: public:
~SpriteBlur(); ~SpriteBlur();
void setBlurSize(float f);
bool initWithTexture(Texture2D* texture, const Rect& rect); bool initWithTexture(Texture2D* texture, const Rect& rect);
void initGLProgram(); void initGLProgram();
static SpriteBlur* create(const char *pszFileName); static SpriteBlur* create(const char *pszFileName);
void setBlurRadius(float radius);
void setBlurSampleNum(float num);
protected: protected:
float _blurRadius;
int _blurRadius; float _blurSampleNum;
Vec2 _pixelSize;
int _samplingRadius;
//gaussian = cons * exp( (dx*dx + dy*dy) * scale);
float _scale;
float _cons;
float _weightSum;
}; };
SpriteBlur::~SpriteBlur() SpriteBlur::~SpriteBlur()
@ -472,14 +466,7 @@ bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif #endif
auto s = getTexture()->getContentSizeInPixels(); initGLProgram();
_pixelSize = Vec2(1/s.width, 1/s.height);
_samplingRadius = 0;
this->initGLProgram();
getGLProgramState()->setUniformVec2("onePixelSize", _pixelSize);
return true; return true;
} }
@ -495,43 +482,23 @@ void SpriteBlur::initGLProgram()
auto glProgramState = GLProgramState::getOrCreateWithGLProgram(program); auto glProgramState = GLProgramState::getOrCreateWithGLProgram(program);
setGLProgramState(glProgramState); setGLProgramState(glProgramState);
auto size = getTexture()->getContentSizeInPixels();
getGLProgramState()->setUniformVec2("resolution", size);
getGLProgramState()->setUniformFloat("blurRadius", _blurRadius);
getGLProgramState()->setUniformFloat("sampleNum", 7.0f);
} }
void SpriteBlur::setBlurSize(float f) void SpriteBlur::setBlurRadius(float radius)
{ {
if(_blurRadius == (int)f) _blurRadius = radius;
return; getGLProgramState()->setUniformFloat("blurRadius", _blurRadius);
_blurRadius = (int)f; }
_samplingRadius = _blurRadius; void SpriteBlur::setBlurSampleNum(float num)
if (_samplingRadius > 10) {
{ _blurSampleNum = num;
_samplingRadius = 10; getGLProgramState()->setUniformFloat("sampleNum", _blurSampleNum);
}
if (_blurRadius > 0)
{
float sigma = _blurRadius / 2.0f;
_scale = -0.5f / (sigma * sigma);
_cons = -1.0f * _scale / 3.141592f;
_weightSum = -_cons;
float weight;
int squareX;
for(int dx = 0; dx <= _samplingRadius; ++dx)
{
squareX = dx * dx;
weight = _cons * exp(squareX * _scale);
_weightSum += 2.0 * weight;
for (int dy = 1; dy <= _samplingRadius; ++dy)
{
weight = _cons * exp((squareX + dy * dy) * _scale);
_weightSum += 4.0 * weight;
}
}
}
log("_blurRadius:%d",_blurRadius);
getGLProgramState()->setUniformVec4("gaussianCoefficient", Vec4(_samplingRadius, _scale, _cons, _weightSum));
} }
// ShaderBlur // ShaderBlur
@ -551,22 +518,43 @@ std::string ShaderBlur::subtitle() const
return "Gaussian blur"; return "Gaussian blur";
} }
ControlSlider* ShaderBlur::createSliderCtl() void ShaderBlur::createSliderCtls()
{ {
auto screenSize = Director::getInstance()->getWinSize(); auto screenSize = Director::getInstance()->getWinSize();
{
ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png"); ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
slider->setAnchorPoint(Vec2(0.5f, 1.0f)); slider->setAnchorPoint(Vec2(0.5f, 1.0f));
slider->setMinimumValue(0.0f); // Sets the min value of range slider->setMinimumValue(0.0f);
slider->setMaximumValue(25.0f); // Sets the max value of range slider->setMaximumValue(25.0f);
slider->setScale(0.6f);
slider->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 3.0f)); slider->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f));
slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::onRadiusChanged), Control::EventType::VALUE_CHANGED);
// When the value of the slider will change, the given selector will be call
slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::sliderAction), Control::EventType::VALUE_CHANGED);
slider->setValue(2.0f); slider->setValue(2.0f);
addChild(slider);
_sliderRadiusCtl = slider;
return slider; auto label = Label::createWithTTF("Blur Radius", "fonts/arial.ttf", 12.0f);
addChild(label);
label->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f - 24.0f));
}
{
ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
slider->setAnchorPoint(Vec2(0.5f, 1.0f));
slider->setMinimumValue(0.0f);
slider->setMaximumValue(11.0f);
slider->setScale(0.6f);
slider->setPosition(Vec2(screenSize.width * 3 / 4.0f, screenSize.height / 3.0f));
slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::onSampleNumChanged), Control::EventType::VALUE_CHANGED);
slider->setValue(7.0f);
addChild(slider);
_sliderNumCtrl = slider;
auto label = Label::createWithTTF("Blur Sample Num", "fonts/arial.ttf", 12.0f);
addChild(label);
label->setPosition(Vec2(screenSize.width * 3 / 4.0f, screenSize.height / 3.0f - 24.0f));
}
} }
@ -575,9 +563,7 @@ bool ShaderBlur::init()
if( ShaderTestDemo::init() ) if( ShaderTestDemo::init() )
{ {
_blurSprite = SpriteBlur::create("Images/grossini.png"); _blurSprite = SpriteBlur::create("Images/grossini.png");
auto sprite = Sprite::create("Images/grossini.png"); auto sprite = Sprite::create("Images/grossini.png");
auto s = Director::getInstance()->getWinSize(); auto s = Director::getInstance()->getWinSize();
_blurSprite->setPosition(Vec2(s.width/3, s.height/2)); _blurSprite->setPosition(Vec2(s.width/3, s.height/2));
sprite->setPosition(Vec2(2*s.width/3, s.height/2)); sprite->setPosition(Vec2(2*s.width/3, s.height/2));
@ -585,19 +571,24 @@ bool ShaderBlur::init()
addChild(_blurSprite); addChild(_blurSprite);
addChild(sprite); addChild(sprite);
_sliderCtl = createSliderCtl(); createSliderCtls();
addChild(_sliderCtl);
return true; return true;
} }
return false; return false;
} }
void ShaderBlur::sliderAction(Ref* sender, Control::EventType controlEvent) void ShaderBlur::onRadiusChanged(Ref* sender, Control::EventType)
{ {
ControlSlider* slider = (ControlSlider*)sender; ControlSlider* slider = (ControlSlider*)sender;
_blurSprite->setBlurSize(slider->getValue()); _blurSprite->setBlurRadius(slider->getValue());
}
void ShaderBlur::onSampleNumChanged(Ref* sender, Control::EventType)
{
ControlSlider* slider = (ControlSlider*)sender;
_blurSprite->setBlurSampleNum(slider->getValue());
} }
// ShaderRetroEffect // ShaderRetroEffect

View File

@ -92,11 +92,14 @@ public:
virtual std::string title() const override; virtual std::string title() const override;
virtual std::string subtitle() const override; virtual std::string subtitle() const override;
virtual bool init(); virtual bool init();
ControlSlider* createSliderCtl(); void createSliderCtls();
void sliderAction(Ref* sender, Control::EventType controlEvent); void onRadiusChanged(Ref* sender, Control::EventType controlEvent);
void onSampleNumChanged(Ref* sender, Control::EventType controlEvent);
protected: protected:
SpriteBlur* _blurSprite; SpriteBlur* _blurSprite;
ControlSlider* _sliderCtl; ControlSlider* _sliderRadiusCtl;
ControlSlider* _sliderNumCtrl;
}; };
class ShaderRetroEffect : public ShaderTestDemo class ShaderRetroEffect : public ShaderTestDemo

View File

@ -249,80 +249,42 @@ class EffectBlur : public Effect
{ {
public: public:
CREATE_FUNC(EffectBlur); CREATE_FUNC(EffectBlur);
virtual void setTarget(EffectSprite *sprite) override; virtual void setTarget(EffectSprite *sprite) override;
void setBlurRadius(float radius);
void setGaussian(float value); void setBlurSampleNum(float num);
void setCustomUniforms();
void setBlurSize(float f);
protected: protected:
bool init(float blurSize=3.0); bool init(float blurRadius = 10.0f, float sampleNum = 5.0f);
int _blurRadius; float _blurRadius;
Vec2 _pixelSize; float _blurSampleNum;
int _samplingRadius;
float _scale;
float _cons;
float _weightSum;
}; };
void EffectBlur::setTarget(EffectSprite *sprite) void EffectBlur::setTarget(EffectSprite *sprite)
{ {
Size s = sprite->getTexture()->getContentSizeInPixels(); Size size = sprite->getTexture()->getContentSizeInPixels();
_pixelSize = Vec2(1/s.width, 1/s.height); _glprogramstate->setUniformVec2("resolution", size);
_glprogramstate->setUniformVec2("onePixelSize", _pixelSize); _glprogramstate->setUniformFloat("blurRadius", _blurRadius);
_glprogramstate->setUniformFloat("sampleNum", _blurSampleNum);
} }
bool EffectBlur::init(float blurSize) bool EffectBlur::init(float blurRadius, float sampleNum)
{ {
initGLProgramState("Shaders/example_Blur.fsh"); initGLProgramState("Shaders/example_Blur.fsh");
auto s = Size(100,100); _blurRadius = blurRadius;
_blurSampleNum = sampleNum;
_blurRadius = 0;
_pixelSize = Vec2(1/s.width, 1/s.height);
_samplingRadius = 0;
setBlurSize(blurSize);
_glprogramstate->setUniformVec2("onePixelSize", _pixelSize);
_glprogramstate->setUniformVec4("gaussianCoefficient", Vec4(_samplingRadius, _scale, _cons, _weightSum));
return true; return true;
} }
void EffectBlur::setBlurSize(float f) void EffectBlur::setBlurRadius(float radius)
{ {
if(_blurRadius == (int)f) _blurRadius = radius;
return; }
_blurRadius = (int)f;
_samplingRadius = _blurRadius; void EffectBlur::setBlurSampleNum(float num)
if (_samplingRadius > 10) {
{ _blurSampleNum = num;
_samplingRadius = 10;
}
if (_blurRadius > 0)
{
float sigma = _blurRadius / 2.0f;
_scale = -0.5f / (sigma * sigma);
_cons = -1.0f * _scale / 3.141592f;
_weightSum = -_cons;
float weight;
int squareX;
for(int dx = 0; dx <= _samplingRadius; ++dx)
{
squareX = dx * dx;
weight = _cons * exp(squareX * _scale);
_weightSum += 2.0 * weight;
for (int dy = 1; dy <= _samplingRadius; ++dy)
{
weight = _cons * exp((squareX + dy * dy) * _scale);
_weightSum += 4.0 * weight;
}
}
}
} }
// Outline // Outline

View File

@ -1,5 +1,3 @@
// Shader taken from: http://webglsamples.googlecode.com/hg/electricflower/electricflower.html
#ifdef GL_ES #ifdef GL_ES
precision mediump float; precision mediump float;
#endif #endif
@ -7,50 +5,42 @@ precision mediump float;
varying vec4 v_fragmentColor; varying vec4 v_fragmentColor;
varying vec2 v_texCoord; varying vec2 v_texCoord;
uniform vec4 gaussianCoefficient; uniform vec2 resolution;
uniform vec2 onePixelSize; uniform float blurRadius;
uniform float sampleNum;
void main() { vec3 blur(vec2);
if(gaussianCoefficient.x > 0.0) {
vec4 sum = vec4(0.0);
vec2 offset;
float weight;
float squareX;
for(float dx = 0.0; dx <= gaussianCoefficient.x; dx += 1.0) { void main(void)
squareX = dx * dx; {
weight = gaussianCoefficient.z * exp(squareX * gaussianCoefficient.y); vec3 col = blur(v_texCoord);
gl_FragColor = vec4(col, 1.0) * v_fragmentColor;
offset.x = -dx * onePixelSize.x;
offset.y = 0.0;
sum += texture2D(CC_Texture0, v_texCoord + offset) * weight;
offset.x = dx * onePixelSize.x;
sum += texture2D(CC_Texture0, v_texCoord + offset) * weight;
for(float dy = 1.0; dy <= gaussianCoefficient.x; dy += 1.0) {
weight = gaussianCoefficient.z * exp((squareX + dy * dy) * gaussianCoefficient.y);
offset.x = -dx * onePixelSize.x;
offset.y = -dy * onePixelSize.y;
sum += texture2D(CC_Texture0, v_texCoord + offset) * weight;
offset.y = dy * onePixelSize.y;
sum += texture2D(CC_Texture0, v_texCoord + offset) * weight;
offset.x = dx * onePixelSize.x;
sum += texture2D(CC_Texture0, v_texCoord + offset) * weight;
offset.y = -dy * onePixelSize.y;
sum += texture2D(CC_Texture0, v_texCoord + offset) * weight;
}
}
sum -= texture2D(CC_Texture0, v_texCoord) * gaussianCoefficient.z;
sum /= gaussianCoefficient.w;
gl_FragColor = sum * v_fragmentColor;
}
else {
gl_FragColor = texture2D(CC_Texture0, v_texCoord) * v_fragmentColor;
}
} }
vec3 blur(vec2 p)
{
if (blurRadius > 0.0 && sampleNum > 1.0)
{
vec3 col = vec3(0);
vec2 unit = 1.0 / resolution.xy;
float r = blurRadius;
float sampleStep = r / sampleNum;
float count = 0.0;
for(float x = -r; x < r; x += sampleStep)
{
for(float y = -r; y < r; y += sampleStep)
{
float weight = (r - abs(x)) * (r - abs(y));
col += texture2D(CC_Texture0, p + vec2(x * unit.x, y * unit.y)).rgb * weight;
count += weight;
}
}
return col / count;
}
return texture2D(CC_Texture0, p).rgb;
}

@ -1 +1 @@
Subproject commit 498f24c1683e4725ecaad6168a1aab21b283b8d5 Subproject commit f5037bab73a8fb109e8e34656220bed1a1743087