Skip to content

Commit 0b8c582

Browse files
Add IAnimationLibrary.h and some TODOs for @AnastaZIuk
1 parent 3846017 commit 0b8c582

File tree

3 files changed

+203
-6
lines changed

3 files changed

+203
-6
lines changed

include/nbl/asset/IAnimationLibrary.h

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
5+
#ifndef __NBL_ASSET_I_ANIMATION_LIBRARY_H_INCLUDED__
6+
#define __NBL_ASSET_I_ANIMATION_LIBRARY_H_INCLUDED__
7+
8+
#include "nbl/macros.h"
9+
10+
#include "nbl/core/core.h"
11+
#include "nbl/asset/utils/CQuantQuaternionCache.h"
12+
13+
namespace nbl
14+
{
15+
namespace asset
16+
{
17+
18+
//! Class which holds the keyframes for animations.
19+
/** An IAnimationLibrary is nothing more than a Structure of Arrays style collection of
20+
* named keyframe ranges.
21+
*/
22+
template <class BufferType>
23+
class IAnimationLibrary : public virtual core::IReferenceCounted
24+
{
25+
public:
26+
struct alignas(8) Keyframe
27+
{
28+
Keyframe() : quat(), scale(0ull)
29+
{
30+
translation[2] = translation[1] = translation[0] = 0.f;
31+
}
32+
Keyframe(const core::vectorSIMDf& _scale, const core::quaternion& _quat, const CQuantQuaternionCache* quantCache, const core::vectorSIMDf& _translation)
33+
{
34+
std::copy(translation,_translation.pointer,3u);
35+
quat = quantCache->quantize(_quat);
36+
// TODO: encode to RGB18E7S3
37+
//scale = ;
38+
}
39+
40+
/*
41+
inline core::quaternion getRotation() const
42+
{
43+
return quat.getValue(); // TODO: decode from RGBA8_SNORM and normalize
44+
}
45+
*/
46+
47+
inline core::vectorSIMDf getScale() const
48+
{
49+
return core::vectorSIMDf(0.f/0.f); // TODO: decode from RGB18E7S3
50+
}
51+
52+
private:
53+
float translation[3];
54+
CDirQuantCacheBase::Vector8u4 quat;
55+
uint64_t scale;
56+
};
57+
struct alignas(8) Animation
58+
{
59+
enum E_INTERPOLATION_MODE : uint32_t
60+
{
61+
EIM_NEAREST=0u,
62+
EIM_LINEAR=1u<<30u,
63+
EIM_CUBIC=2u<<30u,
64+
EIM_MASK=3u<<30u
65+
};
66+
inline uint32_t getKeyframeOffset() const
67+
{
68+
return data[0];
69+
}
70+
inline auto getTimestampOffset() const
71+
{
72+
return getKeyframeOffset();
73+
}
74+
inline uint32_t getKeyframeCount() const
75+
{
76+
return data[1]&(~EIM_MASK);
77+
}
78+
inline E_INTERPOLATION_MODE getInterpolationMode() const
79+
{
80+
return data[1]&EIM_MASK;
81+
}
82+
private:
83+
uint32_t data[2];
84+
};
85+
86+
inline const auto& getNameToAnimationMap() const
87+
{
88+
return m_nameToAnimation;
89+
}
90+
inline uint32_t getAnimationOffsetFromName(const char* animationName) const
91+
{
92+
auto found = m_nameToAnimation.find(animationName);
93+
if (found != m_nameToAnimation.end())
94+
return found->second;
95+
return getAnimationCapacity();
96+
}
97+
98+
inline uint32_t getAnimationCapacity() const
99+
{
100+
return m_animationStorageRange.size()/sizeof(Animation);
101+
}
102+
103+
inline const SBufferRange<const BufferType>& getKeyframeStorageRange() const
104+
{
105+
return reinterpret_cast<const SBufferRange<const BufferType>*>(m_keyframeStorageRange);
106+
}
107+
inline const SBufferRange<const BufferType>& getAnimationStorageRange() const
108+
{
109+
return reinterpret_cast<const SBufferRange<const BufferType>*>(m_animationStorageRange);
110+
}
111+
112+
113+
protected:
114+
IAnimationLibrary(SBufferRange<BufferType>&& _keyframeStorageRange, SBufferRange<BufferType>&& _animationStorageRange) :
115+
m_stringPool(), m_nameToAnimation(StringComparator(&m_stringPool)), m_keyframeStorageRange(std::move(_keyframeStorageRange)),
116+
m_animationStorageRange(std::move(_animationStorageRange))
117+
{
118+
assert(m_keyframeStorageRange.isValid() && (m_keyframeStorageRange.offset%sizeof(Keyframe)==0u) && m_keyframeStorageRange.size>=sizeof(Keyframe));
119+
assert(m_animationStorageRange.isValid() && (m_animationStorageRange.offset%sizeof(Animation)==0u) && m_animationStorageRange.size>=sizeof(Animation));
120+
}
121+
virtual ~IAnimationLibrary()
122+
{
123+
m_nameToAnimation.clear();
124+
}
125+
126+
template <typename>
127+
friend struct IAnimationLibrary;
128+
129+
// map must contain one `const char*` per bone
130+
template<class OtherBufferType>
131+
inline void setAnimationNames(const IAnimationLibrary<OtherBufferType>* other)
132+
{
133+
m_stringPool = other->m_stringPool;
134+
m_nameToAnimation.clear();
135+
m_nameToAnimation.insert(other->m_nameToAnimation.begin(),other->m_nameToAnimation.end());
136+
}
137+
//
138+
template<typename NameIterator, typename OffsetIterator>
139+
inline void addAnimationNames(NameIterator nameBegin, NameIterator nameEnd, OffsetIterator offsetBegin)
140+
{
141+
// size the pool
142+
size_t extraChars = 0ull;
143+
for (auto it=nameBegin; it!=nameEnd; it++)
144+
{
145+
const auto nameLen = strlen(*it);
146+
if (nameLen)
147+
extraChars += nameLen+1ull;
148+
}
149+
size_t stringPoolEnd = m_stringPool.size();
150+
m_stringPool.resize(stringPoolEnd+extraChars);
151+
152+
// useless names
153+
if (extraChars==0ull)
154+
return;
155+
156+
auto offsetIt = offsetBegin;
157+
for (auto it=nameBegin; it!=nameEnd; it++,offsetIt++)
158+
{
159+
const char* inName = *it;
160+
const size_t newNameBegin = stringPoolEnd;
161+
while (*inName) { m_stringPool[stringPoolEnd++] = *(inName++); }
162+
if (stringPoolEnd!=newNameBegin)
163+
{
164+
m_stringPool[stringPoolEnd++] = 0;
165+
m_nameToAnimation.emplace(newNameBegin,*offsetIt);
166+
}
167+
}
168+
}
169+
//
170+
inline clearAnimationNames()
171+
{
172+
m_nameToAnimation.clear();
173+
m_stringPool.clear();
174+
}
175+
176+
struct StringComparator
177+
{
178+
StringComparator(const core::vector<char>* const _stringPool) : stringPool(_stringPool) {}
179+
180+
inline bool operator()(const uint32_t lhs, const uint32_t rhs) const
181+
{
182+
return strcmp(stringPool.data()+lhs,stringPool.data()+rhs)<0;
183+
}
184+
185+
private:
186+
core::vector<char>* const stringPool;
187+
};
188+
core::vector<char> m_stringPool;
189+
core::map<uint32_t,uint32_t,StringComparator> m_nameToAnimation;
190+
191+
SBufferRange<BufferType> m_keyframeStorageRange,m_animationStorageRange;
192+
};
193+
194+
} // end namespace asset
195+
} // end namespace nbl
196+
197+
#endif
198+

include/nbl/asset/IAsset.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@ class IAsset : virtual public core::IReferenceCounted
6464
6565
@see IAsset
6666
67-
ET_BUFFER represents asset::ICPUBuffer
68-
ET_SUB_IMAGE represents asset::CImageData
67+
ET_BUFFER represents ICPUBuffer
6968
ET_IMAGE represents ICPUTexture
7069
ET_SUB_MESH represents
7170
ET_MESH represents
7271
ET_SKELETON represents
73-
ET_KEYFRAME_ANIMATION represents
72+
ET_ANIMATION_LIBRARY represents
7473
ET_SHADER represents
7574
ET_SPECIALIZED_SHADER represents
7675
ET_MESH_DATA_DESCRIPTOR represents
@@ -91,7 +90,7 @@ class IAsset : virtual public core::IReferenceCounted
9190
ET_DESCRIPTOR_SET = 1ull<<5, //!< asset::ICPUDescriptorSet
9291
ET_DESCRIPTOR_SET_LAYOUT = 1ull<<6, //!< asset::ICPUDescriptorSetLayout
9392
ET_SKELETON = 1ull<<7, //!< asset::ICPUSkeleton
94-
ET_KEYFRAME_ANIMATION = 1ull<<8, //!< asset::ICPUKeyframeAnimation
93+
ET_ANIMATION_LIBRARY = 1ull<<8, //!< asset::ICPUAnimationLibrary
9594
ET_PIPELINE_LAYOUT = 1ull<<9, //!< asset::ICPUPipelineLayout
9695
ET_SHADER = 1ull<<10, //!< asset::ICPUShader
9796
ET_SPECIALIZED_SHADER = 1ull<<11, //!< asset::ICPUSpecializedShader
@@ -100,7 +99,7 @@ class IAsset : virtual public core::IReferenceCounted
10099
ET_MESH = 1ull<<14, //!< asset::ICPUMesh
101100
ET_COMPUTE_PIPELINE = 1ull<<15, //!< asset::ICPUComputePipeline
102101
ET_PIPELINE_CACHE = 1ull<<16, //!< asset::ICPUPipelineCache
103-
ET_SCENE = 1ull<<17, //!< asset::ICPUScene
102+
ET_SCENE = 1ull<<17, //!< asset::ICPUScene (TODO)
104103
//! Reserved special value used for things like terminating lists of this enum
105104

106105
ET_TERMINATING_ZERO = 0

include/nbl/builtin/glsl/scene/keyframe.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct nbl_glsl_scene_Keyframe_t
1515

1616
vec3 nbl_glsl_scene_Keyframe_t_getScale(in nbl_glsl_scene_Keyframe_t keyframe)
1717
{
18-
return nbl_glsl_decodeRGB18E7S3(keyframe.data[3]);
18+
return nbl_glsl_decodeRGB18E7S3(keyframe.data[2]);
1919
}
2020

2121
nbl_glsl_quaternion_t nbl_glsl_scene_Keyframe_t_getRotation(in nbl_glsl_scene_Keyframe_t keyframe)

0 commit comments

Comments
 (0)