diff --git a/native/cocos/3d/models/BakedSkinningModel.cpp b/native/cocos/3d/models/BakedSkinningModel.cpp index dc78de64c0..c3c2bc9056 100644 --- a/native/cocos/3d/models/BakedSkinningModel.cpp +++ b/native/cocos/3d/models/BakedSkinningModel.cpp @@ -48,7 +48,6 @@ const std::string INST_JOINT_ANIM_INFO = "a_jointAnimInfo"; namespace cc { BakedSkinningModel::BakedSkinningModel() -: Super() //, _dataPoolManager(Root::getInstance()->getDataPoolManager()) { _type = Model::Type::BAKED_SKINNING; @@ -64,6 +63,9 @@ void BakedSkinningModel::destroy() { if (_jointMedium.buffer != nullptr) { CC_SAFE_DESTROY(_jointMedium.buffer); } + if (_jointMedium.texture.has_value()) { + CC_SAFE_DELETE(_jointMedium.texture.value()); + } applyJointTexture(cc::nullopt); Super::destroy(); } @@ -125,7 +127,7 @@ void BakedSkinningModel::updateUBOs(uint32_t stamp) { } } -void BakedSkinningModel::applyJointTexture(const cc::optional &texture) { +void BakedSkinningModel::applyJointTexture(const cc::optional &texture) { auto oldTex = _jointMedium.texture; if (oldTex.has_value() && texture.has_value() && (&oldTex.value() != &texture.value())) { // _dataPoolManager->jointTexturePool->releaseHandle(oldTex.value()); @@ -134,17 +136,18 @@ void BakedSkinningModel::applyJointTexture(const cc::optional(texture->handle.texture->getWidth()); + jointTextureInfo[0] = static_cast(textureHandle->handle.texture->getWidth()); jointTextureInfo[1] = static_cast(_skeleton->getJoints().size()); - jointTextureInfo[2] = static_cast(texture->pixelOffset) + 0.1F; // guard against floor() underflow + jointTextureInfo[2] = static_cast(textureHandle->pixelOffset) + 0.1F; // guard against floor() underflow jointTextureInfo[3] = 1 / jointTextureInfo[0]; updateInstancedJointTextureInfo(); if (buffer != nullptr) { buffer->update(&jointTextureInfo[0], jointTextureInfo.byteLength()); } - auto *tex = texture->handle.texture; + auto *tex = textureHandle->handle.texture; for (const auto &subModel : _subModels) { auto *descriptorSet = subModel->getDescriptorSet(); @@ -168,7 +171,7 @@ void BakedSkinningModel::updateLocalDescriptors(index_t subModelIndex, gfx::Desc descriptorSet->bindBuffer(pipeline::UBOSkinningAnimation::BINDING, animInfo.buffer); if (texture.has_value()) { auto *sampler = _device->getSampler(JOINT_TEXTURE_SAMPLER_INFO); - descriptorSet->bindTexture(pipeline::JOINTTEXTURE::BINDING, texture->handle.texture); + descriptorSet->bindTexture(pipeline::JOINTTEXTURE::BINDING, texture.value()->handle.texture); descriptorSet->bindSampler(pipeline::JOINTTEXTURE::BINDING, sampler); } } @@ -222,9 +225,9 @@ void BakedSkinningModel::syncDataForJS(const std::vectorhandle.texture = tex; + _jointMedium.texture = textureInfo; updateInstancedJointTextureInfo(); diff --git a/native/cocos/3d/models/BakedSkinningModel.h b/native/cocos/3d/models/BakedSkinningModel.h index 2cbb5d614f..70beeefee1 100644 --- a/native/cocos/3d/models/BakedSkinningModel.h +++ b/native/cocos/3d/models/BakedSkinningModel.h @@ -44,7 +44,7 @@ class DataPoolManager; struct BakedJointInfo { IntrusivePtr buffer; Float32Array jointTextureInfo; - cc::optional texture; + cc::optional texture; IAnimInfo animInfo; std::vector> boundsInfo; }; @@ -88,7 +88,7 @@ public: void setUploadedAnimForJS(bool value) { _isUploadedAnim = value; } protected: - void applyJointTexture(const cc::optional &texture); + void applyJointTexture(const cc::optional &texture); private: BakedJointInfo _jointMedium; diff --git a/native/cocos/3d/skeletal-animation/SkeletalAnimationUtils.cpp b/native/cocos/3d/skeletal-animation/SkeletalAnimationUtils.cpp index 0be08c80ac..40c49521fa 100644 --- a/native/cocos/3d/skeletal-animation/SkeletalAnimationUtils.cpp +++ b/native/cocos/3d/skeletal-animation/SkeletalAnimationUtils.cpp @@ -134,7 +134,7 @@ JointTexturePool::JointTexturePool(gfx::Device *device) { _device = device; const auto &format = selectJointsMediumFormat(_device); _formatSize = gfx::GFX_FORMAT_INFOS[static_cast(format)].size; - _pixelsPerJoint = 48.F / static_cast(_formatSize); + _pixelsPerJoint = 48 / _formatSize; _pool = new TextureBufferPool(device); ITextureBufferPoolInfo poolInfo; poolInfo.format = format; @@ -165,9 +165,9 @@ void JointTexturePool::registerCustomTextureLayouts(const std::vector JointTexturePool::getDefaultPoseTexture(Skeleton *skeleton, Mesh *mesh, Node *skinningRoot) { - uint64_t hash = skeleton->getHash() ^ 0; // may not equal to skeleton.hash - cc::optional texture; +cc::optional JointTexturePool::getDefaultPoseTexture(Skeleton *skeleton, Mesh *mesh, Node *skinningRoot) { + uint64_t hash = skeleton->getHash() ^ 0; // may not equal to skeleton.hash + cc::optional texture; if (_textureBuffers.find(hash) != _textureBuffers.end()) { texture = _textureBuffers[hash]; } @@ -186,18 +186,18 @@ cc::optional JointTexturePool::getDefaultPoseTexture(Skelet handle = _pool->alloc(bufSize * Float32Array::BYTES_PER_ELEMENT); return texture; } - IJointTextureHandle textureHandle; - textureHandle.pixelOffset = handle.start / _formatSize; - textureHandle.refCount = 1; - textureHandle.clipHash = 0; - textureHandle.skeletonHash = skeleton->getHash(); - textureHandle.readyToBeDeleted = false; - textureHandle.handle = handle; - texture = textureHandle; - textureBuffer = Float32Array(bufSize); - buildTexture = true; + IJointTextureHandle *textureHandle = IJointTextureHandle::createJoinTextureHandle(); + textureHandle->pixelOffset = handle.start / _formatSize; + textureHandle->refCount = 1; + textureHandle->clipHash = 0; + textureHandle->skeletonHash = skeleton->getHash(); + textureHandle->readyToBeDeleted = false; + textureHandle->handle = handle; + texture = textureHandle; + textureBuffer = Float32Array(bufSize); + buildTexture = true; } else { - texture->refCount++; + texture.value()->refCount++; } geometry::AABB ab1; @@ -227,10 +227,10 @@ cc::optional JointTexturePool::getDefaultPoseTexture(Skelet } std::vector bounds; - texture->bounds[static_cast(mesh->getHash())] = bounds; + texture.value()->bounds[static_cast(mesh->getHash())] = bounds; geometry::AABB::fromPoints(v3Min, v3Max, &bounds[0]); if (buildTexture) { - _pool->update(texture->handle, textureBuffer.buffer()); + _pool->update(texture.value()->handle, textureBuffer.buffer()); _textureBuffers[hash] = texture.value(); } @@ -335,31 +335,32 @@ cc::optional JointTexturePool::getDefaultPoseTexture(Skelet // } // } -void JointTexturePool::releaseHandle(IJointTextureHandle &handle) { - if (handle.refCount > 0) { - handle.refCount--; +void JointTexturePool::releaseHandle(IJointTextureHandle *handle) { + if (handle->refCount > 0) { + handle->refCount--; } - if (!handle.refCount && handle.readyToBeDeleted) { - uint64_t hash = handle.skeletonHash ^ handle.clipHash; + if (!handle->refCount && handle->readyToBeDeleted) { + uint64_t hash = handle->skeletonHash ^ handle->clipHash; if (_chunkIdxMap.find(hash) != _chunkIdxMap.end()) { - _customPool->free(handle.handle); + _customPool->free(handle->handle); } else { - _pool->free(handle.handle); + _pool->free(handle->handle); } - if (&_textureBuffers[hash] == &handle) { // TODO(xwx): value equal or address equal? + if (_textureBuffers[hash] == handle) { _textureBuffers.erase(hash); + CC_SAFE_DELETE(handle); } } } void JointTexturePool::releaseSkeleton(Skeleton *skeleton) { for (const auto &texture : _textureBuffers) { - auto handle = texture.second; - if (handle.skeletonHash == skeleton->getHash()) { - handle.readyToBeDeleted = true; - if (handle.refCount > 0) { + auto *handle = texture.second; + if (handle->skeletonHash == skeleton->getHash()) { + handle->readyToBeDeleted = true; + if (handle->refCount > 0) { // delete handle record immediately so new allocations with the same asset could work - _textureBuffers.erase(handle.skeletonHash ^ handle.clipHash); + _textureBuffers.erase(handle->skeletonHash ^ handle->clipHash); } else { releaseHandle(handle); } diff --git a/native/cocos/3d/skeletal-animation/SkeletalAnimationUtils.h b/native/cocos/3d/skeletal-animation/SkeletalAnimationUtils.h index 7e06442339..ab99b57a6f 100644 --- a/native/cocos/3d/skeletal-animation/SkeletalAnimationUtils.h +++ b/native/cocos/3d/skeletal-animation/SkeletalAnimationUtils.h @@ -57,7 +57,8 @@ struct IInternalJointAnimInfo { cc::optional bindposeCorrection; // correction factor from the original bindpose }; -struct IJointTextureHandle { +class IJointTextureHandle { +public: uint32_t pixelOffset{0}; uint32_t refCount{0}; uint64_t clipHash{0}; @@ -66,6 +67,13 @@ struct IJointTextureHandle { ITextureBufferHandle handle; std::unordered_map> bounds; cc::optional> animInfos; + + static IJointTextureHandle *createJoinTextureHandle() { + return new IJointTextureHandle(); + } + +private: + IJointTextureHandle() = default; }; class JointTexturePool : public RefCounted { @@ -86,7 +94,7 @@ public: * @zh * 获取默认姿势的骨骼贴图。 */ - cc::optional getDefaultPoseTexture(Skeleton *skeleton, Mesh *mesh, Node *skinningRoot); + cc::optional getDefaultPoseTexture(Skeleton *skeleton, Mesh *mesh, Node *skinningRoot); /** * @en @@ -96,7 +104,7 @@ public: */ // cc::optional getSequencePoseTexture(Skeleton *skeleton, AnimationClip *clip, Mesh *mesh, Node *skinningRoot); - void releaseHandle(IJointTextureHandle &handle); + void releaseHandle(IJointTextureHandle *handle); void releaseSkeleton(Skeleton *skeleton); @@ -105,13 +113,13 @@ public: private: // const IInternalJointAnimInfo &createAnimInfos(Skeleton *skeleton, AnimationClip *clip, Node *skinningRoot); // TODO(xwx): AnimationClip not define - gfx::Device * _device{nullptr}; - IntrusivePtr _pool; - std::unordered_map _textureBuffers; - uint32_t _formatSize{0}; - float _pixelsPerJoint{0}; // TODO(xwx): int or float? - IntrusivePtr _customPool; - std::unordered_map _chunkIdxMap; // hash -> chunkIdx + gfx::Device * _device{nullptr}; + IntrusivePtr _pool; + std::unordered_map _textureBuffers; + uint32_t _formatSize{0}; + uint32_t _pixelsPerJoint{0}; + IntrusivePtr _customPool; + std::unordered_map _chunkIdxMap; // hash -> chunkIdx CC_DISALLOW_COPY_MOVE_ASSIGN(JointTexturePool); };