Skip to content

Commit

Permalink
fix vb / ib leaks in ui mesh && renderDrawInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
bluesky013 committed Aug 7, 2023
1 parent 188afc0 commit 9f198b3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
10 changes: 6 additions & 4 deletions native/cocos/2d/renderer/RenderDrawInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ void RenderDrawInfo::resetMeshIA() { // NOLINT(readability-make-member-function-
}

void RenderDrawInfo::destroy() {
_vb = nullptr;
_ib = nullptr;
_ia = nullptr;
if (_localDSBF) {
CC_SAFE_DELETE(_localDSBF->ds);
Expand All @@ -85,22 +87,22 @@ gfx::InputAssembler* RenderDrawInfo::initIAInfo(gfx::Device* device) {
gfx::InputAssemblerInfo iaInfo = {};
uint32_t vbStride = 9 * sizeof(float); // magic Number
uint32_t ibStride = sizeof(uint16_t);
auto* vertexBuffer = device->createBuffer({
_vb = device->createBuffer({
gfx::BufferUsageBit::VERTEX | gfx::BufferUsageBit::TRANSFER_DST,
gfx::MemoryUsageBit::DEVICE | gfx::MemoryUsageBit::HOST,
vbStride * 3,
vbStride,
});
auto* indexBuffer = device->createBuffer({
_ib = device->createBuffer({
gfx::BufferUsageBit::INDEX | gfx::BufferUsageBit::TRANSFER_DST,
gfx::MemoryUsageBit::DEVICE | gfx::MemoryUsageBit::HOST,
ibStride * 3,
ibStride,
});

iaInfo.attributes = *(Root::getInstance()->getBatcher2D()->getDefaultAttribute());
iaInfo.vertexBuffers.emplace_back(vertexBuffer);
iaInfo.indexBuffer = indexBuffer;
iaInfo.vertexBuffers.emplace_back(_vb);
iaInfo.indexBuffer = _ib;

_ia = device->createInputAssembler(iaInfo);
}
Expand Down
2 changes: 2 additions & 0 deletions native/cocos/2d/renderer/RenderDrawInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,7 @@ class RenderDrawInfo final {

// ia
IntrusivePtr<gfx::InputAssembler> _ia;
IntrusivePtr<gfx::Buffer> _vb;
IntrusivePtr<gfx::Buffer> _ib;
};
} // namespace cc
20 changes: 9 additions & 11 deletions native/cocos/2d/renderer/UIMeshBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ void UIMeshBuffer::resetIA() {
void UIMeshBuffer::destroy() {
reset();
_attributes.clear();
for (auto* vb : _iaInfo.vertexBuffers) {
delete vb;
}
_iaInfo.vertexBuffers.clear();
CC_SAFE_DELETE(_iaInfo.indexBuffer);
_vb = nullptr;
_ib = nullptr;
if (_needDeleteVData) {
delete _vData;
delete _iData;
Expand Down Expand Up @@ -130,23 +127,24 @@ gfx::InputAssembler* UIMeshBuffer::createNewIA(gfx::Device* device) {
uint32_t vbStride = _vertexFormatBytes;
uint32_t ibStride = sizeof(uint16_t);

auto* vertexBuffer = device->createBuffer({
gfx::InputAssemblerInfo iaInfo = {};
_vb = device->createBuffer({
gfx::BufferUsageBit::VERTEX | gfx::BufferUsageBit::TRANSFER_DST,
gfx::MemoryUsageBit::DEVICE | gfx::MemoryUsageBit::HOST,
vbStride * 3,
vbStride,
});
auto* indexBuffer = device->createBuffer({
_ib = device->createBuffer({
gfx::BufferUsageBit::INDEX | gfx::BufferUsageBit::TRANSFER_DST,
gfx::MemoryUsageBit::DEVICE | gfx::MemoryUsageBit::HOST,
ibStride * 3,
ibStride,
});

_iaInfo.attributes = _attributes;
_iaInfo.vertexBuffers.emplace_back(vertexBuffer);
_iaInfo.indexBuffer = indexBuffer;
_ia = device->createInputAssembler(_iaInfo);
iaInfo.attributes = _attributes;
iaInfo.vertexBuffers.emplace_back(_vb);
iaInfo.indexBuffer = _ib;
_ia = device->createInputAssembler(iaInfo);
}

return _ia;
Expand Down
4 changes: 3 additions & 1 deletion native/cocos/2d/renderer/UIMeshBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "base/TypeDef.h"
#include "renderer/gfx-base/GFXInputAssembler.h"
#include "renderer/gfx-base/GFXDef-common.h"
#include "renderer/gfx-base/GFXBuffer.h"

namespace cc {

Expand Down Expand Up @@ -89,7 +90,8 @@ class UIMeshBuffer final {

ccstd::vector<gfx::Attribute> _attributes;
IntrusivePtr<gfx::InputAssembler> _ia;
gfx::InputAssemblerInfo _iaInfo;
IntrusivePtr<gfx::Buffer> _vb;
IntrusivePtr<gfx::Buffer> _ib;

bool _dirty{false};
bool _needDeleteVData{false};
Expand Down

0 comments on commit 9f198b3

Please sign in to comment.