Merge pull request #14715 from liamcindy/v3_textureCache

update for 3D object render, revert old changes
This commit is contained in:
XiaoYang 2015-12-18 21:22:31 +08:00
commit 593a619dc8
15 changed files with 98 additions and 117 deletions

View File

@ -133,7 +133,6 @@ Mesh::Mesh()
, _blendDirty(true) , _blendDirty(true)
, _force2DQueue(false) , _force2DQueue(false)
, _texFile("") , _texFile("")
, _enableCheckTexture(false)
{ {
} }
@ -282,7 +281,7 @@ void Mesh::setTexture(Texture2D* tex)
setTexture(tex, NTextureData::Usage::Diffuse); setTexture(tex, NTextureData::Usage::Diffuse);
} }
void Mesh::setTexture(Texture2D* tex, NTextureData::Usage usage) void Mesh::setTexture(Texture2D* tex, NTextureData::Usage usage, bool cacheFileName)
{ {
// Texture must be saved for future use // Texture must be saved for future use
// it doesn't matter if the material is already set or not // it doesn't matter if the material is already set or not
@ -307,7 +306,8 @@ void Mesh::setTexture(Texture2D* tex, NTextureData::Usage usage)
} }
bindMeshCommand(); bindMeshCommand();
_texFile = tex->getPath(); if (cacheFileName)
_texFile = tex->getPath();
} }
else if (usage == NTextureData::Usage::Normal) // currently only diffuse and normal are supported else if (usage == NTextureData::Usage::Normal) // currently only diffuse and normal are supported
{ {
@ -382,9 +382,6 @@ void Mesh::draw(Renderer* renderer, float globalZOrder, const Mat4& transform, u
if (isTransparent) if (isTransparent)
flags |= Node::FLAGS_RENDER_AS_3D; flags |= Node::FLAGS_RENDER_AS_3D;
if (_enableCheckTexture)
this->checkTexture();
_meshCommand.init(globalZ, _meshCommand.init(globalZ,
_material, _material,
getVertexBuffer(), getVertexBuffer(),
@ -706,39 +703,4 @@ GLuint Mesh::getIndexBuffer() const
{ {
return _meshIndexData->getIndexBuffer()->getVBO(); return _meshIndexData->getIndexBuffer()->getVBO();
} }
void Mesh::checkTexture()
{
Texture2D* cacheTex = nullptr;
auto& texture = _textures[NTextureData::Usage::Diffuse];
if (Director::getInstance()->getTextureCache()->isDirty())
{
cacheTex = Director::getInstance()->getTextureCache()->getTextureForKey(_texFile);
if (cacheTex == nullptr)
{
cacheTex = getDummyTexture();
}
}
else if (texture != nullptr && !texture->isValid())
{
cacheTex = getDummyTexture();
}
if (cacheTex != nullptr && texture != cacheTex)
{
CC_SAFE_RETAIN(cacheTex);
CC_SAFE_RELEASE(texture);
texture = cacheTex;
if (_material) {
auto technique = _material->_currentTechnique;
for (auto& pass : technique->_passes)
{
pass->setTexture(texture);
}
}
bindMeshCommand();
}
}
NS_CC_END NS_CC_END

View File

@ -109,8 +109,9 @@ public:
* set texture * set texture
* @param tex texture to be set * @param tex texture to be set
* @param usage Usage of this texture * @param usage Usage of this texture
* @param whether refresh the cache file name
*/ */
void setTexture(Texture2D* tex, NTextureData::Usage usage); void setTexture(Texture2D* tex, NTextureData::Usage usage,bool cacheFileName = true);
/** /**
* set texture * set texture
* @param texPath texture path * @param texPath texture path
@ -224,19 +225,7 @@ public:
*/ */
void setForce2DQueue(bool force2D) { _force2DQueue = force2D; } void setForce2DQueue(bool force2D) { _force2DQueue = force2D; }
/** std::string getTextureFileName(){ return _texFile; }
* check texture
*/
void checkTexture();
/**
* set enable check texture, check texture each frame if enable is true. It is false by default
*/
void setEnableCheckTexture(bool enableCheckTexture) { _enableCheckTexture = enableCheckTexture; }
/**
* check texture each frame?
*/
bool enableCheckTexture() const { return _enableCheckTexture; }
CC_CONSTRUCTOR_ACCESS: CC_CONSTRUCTOR_ACCESS:
@ -280,7 +269,6 @@ protected:
std::vector<float> _spotLightUniformRangeInverseValues; std::vector<float> _spotLightUniformRangeInverseValues;
std::string _texFile; std::string _texFile;
bool _enableCheckTexture;
}; };
// end of 3d group // end of 3d group

View File

@ -328,9 +328,6 @@ void Director::drawScene()
{ {
calculateMPF(); calculateMPF();
} }
if (_textureCache != nullptr)
_textureCache->setDirty(false);
} }
void Director::calculateDeltaTime() void Director::calculateDeltaTime()

View File

@ -406,10 +406,6 @@ public:
/** Get a shader program from the texture.*/ /** Get a shader program from the texture.*/
GLProgram* getGLProgram() const; GLProgram* getGLProgram() const;
/** Set if the texture is valid, when it been set as false, it will display as default "file missing texture" */
void setValid(bool valid) { _valid = valid; }
bool isValid() const { return _valid; }
std::string getPath()const { return _filePath; } std::string getPath()const { return _filePath; }
public: public:

View File

@ -59,7 +59,6 @@ TextureCache::TextureCache()
: _loadingThread(nullptr) : _loadingThread(nullptr)
, _needQuit(false) , _needQuit(false)
, _asyncRefCount(0) , _asyncRefCount(0)
, _dirty(false)
{ {
} }
@ -496,8 +495,7 @@ void TextureCache::removeTexture(Texture2D* texture)
for( auto it=_textures.cbegin(); it!=_textures.cend(); /* nothing */ ) { for( auto it=_textures.cbegin(); it!=_textures.cend(); /* nothing */ ) {
if( it->second == texture ) { if( it->second == texture ) {
texture->setValid(false); it->second->release();
texture->autorelease();
_textures.erase(it++); _textures.erase(it++);
break; break;
} else } else
@ -516,8 +514,7 @@ void TextureCache::removeTextureForKey(const std::string &textureKeyName)
} }
if( it != _textures.end() ) { if( it != _textures.end() ) {
it->second->setValid(false); it->second->release();
(it->second)->autorelease();
_textures.erase(it); _textures.erase(it);
} }
} }
@ -626,7 +623,6 @@ void TextureCache::renameTextureWithKey(const std::string srcName, const std::st
tex->initWithImage(image); tex->initWithImage(image);
_textures.insert(std::make_pair(fullpath, tex)); _textures.insert(std::make_pair(fullpath, tex));
_textures.erase(it); _textures.erase(it);
this->setDirty(true);
} }
CC_SAFE_DELETE(image); CC_SAFE_DELETE(image);
} }

View File

@ -204,9 +204,6 @@ public:
*/ */
const std::string getTextureFilePath(Texture2D* texture)const; const std::string getTextureFilePath(Texture2D* texture)const;
void setDirty(bool dirty) { _dirty = dirty; }
bool isDirty() const { return _dirty; }
/** Reload texture from a new file. /** Reload texture from a new file.
* This function is mainly for editor, won't suggest use it in game for performance reason. * This function is mainly for editor, won't suggest use it in game for performance reason.
* *
@ -242,8 +239,6 @@ protected:
int _asyncRefCount; int _asyncRefCount;
std::unordered_map<std::string, Texture2D*> _textures; std::unordered_map<std::string, Texture2D*> _textures;
bool _dirty;
}; };
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA

View File

@ -43,6 +43,7 @@ Particle3DQuadRender::Particle3DQuadRender()
, _glProgramState(nullptr) , _glProgramState(nullptr)
, _indexBuffer(nullptr) , _indexBuffer(nullptr)
, _vertexBuffer(nullptr) , _vertexBuffer(nullptr)
, _texFile("")
{ {
} }
@ -60,6 +61,7 @@ Particle3DQuadRender* Particle3DQuadRender::create(const std::string& texFile)
auto ret = new (std::nothrow)Particle3DQuadRender(); auto ret = new (std::nothrow)Particle3DQuadRender();
if (ret && ret->initQuadRender(texFile)) if (ret && ret->initQuadRender(texFile))
{ {
ret->_texFile = texFile;
ret->autorelease(); ret->autorelease();
} }
else else
@ -187,6 +189,8 @@ bool Particle3DQuadRender::initQuadRender( const std::string& texFile )
_texture = tex; _texture = tex;
glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE); glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE);
} }
else
_texture = nullptr;
} }
auto glProgramState = GLProgramState::create(glProgram); auto glProgramState = GLProgramState::create(glProgram);
glProgramState->retain(); glProgramState->retain();
@ -213,6 +217,11 @@ bool Particle3DQuadRender::initQuadRender( const std::string& texFile )
return true; return true;
} }
void Particle3DQuadRender::reset()
{
this->initQuadRender(_texFile);
}
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
Particle3DModelRender::Particle3DModelRender() Particle3DModelRender::Particle3DModelRender()
: _spriteSize(Vec3::ONE) : _spriteSize(Vec3::ONE)
@ -284,6 +293,14 @@ void Particle3DModelRender::render(Renderer* renderer, const Mat4 &transform, Pa
} }
} }
void Particle3DModelRender::reset()
{
for (auto iter : _spriteList){
iter->release();
}
_spriteList.clear();
}
// MARK: Particle3DRender // MARK: Particle3DRender
Particle3DRender::Particle3DRender() Particle3DRender::Particle3DRender()

View File

@ -73,6 +73,8 @@ public:
void copyAttributesTo (Particle3DRender *render); void copyAttributesTo (Particle3DRender *render);
virtual void reset(){}
CC_CONSTRUCTOR_ACCESS: CC_CONSTRUCTOR_ACCESS:
Particle3DRender(); Particle3DRender();
virtual ~Particle3DRender(); virtual ~Particle3DRender();
@ -94,6 +96,7 @@ public:
virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override; virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override;
virtual void reset()override;
CC_CONSTRUCTOR_ACCESS: CC_CONSTRUCTOR_ACCESS:
Particle3DQuadRender(); Particle3DQuadRender();
virtual ~Particle3DQuadRender(); virtual ~Particle3DQuadRender();
@ -118,6 +121,7 @@ protected:
std::vector<posuvcolor> _posuvcolors; //vertex data std::vector<posuvcolor> _posuvcolors; //vertex data
std::vector<unsigned short> _indexData; //index data std::vector<unsigned short> _indexData; //index data
std::string _texFile;
}; };
// particle render for Sprite3D // particle render for Sprite3D
@ -128,6 +132,7 @@ public:
virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override; virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override;
virtual void reset()override;
CC_CONSTRUCTOR_ACCESS: CC_CONSTRUCTOR_ACCESS:
Particle3DModelRender(); Particle3DModelRender();
virtual ~Particle3DModelRender(); virtual ~Particle3DModelRender();

View File

@ -204,6 +204,10 @@ public:
* set particle render, can set your own particle render * set particle render, can set your own particle render
*/ */
void setRender(Particle3DRender* render); void setRender(Particle3DRender* render);
/**
* return particle render
*/
Particle3DRender* getRender(){ return _render; }
/** /**
* add particle affector * add particle affector
*/ */

View File

@ -734,21 +734,15 @@ void PUBillboardChain::setBlendFunc(const BlendFunc& blendFunc)
GLuint PUBillboardChain::getTextureName() GLuint PUBillboardChain::getTextureName()
{ {
if (Director::getInstance()->getTextureCache()->isDirty()) if (Director::getInstance()->getTextureCache()->getTextureForKey(_texFile) == nullptr)
{
if (Director::getInstance()->getTextureCache()->getTextureForKey(_texFile) == nullptr)
{
_texture = nullptr;
this->init("");
}
else
this->init(_texFile);
}
else if (_texture != nullptr && !_texture->isValid())
{ {
_texture = nullptr; _texture = nullptr;
this->init(""); this->init("");
} }
else if (_texture == nullptr)
{
this->init(_texFile);
}
if (_texture == nullptr) if (_texture == nullptr)
return 0; return 0;

View File

@ -244,7 +244,7 @@ void PUParticle3DQuadRender::render(Renderer* renderer, const Mat4 &transform, P
_stateBlock->setBlendFunc(particleSystem->getBlendFunc()); _stateBlock->setBlendFunc(particleSystem->getBlendFunc());
GLuint texId = this->getTextureName(); GLuint texId = (_texture ? _texture->getName() : 0);
_meshCommand->init(0, _meshCommand->init(0,
texId, texId,
_glProgramState, _glProgramState,
@ -525,6 +525,14 @@ PUParticle3DModelRender* PUParticle3DModelRender::clone()
return mr; return mr;
} }
void PUParticle3DModelRender::reset()
{
for (auto iter : _spriteList){
iter->release();
}
_spriteList.clear();
}
PUParticle3DEntityRender::PUParticle3DEntityRender() PUParticle3DEntityRender::PUParticle3DEntityRender()
: _meshCommand(nullptr) : _meshCommand(nullptr)
@ -564,6 +572,8 @@ bool PUParticle3DEntityRender::initRender( const std::string &texFile )
_texture = tex; _texture = tex;
glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE); glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE);
} }
else
_texture = nullptr;
} }
auto glProgramState = GLProgramState::create(glProgram); auto glProgramState = GLProgramState::create(glProgram);
glProgramState->retain(); glProgramState->retain();
@ -587,35 +597,16 @@ bool PUParticle3DEntityRender::initRender( const std::string &texFile )
return true; return true;
} }
GLuint PUParticle3DEntityRender::getTextureName()
{
if (Director::getInstance()->getTextureCache()->isDirty())
{
if (Director::getInstance()->getTextureCache()->getTextureForKey(_texFile) == nullptr)
{
_texture = nullptr;
this->initRender("");
}
else
this->initRender(_texFile);
}
else if (_texture != nullptr && !_texture->isValid())
{
_texture = nullptr;
this->initRender("");
}
if (_texture == nullptr)
return 0;
return _texture->getName();
}
void PUParticle3DEntityRender::copyAttributesTo(PUParticle3DEntityRender *render) void PUParticle3DEntityRender::copyAttributesTo(PUParticle3DEntityRender *render)
{ {
PURender::copyAttributesTo(render); PURender::copyAttributesTo(render);
} }
void PUParticle3DEntityRender::reset()
{
this->initRender(_texFile);
}
PUParticle3DBoxRender::PUParticle3DBoxRender() PUParticle3DBoxRender::PUParticle3DBoxRender()
{ {
autoRotate = false; autoRotate = false;
@ -725,7 +716,7 @@ void PUParticle3DBoxRender::render( Renderer* renderer, const Mat4 &transform, P
_vertexBuffer->updateVertices(&_vertices[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0); _vertexBuffer->updateVertices(&_vertices[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0);
_indexBuffer->updateIndices(&_indices[0], index/* * sizeof(unsigned short)*/, 0); _indexBuffer->updateIndices(&_indices[0], index/* * sizeof(unsigned short)*/, 0);
GLuint texId = this->getTextureName(); GLuint texId = (_texture ? _texture->getName() : 0);
_stateBlock->setBlendFunc(_particleSystem->getBlendFunc()); _stateBlock->setBlendFunc(_particleSystem->getBlendFunc());
_meshCommand->init(0, _meshCommand->init(0,
texId, texId,
@ -889,8 +880,7 @@ void PUSphereRender::render( Renderer* renderer, const Mat4 &transform, Particle
_vertexBuffer->updateVertices(&_vertices[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0); _vertexBuffer->updateVertices(&_vertices[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0);
_indexBuffer->updateIndices(&_indices[0], index/* * sizeof(unsigned short)*/, 0); _indexBuffer->updateIndices(&_indices[0], index/* * sizeof(unsigned short)*/, 0);
GLuint texId = this->getTextureName(); GLuint texId = (_texture ? _texture->getName() : 0);
_stateBlock->setBlendFunc(particleSystem->getBlendFunc()); _stateBlock->setBlendFunc(particleSystem->getBlendFunc());
_meshCommand->init( _meshCommand->init(
0, 0,

View File

@ -65,7 +65,7 @@ class CC_DLL PUParticle3DEntityRender : public PURender
{ {
public: public:
void copyAttributesTo(PUParticle3DEntityRender *render); void copyAttributesTo(PUParticle3DEntityRender *render);
virtual void reset()override;
CC_CONSTRUCTOR_ACCESS: CC_CONSTRUCTOR_ACCESS:
PUParticle3DEntityRender(); PUParticle3DEntityRender();
virtual ~PUParticle3DEntityRender(); virtual ~PUParticle3DEntityRender();
@ -73,8 +73,6 @@ CC_CONSTRUCTOR_ACCESS:
protected: protected:
bool initRender(const std::string &texFile); bool initRender(const std::string &texFile);
GLuint getTextureName();
protected: protected:
struct VertexInfo struct VertexInfo
@ -189,6 +187,7 @@ public:
virtual PUParticle3DModelRender* clone() override; virtual PUParticle3DModelRender* clone() override;
void copyAttributesTo(PUParticle3DModelRender *render); void copyAttributesTo(PUParticle3DModelRender *render);
virtual void reset()override;
CC_CONSTRUCTOR_ACCESS: CC_CONSTRUCTOR_ACCESS:
PUParticle3DModelRender(); PUParticle3DModelRender();
virtual ~PUParticle3DModelRender(); virtual ~PUParticle3DModelRender();

View File

@ -21,6 +21,7 @@
#include "ExtensionDeprecated.h" #include "ExtensionDeprecated.h"
// Particle System, include Particle Universe Particle System // Particle System, include Particle Universe Particle System
#include "Particle3D/CCParticle3DRender.h"
#include "Particle3D/CCParticleSystem3D.h" #include "Particle3D/CCParticleSystem3D.h"
#include "Particle3D/PU/CCPUParticleSystem3D.h" #include "Particle3D/PU/CCPUParticleSystem3D.h"

View File

@ -2602,6 +2602,7 @@ Sprite3DPropertyTest::Sprite3DPropertyTest()
_sprite->setPosition(20.f, 0.f); _sprite->setPosition(20.f, 0.f);
_sprite->setRotation3D(Vec3(0, 180, 0)); _sprite->setRotation3D(Vec3(0, 180, 0));
_meshTex = _sprite->getMesh()->getTexture(); _meshTex = _sprite->getMesh()->getTexture();
_texFile = _meshTex->getPath();
addChild(_sprite); addChild(_sprite);
setCameraMask(2); setCameraMask(2);
@ -2616,11 +2617,14 @@ Sprite3DPropertyTest::Sprite3DPropertyTest()
auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Sprite3DPropertyTest::printMeshName, this)); auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Sprite3DPropertyTest::printMeshName, this));
auto label2 = Label::createWithTTF(ttfConfig, "Remove Used Texture"); auto label2 = Label::createWithTTF(ttfConfig, "Remove Used Texture");
auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DPropertyTest::removeUsedTexture, this)); auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DPropertyTest::removeUsedTexture, this));
auto label3 = Label::createWithTTF(ttfConfig, "Reset");
auto item3 = MenuItemLabel::create(label3, CC_CALLBACK_1(Sprite3DPropertyTest::resetTexture, this));
item1->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 4)); item1->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 4));
item2->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 5)); item2->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 5));
item3->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 6));
auto pMenu1 = Menu::create(item1, item2, nullptr); auto pMenu1 = Menu::create(item1, item2, item3,nullptr);
pMenu1->setPosition(Vec2(0, 0)); pMenu1->setPosition(Vec2(0, 0));
this->addChild(pMenu1, 10); this->addChild(pMenu1, 10);
@ -2654,5 +2658,34 @@ void Sprite3DPropertyTest::removeUsedTexture(cocos2d::Ref* sender)
if (_meshTex != nullptr) if (_meshTex != nullptr)
{ {
TextureCache::getInstance()->removeTexture(_meshTex); TextureCache::getInstance()->removeTexture(_meshTex);
this->refreshSpriteRender();
}
}
void Sprite3DPropertyTest::resetTexture(cocos2d::Ref* sender)
{
if (_meshTex != nullptr)
{
_meshTex = TextureCache::getInstance()->addImage(_texFile);
this->refreshSpriteRender();
}
}
void Sprite3DPropertyTest::refreshSpriteRender()
{
Vector<Mesh*> meshes = _sprite->getMeshes();
for (Mesh* mesh : meshes)
{
std::string file = mesh->getTextureFileName();
Texture2D* cacheTex = Director::getInstance()->getTextureCache()->getTextureForKey(file);
if (cacheTex == nullptr)
{
unsigned char data[] = { 255, 0, 0, 255 };//1*1 red picture
Image * image = new (std::nothrow) Image();
image->initWithRawData(data, sizeof(data), 1, 1, sizeof(unsigned char));
cacheTex = Director::getInstance()->getTextureCache()->addImage(image, "/dummyTexture");
image->release();
}
mesh->setTexture(cacheTex, cocos2d::NTextureData::Usage::Diffuse, false);
} }
} }

View File

@ -606,9 +606,13 @@ public:
void printMeshName(cocos2d::Ref* sender); void printMeshName(cocos2d::Ref* sender);
void removeUsedTexture(cocos2d::Ref* sender); void removeUsedTexture(cocos2d::Ref* sender);
void resetTexture(cocos2d::Ref* sender);
void refreshSpriteRender();
protected: protected:
cocos2d::Sprite3D* _sprite; cocos2d::Sprite3D* _sprite;
cocos2d::Texture2D* _meshTex; cocos2d::Texture2D* _meshTex;
std::string _texFile;
}; };
#endif #endif