From a735abaf6544c8809b96ce1f6a534117e9edbb94 Mon Sep 17 00:00:00 2001 From: halx99 Date: Tue, 21 Jul 2020 23:11:03 +0800 Subject: [PATCH] Take care _programState use --- cocos/2d/CCAtlasNode.cpp | 2 +- cocos/2d/CCDrawNode.cpp | 4 +--- cocos/2d/CCLabel.cpp | 10 ++-------- cocos/2d/CCLayer.cpp | 4 ++-- cocos/2d/CCMotionStreak.cpp | 7 +------ cocos/2d/CCNode.cpp | 9 ++++++++- cocos/2d/CCNode.h | 4 +++- cocos/2d/CCParticleBatchNode.cpp | 2 +- cocos/2d/CCParticleSystemQuad.cpp | 2 +- cocos/2d/CCProgressTimer.cpp | 5 ++--- cocos/2d/CCSprite.cpp | 7 +------ cocos/2d/CCSpriteBatchNode.cpp | 7 +------ cocos/3d/CCMotionStreak3D.cpp | 2 +- cocos/3d/CCSkybox.cpp | 2 +- cocos/3d/CCTerrain.cpp | 2 +- .../cocostudio/ActionTimeline/CCBoneNode.cpp | 2 +- .../cocostudio/ActionTimeline/CCSkeletonNode.cpp | 2 +- 17 files changed, 29 insertions(+), 44 deletions(-) diff --git a/cocos/2d/CCAtlasNode.cpp b/cocos/2d/CCAtlasNode.cpp index fdc7b099bd..f6da85b5f2 100644 --- a/cocos/2d/CCAtlasNode.cpp +++ b/cocos/2d/CCAtlasNode.cpp @@ -46,7 +46,7 @@ AtlasNode::AtlasNode() { auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor(); auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR); - _programState = new (std::nothrow) backend::ProgramState(program); + attachProgramState(new (std::nothrow) backend::ProgramState(program)); pipelineDescriptor.programState = _programState; _mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix"); _textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture"); diff --git a/cocos/2d/CCDrawNode.cpp b/cocos/2d/CCDrawNode.cpp index 42b21fb091..ba0d32d3e6 100644 --- a/cocos/2d/CCDrawNode.cpp +++ b/cocos/2d/CCDrawNode.cpp @@ -72,7 +72,6 @@ DrawNode::~DrawNode() free(_bufferGLLine); _bufferGLLine = nullptr; - CC_SAFE_RELEASE(_programState); CC_SAFE_RELEASE(_programStatePoint); CC_SAFE_RELEASE(_programStateLine); } @@ -150,9 +149,8 @@ bool DrawNode::init() void DrawNode::updateShader() { - CC_SAFE_RELEASE(_programState); auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR_LENGTH_TEXTURE); - _programState = new (std::nothrow) backend::ProgramState(program); + setProgramState(new (std::nothrow) backend::ProgramState(program)); _customCommand.getPipelineDescriptor().programState = _programState; setVertexLayout(_customCommand); _customCommand.setDrawType(CustomCommand::DrawType::ARRAY); diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index d3dc963ec0..c6000f07d4 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -664,12 +664,7 @@ void Label::setVertexLayout(PipelineDescriptor& pipelineDescriptor) void Label::setProgramState(backend::ProgramState *programState) { - if (_programState != programState) - { - CC_SAFE_RELEASE_NULL(_programState); - _programState = programState; - CC_SAFE_RETAIN(programState); - } + Node::setProgramState(programState); updateUniformLocations(); for (auto &batch : _batchCommands) { @@ -730,9 +725,8 @@ void Label::updateShaderProgram() } } - CC_SAFE_RELEASE(_programState); auto* program = backend::Program::getBuiltinProgram(programType); - _programState = new backend::ProgramState(program); + attachProgramState(new backend::ProgramState(program)); updateUniformLocations(); diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index 19ea59f66a..4458c22611 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -246,7 +246,7 @@ LayerColor::LayerColor() auto& pipelineDescriptor = _customCommand.getPipelineDescriptor(); auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR); // TODO: noMVP? - _programState = new (std::nothrow) backend::ProgramState(program); + attachProgramState(new (std::nothrow) backend::ProgramState(program)); pipelineDescriptor.programState = _programState; auto vertexLayout = _programState->getVertexLayout(); @@ -662,7 +662,7 @@ LayerRadialGradient::LayerRadialGradient() { auto& pipelineDescriptor = _customCommand.getPipelineDescriptor(); auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::LAYER_RADIA_GRADIENT); - _programState = new (std::nothrow) backend::ProgramState(program); + attachProgramState(new (std::nothrow) backend::ProgramState(program)); pipelineDescriptor.programState = _programState; _mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix"); _startColorLocation = pipelineDescriptor.programState->getUniformLocation("u_startColor"); diff --git a/cocos/2d/CCMotionStreak.cpp b/cocos/2d/CCMotionStreak.cpp index ef94fafeeb..5628bc733c 100644 --- a/cocos/2d/CCMotionStreak.cpp +++ b/cocos/2d/CCMotionStreak.cpp @@ -221,12 +221,7 @@ void MotionStreak::setProgramState(backend::ProgramState* programState) { CCASSERT(programState, "argument should not be nullptr"); auto& pipelineDescriptor = _customCommand.getPipelineDescriptor(); - if (_programState != programState) - { - CC_SAFE_RELEASE(_programState); - _programState = programState; - CC_SAFE_RETAIN(programState); - } + Node::setProgramState(programState); pipelineDescriptor.programState = _programState; _mvpMatrixLocaiton = _programState->getUniformLocation("u_MVPMatrix"); diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index c5747c4f08..89c9295afd 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -2143,13 +2143,20 @@ void Node::updateProgramStateTexture(Texture2D* texture) } void Node::setProgramState(backend::ProgramState* programState) +{ + if(attachProgramState(programState)) + programState->retain(); +} + +bool Node::attachProgramState(backend::ProgramState* programState) { if (_programState != programState) { CC_SAFE_RELEASE(_programState); _programState = programState; - CC_SAFE_RETAIN(programState); + return !!_programState; } + return false; } backend::ProgramState* Node::getProgramState() const diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 1d52e5f17d..885c01bd87 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -178,7 +178,8 @@ public: * Sets the arrival order when this node has a same ZOrder with other children. * * A node which called addChild subsequently will take a larger arrival order, - * If two children have the same Z order, the child with larger arrival order will be drawn later. + * If two children have the same Z order, the child with larger arrival order will be + n later. * * @warning This method is used internally for localZOrder sorting, don't change this manually * @@ -1768,6 +1769,7 @@ public: virtual void setCameraMask(unsigned short mask, bool applyChildren = true); virtual void setProgramState(backend::ProgramState* programState); + bool attachProgramState(backend::ProgramState* programState); void setProgramStateWithRegistry(backend::ProgramType programType, Texture2D* texture); void updateProgramStateTexture(Texture2D* texture); diff --git a/cocos/2d/CCParticleBatchNode.cpp b/cocos/2d/CCParticleBatchNode.cpp index 64749c0de4..b32de0066a 100644 --- a/cocos/2d/CCParticleBatchNode.cpp +++ b/cocos/2d/CCParticleBatchNode.cpp @@ -49,7 +49,7 @@ ParticleBatchNode::ParticleBatchNode() { auto& pipelineDescriptor = _customCommand.getPipelineDescriptor(); auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR); - _programState = new (std::nothrow) backend::ProgramState(program); + attachProgramState(new (std::nothrow) backend::ProgramState(program)); pipelineDescriptor.programState = _programState; _mvpMatrixLocaiton = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix"); _textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture"); diff --git a/cocos/2d/CCParticleSystemQuad.cpp b/cocos/2d/CCParticleSystemQuad.cpp index a4ee52bd18..1ea3b43c69 100644 --- a/cocos/2d/CCParticleSystemQuad.cpp +++ b/cocos/2d/CCParticleSystemQuad.cpp @@ -49,7 +49,7 @@ ParticleSystemQuad::ParticleSystemQuad() { auto& pipelieDescriptor = _quadCommand.getPipelineDescriptor(); auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR); - _programState = new (std::nothrow) backend::ProgramState(program); + attachProgramState(new (std::nothrow) backend::ProgramState(program)); pipelieDescriptor.programState = _programState; _mvpMatrixLocaiton = pipelieDescriptor.programState->getUniformLocation("u_MVPMatrix"); _textureLocation = pipelieDescriptor.programState->getUniformLocation("u_texture"); diff --git a/cocos/2d/CCProgressTimer.cpp b/cocos/2d/CCProgressTimer.cpp index d385c3d570..a5f7c8d82c 100644 --- a/cocos/2d/CCProgressTimer.cpp +++ b/cocos/2d/CCProgressTimer.cpp @@ -112,9 +112,9 @@ bool ProgressTimer::initWithSprite(Sprite* sp) setBarChangeRate(Vec2(1,1)); setSprite(sp); - CC_SAFE_RELEASE(_programState); + // TODO: Use ProgramState Vector to Node CC_SAFE_RELEASE(_programState2); - _programState = initPipelineDescriptor(_customCommand, true, _locMVP1, _locTex1); + attachProgramState(initPipelineDescriptor(_customCommand, true, _locMVP1, _locTex1)); _programState2 = initPipelineDescriptor(_customCommand2, false, _locMVP2, _locTex2); return true; @@ -123,7 +123,6 @@ bool ProgressTimer::initWithSprite(Sprite* sp) ProgressTimer::~ProgressTimer() { CC_SAFE_RELEASE(_sprite); - CC_SAFE_RELEASE(_programState); CC_SAFE_RELEASE(_programState2); } diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 17ab9163fb..6a2b28aaeb 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -399,12 +399,7 @@ void Sprite::setProgramState(backend::ProgramState *programState) { CCASSERT(programState, "argument should not be nullptr"); auto& pipelineDescriptor = _trianglesCommand.getPipelineDescriptor(); - if (_programState != programState) - { - CC_SAFE_RELEASE(_programState); - _programState = programState; - CC_SAFE_RETAIN(programState); - } + Node::setProgramState(programState); pipelineDescriptor.programState = _programState; _mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation(backend::Uniform::MVP_MATRIX); diff --git a/cocos/2d/CCSpriteBatchNode.cpp b/cocos/2d/CCSpriteBatchNode.cpp index b116afd319..1382b7e5f0 100644 --- a/cocos/2d/CCSpriteBatchNode.cpp +++ b/cocos/2d/CCSpriteBatchNode.cpp @@ -152,12 +152,7 @@ void SpriteBatchNode::setProgramState(backend::ProgramState *programState) { CCASSERT(programState, "programState should not be nullptr"); auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor(); - if (_programState != programState) - { - CC_SAFE_RELEASE(_programState); - _programState = programState; - CC_SAFE_RETAIN(programState); - } + Node::setProgramState(programState); pipelineDescriptor.programState = _programState; setVertexLayout(); diff --git a/cocos/3d/CCMotionStreak3D.cpp b/cocos/3d/CCMotionStreak3D.cpp index 14a64e5b64..67ed8a585e 100644 --- a/cocos/3d/CCMotionStreak3D.cpp +++ b/cocos/3d/CCMotionStreak3D.cpp @@ -119,7 +119,7 @@ bool MotionStreak3D::initWithFade(float fade, float minSeg, float stroke, const // shader state auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR); - _programState = new backend::ProgramState(program); + attachProgramState(new backend::ProgramState(program)); _customCommand.getPipelineDescriptor().programState = _programState; diff --git a/cocos/3d/CCSkybox.cpp b/cocos/3d/CCSkybox.cpp index be199e2bc5..bf7a71fed3 100644 --- a/cocos/3d/CCSkybox.cpp +++ b/cocos/3d/CCSkybox.cpp @@ -65,7 +65,7 @@ bool Skybox::init() // create and set our custom shader auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::SKYBOX_3D); - _programState = new backend::ProgramState(program); + attachProgramState(new backend::ProgramState(program)); auto &pipelineDescriptor = _customCommand.getPipelineDescriptor(); auto layout = _programState->getVertexLayout(); diff --git a/cocos/3d/CCTerrain.cpp b/cocos/3d/CCTerrain.cpp index 52dac65378..49f0f82b09 100644 --- a/cocos/3d/CCTerrain.cpp +++ b/cocos/3d/CCTerrain.cpp @@ -118,7 +118,7 @@ void cocos2d::Terrain::setLightDir(const Vec3& lightDir) bool Terrain::initProperties() { auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::TERRAIN_3D); - _programState = new backend::ProgramState(program); + attachProgramState(new backend::ProgramState(program)); _stateBlock.depthWrite = true; _stateBlock.depthTest = true; diff --git a/cocos/editor-support/cocostudio/ActionTimeline/CCBoneNode.cpp b/cocos/editor-support/cocostudio/ActionTimeline/CCBoneNode.cpp index b81ea6a2ab..1310889f66 100644 --- a/cocos/editor-support/cocostudio/ActionTimeline/CCBoneNode.cpp +++ b/cocos/editor-support/cocostudio/ActionTimeline/CCBoneNode.cpp @@ -415,7 +415,7 @@ bool BoneNode::init() auto& pipelineDescriptor = _customCommand.getPipelineDescriptor(); auto* program = cocos2d::backend::Program::getBuiltinProgram(cocos2d::backend::ProgramType::POSITION_COLOR); // TODO: noMVP? - _programState = new (std::nothrow) cocos2d::backend::ProgramState(program); + attachProgramState(new (std::nothrow) cocos2d::backend::ProgramState(program)); pipelineDescriptor.programState = _programState; _mvpLocation = _programState->getUniformLocation("u_MVPMatrix"); diff --git a/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.cpp b/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.cpp index ec75297f41..f9f5b5694b 100644 --- a/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.cpp +++ b/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.cpp @@ -54,7 +54,7 @@ bool SkeletonNode::init() // init _customCommand auto& pipelineDescriptor = _customCommand.getPipelineDescriptor(); auto* program = cocos2d::backend::Program::getBuiltinProgram(cocos2d::backend::ProgramType::POSITION_COLOR); // TODO: noMVP? - _programState = new (std::nothrow) cocos2d::backend::ProgramState(program); + attachProgramState(new (std::nothrow) cocos2d::backend::ProgramState(program)); pipelineDescriptor.programState = _programState; _mvpLocation = _programState->getUniformLocation("u_MVPMatrix");