diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index 39c726315d..f6dbf8be87 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -149,7 +149,10 @@ bool Director::init(void) _eventDispatcher = new EventDispatcher(); //init TextureCache initTextureCache(); - + + // Renderer + _renderer = new Renderer; + // create autorelease pool PoolManager::sharedPoolManager()->push(); @@ -169,7 +172,9 @@ Director::~Director(void) CC_SAFE_RELEASE(_scheduler); CC_SAFE_RELEASE(_actionManager); CC_SAFE_RELEASE(_eventDispatcher); - + + delete _renderer; + // pop the autorelease pool PoolManager::sharedPoolManager()->pop(); PoolManager::purgePoolManager(); @@ -288,7 +293,7 @@ void Director::drawScene() showStats(); } - Renderer::getInstance()->render(); + _renderer->render(); kmGLPopMatrix(); @@ -368,7 +373,7 @@ void Director::setOpenGLView(EGLView *openGLView) setGLDefaultValues(); } - Renderer::getInstance()->initGLView(); + _renderer->initGLView(); CHECK_GL_ERROR_DEBUG(); @@ -1027,6 +1032,12 @@ void Director::setEventDispatcher(EventDispatcher* dispatcher) } } +Renderer* Director::getRenderer() const +{ + return _renderer; +} + + /*************************************************** * implementation of DisplayLinkDirector **************************************************/ diff --git a/cocos/2d/CCDirector.h b/cocos/2d/CCDirector.h index 67e4ad5153..f6836afcfb 100644 --- a/cocos/2d/CCDirector.h +++ b/cocos/2d/CCDirector.h @@ -56,6 +56,7 @@ class ActionManager; class EventDispatcher; class TextureCache; class Frustum; +class Renderer; /** @brief Class that creates and handles the main Window and manages how @@ -338,7 +339,6 @@ public: Frustum* getFrustum() const { return _cullingFrustum; } -public: /** Gets the Scheduler associated with this director @since v2.0 */ @@ -368,7 +368,12 @@ public: @since v3.0 */ void setEventDispatcher(EventDispatcher* dispatcher); - + + /** Returns the Renderer + @since v3.0 + */ + Renderer* getRenderer() const; + /* Gets delta time since last tick to main loop */ float getDeltaTime() const; @@ -395,16 +400,15 @@ protected: void initTextureCache(); void destroyTextureCache(); -protected: /** Scheduler associated with this director @since v2.0 */ - Scheduler* _scheduler; + Scheduler *_scheduler; /** ActionManager associated with this director @since v2.0 */ - ActionManager* _actionManager; + ActionManager *_actionManager; /** EventDispatcher associated with this director @since v3.0 @@ -415,7 +419,7 @@ protected: float _deltaTime; /* The EGLView, where everything is rendered */ - EGLView *_openGLView; + EGLView *_openGLView; //texture cache belongs to this director TextureCache *_textureCache; @@ -442,7 +446,7 @@ protected: unsigned int _frames; float _secondsPerFrame; - Frustum* _cullingFrustum; + Frustum *_cullingFrustum; /* The running scene */ Scene *_runningScene; @@ -452,7 +456,7 @@ protected: Scene *_nextScene; /* If true, then "old" scene will receive the cleanup message */ - bool _sendCleanupToScene; + bool _sendCleanupToScene; /* scheduled scenes */ Vector _scenesStack; @@ -467,10 +471,10 @@ protected: Projection _projection; /* window size in points */ - Size _winSizeInPoints; + Size _winSizeInPoints; /* content scale factor */ - float _contentScaleFactor; + float _contentScaleFactor; /* store the fps string */ char *_FPS; @@ -480,6 +484,8 @@ protected: /* Projection protocol delegate */ DirectorDelegate *_projectionDelegate; + + Renderer *_renderer; // EGLViewProtocol will recreate stats labels to fit visible rect friend class EGLViewProtocol; diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index be3435880f..20e49ecc1b 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -704,7 +704,7 @@ void LayerColor::draw() CustomCommand* cmd = CustomCommand::getCommandPool().generateCommand(); cmd->init(0, _vertexZ); cmd->func = CC_CALLBACK_0(LayerColor::onDraw, this); - Renderer::getInstance()->addCommand(cmd); + Director::getInstance()->getRenderer()->addCommand(cmd); } void LayerColor::onDraw() diff --git a/cocos/2d/CCParticleBatchNode.cpp b/cocos/2d/CCParticleBatchNode.cpp index 7b8d700082..0448a940b1 100644 --- a/cocos/2d/CCParticleBatchNode.cpp +++ b/cocos/2d/CCParticleBatchNode.cpp @@ -423,7 +423,7 @@ void ParticleBatchNode::draw(void) _textureAtlas->getQuads(), _textureAtlas->getTotalQuads(), mv); - Renderer::getInstance()->addCommand(cmd); + Director::getInstance()->getRenderer()->addCommand(cmd); CC_PROFILER_STOP("CCParticleBatchNode - draw"); } diff --git a/cocos/2d/CCParticleSystemQuad.cpp b/cocos/2d/CCParticleSystemQuad.cpp index a971ad1fbc..a6965c3ebd 100644 --- a/cocos/2d/CCParticleSystemQuad.cpp +++ b/cocos/2d/CCParticleSystemQuad.cpp @@ -446,7 +446,7 @@ void ParticleSystemQuad::draw() QuadCommand* cmd = QuadCommand::getCommandPool().generateCommand(); cmd->init(0, _vertexZ, _texture->getName(), shader, _blendFunc, _quads, _particleIdx, _modelViewTransform); - Renderer::getInstance()->addCommand(cmd); + Director::getInstance()->getRenderer()->addCommand(cmd); } } diff --git a/cocos/2d/CCProgressTimer.cpp b/cocos/2d/CCProgressTimer.cpp index a1c2c4ac5c..75bd34b4e1 100644 --- a/cocos/2d/CCProgressTimer.cpp +++ b/cocos/2d/CCProgressTimer.cpp @@ -557,7 +557,7 @@ void ProgressTimer::draw() CustomCommand* cmd = CustomCommand::getCommandPool().generateCommand(); cmd->init(0, _vertexZ); cmd->func = CC_CALLBACK_0(ProgressTimer::onDraw, this); - Renderer::getInstance()->addCommand(cmd); + Director::getInstance()->getRenderer()->addCommand(cmd); } diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 5af035faea..01cf81a4cc 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -673,7 +673,7 @@ void Sprite::draw(void) //TODO implement z order QuadCommand* renderCommand = QuadCommand::getCommandPool().generateCommand(); renderCommand->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1, mv); - Renderer::getInstance()->addCommand(renderCommand); + Director::getInstance()->getRenderer()->addCommand(renderCommand); } void Sprite::updateQuadVertices() diff --git a/cocos/2d/renderer/CCNewDrawNode.cpp b/cocos/2d/renderer/CCNewDrawNode.cpp index c0379eb000..dd37006bd9 100644 --- a/cocos/2d/renderer/CCNewDrawNode.cpp +++ b/cocos/2d/renderer/CCNewDrawNode.cpp @@ -7,6 +7,7 @@ #include "QuadCommand.h" #include "Renderer.h" #include "CustomCommand.h" +#include "CCDirector.h" NS_CC_BEGIN @@ -45,7 +46,7 @@ void NewDrawNode::draw() CustomCommand* cmd = CustomCommand::getCommandPool().generateCommand(); cmd->init(0, _vertexZ); cmd->func = CC_CALLBACK_0(NewDrawNode::onDraw, this); - Renderer::getInstance()->addCommand(cmd); + Director::getInstance()->getRenderer()->addCommand(cmd); } void NewDrawNode::onDraw() diff --git a/cocos/2d/renderer/CCNewLabelAtlas.cpp b/cocos/2d/renderer/CCNewLabelAtlas.cpp index 004f021693..b9718d923e 100644 --- a/cocos/2d/renderer/CCNewLabelAtlas.cpp +++ b/cocos/2d/renderer/CCNewLabelAtlas.cpp @@ -60,7 +60,7 @@ void NewLabelAtlas::draw() _textureAtlas->getTotalQuads(), mv); - Renderer::getInstance()->addCommand(cmd); + Director::getInstance()->getRenderer()->addCommand(cmd); } diff --git a/cocos/2d/renderer/CCNewRenderTexture.cpp b/cocos/2d/renderer/CCNewRenderTexture.cpp index 3d12f831e5..a1b1d2d974 100644 --- a/cocos/2d/renderer/CCNewRenderTexture.cpp +++ b/cocos/2d/renderer/CCNewRenderTexture.cpp @@ -62,7 +62,7 @@ void NewRenderTexture::draw() CustomCommand* clearCmd = CustomCommand::getCommandPool().generateCommand(); clearCmd->init(0, _vertexZ); clearCmd->func = CC_CALLBACK_0(NewRenderTexture::onClear, this); - Renderer::getInstance()->addCommand(clearCmd); + Director::getInstance()->getRenderer()->addCommand(clearCmd); //! make sure all children are drawn sortAllChildren(); @@ -109,7 +109,7 @@ void NewRenderTexture::beginWithClear(float r, float g, float b, float a, float CustomCommand* clearCmd = CustomCommand::getCommandPool().generateCommand(); clearCmd->init(0, _vertexZ); clearCmd->func = CC_CALLBACK_0(NewRenderTexture::onClear, this); - Renderer::getInstance()->addCommand(clearCmd); + Director::getInstance()->getRenderer()->addCommand(clearCmd); } void NewRenderTexture::begin() @@ -125,14 +125,15 @@ void NewRenderTexture::begin() GroupCommand* groupCommand = GroupCommand::getCommandPool().generateCommand(); groupCommand->init(0, _vertexZ); - Renderer::getInstance()->addCommand(groupCommand); - Renderer::getInstance()->pushGroup(groupCommand->getRenderQueueID()); + Renderer *renderer = Director::getInstance()->getRenderer(); + renderer->addCommand(groupCommand); + renderer->pushGroup(groupCommand->getRenderQueueID()); CustomCommand* beginCmd = CustomCommand::getCommandPool().generateCommand(); beginCmd->init(0, _vertexZ); beginCmd->func = CC_CALLBACK_0(NewRenderTexture::onBegin, this); - Renderer::getInstance()->addCommand(beginCmd); + Director::getInstance()->getRenderer()->addCommand(beginCmd); } void NewRenderTexture::end() @@ -141,9 +142,9 @@ void NewRenderTexture::end() endCmd->init(0, _vertexZ); endCmd->func = CC_CALLBACK_0(NewRenderTexture::onEnd, this); - Renderer::getInstance()->addCommand(endCmd); - - Renderer::getInstance()->popGroup(); + Renderer *renderer = Director::getInstance()->getRenderer(); + renderer->addCommand(endCmd); + renderer->popGroup(); } void NewRenderTexture::onBegin() @@ -263,7 +264,7 @@ void NewRenderTexture::clearDepth(float depthValue) cmd->init(0, _vertexZ); cmd->func = CC_CALLBACK_0(NewRenderTexture::onClearDepth, this); - Renderer::getInstance()->addCommand(cmd); + Director::getInstance()->getRenderer()->addCommand(cmd); this->end(); } diff --git a/cocos/2d/renderer/CCNewSprite.cpp b/cocos/2d/renderer/CCNewSprite.cpp index 685f854052..97f9e692d8 100644 --- a/cocos/2d/renderer/CCNewSprite.cpp +++ b/cocos/2d/renderer/CCNewSprite.cpp @@ -118,7 +118,7 @@ void NewSprite::draw(void) return; } - Renderer::getInstance()->addCommand(renderCommand); + Director::getInstance()->getRenderer()->addCommand(renderCommand); } bool NewSprite::culling() const diff --git a/cocos/2d/renderer/CCNewSpriteBatchNode.cpp b/cocos/2d/renderer/CCNewSpriteBatchNode.cpp index 11c11ccb35..1183e99c93 100644 --- a/cocos/2d/renderer/CCNewSpriteBatchNode.cpp +++ b/cocos/2d/renderer/CCNewSpriteBatchNode.cpp @@ -77,7 +77,7 @@ void NewSpriteBatchNode::draw() _textureAtlas->getQuads(), _textureAtlas->getTotalQuads(), mv); - Renderer::getInstance()->addCommand(cmd); + Director::getInstance()->getRenderer()->addCommand(cmd); } NS_CC_END \ No newline at end of file diff --git a/cocos/2d/renderer/GroupCommand.cpp b/cocos/2d/renderer/GroupCommand.cpp index 46939b267e..977cf603f4 100644 --- a/cocos/2d/renderer/GroupCommand.cpp +++ b/cocos/2d/renderer/GroupCommand.cpp @@ -5,6 +5,7 @@ #include "GroupCommand.h" #include "Renderer.h" +#include "CCDirector.h" NS_CC_BEGIN RenderCommandPool GroupCommand::_commandPool; @@ -53,7 +54,7 @@ int GroupCommandManager::getGroupID() //Create new ID // int newID = _groupMapping.size(); - int newID = Renderer::getInstance()->createRenderQueue(); + int newID = Director::getInstance()->getRenderer()->createRenderQueue(); _groupMapping[newID] = true; return newID; diff --git a/cocos/2d/renderer/NewClippingNode.cpp b/cocos/2d/renderer/NewClippingNode.cpp index 425c63fcb8..70f95da9ab 100644 --- a/cocos/2d/renderer/NewClippingNode.cpp +++ b/cocos/2d/renderer/NewClippingNode.cpp @@ -8,6 +8,7 @@ #include "Renderer.h" #include "CustomCommand.h" #include "CCShaderCache.h" +#include "CCDirector.h" NS_CC_BEGIN @@ -79,7 +80,7 @@ void NewClippingNode::visit() { //Add group command - Renderer* renderer = Renderer::getInstance(); + Renderer* renderer = Director::getInstance()->getRenderer(); GroupCommand* groupCommand = GroupCommand::getCommandPool().generateCommand(); groupCommand->init(0,_vertexZ); diff --git a/cocos/2d/renderer/Renderer.cpp b/cocos/2d/renderer/Renderer.cpp index 66b89c19ae..484f017591 100644 --- a/cocos/2d/renderer/Renderer.cpp +++ b/cocos/2d/renderer/Renderer.cpp @@ -17,25 +17,8 @@ NS_CC_BEGIN using namespace std; -static Renderer* s_instance = nullptr; -Renderer *Renderer::getInstance() -{ - if(!s_instance) - { - s_instance = new Renderer(); - if(!s_instance->init()) - { - CC_SAFE_DELETE(s_instance); - } - } - return s_instance; -} - -void Renderer::destroyInstance() -{ - CC_SAFE_RELEASE_NULL(s_instance); -} +#define DEFAULT_RENDER_QUEUE 0 Renderer::Renderer() :_lastMaterialID(0) @@ -64,11 +47,6 @@ Renderer::~Renderer() } } -bool Renderer::init() -{ - return true; -} - void Renderer::initGLView() { #if CC_ENABLE_CACHE_TEXTURE_DATA @@ -94,7 +72,7 @@ void Renderer::onBackToForeground(Object* obj) void Renderer::setupIndices() { - for( int i=0; i < VBO_SIZE; i++) + for( int i=0; i < vbo_size; i++) { _indices[i*6+0] = (GLushort) (i*4+0); _indices[i*6+1] = (GLushort) (i*4+1); @@ -125,7 +103,7 @@ void Renderer::setupVBOAndVAO() glGenBuffers(2, &_buffersVBO[0]); glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); - glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * VBO_SIZE, _quads, GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * vbo_size, _quads, GL_DYNAMIC_DRAW); // vertices glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); @@ -140,7 +118,7 @@ void Renderer::setupVBOAndVAO() glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * VBO_SIZE * 6, _indices, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * vbo_size * 6, _indices, GL_STATIC_DRAW); // Must unbind the VAO before changing the element buffer. GL::bindVAO(0); @@ -163,11 +141,11 @@ void Renderer::mapBuffers() GL::bindVAO(0); glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); - glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * VBO_SIZE, _quads, GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * vbo_size, _quads, GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * VBO_SIZE * 6, _indices, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * vbo_size * 6, _indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); CHECK_GL_ERROR_DEBUG(); @@ -242,9 +220,9 @@ void Renderer::render() QuadCommand* cmd = static_cast(command); //Batch quads - if(_numQuads + cmd->getQuadCount() > VBO_SIZE) + if(_numQuads + cmd->getQuadCount() > vbo_size) { - CCASSERT(cmd->getQuadCount() < VBO_SIZE, "VBO is not big enough for quad data, please break the quad data down or use customized render command"); + CCASSERT(cmd->getQuadCount() < vbo_size, "VBO is not big enough for quad data, please break the quad data down or use customized render command"); //Draw batched quads if VBO is full drawBatchedQuads(); diff --git a/cocos/2d/renderer/Renderer.h b/cocos/2d/renderer/Renderer.h index 712569ac50..f083045e53 100644 --- a/cocos/2d/renderer/Renderer.h +++ b/cocos/2d/renderer/Renderer.h @@ -14,9 +14,6 @@ #include #include -#define VBO_SIZE 10500 -#define DEFAULT_RENDER_QUEUE 0 - NS_CC_BEGIN typedef std::vector RenderQueue; @@ -27,12 +24,14 @@ struct RenderStackElement size_t currentIndex; }; -class Renderer : public Object +class Renderer { public: - static Renderer* getInstance(); - static void destroyInstance(); - + static const int vbo_size = 65536 / 6; + + Renderer(); + ~Renderer(); + //TODO manage GLView inside Render itself void initGLView(); @@ -46,11 +45,7 @@ public: void render(); protected: - Renderer(); - ~Renderer(); - bool init(); - void setupIndices(); //Setup VBO or VAO based on OpenGL extensions void setupBuffer(); @@ -64,7 +59,6 @@ protected: void onBackToForeground(Object* obj); -protected: std::stack _commandGroupStack; std::stack _renderStack; @@ -75,8 +69,8 @@ protected: size_t _firstCommand; size_t _lastCommand; - V3F_C4B_T2F_Quad _quads[VBO_SIZE]; - GLushort _indices[6 * VBO_SIZE]; + V3F_C4B_T2F_Quad _quads[vbo_size]; + GLushort _indices[6 * vbo_size]; GLuint _quadVAO; GLuint _buffersVBO[2]; //0: vertex 1: indices diff --git a/cocos/editor-support/cocostudio/CCBatchNode.cpp b/cocos/editor-support/cocostudio/CCBatchNode.cpp index 1963cdd2fc..4667bcd9ac 100644 --- a/cocos/editor-support/cocostudio/CCBatchNode.cpp +++ b/cocos/editor-support/cocostudio/CCBatchNode.cpp @@ -146,7 +146,7 @@ void BatchNode::draw() } else { - Renderer::getInstance()->popGroup(); + Director::getInstance()->getRenderer()->popGroup(); _popGroupCommand = true; ((Node *)object)->visit(); @@ -156,7 +156,7 @@ void BatchNode::draw() void BatchNode::generateGroupCommand() { - Renderer* renderer = Renderer::getInstance(); + Renderer* renderer = Director::getInstance()->getRenderer(); GroupCommand* groupCommand = GroupCommand::getCommandPool().generateCommand(); groupCommand->init(0,_vertexZ); renderer->addCommand(groupCommand);