From f6f59892afeef4acde53ec5f8a43eb2ef60323b3 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 26 Jan 2022 11:57:02 +0800 Subject: [PATCH] Refactor IntrusivePtr, remove reinterpret_cast (#4221) * Refactor IntrusivePtr, remove reinterpret_cast --- native/cocos/3d/assets/Mesh.cpp | 2 ++ native/cocos/3d/assets/Mesh.h | 4 ++-- native/cocos/3d/misc/CreateMesh.cpp | 5 +++-- native/cocos/base/Ptr.h | 19 ++++++------------- native/cocos/core/assets/TextureBase.cpp | 2 ++ native/cocos/core/assets/TextureBase.h | 2 +- native/cocos/core/assets/TextureCube.cpp | 2 ++ native/cocos/core/assets/TextureCube.h | 6 +++--- .../cocos/core/scene-graph/SceneGlobals.cpp | 6 ++++++ native/cocos/core/scene-graph/SceneGlobals.h | 10 +++++----- native/cocos/primitive/Primitive.cpp | 6 ++++-- native/cocos/primitive/Primitive.h | 2 +- native/cocos/renderer/core/PassInstance.cpp | 2 ++ .../renderer/pipeline/RenderPipeline.cpp | 4 ++++ .../cocos/renderer/pipeline/RenderPipeline.h | 2 +- 15 files changed, 44 insertions(+), 30 deletions(-) diff --git a/native/cocos/3d/assets/Mesh.cpp b/native/cocos/3d/assets/Mesh.cpp index 1473830a0b..d76bb4d00c 100644 --- a/native/cocos/3d/assets/Mesh.cpp +++ b/native/cocos/3d/assets/Mesh.cpp @@ -169,6 +169,8 @@ DataWritterCallback getWriter(DataView &dataView, gfx::Format format) { } // namespace +Mesh::~Mesh() = default; + cc::any Mesh::getNativeAsset() const { return _data; //cjh FIXME: need copy? could be _data pointer? } diff --git a/native/cocos/3d/assets/Mesh.h b/native/cocos/3d/assets/Mesh.h index d8dd830193..ef4fe8536c 100644 --- a/native/cocos/3d/assets/Mesh.h +++ b/native/cocos/3d/assets/Mesh.h @@ -160,8 +160,8 @@ public: Uint8Array data; }; - Mesh() = default; - ~Mesh() override = default; + Mesh() = default; + ~Mesh() override; cc::any getNativeAsset() const override; void setNativeAsset(const cc::any &obj) override; diff --git a/native/cocos/3d/misc/CreateMesh.cpp b/native/cocos/3d/misc/CreateMesh.cpp index 4e15eddd3b..0946d19841 100644 --- a/native/cocos/3d/misc/CreateMesh.cpp +++ b/native/cocos/3d/misc/CreateMesh.cpp @@ -5,6 +5,7 @@ #include "3d/misc/BufferBlob.h" #include "core/ArrayBuffer.h" #include "core/DataView.h" +#include "core/assets/RenderingSubMesh.h" #include "renderer/gfx-base/GFXDef-common.h" namespace cc { @@ -178,8 +179,8 @@ Mesh::ICreateInfo createMeshInfo(const IGeometry &geometry, const ICreateMeshOpt // Fill index buffer. ArrayBuffer::Ptr indexBuffer; - uint32_t idxCount = 0; - const uint32_t idxStride = 2; + uint32_t idxCount = 0; + const uint32_t idxStride = 2; if (geometry.indices.has_value()) { const std::vector &indices = geometry.indices.value(); idxCount = static_cast(indices.size()); diff --git a/native/cocos/base/Ptr.h b/native/cocos/base/Ptr.h index 825bc088bc..55f554174a 100644 --- a/native/cocos/base/Ptr.h +++ b/native/cocos/base/Ptr.h @@ -78,7 +78,6 @@ #pragma once #include -#include "base/RefCounted.h" namespace cc { @@ -88,44 +87,38 @@ public: using element_type = T; IntrusivePtr() { - static_assert(std::is_base_of::value, "RefCounted required!"); } IntrusivePtr(T *p) : _ptr(p) { // NOLINT - static_assert(std::is_base_of::value, "RefCounted required!"); if (_ptr) { - reinterpret_cast(_ptr)->addRef(); + _ptr->addRef(); } } IntrusivePtr(const IntrusivePtr &r) : _ptr(r._ptr) { - static_assert(std::is_base_of::value, "RefCounted required!"); if (_ptr) { - reinterpret_cast(_ptr)->addRef(); + _ptr->addRef(); } } template IntrusivePtr(const IntrusivePtr &r) : _ptr(r.get()) { // NOLINT - static_assert(std::is_base_of::value, "RefCounted required!"); if (_ptr) { - reinterpret_cast(_ptr)->addRef(); + _ptr->addRef(); } } // Move constructors. IntrusivePtr(IntrusivePtr &&r) noexcept : _ptr(r.release()) { - static_assert(std::is_base_of::value, "RefCounted required!"); } template IntrusivePtr(IntrusivePtr &&r) noexcept : _ptr(r.release()) { // NOLINT - static_assert(std::is_base_of::value, "RefCounted required!"); } ~IntrusivePtr() { if (_ptr) { - reinterpret_cast(_ptr)->release(); + _ptr->release(); } } @@ -139,10 +132,10 @@ public: IntrusivePtr &operator=(T *p) { // AddRef first so that self assignment should work if (p) { - reinterpret_cast(p)->addRef(); + p->addRef(); } if (_ptr) { - reinterpret_cast(_ptr)->release(); + _ptr->release(); } _ptr = p; return *this; diff --git a/native/cocos/core/assets/TextureBase.cpp b/native/cocos/core/assets/TextureBase.cpp index 5a4a1f1856..ca05b810e9 100644 --- a/native/cocos/core/assets/TextureBase.cpp +++ b/native/cocos/core/assets/TextureBase.cpp @@ -46,6 +46,8 @@ TextureBase::TextureBase() { _textureHash = murmurhash2::MurmurHash2(_id.data(), static_cast(_id.length()), 666); //cjh TODO: How about using boost hash functionality? } +TextureBase::~TextureBase() = default; + void TextureBase::setWrapMode(WrapMode wrapS, WrapMode wrapT, WrapMode wrapR) { _wrapS = wrapS; _samplerInfo.addressU = static_cast(wrapS), diff --git a/native/cocos/core/assets/TextureBase.h b/native/cocos/core/assets/TextureBase.h index 17a1a7aa2d..5f98b8d692 100644 --- a/native/cocos/core/assets/TextureBase.h +++ b/native/cocos/core/assets/TextureBase.h @@ -66,7 +66,7 @@ public: */ using Filter = Filter; - ~TextureBase() override = default; + ~TextureBase() override; /** * @en Whether the pixel data is compressed. * @zh 此贴图是否为压缩的像素格式。 diff --git a/native/cocos/core/assets/TextureCube.cpp b/native/cocos/core/assets/TextureCube.cpp index 9b0571c9ba..070f48c4b2 100644 --- a/native/cocos/core/assets/TextureCube.cpp +++ b/native/cocos/core/assets/TextureCube.cpp @@ -70,6 +70,8 @@ TextureCube *TextureCube::fromTexture2DArray(const std::vector &tex return out; } +TextureCube::~TextureCube() = default; + void TextureCube::setMipmaps(const std::vector &value) { _mipmaps = value; setMipmapLevel(static_cast(_mipmaps.size())); diff --git a/native/cocos/core/assets/TextureCube.h b/native/cocos/core/assets/TextureCube.h index 245643edbc..cf5ab2eacf 100644 --- a/native/cocos/core/assets/TextureCube.h +++ b/native/cocos/core/assets/TextureCube.h @@ -73,8 +73,8 @@ class TextureCube final : public SimpleTexture { public: using Super = SimpleTexture; - explicit TextureCube() = default; - ~TextureCube() override = default; + explicit TextureCube() = default; + ~TextureCube() override; /** * @en The index for all faces of the cube @@ -122,7 +122,7 @@ public: void setMipmaps(const std::vector &value); - void setMipmapsForJS(const std::vector& value) { + void setMipmapsForJS(const std::vector &value) { _mipmaps = value; } diff --git a/native/cocos/core/scene-graph/SceneGlobals.cpp b/native/cocos/core/scene-graph/SceneGlobals.cpp index 603797ce87..e8b6040049 100644 --- a/native/cocos/core/scene-graph/SceneGlobals.cpp +++ b/native/cocos/core/scene-graph/SceneGlobals.cpp @@ -64,4 +64,10 @@ void SceneGlobals::activate() { } } +void SceneGlobals::setAmbientInfo(scene::AmbientInfo *info) { _ambientInfo = info; } +void SceneGlobals::setShadowsInfo(scene::ShadowsInfo *info) { _shadowInfo = info; } +void SceneGlobals::setSkyboxInfo(scene::SkyboxInfo *info) { _skyboxInfo = info; } +void SceneGlobals::setFogInfo(scene::FogInfo *info) { _fogInfo = info; } +void SceneGlobals::setOctreeInfo(scene::OctreeInfo *info) { _octreeInfo = info; } + } // namespace cc diff --git a/native/cocos/core/scene-graph/SceneGlobals.h b/native/cocos/core/scene-graph/SceneGlobals.h index b2c1e3ac2f..c7f8346fe7 100644 --- a/native/cocos/core/scene-graph/SceneGlobals.h +++ b/native/cocos/core/scene-graph/SceneGlobals.h @@ -51,11 +51,11 @@ public: inline scene::FogInfo * getFogInfo() const { return _fogInfo.get(); } inline scene::OctreeInfo * getOctreeInfo() const { return _octreeInfo.get(); } - inline void setAmbientInfo(scene::AmbientInfo *info) { _ambientInfo = info; } - inline void setShadowsInfo(scene::ShadowsInfo *info) { _shadowInfo = info; } - inline void setSkyboxInfo(scene::SkyboxInfo *info) { _skyboxInfo = info; } - inline void setFogInfo(scene::FogInfo *info) { _fogInfo = info; } - inline void setOctreeInfo(scene::OctreeInfo *info) { _octreeInfo = info; } + void setAmbientInfo(scene::AmbientInfo *info); + void setShadowsInfo(scene::ShadowsInfo *info); + void setSkyboxInfo(scene::SkyboxInfo *info); + void setFogInfo(scene::FogInfo *info); + void setOctreeInfo(scene::OctreeInfo *info); private: IntrusivePtr _ambientInfo; diff --git a/native/cocos/primitive/Primitive.cpp b/native/cocos/primitive/Primitive.cpp index ef6aa8fdcb..7b82d0cdb8 100644 --- a/native/cocos/primitive/Primitive.cpp +++ b/native/cocos/primitive/Primitive.cpp @@ -24,13 +24,15 @@ ****************************************************************************/ #include "primitive/Primitive.h" #include "3d/misc/CreateMesh.h" +#include "core/assets/RenderingSubMesh.h" namespace cc { -Primitive::Primitive(PrimitiveType type) -: Mesh(), type(type) { +Primitive::Primitive(PrimitiveType type) : type(type) { } +Primitive::~Primitive() = default; + void Primitive::onLoaded() { reset(createMeshInfo(createGeometry(type))); } diff --git a/native/cocos/primitive/Primitive.h b/native/cocos/primitive/Primitive.h index 2497a25b21..520ec8b296 100644 --- a/native/cocos/primitive/Primitive.h +++ b/native/cocos/primitive/Primitive.h @@ -81,7 +81,7 @@ public: Record info; //cjh float? explicit Primitive(PrimitiveType type = PrimitiveType::BOX); - ~Primitive() override = default; + ~Primitive() override; /** * @en * Construct the primitive mesh with `type` and `info`. diff --git a/native/cocos/renderer/core/PassInstance.cpp b/native/cocos/renderer/core/PassInstance.cpp index 5ebbb7893e..235c5edf1b 100644 --- a/native/cocos/renderer/core/PassInstance.cpp +++ b/native/cocos/renderer/core/PassInstance.cpp @@ -26,6 +26,8 @@ #include "renderer/core/PassInstance.h" #include #include "renderer/core/ProgramLib.h" +#include "renderer/pipeline/BatchedBuffer.h" +#include "renderer/pipeline/InstancedBuffer.h" namespace cc { diff --git a/native/cocos/renderer/pipeline/RenderPipeline.cpp b/native/cocos/renderer/pipeline/RenderPipeline.cpp index d6ac84e971..9dcf4f67cf 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.cpp +++ b/native/cocos/renderer/pipeline/RenderPipeline.cpp @@ -341,5 +341,9 @@ void RenderPipeline::framegraphGC() { } } +void RenderPipeline::setGeometryRenderer(GeometryRenderer *geometryRenderer) { + _geometryRenderer = geometryRenderer; +} + } // namespace pipeline } // namespace cc diff --git a/native/cocos/renderer/pipeline/RenderPipeline.h b/native/cocos/renderer/pipeline/RenderPipeline.h index faa0a248f0..67966032d1 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.h +++ b/native/cocos/renderer/pipeline/RenderPipeline.h @@ -115,7 +115,7 @@ public: inline void setProfiler(scene::Model *value) { _profiler = value; } inline GeometryRenderer *getGeometryRenderer() const { return _geometryRenderer; } - inline void setGeometryRenderer(GeometryRenderer *geometryRenderer) { _geometryRenderer = geometryRenderer; } + void setGeometryRenderer(GeometryRenderer *geometryRenderer); inline bool isClusterEnabled() const { return _clusterEnabled; } inline void setClusterEnabled(bool enable) { _clusterEnabled = enable; }