From 8caec597116a0980704e90520105da0fd4d923f3 Mon Sep 17 00:00:00 2001 From: minggo Date: Tue, 28 Jun 2022 18:01:47 +0800 Subject: [PATCH] use IntrusivePtr to share object (#11729) --- native/cocos/base/Agent.h | 3 +- native/cocos/engine/Engine.cpp | 2 +- .../cocos/renderer/gfx-agent/BufferAgent.cpp | 2 +- .../renderer/gfx-agent/CommandBufferAgent.cpp | 2 +- .../renderer/gfx-agent/DescriptorSetAgent.cpp | 2 +- .../gfx-agent/DescriptorSetLayoutAgent.cpp | 2 +- .../cocos/renderer/gfx-agent/DeviceAgent.cpp | 45 +++++++++---------- .../renderer/gfx-agent/FramebufferAgent.cpp | 2 +- .../gfx-agent/InputAssemblerAgent.cpp | 2 +- .../gfx-agent/PipelineLayoutAgent.cpp | 2 +- .../renderer/gfx-agent/PipelineStateAgent.cpp | 2 +- .../renderer/gfx-agent/QueryPoolAgent.cpp | 2 +- .../cocos/renderer/gfx-agent/QueueAgent.cpp | 2 +- .../renderer/gfx-agent/RenderPassAgent.cpp | 2 +- .../cocos/renderer/gfx-agent/ShaderAgent.cpp | 2 +- .../renderer/gfx-agent/SwapchainAgent.cpp | 2 +- .../cocos/renderer/gfx-agent/TextureAgent.cpp | 2 +- native/cocos/renderer/gfx-base/GFXDevice.cpp | 6 +-- native/cocos/renderer/gfx-base/GFXDevice.h | 6 +-- native/cocos/renderer/gfx-base/GFXQueryPool.h | 3 +- native/cocos/renderer/gfx-base/GFXSwapchain.h | 3 +- .../cocos/renderer/gfx-gles2/GLES2Device.cpp | 9 ++-- .../cocos/renderer/gfx-gles3/GLES3Device.cpp | 8 ++-- native/cocos/renderer/gfx-metal/MTLDevice.mm | 14 +++--- .../gfx-validator/BufferValidator.cpp | 2 - .../gfx-validator/CommandBufferValidator.cpp | 1 - .../DescriptorSetLayoutValidator.cpp | 2 - .../gfx-validator/DescriptorSetValidator.cpp | 2 - .../gfx-validator/DeviceValidator.cpp | 37 +++++++-------- .../gfx-validator/FramebufferValidator.cpp | 2 - .../gfx-validator/InputAssemblerValidator.cpp | 2 - .../gfx-validator/PipelineLayoutValidator.cpp | 1 - .../gfx-validator/PipelineStateValidator.cpp | 1 - .../gfx-validator/QueryPoolValidator.cpp | 1 - .../renderer/gfx-validator/QueueValidator.cpp | 1 - .../gfx-validator/RenderPassValidator.cpp | 2 - .../gfx-validator/ShaderValidator.cpp | 2 - .../gfx-validator/SwapchainValidator.cpp | 1 - .../gfx-validator/TextureValidator.cpp | 2 - native/cocos/renderer/gfx-vulkan/VKDevice.cpp | 13 +++--- 40 files changed, 83 insertions(+), 116 deletions(-) diff --git a/native/cocos/base/Agent.h b/native/cocos/base/Agent.h index 8afe77b53a..e2fa55a5a7 100644 --- a/native/cocos/base/Agent.h +++ b/native/cocos/base/Agent.h @@ -27,6 +27,7 @@ #include #include "Macros.h" +#include "base/Ptr.h" namespace cc { @@ -51,7 +52,7 @@ public: inline Actor *getActor() const noexcept { return _actor; } protected: - Actor *_actor{nullptr}; + IntrusivePtr _actor; }; } // namespace cc diff --git a/native/cocos/engine/Engine.cpp b/native/cocos/engine/Engine.cpp index cb9bf70bda..664936adcc 100644 --- a/native/cocos/engine/Engine.cpp +++ b/native/cocos/engine/Engine.cpp @@ -171,7 +171,7 @@ void Engine::destroy() { delete _builtinResMgr; delete _programLib; - CC_SAFE_DESTROY_AND_DELETE(_gfxDevice); + CC_SAFE_DELETE(_gfxDevice); delete _fs; _scheduler.reset(); diff --git a/native/cocos/renderer/gfx-agent/BufferAgent.cpp b/native/cocos/renderer/gfx-agent/BufferAgent.cpp index 3f8ef82282..6ddeb98456 100644 --- a/native/cocos/renderer/gfx-agent/BufferAgent.cpp +++ b/native/cocos/renderer/gfx-agent/BufferAgent.cpp @@ -43,7 +43,7 @@ BufferAgent::~BufferAgent() { BufferDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/CommandBufferAgent.cpp b/native/cocos/renderer/gfx-agent/CommandBufferAgent.cpp index 930db339d4..5649e5c180 100644 --- a/native/cocos/renderer/gfx-agent/CommandBufferAgent.cpp +++ b/native/cocos/renderer/gfx-agent/CommandBufferAgent.cpp @@ -83,7 +83,7 @@ CommandBufferAgent::~CommandBufferAgent() { DeviceAgent::getInstance()->getMessageQueue(), CommandBufferDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/DescriptorSetAgent.cpp b/native/cocos/renderer/gfx-agent/DescriptorSetAgent.cpp index a02a5cb265..ccfff61211 100644 --- a/native/cocos/renderer/gfx-agent/DescriptorSetAgent.cpp +++ b/native/cocos/renderer/gfx-agent/DescriptorSetAgent.cpp @@ -45,7 +45,7 @@ DescriptorSetAgent::~DescriptorSetAgent() { DescriptorSetDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/DescriptorSetLayoutAgent.cpp b/native/cocos/renderer/gfx-agent/DescriptorSetLayoutAgent.cpp index 88031d0b0e..ada925a702 100644 --- a/native/cocos/renderer/gfx-agent/DescriptorSetLayoutAgent.cpp +++ b/native/cocos/renderer/gfx-agent/DescriptorSetLayoutAgent.cpp @@ -42,7 +42,7 @@ DescriptorSetLayoutAgent::~DescriptorSetLayoutAgent() { DescriptorSetLayoutDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/DeviceAgent.cpp b/native/cocos/renderer/gfx-agent/DeviceAgent.cpp index c151b3c1a7..75e594fdda 100644 --- a/native/cocos/renderer/gfx-agent/DeviceAgent.cpp +++ b/native/cocos/renderer/gfx-agent/DeviceAgent.cpp @@ -59,7 +59,7 @@ DeviceAgent::DeviceAgent(Device *device) : Agent(device) { } DeviceAgent::~DeviceAgent() { - CC_SAFE_DELETE(_actor); + destroy(); DeviceAgent::instance = nullptr; } @@ -83,8 +83,8 @@ bool DeviceAgent::doInit(const DeviceInfo &info) { // TODO(PatriceJiang): replace with: _mainMessageQueue = ccnew MessageQueue; _mainMessageQueue = ccnew_placement(CC_MALLOC_ALIGN(sizeof(MessageQueue), alignof(MessageQueue))) MessageQueue; // NOLINT - static_cast(_cmdBuff)->_queue = _queue; - static_cast(_cmdBuff)->initAgent(); + static_cast(_cmdBuff.get())->_queue = _queue; + static_cast(_cmdBuff.get())->initAgent(); setMultithreaded(true); @@ -96,33 +96,28 @@ void DeviceAgent::doDestroy() { _mainMessageQueue, DeviceDestroy, actor, _actor, { - actor->destroy(); + if (actor) { + actor->destroy(); + } }); if (_cmdBuff) { - static_cast(_cmdBuff)->destroyAgent(); - static_cast(_cmdBuff)->_actor = nullptr; - delete _cmdBuff; + static_cast(_cmdBuff.get())->destroyAgent(); _cmdBuff = nullptr; } - if (_queryPool) { - static_cast(_queryPool)->_actor = nullptr; - delete _queryPool; - _queryPool = nullptr; - } - if (_queue) { - static_cast(_queue)->_actor = nullptr; - delete _queue; - _queue = nullptr; - } - _mainMessageQueue->terminateConsumerThread(); + _queryPool = nullptr; + _queue = nullptr; - // NOTE: C++17 required when enable alignment - // TODO(PatriceJiang): replace with: CC_SAFE_DELETE(_mainMessageQueue); - _mainMessageQueue->~MessageQueue(); - CC_FREE_ALIGN(_mainMessageQueue); - _mainMessageQueue = nullptr; + if (_mainMessageQueue) { + _mainMessageQueue->terminateConsumerThread(); + + // NOTE: C++17 required when enable alignment + // TODO(PatriceJiang): replace with: CC_SAFE_DELETE(_mainMessageQueue); + _mainMessageQueue->~MessageQueue(); + CC_FREE_ALIGN(_mainMessageQueue); + _mainMessageQueue = nullptr; + } } void DeviceAgent::acquire(Swapchain *const *swapchains, uint32_t count) { @@ -357,11 +352,11 @@ void doBufferTextureCopy(const uint8_t *const *buffers, Texture *texture, const } void DeviceAgent::copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) { - doBufferTextureCopy(buffers, dst, regions, count, _mainMessageQueue, _actor); + doBufferTextureCopy(buffers, dst, regions, count, _mainMessageQueue, _actor.get()); } void CommandBufferAgent::copyBuffersToTexture(const uint8_t *const *buffers, Texture *texture, const BufferTextureCopy *regions, uint32_t count) { - doBufferTextureCopy(buffers, texture, regions, count, _messageQueue, _actor); + doBufferTextureCopy(buffers, texture, regions, count, _messageQueue, _actor.get()); } void DeviceAgent::copyTextureToBuffers(Texture *srcTexture, uint8_t *const *buffers, const BufferTextureCopy *regions, uint32_t count) { diff --git a/native/cocos/renderer/gfx-agent/FramebufferAgent.cpp b/native/cocos/renderer/gfx-agent/FramebufferAgent.cpp index 4f5ad304e6..c715a4debe 100644 --- a/native/cocos/renderer/gfx-agent/FramebufferAgent.cpp +++ b/native/cocos/renderer/gfx-agent/FramebufferAgent.cpp @@ -44,7 +44,7 @@ FramebufferAgent::~FramebufferAgent() { FramebufferDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/InputAssemblerAgent.cpp b/native/cocos/renderer/gfx-agent/InputAssemblerAgent.cpp index 88e8913317..8b81f23da3 100644 --- a/native/cocos/renderer/gfx-agent/InputAssemblerAgent.cpp +++ b/native/cocos/renderer/gfx-agent/InputAssemblerAgent.cpp @@ -43,7 +43,7 @@ InputAssemblerAgent::~InputAssemblerAgent() { InputAssemblerDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/PipelineLayoutAgent.cpp b/native/cocos/renderer/gfx-agent/PipelineLayoutAgent.cpp index 0bc89d59c8..7351e28f31 100644 --- a/native/cocos/renderer/gfx-agent/PipelineLayoutAgent.cpp +++ b/native/cocos/renderer/gfx-agent/PipelineLayoutAgent.cpp @@ -43,7 +43,7 @@ PipelineLayoutAgent::~PipelineLayoutAgent() { PipelineLayoutDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/PipelineStateAgent.cpp b/native/cocos/renderer/gfx-agent/PipelineStateAgent.cpp index 5478014b4c..3f4db17f2e 100644 --- a/native/cocos/renderer/gfx-agent/PipelineStateAgent.cpp +++ b/native/cocos/renderer/gfx-agent/PipelineStateAgent.cpp @@ -45,7 +45,7 @@ PipelineStateAgent::~PipelineStateAgent() { PipelineStateDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/QueryPoolAgent.cpp b/native/cocos/renderer/gfx-agent/QueryPoolAgent.cpp index 928972c2af..69eca3c8ae 100644 --- a/native/cocos/renderer/gfx-agent/QueryPoolAgent.cpp +++ b/native/cocos/renderer/gfx-agent/QueryPoolAgent.cpp @@ -46,7 +46,7 @@ QueryPoolAgent::~QueryPoolAgent() { QueryDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/QueueAgent.cpp b/native/cocos/renderer/gfx-agent/QueueAgent.cpp index 79372e08e0..d049b6ca4c 100644 --- a/native/cocos/renderer/gfx-agent/QueueAgent.cpp +++ b/native/cocos/renderer/gfx-agent/QueueAgent.cpp @@ -44,7 +44,7 @@ QueueAgent::~QueueAgent() { QueueDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/RenderPassAgent.cpp b/native/cocos/renderer/gfx-agent/RenderPassAgent.cpp index a181692804..c764a82e45 100644 --- a/native/cocos/renderer/gfx-agent/RenderPassAgent.cpp +++ b/native/cocos/renderer/gfx-agent/RenderPassAgent.cpp @@ -42,7 +42,7 @@ RenderPassAgent::~RenderPassAgent() { RenderPassDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/ShaderAgent.cpp b/native/cocos/renderer/gfx-agent/ShaderAgent.cpp index 37910b4afa..9f6071618e 100644 --- a/native/cocos/renderer/gfx-agent/ShaderAgent.cpp +++ b/native/cocos/renderer/gfx-agent/ShaderAgent.cpp @@ -42,7 +42,7 @@ ShaderAgent::~ShaderAgent() { ShaderDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/SwapchainAgent.cpp b/native/cocos/renderer/gfx-agent/SwapchainAgent.cpp index 794e72b12f..5ea054981d 100644 --- a/native/cocos/renderer/gfx-agent/SwapchainAgent.cpp +++ b/native/cocos/renderer/gfx-agent/SwapchainAgent.cpp @@ -43,7 +43,7 @@ SwapchainAgent::~SwapchainAgent() { DeviceAgent::getInstance()->getMessageQueue(), SwapchainDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } diff --git a/native/cocos/renderer/gfx-agent/TextureAgent.cpp b/native/cocos/renderer/gfx-agent/TextureAgent.cpp index 15faaae43e..9ec7ece010 100644 --- a/native/cocos/renderer/gfx-agent/TextureAgent.cpp +++ b/native/cocos/renderer/gfx-agent/TextureAgent.cpp @@ -45,7 +45,7 @@ TextureAgent::~TextureAgent() { TextureDestruct, actor, _actor, { - CC_SAFE_DELETE(actor); + actor = nullptr; }); } } diff --git a/native/cocos/renderer/gfx-base/GFXDevice.cpp b/native/cocos/renderer/gfx-base/GFXDevice.cpp index 415da620c9..ea67b5b2b8 100644 --- a/native/cocos/renderer/gfx-base/GFXDevice.cpp +++ b/native/cocos/renderer/gfx-base/GFXDevice.cpp @@ -61,11 +61,7 @@ bool Device::initialize(const DeviceInfo &info) { static_assert(sizeof(void *) == 8, "pointer size assumption broken"); #endif - bool result = doInit(info); - _cmdBuff->addRef(); - _queue->addRef(); - - return result; + return doInit(info); } void Device::destroy() { diff --git a/native/cocos/renderer/gfx-base/GFXDevice.h b/native/cocos/renderer/gfx-base/GFXDevice.h index cb74d477d5..c42bcd5cf7 100644 --- a/native/cocos/renderer/gfx-base/GFXDevice.h +++ b/native/cocos/renderer/gfx-base/GFXDevice.h @@ -173,9 +173,9 @@ protected: ccstd::array(Feature::COUNT)> _features; ccstd::array(Format::COUNT)> _formatFeatures; - Queue *_queue{nullptr}; - QueryPool *_queryPool{nullptr}; - CommandBuffer *_cmdBuff{nullptr}; + IntrusivePtr _queue; + IntrusivePtr _queryPool; + IntrusivePtr _cmdBuff; Executable *_onAcquire{nullptr}; uint32_t _numDrawCalls{0U}; diff --git a/native/cocos/renderer/gfx-base/GFXQueryPool.h b/native/cocos/renderer/gfx-base/GFXQueryPool.h index adbba02809..801774b08a 100644 --- a/native/cocos/renderer/gfx-base/GFXQueryPool.h +++ b/native/cocos/renderer/gfx-base/GFXQueryPool.h @@ -28,6 +28,7 @@ #include #include #include "GFXObject.h" +#include "base/RefCounted.h" #include "base/std/container/unordered_map.h" namespace cc { @@ -46,7 +47,7 @@ namespace gfx { * completeQueryPool */ -class CC_DLL QueryPool : public GFXObject { +class CC_DLL QueryPool : public GFXObject, public RefCounted { public: QueryPool(); ~QueryPool() override; diff --git a/native/cocos/renderer/gfx-base/GFXSwapchain.h b/native/cocos/renderer/gfx-base/GFXSwapchain.h index a6fa56de2d..b7bd06d17e 100644 --- a/native/cocos/renderer/gfx-base/GFXSwapchain.h +++ b/native/cocos/renderer/gfx-base/GFXSwapchain.h @@ -27,12 +27,13 @@ #include "GFXTexture.h" #include "base/Ptr.h" +#include "base/RefCounted.h" #include "gfx-base/GFXDef-common.h" namespace cc { namespace gfx { -class CC_DLL Swapchain : public GFXObject { +class CC_DLL Swapchain : public GFXObject, public RefCounted { public: Swapchain(); ~Swapchain() override; diff --git a/native/cocos/renderer/gfx-gles2/GLES2Device.cpp b/native/cocos/renderer/gfx-gles2/GLES2Device.cpp index ff2f3d6433..33325492d8 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Device.cpp +++ b/native/cocos/renderer/gfx-gles2/GLES2Device.cpp @@ -228,9 +228,10 @@ void GLES2Device::doDestroy() { CC_ASSERT(!_memoryStatus.bufferSize); // Buffer memory leaked. CC_ASSERT(!_memoryStatus.textureSize); // Texture memory leaked. - CC_SAFE_DESTROY_AND_DELETE(_cmdBuff) - CC_SAFE_DESTROY_AND_DELETE(_queryPool) - CC_SAFE_DESTROY_AND_DELETE(_queue) + _queryPool = nullptr; + _queue = nullptr; + _cmdBuff = nullptr; + CC_SAFE_DESTROY_AND_DELETE(_gpuContext) } @@ -245,7 +246,7 @@ void GLES2Device::acquire(Swapchain *const *swapchains, uint32_t count) { void GLES2Device::present() { CC_PROFILE(GLES2DevicePresent); - auto *queue = static_cast(_queue); + auto *queue = static_cast(_queue.get()); _numDrawCalls = queue->_numDrawCalls; _numInstances = queue->_numInstances; _numTriangles = queue->_numTriangles; diff --git a/native/cocos/renderer/gfx-gles3/GLES3Device.cpp b/native/cocos/renderer/gfx-gles3/GLES3Device.cpp index 1ed8e04628..c0df9910ee 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Device.cpp +++ b/native/cocos/renderer/gfx-gles3/GLES3Device.cpp @@ -236,9 +236,9 @@ void GLES3Device::doDestroy() { CC_ASSERT(!_memoryStatus.bufferSize); // Buffer memory leaked CC_ASSERT(!_memoryStatus.textureSize); // Texture memory leaked - CC_SAFE_DESTROY_AND_DELETE(_cmdBuff) - CC_SAFE_DESTROY_AND_DELETE(_queryPool) - CC_SAFE_DESTROY_AND_DELETE(_queue) + _cmdBuff = nullptr; + _queryPool = nullptr; + _queue = nullptr; CC_SAFE_DESTROY_AND_DELETE(_gpuContext) } @@ -253,7 +253,7 @@ void GLES3Device::acquire(Swapchain *const *swapchains, uint32_t count) { void GLES3Device::present() { CC_PROFILE(GLES3DevicePresent); - auto *queue = static_cast(_queue); + auto *queue = static_cast(_queue.get()); _numDrawCalls = queue->_numDrawCalls; _numInstances = queue->_numInstances; _numTriangles = queue->_numTriangles; diff --git a/native/cocos/renderer/gfx-metal/MTLDevice.mm b/native/cocos/renderer/gfx-metal/MTLDevice.mm index 59622144c8..9bb69e26ef 100644 --- a/native/cocos/renderer/gfx-metal/MTLDevice.mm +++ b/native/cocos/renderer/gfx-metal/MTLDevice.mm @@ -156,9 +156,9 @@ void CCMTLDevice::doDestroy() { CC_SAFE_DELETE(_gpuDeviceObj); - CC_SAFE_DESTROY_AND_DELETE(_queryPool) - CC_SAFE_DESTROY_AND_DELETE(_queue); - CC_SAFE_DESTROY_AND_DELETE(_cmdBuff); + _queryPool = nullptr; + _queue = nullptr; + _cmdBuff = nullptr; CCMTLGPUGarbageCollectionPool::getInstance()->flush(); @@ -193,7 +193,7 @@ void CCMTLDevice::acquire(Swapchain *const *swapchains, uint32_t count) { } // Clear queue stats - CCMTLQueue *queue = static_cast(_queue); + CCMTLQueue *queue = static_cast(_queue.get()); queue->gpuQueueObj()->numDrawCalls = 0; queue->gpuQueueObj()->numInstances = 0; queue->gpuQueueObj()->numTriangles = 0; @@ -201,7 +201,7 @@ void CCMTLDevice::acquire(Swapchain *const *swapchains, uint32_t count) { void CCMTLDevice::present() { CC_PROFILE(CCMTLDevicePresent); - CCMTLQueue *queue = (CCMTLQueue *)_queue; + CCMTLQueue *queue = static_cast(_queue.get()); _numDrawCalls = queue->gpuQueueObj()->numDrawCalls; _numInstances = queue->gpuQueueObj()->numInstances; _numTriangles = queue->gpuQueueObj()->numTriangles; @@ -313,12 +313,12 @@ void CCMTLDevice::copyBuffersToTexture(const uint8_t *const *buffers, Texture *t // the wiggle room to leverage immediate update vs. copy-upload strategies without // breaking compatibilities. When we reached some conclusion on this subject, // getting rid of this interface all together may become a better option. - static_cast(_cmdBuff)->copyBuffersToTexture(buffers, texture, regions, count); + static_cast(_cmdBuff.get())->copyBuffersToTexture(buffers, texture, regions, count); } void CCMTLDevice::copyTextureToBuffers(Texture *src, uint8_t *const *buffers, const BufferTextureCopy *region, uint32_t count) { CC_PROFILE(CCMTLDeviceCopyTextureToBuffers); - static_cast(_cmdBuff)->copyTextureToBuffers(src, buffers, region, count); + static_cast(_cmdBuff.get())->copyTextureToBuffers(src, buffers, region, count); } void CCMTLDevice::getQueryPoolResults(QueryPool *queryPool) { diff --git a/native/cocos/renderer/gfx-validator/BufferValidator.cpp b/native/cocos/renderer/gfx-validator/BufferValidator.cpp index b176ec26c0..425823260f 100644 --- a/native/cocos/renderer/gfx-validator/BufferValidator.cpp +++ b/native/cocos/renderer/gfx-validator/BufferValidator.cpp @@ -34,13 +34,11 @@ namespace gfx { BufferValidator::BufferValidator(Buffer *actor) : Agent(actor) { - CC_SAFE_ADD_REF(actor); _typedID = actor->getTypedID(); } BufferValidator::~BufferValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_RELEASE(_actor); uint64_t lifeTime = DeviceValidator::getInstance()->currentFrame() - _creationFrame; // skip those that have never been updated diff --git a/native/cocos/renderer/gfx-validator/CommandBufferValidator.cpp b/native/cocos/renderer/gfx-validator/CommandBufferValidator.cpp index df0b19a71d..26c9c887a4 100644 --- a/native/cocos/renderer/gfx-validator/CommandBufferValidator.cpp +++ b/native/cocos/renderer/gfx-validator/CommandBufferValidator.cpp @@ -50,7 +50,6 @@ CommandBufferValidator::CommandBufferValidator(CommandBuffer *actor) CommandBufferValidator::~CommandBufferValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_DELETE(_actor); } void CommandBufferValidator::initValidator() { diff --git a/native/cocos/renderer/gfx-validator/DescriptorSetLayoutValidator.cpp b/native/cocos/renderer/gfx-validator/DescriptorSetLayoutValidator.cpp index 39c4f3644c..6f2081b5d3 100644 --- a/native/cocos/renderer/gfx-validator/DescriptorSetLayoutValidator.cpp +++ b/native/cocos/renderer/gfx-validator/DescriptorSetLayoutValidator.cpp @@ -36,13 +36,11 @@ namespace gfx { DescriptorSetLayoutValidator::DescriptorSetLayoutValidator(DescriptorSetLayout *actor) : Agent(actor) { - CC_SAFE_ADD_REF(actor); _typedID = actor->getTypedID(); } DescriptorSetLayoutValidator::~DescriptorSetLayoutValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_RELEASE(_actor); } void DescriptorSetLayoutValidator::doInit(const DescriptorSetLayoutInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/DescriptorSetValidator.cpp b/native/cocos/renderer/gfx-validator/DescriptorSetValidator.cpp index f1630c6393..8ba346b919 100644 --- a/native/cocos/renderer/gfx-validator/DescriptorSetValidator.cpp +++ b/native/cocos/renderer/gfx-validator/DescriptorSetValidator.cpp @@ -38,13 +38,11 @@ namespace gfx { DescriptorSetValidator::DescriptorSetValidator(DescriptorSet *actor) : Agent(actor) { - CC_SAFE_ADD_REF(actor); _typedID = actor->getTypedID(); } DescriptorSetValidator::~DescriptorSetValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_RELEASE(_actor); } void DescriptorSetValidator::doInit(const DescriptorSetInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/DeviceValidator.cpp b/native/cocos/renderer/gfx-validator/DeviceValidator.cpp index 84a0da9712..3807ccf3a8 100644 --- a/native/cocos/renderer/gfx-validator/DeviceValidator.cpp +++ b/native/cocos/renderer/gfx-validator/DeviceValidator.cpp @@ -60,7 +60,7 @@ DeviceValidator::DeviceValidator(Device *device) : Agent(device) { } DeviceValidator::~DeviceValidator() { - CC_SAFE_DELETE(_actor); + destroy(); DeviceValidator::instance = nullptr; } @@ -89,14 +89,14 @@ bool DeviceValidator::doInit(const DeviceInfo &info) { memcpy(_features.data(), _actor->_features.data(), static_cast(Feature::COUNT) * sizeof(bool)); memcpy(_formatFeatures.data(), _actor->_formatFeatures.data(), static_cast(Format::COUNT) * sizeof(FormatFeatureBit)); - static_cast(_queue)->_inited = true; - static_cast(_queryPool)->_inited = true; - static_cast(_cmdBuff)->_queue = _queue; - static_cast(_cmdBuff)->initValidator(); + static_cast(_queue.get())->_inited = true; + static_cast(_queryPool.get())->_inited = true; + static_cast(_cmdBuff.get())->_queue = _queue; + static_cast(_cmdBuff.get())->initValidator(); - DeviceResourceTracker::push(_cmdBuff); - DeviceResourceTracker::push(_queryPool); - DeviceResourceTracker::push(_queue); + DeviceResourceTracker::push(_cmdBuff.get()); + DeviceResourceTracker::push(_queryPool.get()); + DeviceResourceTracker::push(_queue.get()); CC_LOG_INFO("Device validator enabled."); @@ -105,21 +105,12 @@ bool DeviceValidator::doInit(const DeviceInfo &info) { void DeviceValidator::doDestroy() { if (_cmdBuff) { - static_cast(_cmdBuff)->destroyValidator(); - static_cast(_cmdBuff)->_actor = nullptr; - delete _cmdBuff; + static_cast(_cmdBuff.get())->destroyValidator(); _cmdBuff = nullptr; } - if (_queryPool) { - static_cast(_queryPool)->_actor = nullptr; - delete _queryPool; - _queryPool = nullptr; - } - if (_queue) { - static_cast(_queue)->_actor = nullptr; - delete _queue; - _queue = nullptr; - } + + _queryPool = nullptr; + _queue = nullptr; DeviceResourceTracker::checkEmpty(); DeviceResourceTracker::checkEmpty(); @@ -137,7 +128,9 @@ void DeviceValidator::doDestroy() { DeviceResourceTracker::checkEmpty(); DeviceResourceTracker::checkEmpty(); - _actor->destroy(); + if (_actor) { + _actor->destroy(); + } } void DeviceValidator::acquire(Swapchain *const *swapchains, uint32_t count) { diff --git a/native/cocos/renderer/gfx-validator/FramebufferValidator.cpp b/native/cocos/renderer/gfx-validator/FramebufferValidator.cpp index 894074d426..4693869bc8 100644 --- a/native/cocos/renderer/gfx-validator/FramebufferValidator.cpp +++ b/native/cocos/renderer/gfx-validator/FramebufferValidator.cpp @@ -37,13 +37,11 @@ namespace gfx { FramebufferValidator::FramebufferValidator(Framebuffer *actor) : Agent(actor) { - CC_SAFE_ADD_REF(actor); _typedID = actor->getTypedID(); } FramebufferValidator::~FramebufferValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_RELEASE(_actor); } void FramebufferValidator::doInit(const FramebufferInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/InputAssemblerValidator.cpp b/native/cocos/renderer/gfx-validator/InputAssemblerValidator.cpp index 2501f4731c..fac7d2450f 100644 --- a/native/cocos/renderer/gfx-validator/InputAssemblerValidator.cpp +++ b/native/cocos/renderer/gfx-validator/InputAssemblerValidator.cpp @@ -35,13 +35,11 @@ namespace gfx { InputAssemblerValidator::InputAssemblerValidator(InputAssembler *actor) : Agent(actor) { - CC_SAFE_ADD_REF(actor); _typedID = actor->getTypedID(); } InputAssemblerValidator::~InputAssemblerValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_RELEASE(_actor); } void InputAssemblerValidator::doInit(const InputAssemblerInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/PipelineLayoutValidator.cpp b/native/cocos/renderer/gfx-validator/PipelineLayoutValidator.cpp index 2273fbd48d..1a6cc15297 100644 --- a/native/cocos/renderer/gfx-validator/PipelineLayoutValidator.cpp +++ b/native/cocos/renderer/gfx-validator/PipelineLayoutValidator.cpp @@ -41,7 +41,6 @@ PipelineLayoutValidator::PipelineLayoutValidator(PipelineLayout *actor) PipelineLayoutValidator::~PipelineLayoutValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_DELETE(_actor); } void PipelineLayoutValidator::doInit(const PipelineLayoutInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/PipelineStateValidator.cpp b/native/cocos/renderer/gfx-validator/PipelineStateValidator.cpp index 46c01aedcd..8a29b3992e 100644 --- a/native/cocos/renderer/gfx-validator/PipelineStateValidator.cpp +++ b/native/cocos/renderer/gfx-validator/PipelineStateValidator.cpp @@ -42,7 +42,6 @@ PipelineStateValidator::PipelineStateValidator(PipelineState *actor) PipelineStateValidator::~PipelineStateValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_DELETE(_actor); } void PipelineStateValidator::doInit(const PipelineStateInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/QueryPoolValidator.cpp b/native/cocos/renderer/gfx-validator/QueryPoolValidator.cpp index 3e49eb2f87..1f3508803b 100644 --- a/native/cocos/renderer/gfx-validator/QueryPoolValidator.cpp +++ b/native/cocos/renderer/gfx-validator/QueryPoolValidator.cpp @@ -40,7 +40,6 @@ QueryPoolValidator::QueryPoolValidator(QueryPool *actor) QueryPoolValidator::~QueryPoolValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_DELETE(_actor); } void QueryPoolValidator::doInit(const QueryPoolInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/QueueValidator.cpp b/native/cocos/renderer/gfx-validator/QueueValidator.cpp index f01089ea4c..4010ea2c0a 100644 --- a/native/cocos/renderer/gfx-validator/QueueValidator.cpp +++ b/native/cocos/renderer/gfx-validator/QueueValidator.cpp @@ -38,7 +38,6 @@ QueueValidator::QueueValidator(Queue *actor) QueueValidator::~QueueValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_DELETE(_actor); } void QueueValidator::doInit(const QueueInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/RenderPassValidator.cpp b/native/cocos/renderer/gfx-validator/RenderPassValidator.cpp index ebc06bbbe0..46339ec7f2 100644 --- a/native/cocos/renderer/gfx-validator/RenderPassValidator.cpp +++ b/native/cocos/renderer/gfx-validator/RenderPassValidator.cpp @@ -34,13 +34,11 @@ namespace gfx { RenderPassValidator::RenderPassValidator(RenderPass *actor) : Agent(actor) { - CC_SAFE_ADD_REF(actor); _typedID = actor->getTypedID(); } RenderPassValidator::~RenderPassValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_RELEASE(_actor); } void RenderPassValidator::doInit(const RenderPassInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/ShaderValidator.cpp b/native/cocos/renderer/gfx-validator/ShaderValidator.cpp index 99502ba669..91bef26201 100644 --- a/native/cocos/renderer/gfx-validator/ShaderValidator.cpp +++ b/native/cocos/renderer/gfx-validator/ShaderValidator.cpp @@ -34,13 +34,11 @@ namespace gfx { ShaderValidator::ShaderValidator(Shader *actor) : Agent(actor) { - CC_SAFE_ADD_REF(actor); _typedID = actor->getTypedID(); } ShaderValidator::~ShaderValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_RELEASE(_actor); } void ShaderValidator::doInit(const ShaderInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/SwapchainValidator.cpp b/native/cocos/renderer/gfx-validator/SwapchainValidator.cpp index 286d29444b..c744176a29 100644 --- a/native/cocos/renderer/gfx-validator/SwapchainValidator.cpp +++ b/native/cocos/renderer/gfx-validator/SwapchainValidator.cpp @@ -40,7 +40,6 @@ SwapchainValidator::SwapchainValidator(Swapchain *actor) SwapchainValidator::~SwapchainValidator() { DeviceResourceTracker::erase(this); - CC_SAFE_DELETE(_actor); } void SwapchainValidator::doInit(const SwapchainInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/TextureValidator.cpp b/native/cocos/renderer/gfx-validator/TextureValidator.cpp index ff227d2378..4f35f0b297 100644 --- a/native/cocos/renderer/gfx-validator/TextureValidator.cpp +++ b/native/cocos/renderer/gfx-validator/TextureValidator.cpp @@ -45,13 +45,11 @@ ccstd::unordered_map featureCheckMap{}; TextureValidator::TextureValidator(Texture *actor) : Agent(actor) { - CC_SAFE_ADD_REF(actor); _typedID = actor->getTypedID(); } TextureValidator::~TextureValidator() { DeviceResourceTracker::erase(this); - if (_ownTheActor) CC_SAFE_RELEASE(_actor); } void TextureValidator::doInit(const TextureInfo &info) { diff --git a/native/cocos/renderer/gfx-vulkan/VKDevice.cpp b/native/cocos/renderer/gfx-vulkan/VKDevice.cpp index 6fe322dda0..d8e36b5055 100644 --- a/native/cocos/renderer/gfx-vulkan/VKDevice.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKDevice.cpp @@ -391,7 +391,7 @@ bool CCVKDevice::doInit(const DeviceInfo & /*info*/) { } _gpuBufferHub = ccnew CCVKGPUBufferHub(_gpuDevice); - _gpuTransportHub = ccnew CCVKGPUTransportHub(_gpuDevice, static_cast(_queue)->gpuQueue()); + _gpuTransportHub = ccnew CCVKGPUTransportHub(_gpuDevice, static_cast(_queue.get())->gpuQueue()); _gpuDescriptorHub = ccnew CCVKGPUDescriptorHub(_gpuDevice); _gpuSemaphorePool = ccnew CCVKGPUSemaphorePool(_gpuDevice); _gpuBarrierManager = ccnew CCVKGPUBarrierManager(_gpuDevice); @@ -490,9 +490,10 @@ void CCVKDevice::doDestroy() { } _depthStencilTextures.clear(); - CC_SAFE_DESTROY_AND_DELETE(_queryPool) - CC_SAFE_DESTROY_AND_DELETE(_queue) - CC_SAFE_DESTROY_AND_DELETE(_cmdBuff) + _queryPool = nullptr; + _queue = nullptr; + _cmdBuff = nullptr; + CC_SAFE_DELETE(_gpuBufferHub) CC_SAFE_DELETE(_gpuTransportHub) @@ -601,7 +602,7 @@ VkImageMemoryBarrier presentBarrier{ void CCVKDevice::acquire(Swapchain *const *swapchains, uint32_t count) { if (_onAcquire) _onAcquire->execute(); - auto *queue = static_cast(_queue); + auto *queue = static_cast(_queue.get()); queue->gpuQueue()->lastSignaledSemaphores.clear(); vkSwapchainIndices.clear(); gpuSwapchains.clear(); @@ -651,7 +652,7 @@ void CCVKDevice::acquire(Swapchain *const *swapchains, uint32_t count) { void CCVKDevice::present() { CC_PROFILE(CCVKDevicePresent); - auto *queue = static_cast(_queue); + auto *queue = static_cast(_queue.get()); _numDrawCalls = queue->_numDrawCalls; _numInstances = queue->_numInstances; _numTriangles = queue->_numTriangles;