1. protected render command construction/destruction function.

2. use renderCommandPool<T> to allocate and deallocate command
3. use releaseToCommandPool interface to push command back to pool
This commit is contained in:
Huabing.Xu 2013-12-03 14:08:47 +08:00
parent 44f12ce8e4
commit 757f1abc7f
15 changed files with 69 additions and 23 deletions

View File

@ -695,7 +695,7 @@ void LayerColor::updateColor()
void LayerColor::draw()
{
kmGLGetMatrix(KM_GL_MODELVIEW, &_transformMatrix);
CustomCommand* cmd = new CustomCommand();
CustomCommand* cmd = CustomCommand::getCommandPool().generateCommand();
cmd->init(0, _vertexZ);
cmd->func = CC_CALLBACK_0(LayerColor::onDraw, this);
Renderer::getInstance()->addCommand(cmd);

View File

@ -415,7 +415,7 @@ void ParticleSystemQuad::draw()
CCASSERT( _particleIdx == _particleCount, "Abnormal error in particle quad");
kmGLGetMatrix(KM_GL_MODELVIEW, &_transformMatrix);
CustomCommand* cmd = new CustomCommand();
CustomCommand* cmd = CustomCommand::getCommandPool().generateCommand();
cmd->init(0, _vertexZ);
cmd->func = CC_CALLBACK_0(ParticleSystemQuad::onDraw, this);
Renderer::getInstance()->addCommand(cmd);

View File

@ -663,7 +663,7 @@ void Sprite::draw(void)
{
updateQuadVertices();
//TODO implement z order
QuadCommand* renderCommand = new QuadCommand();
QuadCommand* renderCommand = QuadCommand::getCommandPool().generateCommand();
renderCommand->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1);
Renderer::getInstance()->addCommand(renderCommand);
}

View File

@ -44,7 +44,7 @@ void NewDrawNode::draw()
{
kmGLGetMatrix(KM_GL_MODELVIEW, &_transformMatrix);
CustomCommand* cmd = new CustomCommand();
CustomCommand* cmd = CustomCommand::getCommandPool().generateCommand();
cmd->init(0, _vertexZ);
cmd->func = CC_CALLBACK_0(NewDrawNode::onDraw, this);
Renderer::getInstance()->addCommand(cmd);

View File

@ -142,7 +142,7 @@ void NewSprite::draw(void)
}
//TODO implement z order
QuadCommand* renderCommand = new QuadCommand();
QuadCommand* renderCommand = QuadCommand::getCommandPool().generateCommand();
renderCommand->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1);
Renderer::getInstance()->addCommand(renderCommand);

View File

@ -60,7 +60,7 @@ void NewSpriteBatchNode::draw()
arrayMakeObjectsPerformSelector(_children, updateTransform, NewSprite*);
QuadCommand* cmd = new QuadCommand();
QuadCommand* cmd = QuadCommand::getCommandPool().generateCommand();
cmd->init(0, _vertexZ, _textureAtlas->getTexture()->getName(), _shaderProgram, _blendFunc, _textureAtlas->getQuads(), _textureAtlas->getTotalQuads());
Renderer::getInstance()->addCommand(cmd);
}

View File

@ -6,6 +6,7 @@
#include "CustomCommand.h"
NS_CC_BEGIN
RenderCommandPool<CustomCommand> CustomCommand::_commandPool;
CustomCommand::CustomCommand()
:RenderCommand()
@ -46,4 +47,9 @@ void CustomCommand::execute()
}
}
void CustomCommand::releaseToCommandPool()
{
getCommandPool().pushBackCommand(this);
}
NS_CC_END

View File

@ -8,16 +8,19 @@
#define _CC_CUSTOMCOMMAND_H_
#include "RenderCommand.h"
#include "RenderCommandPool.h"
NS_CC_BEGIN
using namespace std;
class CustomCommand : public RenderCommand
{
public:
protected:
CustomCommand();
void init(int viewport, int32_t depth);
~CustomCommand();
public:
void init(int viewport, int32_t depth);
// +----------+----------+-----+-----------------------------------+
// | | | | | |
@ -29,6 +32,7 @@ public:
void execute();
inline bool isTranslucent() { return true; }
virtual void releaseToCommandPool() override;
public:
function<void()> func;
@ -37,6 +41,12 @@ protected:
int _viewport;
int32_t _depth;
public:
friend class RenderCommandPool<CustomCommand>;
static RenderCommandPool<CustomCommand>& getCommandPool() { return _commandPool; }
protected:
static RenderCommandPool<CustomCommand> _commandPool;
};

View File

@ -7,6 +7,7 @@
#include "Renderer.h"
NS_CC_BEGIN
RenderCommandPool<GroupCommand> GroupCommand::_commandPool;
static GroupCommandManager* s_instance;
GroupCommandManager *GroupCommandManager::getInstance()
@ -96,5 +97,10 @@ int64_t GroupCommand::generateID()
return _id;
}
void GroupCommand::releaseToCommandPool()
{
getCommandPool().pushBackCommand(this);
}
NS_CC_END

View File

@ -9,6 +9,7 @@
#include "CCPlatformMacros.h"
#include "RenderCommand.h"
#include "RenderCommandPool.h"
#include <map>
NS_CC_BEGIN
@ -33,11 +34,11 @@ protected:
class GroupCommand : public RenderCommand
{
public:
protected:
GroupCommand();
void init(int viewport, int32_t depth);
~GroupCommand();
public:
void init(int viewport, int32_t depth);
// +----------+----------+-----+-----------------------------------+
// | | | | | |
@ -48,11 +49,18 @@ public:
inline bool isTranslucent() {return true;}
inline int getRenderQueueID() {return _renderQueueID;}
virtual void releaseToCommandPool() override;
protected:
int _viewport;
int32_t _depth;
int _renderQueueID;
public:
friend class RenderCommandPool<GroupCommand>;
static RenderCommandPool<GroupCommand>& getCommandPool() { return _commandPool; }
protected:
static RenderCommandPool<GroupCommand> _commandPool;
};
NS_CC_END

View File

@ -85,27 +85,27 @@ void NewClippingNode::visit()
Renderer* renderer = Renderer::getInstance();
GroupCommand* groupCommand = new GroupCommand();
GroupCommand* groupCommand = GroupCommand::getCommandPool().generateCommand();
groupCommand->init(0,_vertexZ);
renderer->addCommand(groupCommand);
renderer->pushGroup(groupCommand->getRenderQueueID());
CustomCommand* beforeVisitCmd = new CustomCommand();
CustomCommand* beforeVisitCmd = CustomCommand::getCommandPool().generateCommand();
beforeVisitCmd->init(0,_vertexZ);
beforeVisitCmd->func = CC_CALLBACK_0(NewClippingNode::beforeVisit, this);
renderer->addCommand(beforeVisitCmd, groupCommand->getRenderQueueID());
_stencil->visit();
CustomCommand* afterDrawStencilCmd = new CustomCommand();
CustomCommand* afterDrawStencilCmd = CustomCommand::getCommandPool().generateCommand();
afterDrawStencilCmd->init(0,_vertexZ);
afterDrawStencilCmd->func = CC_CALLBACK_0(NewClippingNode::afterDrawStencil, this);
renderer->addCommand(afterDrawStencilCmd, groupCommand->getRenderQueueID());
Node::visit();
CustomCommand* afterVisitCmd = new CustomCommand();
CustomCommand* afterVisitCmd = CustomCommand::getCommandPool().generateCommand();
afterVisitCmd->init(0,_vertexZ);
afterVisitCmd->func = CC_CALLBACK_0(NewClippingNode::afterVisit, this);
renderer->addCommand(afterVisitCmd, groupCommand->getRenderQueueID());

View File

@ -7,6 +7,7 @@
#include "ccGLStateCache.h"
NS_CC_BEGIN
RenderCommandPool<QuadCommand> QuadCommand::_commandPool;
QuadCommand::QuadCommand()
:RenderCommand()
@ -97,4 +98,9 @@ void QuadCommand::useMaterial()
GL::blendFunc(_blendType.src, _blendType.dst);
}
void QuadCommand::releaseToCommandPool()
{
getCommandPool().pushBackCommand(this);
}
NS_CC_END

View File

@ -9,6 +9,7 @@
#include "RenderCommand.h"
#include "CCGLProgram.h"
#include "RenderCommandPool.h"
NS_CC_BEGIN
@ -16,10 +17,12 @@ NS_CC_BEGIN
class QuadCommand : public RenderCommand
{
public:
protected:
QuadCommand();
void init(int viewport, int32_t depth, GLuint texutreID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, int quadCount);
~QuadCommand();
public:
void init(int viewport, int32_t depth, GLuint texutreID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, int quadCount);
// +----------+----------+-----+-----------------------------------+
// | | | | | |
@ -44,6 +47,8 @@ public:
inline GLProgram* getShader() { return _shader; }
inline BlendFunc getBlendType() { return _blendType; }
virtual void releaseToCommandPool() override;
protected:
int32_t _materialID;
@ -64,6 +69,11 @@ protected:
V3F_C4B_T2F_Quad* _quad;
int _quadCount;
public:
friend class RenderCommandPool<QuadCommand>;
static RenderCommandPool<QuadCommand>& getCommandPool() { return _commandPool; }
protected:
static RenderCommandPool<QuadCommand> _commandPool;
};
NS_CC_END

View File

@ -25,19 +25,19 @@ enum RenderCommandType
//TODO make RenderCommand inherent from Object
class RenderCommand
{
public:
protected:
RenderCommand();
virtual ~RenderCommand();
public:
virtual int64_t generateID() = 0;
virtual /**
* Get Render Command Id
*/
inline int64_t getID() { return _id; }
virtual inline RenderCommandType getType() { return _type; }
virtual void releaseToCommandPool() =0;
protected:
void printID();

View File

@ -245,7 +245,7 @@ void Renderer::render()
//TODO give command back to command pool
for (size_t j = 0 ; j < _renderGroups.size(); j++)
{
for_each(_renderGroups[j].begin(), _renderGroups[j].end(), [](RenderCommand* cmd){delete cmd;});
for_each(_renderGroups[j].begin(), _renderGroups[j].end(), [](RenderCommand* cmd){ cmd->releaseToCommandPool(); });
_renderGroups[j].clear();
}