Skip to content

Commit

Permalink
Merge pull request #23 from minggo/batcher_2d
Browse files Browse the repository at this point in the history
fix some memory leak
  • Loading branch information
Zhmz authored Jul 7, 2022
2 parents 582e026 + 37352c6 commit f873870
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
31 changes: 17 additions & 14 deletions native/cocos/2d/renderer/RenderDrawInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ RenderDrawInfo::RenderDrawInfo() : RenderDrawInfo(nullptr) {
}

RenderDrawInfo::RenderDrawInfo(Batcher2d* batcher) : _batcher(batcher) {
_seArrayBufferObject = se::Object::createExternalArrayBufferObject(&_drawInfoAttrLayout, sizeof(DrawInfoAttrLayout), [](void* a, size_t b, void* c) {});
_seArrayBufferObject->root();
_attrSharedBuffer = new ArrayBuffer();
_attrSharedBuffer->setJSArrayBuffer(_seArrayBufferObject);
auto* seArrayBufferObject = se::Object::createExternalArrayBufferObject(&_drawInfoAttrLayout, sizeof(DrawInfoAttrLayout), [](void* a, size_t b, void* c) {});
_attrSharedBuffer = ccnew ArrayBuffer();
_attrSharedBuffer->setJSArrayBuffer(seArrayBufferObject);
}

RenderDrawInfo::RenderDrawInfo(const index_t bufferId, const uint32_t vertexOffset, const uint32_t indexOffset) {
Expand All @@ -48,14 +47,12 @@ RenderDrawInfo::RenderDrawInfo(const index_t bufferId, const uint32_t vertexOffs
_size = 0;
_batcher = nullptr;

_seArrayBufferObject = se::Object::createExternalArrayBufferObject(&_drawInfoAttrLayout, sizeof(DrawInfoAttrLayout), [](void* a, size_t b, void* c) {});
_seArrayBufferObject->root();
_attrSharedBuffer = new ArrayBuffer();
_attrSharedBuffer->setJSArrayBuffer(_seArrayBufferObject);
auto* seArrayBufferObject = se::Object::createExternalArrayBufferObject(&_drawInfoAttrLayout, sizeof(DrawInfoAttrLayout), [](void* a, size_t b, void* c) {});
_attrSharedBuffer = ccnew ArrayBuffer();
_attrSharedBuffer->setJSArrayBuffer(seArrayBufferObject);
}

RenderDrawInfo::~RenderDrawInfo() {
_seArrayBufferObject->decRef();
destroy();
}

Expand Down Expand Up @@ -183,14 +180,20 @@ void RenderDrawInfo::resetMeshIA() {
void RenderDrawInfo::destroy() {
_nextFreeIAHandle = 0;
_attributes.clear();
for (auto* vb : _iaInfo.vertexBuffers) {
delete vb;

//TODO(): Should use _iaPool to delete vb, ib.
delete _iaInfo.indexBuffer;
if (!_iaInfo.vertexBuffers.empty()) {
// only one vb
delete _iaInfo.vertexBuffers[0];
_iaInfo.vertexBuffers.clear();
}
_iaInfo.vertexBuffers.clear();
CC_SAFE_DELETE(_iaInfo.indexBuffer);

for (auto* ia : _iaPool) {
ia->destroy();
//TODO(): should use these codes to delete all ib, vb.
// delete ia->getIndexBuffer();
// // only one vertex buffer
// delete ia->getVertexBuffers()[0];
delete ia;
}
_iaPool.clear();
Expand Down
25 changes: 19 additions & 6 deletions native/cocos/2d/renderer/RenderDrawInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,48 +116,57 @@ class RenderDrawInfo final {
void uploadBuffers();
void resetMeshIA();

protected:
private:
CC_DISALLOW_COPY_MOVE_ASSIGN(RenderDrawInfo);
void destroy();

private:
gfx::InputAssembler* _initIAInfo(gfx::Device* device);

// weak reference
Batcher2d* _batcher{nullptr};

// weak reference
uint8_t* _sharedBuffer{nullptr};
uint8_t _stride{0};
uint32_t _size{0};

DrawInfoAttrLayout _drawInfoAttrLayout{};
se::Object* _seArrayBufferObject{nullptr};
ArrayBuffer::Ptr _attrSharedBuffer{nullptr};
DrawInfoAttrLayout _drawInfoAttrLayout;
ArrayBuffer::Ptr _attrSharedBuffer;

index_t _bufferId{0};
index_t _accId{0};
uint32_t _vertexOffset{0};

uint32_t _indexOffset{0};

// weak reference
float_t* _vbBuffer{nullptr};
// weak reference
uint16_t* _ibBuffer{nullptr};
// weak reference
float_t* _vDataBuffer{nullptr};
// weak reference
uint16_t* _iDataBuffer{nullptr};
// weak reference
UIMeshBuffer* _meshBuffer{nullptr};

uint32_t _vbCount{0};
uint32_t _ibCount{0};

bool _vertDirty{false};

// weak reference
scene::Model* _model{nullptr};

ccstd::hash_t _dataHash{0};
uint32_t _stencilStage{0};
bool _isMeshBuffer{false};
// weak reference
Material* _material{nullptr};
// weak reference
gfx::Texture* _texture{nullptr};
uint32_t _textureHash{0};
// weak reference
gfx::Sampler* _sampler{nullptr};

uint32_t _blendHash{0};
Expand All @@ -169,9 +178,13 @@ class RenderDrawInfo final {
gfx::Attribute{gfx::ATTR_NAME_COLOR, gfx::Format::RGBA32F},
};

ccstd::vector<gfx::InputAssembler*> _iaPool{};
//TODO(): it is not a good way to cache IA here.
// manage memory manually
ccstd::vector<gfx::InputAssembler*> _iaPool;
uint32_t _nextFreeIAHandle{0};
// weak reference
gfx::Buffer* _vbGFXBuffer{nullptr};
// weak reference
gfx::Buffer* _ibGFXBuffer{nullptr};
};
} // namespace cc

0 comments on commit f873870

Please sign in to comment.