Take care _programState use

This commit is contained in:
halx99 2020-07-21 23:11:03 +08:00
parent d90aa8708f
commit a735abaf65
17 changed files with 29 additions and 44 deletions

View File

@ -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");

View File

@ -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);

View File

@ -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();

View File

@ -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");

View File

@ -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");

View File

@ -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

View File

@ -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);

View File

@ -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");

View File

@ -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");

View File

@ -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);
}

View File

@ -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);

View File

@ -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();

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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");

View File

@ -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");