Skip to content

Commit 76ebc02

Browse files
Fix some bugs and add CPU to GPU conversion for IAnimationLibrary
1 parent 6a6c46a commit 76ebc02

File tree

10 files changed

+92
-18
lines changed

10 files changed

+92
-18
lines changed

examples_tests/04.Keyframe/main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ int main()
118118
const char* jointNames[] = {"root","bendy"};
119119
skeleton = core::make_smart_refctd_ptr<asset::ICPUSkeleton>(std::move(parentIDs),std::move(defaultTransforms),&jointNames[0],&jointNames[0]+kJointCount);
120120
}
121-
core::smart_refctd_ptr<asset::ICPUAnimationLibrary> animations;
121+
core::smart_refctd_ptr<video::IGPUAnimationLibrary> gpuanimations;
122122
{
123123
constexpr uint32_t kKeyframeCount = 16u;
124124
constexpr uint32_t kAnimationCount = 3u;
125+
core::smart_refctd_ptr<asset::ICPUAnimationLibrary> animations;
125126
{
126127
asset::SBufferBinding<asset::ICPUBuffer> keyframes = {0ull,core::make_smart_refctd_ptr<asset::ICPUBuffer>(sizeof(asset::ICPUAnimationLibrary::Keyframe)*kKeyframeCount)};
127128
asset::SBufferBinding<asset::ICPUBuffer> timestamps = {0ull,core::make_smart_refctd_ptr<asset::ICPUBuffer>(sizeof(asset::ICPUAnimationLibrary::timestamp_t)*kKeyframeCount)};
@@ -142,7 +143,13 @@ int main()
142143
animations->getAnimation(animationOffsets[i]) = anims[i];
143144
const char* animationNames[] = { "moveNearest","moveLinear","moveCubic" };
144145
animations->addAnimationNames(animationNames,animationNames+kAnimationCount,animationOffsets);
146+
147+
for (auto i=0u; i<kAnimationCount; i++)
148+
{
149+
assert(animations->getAnimationOffsetFromName(animationNames[i]) == animationOffsets[i]);
150+
}
145151
}
152+
gpuanimations = driver->getGPUObjectsFromAssets<asset::ICPUAnimationLibrary>(&animations,&animations+1u)->begin()[0];
146153
}
147154

148155
//

include/nbl/asset/IAnimationLibrary.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ class IAnimationLibrary : public virtual core::IReferenceCounted
2626
using timestamp_t = uint32_t;
2727
struct alignas(8) Keyframe
2828
{
29-
Keyframe() : quat(), scale(0ull) // TODO: initialize scale to 1.f
29+
Keyframe() : scale(0ull) // TODO: initialize scale to 1.f
3030
{
3131
translation[2] = translation[1] = translation[0] = 0.f;
32+
quat = core::vectorSIMDu32(128u,128u,128u,255u); // should be (0,0,0,1) encoded
3233
}
3334
Keyframe(const core::vectorSIMDf& _scale, const core::quaternion& _quat, const CQuantQuaternionCache* quantCache, const core::vectorSIMDf& _translation)
3435
{
@@ -38,12 +39,13 @@ class IAnimationLibrary : public virtual core::IReferenceCounted
3839
//scale = ;
3940
}
4041

41-
/*
4242
inline core::quaternion getRotation() const
4343
{
44-
return quat.getValue(); // TODO: decode from RGBA8_SNORM and normalize
44+
const void* _pix[4] = {&quat,nullptr,nullptr,nullptr};
45+
double out[4];
46+
decodePixels<EF_R8G8B8A8_SNORM,double>(_pix,out,0u,0u);
47+
return reinterpret_cast<const core::quaternion&>(core::normalize(core::vectorSIMDf(out[0],out[1],out[2],out[3])));
4548
}
46-
*/
4749

4850
inline core::vectorSIMDf getScale() const
4951
{
@@ -135,11 +137,11 @@ class IAnimationLibrary : public virtual core::IReferenceCounted
135137

136138
protected:
137139
IAnimationLibrary(SBufferBinding<BufferType>&& _keyframeStorageBinding, SBufferBinding<BufferType>&& _timestampStorageBinding, uint32_t _keyframeCount, SBufferRange<BufferType>&& _animationStorageRange) :
138-
m_stringPool(), m_nameToAnimation(StringComparator(&m_stringPool)), m_keyframeStorageBinding(std::move(_keyframeStorageRange)), m_timestampStorageBinding(std::move(_timestampStorageBinding)),
140+
m_stringPool(), m_nameToAnimation(StringComparator(&m_stringPool)), m_keyframeStorageBinding(std::move(_keyframeStorageBinding)), m_timestampStorageBinding(std::move(_timestampStorageBinding)),
139141
m_animationStorageRange(std::move(_animationStorageRange)), m_keyframeCount(_keyframeCount)
140142
{
141143
assert(m_keyframeStorageBinding.buffer && (m_keyframeStorageBinding.offset%sizeof(Keyframe)==0u) && m_keyframeStorageBinding.offset+sizeof(Keyframe)*m_keyframeCount<=m_keyframeStorageBinding.buffer->getSize());
142-
assert(m_timestampStorageBinding.buffer && (m_timestampStorageBinding.offset%sizeof(Keyframe)==0u) && m_timestampStorageBinding.offset+sizeof(Keyframe)*m_keyframeCount<=m_timestampStorageBinding.buffer->getSize());
144+
assert(m_timestampStorageBinding.buffer && (m_timestampStorageBinding.offset%sizeof(Keyframe)==0u) && m_timestampStorageBinding.offset+sizeof(timestamp_t)*m_keyframeCount<=m_timestampStorageBinding.buffer->getSize());
143145

144146
if (!m_animationStorageRange.isValid())
145147
return;
@@ -206,11 +208,11 @@ class IAnimationLibrary : public virtual core::IReferenceCounted
206208

207209
inline bool operator()(const uint32_t lhs, const uint32_t rhs) const
208210
{
209-
return strcmp(stringPool.data()+lhs,stringPool.data()+rhs)<0;
211+
return strcmp(stringPool->data()+lhs,stringPool->data()+rhs)<0;
210212
}
211213

212214
private:
213-
core::vector<char>* const stringPool;
215+
const core::vector<char>* stringPool;
214216
};
215217
core::vector<char> m_stringPool;
216218
core::map<uint32_t,uint32_t,StringComparator> m_nameToAnimation;

include/nbl/asset/ICPUAnimationLibrary.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class ICPUAnimationLibrary final : public IAnimationLibrary<ICPUBuffer>, /*TODO:
128128
size_t estimate = sizeof(SBufferBinding<ICPUBuffer>)*2ull;
129129
estimate += sizeof(SBufferRange<ICPUBuffer>);
130130
estimate += sizeof(uint32_t);
131-
estimate += m_stringPoolSize;
131+
estimate += m_stringPool.size();
132132
estimate += m_nameToAnimation.size()*sizeof(std::pair<uint32_t,uint32_t>);
133133
// do we add other things to the size estimate?
134134
return estimate;
@@ -164,7 +164,7 @@ class ICPUAnimationLibrary final : public IAnimationLibrary<ICPUBuffer>, /*TODO:
164164
protected:
165165
void restoreFromDummy_impl(IAsset* _other, uint32_t _levelsBelow) override
166166
{
167-
auto* other = static_cast<ICPUSkeleton*>(_other);
167+
auto* other = static_cast<ICPUAnimationLibrary*>(_other);
168168

169169
if (_levelsBelow)
170170
{

include/nbl/asset/format/decodePixels.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ namespace asset
328328
_output[3] = ((pix >> 24) & 0xffULL) / 255.;
329329
}
330330

331+
// TODO: @Crisspl your SNORM decodes are WRONG, they need a max(,-1.f)
331332
template<>
332333
inline void decodePixels<asset::EF_R8G8B8A8_SNORM, double>(const void* _pix[4], double* _output, uint32_t _blockX, uint32_t _blockY)
333334
{

include/nbl/asset/utils/IMeshManipulator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "nbl/asset/ICPUMesh.h"
1717

1818
#include "nbl/asset/utils/CQuantNormalCache.h"
19-
//#include "nbl/asset/utils/CQuantQuaternionCache.h"
19+
#include "nbl/asset/utils/CQuantQuaternionCache.h"
2020

2121
namespace nbl
2222
{
@@ -654,7 +654,7 @@ class IMeshManipulator : public virtual core::IReferenceCounted
654654

655655
//!
656656
virtual CQuantNormalCache* getQuantNormalCache() = 0;
657-
//virtual CQuantQuaternionCache* getQuantQuaternionCache() = 0;
657+
virtual CQuantQuaternionCache* getQuantQuaternionCache() = 0;
658658
};
659659

660660
} // end namespace scene

include/nbl/video/IGPUAnimationLibrary.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class IGPUAnimationLibrary final : public asset::IAnimationLibrary<IGPUBuffer>
2222
inline IGPUAnimationLibrary(
2323
asset::SBufferBinding<IGPUBuffer>&& _keyframeStorageBinding,
2424
asset::SBufferBinding<IGPUBuffer>&& _timestampStorageBinding,
25-
SBufferRange<BufferType>&& _animationStorageRange,
25+
asset::SBufferRange<IGPUBuffer>&& _animationStorageRange,
2626
const asset::IAnimationLibrary<OtherBufferType>* animationLibraryToCopyNamedRanges) :
2727
base_t(
2828
std::move(_keyframeStorageBinding),

include/nbl/video/IGPUObjectFromAssetConverter.h

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class IGPUObjectFromAssetConverter
9999
inline virtual created_gpu_object_array<asset::ICPUImageView> create(const asset::ICPUImageView** const _begin, const asset::ICPUImageView** const _end, const SParams& _params);
100100
inline virtual created_gpu_object_array<asset::ICPUDescriptorSet> create(const asset::ICPUDescriptorSet** const _begin, const asset::ICPUDescriptorSet** const _end, const SParams& _params);
101101
inline virtual created_gpu_object_array<asset::ICPUComputePipeline> create(const asset::ICPUComputePipeline** const _begin, const asset::ICPUComputePipeline** const _end, const SParams& _params);
102+
inline virtual created_gpu_object_array<asset::ICPUAnimationLibrary> create(const asset::ICPUAnimationLibrary** const _begin, const asset::ICPUAnimationLibrary** const _end, const SParams& _params);
102103

103104
//! iterator_type is always either `[const] core::smart_refctd_ptr<AssetType>*[const]*` or `[const] AssetType*[const]*`
104105
template<typename AssetType, typename iterator_type>
@@ -383,8 +384,7 @@ auto IGPUObjectFromAssetConverter::create(const asset::ICPUSkeleton** _begin, co
383384
for (ptrdiff_t i=0u; i<assetCount; i++)
384385
{
385386
const asset::ICPUSkeleton* cpusk = _begin[i];
386-
if (const asset::ICPUBuffer* buf = cpusk->getParentJointIDBinding().buffer.get())
387-
cpuBuffers.push_back(buf);
387+
cpuBuffers.push_back(cpusk->getParentJointIDBinding().buffer.get());
388388
if (const asset::ICPUBuffer* buf = cpusk->getDefaultTransformBinding().buffer.get())
389389
cpuBuffers.push_back(buf);
390390
}
@@ -400,7 +400,6 @@ auto IGPUObjectFromAssetConverter::create(const asset::ICPUSkeleton** _begin, co
400400
const asset::ICPUSkeleton* cpusk = _begin[i];
401401

402402
asset::SBufferBinding<IGPUBuffer> parentJointIDBinding;
403-
if (cpusk->getParentJointIDBinding().buffer)
404403
{
405404
parentJointIDBinding.offset = cpusk->getParentJointIDBinding().offset;
406405
auto& gpubuf = (*gpuBuffers)[bufRedirs[bufIter++]];
@@ -1140,7 +1139,63 @@ inline created_gpu_object_array<asset::ICPUComputePipeline> IGPUObjectFromAssetC
11401139

11411140
return res;
11421141
}
1142+
auto IGPUObjectFromAssetConverter::create(const asset::ICPUAnimationLibrary** _begin, const asset::ICPUAnimationLibrary** _end, const SParams& _params) -> created_gpu_object_array<asset::ICPUAnimationLibrary>
1143+
{
1144+
const size_t assetCount = std::distance(_begin, _end);
1145+
auto res = core::make_refctd_dynamic_array<created_gpu_object_array<asset::ICPUAnimationLibrary> >(assetCount);
1146+
1147+
core::vector<const asset::ICPUBuffer*> cpuBuffers;
1148+
cpuBuffers.reserve(assetCount*3u);
1149+
1150+
for (ptrdiff_t i=0u; i<assetCount; i++)
1151+
{
1152+
const asset::ICPUAnimationLibrary* cpuanim = _begin[i];
1153+
cpuBuffers.push_back(cpuanim->getKeyframeStorageBinding().buffer.get());
1154+
cpuBuffers.push_back(cpuanim->getTimestampStorageBinding().buffer.get());
1155+
if (const asset::ICPUBuffer* buf = cpuanim->getAnimationStorageRange().buffer.get())
1156+
cpuBuffers.push_back(buf);
1157+
}
1158+
1159+
using redirs_t = core::vector<size_t>;
1160+
redirs_t bufRedirs = eliminateDuplicatesAndGenRedirs(cpuBuffers);
1161+
1162+
auto gpuBuffers = getGPUObjectsFromAssets<asset::ICPUBuffer>(cpuBuffers.data(), cpuBuffers.data()+cpuBuffers.size(), _params);
1163+
1164+
size_t bufIter = 0ull;
1165+
for (ptrdiff_t i = 0u; i<assetCount; ++i)
1166+
{
1167+
const asset::ICPUAnimationLibrary* cpuanim = _begin[i];
1168+
1169+
asset::SBufferBinding<IGPUBuffer> keyframeBinding,timestampBinding;
1170+
{
1171+
keyframeBinding.offset = cpuanim->getKeyframeStorageBinding().offset;
1172+
auto& gpubuf = (*gpuBuffers)[bufRedirs[bufIter++]];
1173+
keyframeBinding.offset += gpubuf->getOffset();
1174+
keyframeBinding.buffer = core::smart_refctd_ptr<IGPUBuffer>(gpubuf->getBuffer());
1175+
}
1176+
{
1177+
timestampBinding.offset = cpuanim->getTimestampStorageBinding().offset;
1178+
auto& gpubuf = (*gpuBuffers)[bufRedirs[bufIter++]];
1179+
timestampBinding.offset += gpubuf->getOffset();
1180+
timestampBinding.buffer = core::smart_refctd_ptr<IGPUBuffer>(gpubuf->getBuffer());
1181+
}
1182+
asset::SBufferRange<IGPUBuffer> animationRange;
1183+
if (cpuanim->getAnimationStorageRange().buffer)
1184+
{
1185+
animationRange.offset = cpuanim->getAnimationStorageRange().offset;
1186+
animationRange.size = cpuanim->getAnimationStorageRange().size;
1187+
auto& gpubuf = (*gpuBuffers)[bufRedirs[bufIter++]];
1188+
animationRange.offset += gpubuf->getOffset();
1189+
animationRange.buffer = core::smart_refctd_ptr<IGPUBuffer>(gpubuf->getBuffer());
1190+
}
1191+
1192+
(*res)[i] = core::make_smart_refctd_ptr<IGPUAnimationLibrary>(std::move(keyframeBinding),std::move(timestampBinding),std::move(animationRange),cpuanim);
1193+
}
1194+
1195+
return res;
1196+
}
11431197

1144-
}}//nbl::video
1198+
}
1199+
}//nbl::video
11451200

11461201
#endif

include/nbl/video/asset_traits.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "nbl/video/IGPUSampler.h"
3030
#include "nbl/asset/ICPUImageView.h"
3131
#include "nbl/video/IGPUImageView.h"
32+
#include "nbl/asset/ICPUAnimationLibrary.h"
33+
#include "nbl/video/IGPUAnimationLibrary.h"
3234

3335

3436
namespace nbl
@@ -91,6 +93,8 @@ template<>
9193
struct asset_traits<asset::ICPUComputePipeline> { using GPUObjectType = video::IGPUComputePipeline; };
9294
template<>
9395
struct asset_traits<asset::ICPUMesh> { using GPUObjectType = video::IGPUMesh; };
96+
template<>
97+
struct asset_traits<asset::ICPUAnimationLibrary> { using GPUObjectType = video::IGPUAnimationLibrary; };
9498

9599

96100
template<typename AssetType>

source/Nabla/IDriver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ template created_gpu_object_array<asset::ICPUComputePipeline> IDriver::getGPUObj
8686
template created_gpu_object_array<asset::ICPUDescriptorSetLayout> IDriver::getGPUObjectsFromAssets<asset::ICPUDescriptorSetLayout>(const core::SRange<const core::smart_refctd_ptr<asset::IAsset>>&, IGPUObjectFromAssetConverter* _converter);
8787
template created_gpu_object_array<asset::ICPUSampler> IDriver::getGPUObjectsFromAssets<asset::ICPUSampler>(const core::SRange<const core::smart_refctd_ptr<asset::IAsset>>&, IGPUObjectFromAssetConverter* _converter);
8888
template created_gpu_object_array<asset::ICPUDescriptorSet> IDriver::getGPUObjectsFromAssets<asset::ICPUDescriptorSet>(const core::SRange<const core::smart_refctd_ptr<asset::IAsset>>&, IGPUObjectFromAssetConverter* _converter);
89+
template created_gpu_object_array<asset::ICPUAnimationLibrary> IDriver::getGPUObjectsFromAssets<asset::ICPUAnimationLibrary>(const core::SRange<const core::smart_refctd_ptr<asset::IAsset>>&, IGPUObjectFromAssetConverter* _converter);
8990

9091

9192
template<typename AssetType>
@@ -111,6 +112,7 @@ template created_gpu_object_array<asset::ICPUComputePipeline> IDriver::getGPUObj
111112
template created_gpu_object_array<asset::ICPUDescriptorSetLayout> IDriver::getGPUObjectsFromAssets<asset::ICPUDescriptorSetLayout>(const asset::ICPUDescriptorSetLayout* const* const, const asset::ICPUDescriptorSetLayout* const* const, IGPUObjectFromAssetConverter* _converter);
112113
template created_gpu_object_array<asset::ICPUSampler> IDriver::getGPUObjectsFromAssets<asset::ICPUSampler>(const asset::ICPUSampler* const* const, const asset::ICPUSampler* const* const, IGPUObjectFromAssetConverter* _converter);
113114
template created_gpu_object_array<asset::ICPUDescriptorSet> IDriver::getGPUObjectsFromAssets<asset::ICPUDescriptorSet>(const asset::ICPUDescriptorSet* const* const, const asset::ICPUDescriptorSet* const* const, IGPUObjectFromAssetConverter* _converter);
115+
template created_gpu_object_array<asset::ICPUAnimationLibrary> IDriver::getGPUObjectsFromAssets<asset::ICPUAnimationLibrary>(const asset::ICPUAnimationLibrary* const* const, const asset::ICPUAnimationLibrary* const* const, IGPUObjectFromAssetConverter* _converter);
114116

115117

116118
template<typename AssetType>
@@ -136,6 +138,7 @@ template created_gpu_object_array<asset::ICPUComputePipeline> IDriver::getGPUObj
136138
template created_gpu_object_array<asset::ICPUDescriptorSetLayout> IDriver::getGPUObjectsFromAssets<asset::ICPUDescriptorSetLayout>(const core::smart_refctd_ptr<asset::ICPUDescriptorSetLayout>*, const core::smart_refctd_ptr<asset::ICPUDescriptorSetLayout>*, IGPUObjectFromAssetConverter* _converter);
137139
template created_gpu_object_array<asset::ICPUSampler> IDriver::getGPUObjectsFromAssets<asset::ICPUSampler>(const core::smart_refctd_ptr<asset::ICPUSampler>*, const core::smart_refctd_ptr<asset::ICPUSampler>*, IGPUObjectFromAssetConverter* _converter);
138140
template created_gpu_object_array<asset::ICPUDescriptorSet> IDriver::getGPUObjectsFromAssets<asset::ICPUDescriptorSet>(const core::smart_refctd_ptr<asset::ICPUDescriptorSet>*, const core::smart_refctd_ptr<asset::ICPUDescriptorSet>*, IGPUObjectFromAssetConverter* _converter);
141+
template created_gpu_object_array<asset::ICPUAnimationLibrary> IDriver::getGPUObjectsFromAssets<asset::ICPUAnimationLibrary>(const core::smart_refctd_ptr<asset::ICPUAnimationLibrary>*, const core::smart_refctd_ptr<asset::ICPUAnimationLibrary>*, IGPUObjectFromAssetConverter* _converter);
139142

140143

141144
// TODO: would be nice if something like this worked... eh might have to resort to a Macro

src/nbl/asset/utils/CMeshManipulator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CMeshManipulator : public IMeshManipulator
4141
static core::smart_refctd_ptr<ICPUMeshBuffer> createMeshBufferFetchOptimized(const ICPUMeshBuffer* _inbuffer);
4242

4343
CQuantNormalCache* getQuantNormalCache() override { return &quantNormalCache; }
44+
CQuantQuaternionCache* getQuantQuaternionCache() override { return &quantQuaternionCache; }
4445

4546
private:
4647
friend class IMeshManipulator;
@@ -137,6 +138,7 @@ class CMeshManipulator : public IMeshManipulator
137138

138139
private:
139140
CQuantNormalCache quantNormalCache;
141+
CQuantQuaternionCache quantQuaternionCache;
140142
};
141143

142144
} // end namespace scene

0 commit comments

Comments
 (0)