Renderer Only knows about Triangles Command, every command inherit from Triangles Command can be batched

This commit is contained in:
Huabing.Xu 2014-08-28 09:58:39 +08:00
parent c627affa5e
commit bdf92cfbd4
2 changed files with 17 additions and 16 deletions

View File

@ -124,7 +124,7 @@ Renderer::Renderer()
RenderQueue defaultRenderQueue; RenderQueue defaultRenderQueue;
_renderGroups.push_back(defaultRenderQueue); _renderGroups.push_back(defaultRenderQueue);
_batchedQuadCommands.reserve(BATCH_QUADCOMMAND_RESEVER_SIZE); _batchedCommands.reserve(BATCH_QUADCOMMAND_RESEVER_SIZE);
} }
Renderer::~Renderer() Renderer::~Renderer()
@ -269,10 +269,10 @@ void Renderer::visitRenderQueue(const RenderQueue& queue)
{ {
auto command = queue[index]; auto command = queue[index];
auto commandType = command->getType(); auto commandType = command->getType();
if(RenderCommand::Type::QUAD_COMMAND == commandType) if(RenderCommand::Type::QUAD_COMMAND == commandType || RenderCommand::Type::TRIANGLES_COMMAND == commandType)
{ {
flush3D(); flush3D();
auto cmd = static_cast<QuadCommand*>(command); auto cmd = static_cast<TrianglesCommand*>(command);
//Batch quads //Batch quads
if( _filledVertex + cmd->getVertexCount() > VBO_SIZE || _filledIndex + cmd->getIndexCount() > INDEX_VBO_SIZE) if( _filledVertex + cmd->getVertexCount() > VBO_SIZE || _filledIndex + cmd->getIndexCount() > INDEX_VBO_SIZE)
{ {
@ -282,9 +282,9 @@ void Renderer::visitRenderQueue(const RenderQueue& queue)
drawBatchedQuads(); drawBatchedQuads();
} }
_batchedQuadCommands.push_back(cmd); _batchedCommands.push_back(cmd);
fillQuadVertices(cmd); fillVerticesAndIndices(cmd);
} }
else if(RenderCommand::Type::GROUP_COMMAND == commandType) else if(RenderCommand::Type::GROUP_COMMAND == commandType)
@ -374,16 +374,16 @@ void Renderer::clean()
} }
// Clear batch quad commands // Clear batch quad commands
_batchedQuadCommands.clear(); _batchedCommands.clear();
_filledVertex = 0; _filledVertex = 0;
_filledIndex = 0; _filledIndex = 0;
_lastMaterialID = 0; _lastMaterialID = 0;
_lastBatchedMeshCommand = nullptr; _lastBatchedMeshCommand = nullptr;
} }
void Renderer::fillQuadVertices(const QuadCommand* cmd) void Renderer::fillVerticesAndIndices(const TrianglesCommand* cmd)
{ {
memcpy(_verts + _filledVertex, cmd->getQuads(), sizeof(V3F_C4B_T2F) * cmd->getVertexCount()); memcpy(_verts + _filledVertex, cmd->getVertices(), sizeof(V3F_C4B_T2F) * cmd->getVertexCount());
const Mat4& modelView = cmd->getModelView(); const Mat4& modelView = cmd->getModelView();
for(ssize_t i=0; i< cmd->getVertexCount(); ++i) for(ssize_t i=0; i< cmd->getVertexCount(); ++i)
@ -414,7 +414,7 @@ void Renderer::drawBatchedQuads()
int startIndex = 0; int startIndex = 0;
//Upload buffer to VBO //Upload buffer to VBO
if(_filledVertex <= 0 || _filledIndex <= 0 || _batchedQuadCommands.empty()) if(_filledVertex <= 0 || _filledIndex <= 0 || _batchedCommands.empty())
{ {
return; return;
} }
@ -466,10 +466,10 @@ void Renderer::drawBatchedQuads()
} }
//Start drawing verties in batch //Start drawing verties in batch
for(const auto& cmd : _batchedQuadCommands) for(const auto& cmd : _batchedCommands)
{ {
auto newMaterialID = cmd->getMaterialID(); auto newMaterialID = cmd->getMaterialID();
if(_lastMaterialID != newMaterialID || newMaterialID == QuadCommand::MATERIAL_ID_DO_NOT_BATCH) if(_lastMaterialID != newMaterialID || newMaterialID == TrianglesCommand::MATERIAL_ID_DO_NOT_BATCH)
{ {
//Draw quads //Draw quads
if(indexToDraw > 0) if(indexToDraw > 0)
@ -509,7 +509,7 @@ void Renderer::drawBatchedQuads()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
_batchedQuadCommands.clear(); _batchedCommands.clear();
_filledVertex = 0; _filledVertex = 0;
_filledIndex = 0; _filledIndex = 0;
} }

View File

@ -38,6 +38,7 @@ NS_CC_BEGIN
class EventListenerCustom; class EventListenerCustom;
class QuadCommand; class QuadCommand;
class TrianglesCommand;
class MeshCommand; class MeshCommand;
/** Class that knows how to sort `RenderCommand` objects. /** Class that knows how to sort `RenderCommand` objects.
@ -75,8 +76,8 @@ Whenever possible prefer to use `QuadCommand` objects since the renderer will au
class CC_DLL Renderer class CC_DLL Renderer
{ {
public: public:
static const int VBO_SIZE = 65536; static const int VBO_SIZE = 8192;
static const int INDEX_VBO_SIZE = 65536 * 6 / 4; static const int INDEX_VBO_SIZE = 8192 * 6 / 4;
static const int BATCH_QUADCOMMAND_RESEVER_SIZE = 64; static const int BATCH_QUADCOMMAND_RESEVER_SIZE = 64;
@ -140,7 +141,7 @@ protected:
void visitRenderQueue(const RenderQueue& queue); void visitRenderQueue(const RenderQueue& queue);
void fillQuadVertices(const QuadCommand* cmd); void fillVerticesAndIndices(const TrianglesCommand* cmd);
std::stack<int> _commandGroupStack; std::stack<int> _commandGroupStack;
@ -149,7 +150,7 @@ protected:
uint32_t _lastMaterialID; uint32_t _lastMaterialID;
MeshCommand* _lastBatchedMeshCommand; MeshCommand* _lastBatchedMeshCommand;
std::vector<QuadCommand*> _batchedQuadCommands; std::vector<TrianglesCommand*> _batchedCommands;
V3F_C4B_T2F _verts[VBO_SIZE]; V3F_C4B_T2F _verts[VBO_SIZE];
GLushort _indices[INDEX_VBO_SIZE]; GLushort _indices[INDEX_VBO_SIZE];