fix skeletalAnimationUtils.cpp todo (#4141)

This commit is contained in:
bofeng-song
2022-01-08 11:08:57 +08:00
committed by GitHub
parent 5410a48a58
commit 0d147b1e83
4 changed files with 63 additions and 51 deletions
+12 -9
View File
@@ -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<IJointTextureHandle> &texture) {
void BakedSkinningModel::applyJointTexture(const cc::optional<IJointTextureHandle *> &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<IJointTextureHandl
if (!texture.has_value()) {
return;
}
auto *textureHandle = texture.value();
auto *buffer = _jointMedium.buffer.get();
auto &jointTextureInfo = _jointMedium.jointTextureInfo;
jointTextureInfo[0] = static_cast<float>(texture->handle.texture->getWidth());
jointTextureInfo[0] = static_cast<float>(textureHandle->handle.texture->getWidth());
jointTextureInfo[1] = static_cast<float>(_skeleton->getJoints().size());
jointTextureInfo[2] = static_cast<float>(texture->pixelOffset) + 0.1F; // guard against floor() underflow
jointTextureInfo[2] = static_cast<float>(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::vector<cc::optional<geometry::
_jointMedium.animInfo.curFrame = &animInfoData[0];
_jointMedium.animInfo.frameDataBytes = animInfoData.byteLength();
IJointTextureHandle textureInfo;
textureInfo.handle.texture = tex;
_jointMedium.texture = std::move(textureInfo);
IJointTextureHandle *textureInfo = IJointTextureHandle::createJoinTextureHandle();
textureInfo->handle.texture = tex;
_jointMedium.texture = textureInfo;
updateInstancedJointTextureInfo();
+2 -2
View File
@@ -44,7 +44,7 @@ class DataPoolManager;
struct BakedJointInfo {
IntrusivePtr<gfx::Buffer> buffer;
Float32Array jointTextureInfo;
cc::optional<IJointTextureHandle> texture;
cc::optional<IJointTextureHandle *> texture;
IAnimInfo animInfo;
std::vector<cc::optional<geometry::AABB>> boundsInfo;
};
@@ -88,7 +88,7 @@ public:
void setUploadedAnimForJS(bool value) { _isUploadedAnim = value; }
protected:
void applyJointTexture(const cc::optional<IJointTextureHandle> &texture);
void applyJointTexture(const cc::optional<IJointTextureHandle *> &texture);
private:
BakedJointInfo _jointMedium;
@@ -134,7 +134,7 @@ JointTexturePool::JointTexturePool(gfx::Device *device) {
_device = device;
const auto &format = selectJointsMediumFormat(_device);
_formatSize = gfx::GFX_FORMAT_INFOS[static_cast<uint32_t>(format)].size;
_pixelsPerJoint = 48.F / static_cast<float>(_formatSize);
_pixelsPerJoint = 48 / _formatSize;
_pool = new TextureBufferPool(device);
ITextureBufferPoolInfo poolInfo;
poolInfo.format = format;
@@ -165,9 +165,9 @@ void JointTexturePool::registerCustomTextureLayouts(const std::vector<ICustomJoi
}
}
cc::optional<IJointTextureHandle> JointTexturePool::getDefaultPoseTexture(Skeleton *skeleton, Mesh *mesh, Node *skinningRoot) {
uint64_t hash = skeleton->getHash() ^ 0; // may not equal to skeleton.hash
cc::optional<IJointTextureHandle> texture;
cc::optional<IJointTextureHandle *> JointTexturePool::getDefaultPoseTexture(Skeleton *skeleton, Mesh *mesh, Node *skinningRoot) {
uint64_t hash = skeleton->getHash() ^ 0; // may not equal to skeleton.hash
cc::optional<IJointTextureHandle *> texture;
if (_textureBuffers.find(hash) != _textureBuffers.end()) {
texture = _textureBuffers[hash];
}
@@ -186,18 +186,18 @@ cc::optional<IJointTextureHandle> 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<IJointTextureHandle> JointTexturePool::getDefaultPoseTexture(Skelet
}
std::vector<geometry::AABB> bounds;
texture->bounds[static_cast<uint32_t>(mesh->getHash())] = bounds;
texture.value()->bounds[static_cast<uint32_t>(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<IJointTextureHandle> 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);
}
@@ -57,7 +57,8 @@ struct IInternalJointAnimInfo {
cc::optional<Mat4> 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<uint32_t, std::vector<geometry::AABB>> bounds;
cc::optional<std::vector<IInternalJointAnimInfo>> animInfos;
static IJointTextureHandle *createJoinTextureHandle() {
return new IJointTextureHandle();
}
private:
IJointTextureHandle() = default;
};
class JointTexturePool : public RefCounted {
@@ -86,7 +94,7 @@ public:
* @zh
* 获取默认姿势的骨骼贴图。
*/
cc::optional<IJointTextureHandle> getDefaultPoseTexture(Skeleton *skeleton, Mesh *mesh, Node *skinningRoot);
cc::optional<IJointTextureHandle *> getDefaultPoseTexture(Skeleton *skeleton, Mesh *mesh, Node *skinningRoot);
/**
* @en
@@ -96,7 +104,7 @@ public:
*/
// cc::optional<IJointTextureHandle> 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<TextureBufferPool> _pool;
std::unordered_map<uint64_t, IJointTextureHandle> _textureBuffers;
uint32_t _formatSize{0};
float _pixelsPerJoint{0}; // TODO(xwx): int or float?
IntrusivePtr<TextureBufferPool> _customPool;
std::unordered_map<uint64_t, index_t> _chunkIdxMap; // hash -> chunkIdx
gfx::Device * _device{nullptr};
IntrusivePtr<TextureBufferPool> _pool;
std::unordered_map<uint64_t, IJointTextureHandle *> _textureBuffers;
uint32_t _formatSize{0};
uint32_t _pixelsPerJoint{0};
IntrusivePtr<TextureBufferPool> _customPool;
std::unordered_map<uint64_t, index_t> _chunkIdxMap; // hash -> chunkIdx
CC_DISALLOW_COPY_MOVE_ASSIGN(JointTexturePool);
};