diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id index 563be79011..3a7bf34522 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -66497ec4f15de06d504b96da6ade60c7ad59ab44 \ No newline at end of file +a8deccd72bda63a9abc1fd55c4812468dc021fa8 \ No newline at end of file diff --git a/cocos/2d/CCNewSprite.cpp b/cocos/2d/CCNewSprite.cpp index e259a7b27f..77c33318ff 100644 --- a/cocos/2d/CCNewSprite.cpp +++ b/cocos/2d/CCNewSprite.cpp @@ -47,8 +47,9 @@ void NewSprite::draw(void) kmMat4 transform; kmGLGetMatrix(KM_GL_MODELVIEW, &transform); RenderCommand* renderCommand = new RenderCommand(); - renderCommand->setData(0, true, false, _ZOrder); - renderCommand->setQuadData(&transform, _quad, _texture->getName(), 0, 0); + renderCommand->setKeyData(0, true, false, _ZOrder); + renderCommand->setMaterialData(_texture->getName(), _shaderProgram->getProgram(), _blendFunc); + renderCommand->setQuadData(&transform, _quad); Renderer::getInstance()->addRenderCommand(renderCommand); } diff --git a/cocos/2d/ccTypes.h b/cocos/2d/ccTypes.h index f7bb8d19cb..2e6d1846af 100644 --- a/cocos/2d/ccTypes.h +++ b/cocos/2d/ccTypes.h @@ -329,6 +329,16 @@ struct BlendFunc const static BlendFunc ALPHA_NON_PREMULTIPLIED; //! Enables Additive blending. Uses {GL_SRC_ALPHA, GL_ONE} const static BlendFunc ADDITIVE; + + bool const operator==(const BlendFunc &a) + { + return src == a.src && dst == a.dst; + } + + bool const operator<(const BlendFunc &a) + { + return src < a.src || (src < a.src && dst < a.dst); + } }; // Label::VAlignment diff --git a/cocos/2d/renderer/MaterialManager.cpp b/cocos/2d/renderer/MaterialManager.cpp new file mode 100644 index 0000000000..c5ec115cfb --- /dev/null +++ b/cocos/2d/renderer/MaterialManager.cpp @@ -0,0 +1,87 @@ +// +// Created by NiTe Luo on 11/6/13. +// + + +#include "MaterialManager.h" + +NS_CC_BEGIN + +using namespace std; + +static MaterialManager* s_instance = 0; + +MaterialManager *MaterialManager::getInstance() +{ + if(!s_instance) + { + s_instance = new MaterialManager(); + if(!s_instance->init()) + { + CC_SAFE_DELETE(s_instance); + } + } + return s_instance; +} + +void MaterialManager::destroyInstance() +{ + CC_SAFE_RELEASE_NULL(s_instance); +} + +void MaterialManager::getMaterialID(GLuint textureID, GLuint shaderID, BlendFunc blendFunc) +{ + +} + +void MaterialManager::registerTexture(GLuint textureID) +{ + +} + +void MaterialManager::unregisterTexture(GLuint textureID) +{ + +} + +void MaterialManager::registerShader(GLuint shaderID) +{ + +} + +void MaterialManager::unregisterShader(GLuint shaderID) +{ + +} + +MaterialManager::MaterialManager() +{ + +} + +MaterialManager::~MaterialManager() +{ + +} + +bool MaterialManager::init() +{ + return false; +} + +int MaterialManager::getTextureID(GLuint textureID) +{ + return 0; +} + +int MaterialManager::getShaderID(GLuint shaderID) +{ + return 0; +} + +int MaterialManager::getBlendFuncID(GLint blendFunc) +{ + return 0; +} + +NS_CC_END \ No newline at end of file diff --git a/cocos/2d/renderer/MaterialManager.h b/cocos/2d/renderer/MaterialManager.h new file mode 100644 index 0000000000..f0e6e8d902 --- /dev/null +++ b/cocos/2d/renderer/MaterialManager.h @@ -0,0 +1,50 @@ +// +// Created by NiTe Luo on 11/6/13. +// + + + +#ifndef _CC_MATERIALMANAGER_H_ +#define _CC_MATERIALMANAGER_H_ + +#include "CCPlatformMacros.h" +#include "CCObject.h" +#include "ccTypes.h" +#include + +NS_CC_BEGIN +using namespace std; + +class MaterialManager : public Object +{ +public: + static MaterialManager* getInstance(); + static void destroyInstance(); + + + void getMaterialID(GLuint textureID, GLuint shaderID, BlendFunc blendFunc); + + void registerTexture(GLuint textureID); + void unregisterTexture(GLuint textureID); + + void registerShader(GLuint shaderID); + void unregisterShader(GLuint shaderID); + +protected: + MaterialManager(); + virtual ~MaterialManager(); + + bool init(); + + int getTextureID(GLuint textureID); + int getShaderID(GLuint shaderID); + int getBlendFuncID(GLint blendFunc); + + map _textureIDMapping; + map _shaderIDMapping; + map _blendFuncMapping; +}; + +NS_CC_END + +#endif //_CC_MATERIALMANAGER_H_ diff --git a/cocos/2d/renderer/QuadCommand.cpp b/cocos/2d/renderer/QuadCommand.cpp new file mode 100644 index 0000000000..be2f14a365 --- /dev/null +++ b/cocos/2d/renderer/QuadCommand.cpp @@ -0,0 +1,6 @@ +// +// Created by NiTe Luo on 11/6/13. +// + + +#include "QuadCommand.h" diff --git a/cocos/2d/renderer/QuadCommand.h b/cocos/2d/renderer/QuadCommand.h new file mode 100644 index 0000000000..94d96c8ef6 --- /dev/null +++ b/cocos/2d/renderer/QuadCommand.h @@ -0,0 +1,32 @@ +// +// Created by NiTe Luo on 11/6/13. +// + + + +#ifndef _CC_QUADCOMMAND_H_ +#define _CC_QUADCOMMAND_H_ + +#include "RenderCommand.h" + +NS_CC_BEGIN + + +// +----------+----------+-----+-----------------------------------+ +// | | | | | | +// | ViewPort | Transluc | Cmd | Depth | Material ID | +// | 3 bits | 1 bit | 1 | 24 bits | 24 bit2 | +// +----------+----------+-----+----------------+------------------+ +class QuadCommandID : public RenderCommandID +{ + +}; + +class QuadCommand : public RenderCommand +{ + +}; + +NS_CC_END + +#endif //_CC_QUADCOMMAND_H_ diff --git a/cocos/2d/renderer/RenderCommand.cpp b/cocos/2d/renderer/RenderCommand.cpp index dc99e0670e..a514bd963b 100644 --- a/cocos/2d/renderer/RenderCommand.cpp +++ b/cocos/2d/renderer/RenderCommand.cpp @@ -21,6 +21,40 @@ void RenderCommand::generateID() { _renderCommandId = 0; + //Generate Material ID + //TODO fix shader ID generation + CCASSERT(_shaderID < 64, "ShaderID is greater than 64"); + //TODO fix texture ID generation + CCASSERT(_textureID < 1024, "TextureID is greater than 1024"); + + //TODO fix blend id generation + int blendID = 0; + if(_blendType == BlendFunc::DISABLE) + { + blendID = 0; + } + else if(_blendType == BlendFunc::ALPHA_PREMULTIPLIED) + { + blendID = 1; + } + else if(_blendType == BlendFunc::ALPHA_NON_PREMULTIPLIED) + { + blendID = 2; + } + else if(_blendType == BlendFunc::ADDITIVE) + { + blendID = 3; + } + else + { + blendID = 4; + } + + _materialID = (int32_t)_shaderID << 28 + | (int32_t)blendID << 24 + | (int32_t)_textureID << 14; + + //Generate RenderCommandID _renderCommandId = (int64_t)_viewport << 61 | (int64_t)_isTranslucent << 60 | (int64_t)_isCommand << 59 @@ -53,7 +87,7 @@ void RenderCommand::printID() printf("\n"); } -void RenderCommand::setData(int viewport, bool isTranslucent, bool isCommand, int32_t depth) +void RenderCommand::setKeyData(int viewport, bool isTranslucent, bool isCommand, int32_t depth) { _viewport = viewport; _isTranslucent = isTranslucent; @@ -61,13 +95,17 @@ void RenderCommand::setData(int viewport, bool isTranslucent, bool isCommand, in _depth = depth; } -void RenderCommand::setQuadData(kmMat4 *transform, V3F_C4B_T2F_Quad quad, GLuint textureID, int shaderID, int blendType) +void RenderCommand::setMaterialData(GLuint textureID, GLuint shaderID, BlendFunc blendType) { - kmMat4Assign(&_transform, transform); - _quad = quad; _textureID = textureID; _shaderID = shaderID; _blendType = blendType; } +void RenderCommand::setQuadData(kmMat4 *transform, V3F_C4B_T2F_Quad quad) +{ + kmMat4Assign(&_transform, transform); + _quad = quad; +} + NS_CC_END \ No newline at end of file diff --git a/cocos/2d/renderer/RenderCommand.h b/cocos/2d/renderer/RenderCommand.h index 7ffc599090..c903e6f65d 100644 --- a/cocos/2d/renderer/RenderCommand.h +++ b/cocos/2d/renderer/RenderCommand.h @@ -14,13 +14,25 @@ NS_CC_BEGIN +class RenderCommandID +{ +public: + virtual int64_t generateID() = 0; + inline int64_t getID() { return _id; } + +protected: + int64_t _id; + +}; + class RenderCommand { public: RenderCommand(); - void setData(int viewport, bool isTranslucent, bool isCommand, int32_t depth); - void setQuadData(kmMat4* transform, V3F_C4B_T2F_Quad quad, GLuint textureID, int shaderID, int blendType); + void setKeyData(int viewport, bool isTranslucent, bool isCommand, int32_t depth); + void setMaterialData( GLuint textureID, GLuint shaderID, BlendFunc blendType); + void setQuadData(kmMat4* transform, V3F_C4B_T2F_Quad quad); void generateID(); @@ -35,8 +47,8 @@ public: inline GLuint getTextureID() { return _textureID; } inline kmMat4* getTransform() { return &_transform; } inline V3F_C4B_T2F_Quad* getQuad() { return &_quad; } - inline int getShaderID() { return _shaderID; } - inline int getBlendType() { return _blendType; } + inline GLuint getShaderID() { return _shaderID; } + inline BlendFunc getBlendType() { return _blendType; } protected: void printID(); @@ -46,17 +58,18 @@ protected: int64_t _renderCommandId; /// used for sorting render commands int32_t _materialID; - //Data + //Key Data int _viewport; /// Which view port it belongs to bool _isTranslucent; /// Is it translucent, if it is we will have to render it bool _isCommand; int32_t _depth; + //Maternal + GLuint _textureID; + GLuint _shaderID; + BlendFunc _blendType; kmMat4 _transform; V3F_C4B_T2F_Quad _quad; - GLuint _textureID; - int _shaderID; - int _blendType; }; NS_CC_END diff --git a/cocos/2d/renderer/RenderMaterial.cpp b/cocos/2d/renderer/RenderMaterial.cpp new file mode 100644 index 0000000000..594128f8ea --- /dev/null +++ b/cocos/2d/renderer/RenderMaterial.cpp @@ -0,0 +1,6 @@ +// +// Created by NiTe Luo on 11/6/13. +// + + +#include "RenderMaterial.h" diff --git a/cocos/2d/renderer/RenderMaterial.h b/cocos/2d/renderer/RenderMaterial.h new file mode 100644 index 0000000000..fa570a8f49 --- /dev/null +++ b/cocos/2d/renderer/RenderMaterial.h @@ -0,0 +1,17 @@ +// +// Created by NiTe Luo on 11/6/13. +// + + + +#ifndef __RenderMaterial_H_ +#define __RenderMaterial_H_ + + +class RenderMaterial +{ + +}; + + +#endif //__RenderMaterial_H_