|
| 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 | + |
0 commit comments