|
| 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_CPU_ANIMATION_LIBRARY_H_INCLUDED__ |
| 6 | +#define __NBL_ASSET_I_CPU_ANIMATION_LIBRARY_H_INCLUDED__ |
| 7 | + |
| 8 | +#include "nbl/asset/IAnimationLibrary.h" |
| 9 | +#include "nbl/asset/ICPUBuffer.h" |
| 10 | + |
| 11 | +namespace nbl |
| 12 | +{ |
| 13 | +namespace asset |
| 14 | +{ |
| 15 | + |
| 16 | +class ICPUAnimationLibrary final : public IAnimationLibrary<ICPUBuffer>, /*TODO: public BlobSerializable, */public IAsset |
| 17 | +{ |
| 18 | + public: |
| 19 | + using base_t = IAnimationLibrary<ICPUBuffer>; |
| 20 | + |
| 21 | + template<typename... Args> |
| 22 | + inline ICPUAnimationLibrary(Args&&... args) : base_t(std::forward<Args>(args)...) {} |
| 23 | + |
| 24 | + // |
| 25 | + inline const SBufferBinding<ICPUBuffer>& getKeyframeStorageBinding() const |
| 26 | + { |
| 27 | + return m_keyframeStorageBinding; |
| 28 | + } |
| 29 | + inline const SBufferBinding<ICPUBuffer>& getTimestampStorageBinding() const |
| 30 | + { |
| 31 | + return m_timestampStorageBinding; |
| 32 | + } |
| 33 | + |
| 34 | + // |
| 35 | + inline const SBufferRange<ICPUBuffer>& getAnimationStorageRange() const |
| 36 | + { |
| 37 | + return m_animationStorageRange; |
| 38 | + } |
| 39 | + |
| 40 | + //! |
| 41 | + inline const Keyframe& getKeyframe(uint32_t keyframeOffset) const |
| 42 | + { |
| 43 | + const uint8_t* ptr = reinterpret_cast<const uint8_t*>(m_keyframeStorageBinding.buffer->getPointer()); |
| 44 | + return reinterpret_cast<const Keyframe*>(ptr+m_keyframeStorageBinding.offset)[keyframeOffset]; |
| 45 | + } |
| 46 | + inline Keyframe& getKeyframe(uint32_t keyframeOffset) |
| 47 | + { |
| 48 | + assert(!isImmutable_debug()); |
| 49 | + return const_cast<Keyframe&>(const_cast<const ICPUAnimationLibrary*>(this)->getKeyframe(keyframeOffset)); |
| 50 | + } |
| 51 | + //! |
| 52 | + inline const timestamp_t& getTimestamp(uint32_t keyframeOffset) const |
| 53 | + { |
| 54 | + const uint8_t* ptr = reinterpret_cast<const uint8_t*>(m_timestampStorageBinding.buffer->getPointer()); |
| 55 | + return reinterpret_cast<const timestamp_t*>(ptr+m_timestampStorageBinding.offset)[keyframeOffset]; |
| 56 | + } |
| 57 | + inline timestamp_t& getTimestamp(uint32_t keyframeOffset) |
| 58 | + { |
| 59 | + assert(!isImmutable_debug()); |
| 60 | + return const_cast<timestamp_t&>(const_cast<const ICPUAnimationLibrary*>(this)->getTimestamp(keyframeOffset)); |
| 61 | + } |
| 62 | + |
| 63 | + //! |
| 64 | + inline const Animation& getAnimation(uint32_t animationOffset) const |
| 65 | + { |
| 66 | + const uint8_t* ptr = reinterpret_cast<const uint8_t*>(m_animationStorageRange.buffer->getPointer()); |
| 67 | + return reinterpret_cast<const Animation*>(ptr+m_animationStorageRange.offset)[animationOffset]; |
| 68 | + } |
| 69 | + inline Animation& getAnimation(uint32_t animationOffset) |
| 70 | + { |
| 71 | + assert(!isImmutable_debug()); |
| 72 | + return const_cast<Animation&>(const_cast<const ICPUAnimationLibrary*>(this)->getAnimation(animationOffset)); |
| 73 | + } |
| 74 | + |
| 75 | + //! |
| 76 | + template<typename NameIterator, typename OffsetIterator> |
| 77 | + inline void addAnimationNames(NameIterator nameBegin, NameIterator nameEnd, OffsetIterator offsetBegin) |
| 78 | + { |
| 79 | + base_t::addAnimationNames(nameBegin,nameEnd,offsetBegin); |
| 80 | + } |
| 81 | + inline void clearAnimationNames() |
| 82 | + { |
| 83 | + base_t::clearAnimationNames(); |
| 84 | + } |
| 85 | + |
| 86 | + //! Serializes animation library to blob for *.nbl file format. |
| 87 | + /** @param _stackPtr Optional pointer to stack memory to write blob on. If _stackPtr==NULL, sufficient amount of memory will be allocated. |
| 88 | + @param _stackSize Size of stack memory pointed by _stackPtr. |
| 89 | + @returns Pointer to memory on which blob was written. |
| 90 | + * TODO |
| 91 | + virtual void* serializeToBlob(void* _stackPtr = NULL, const size_t& _stackSize = 0) const override |
| 92 | + { |
| 93 | + return CorrespondingBlobTypeFor<ICPUAnimationLibrary>::type::createAndTryOnStack(this, _stackPtr, _stackSize); |
| 94 | + } |
| 95 | + */ |
| 96 | + |
| 97 | + core::smart_refctd_ptr<IAsset> clone(uint32_t _depth = ~0u) const override |
| 98 | + { |
| 99 | + SBufferBinding<ICPUBuffer> _keyframeStorageBinding = {m_keyframeStorageBinding.offset,_depth>0u ? core::smart_refctd_ptr_static_cast<ICPUBuffer>(m_keyframeStorageBinding.buffer->clone(_depth-1u)):m_keyframeStorageBinding.buffer}; |
| 100 | + SBufferBinding<ICPUBuffer> _timestampStorageBinding = {m_timestampStorageBinding.offset,_depth>0u ? core::smart_refctd_ptr_static_cast<ICPUBuffer>(m_timestampStorageBinding.buffer->clone(_depth-1u)):m_timestampStorageBinding.buffer}; |
| 101 | + |
| 102 | + SBufferRange<ICPUBuffer> _animationStorageRange = {m_animationStorageRange.offset,m_animationStorageRange.size,_depth>0u&&m_animationStorageRange.buffer ? core::smart_refctd_ptr_static_cast<ICPUBuffer>(m_animationStorageRange.buffer->clone(_depth-1u)):m_animationStorageRange.buffer}; |
| 103 | + |
| 104 | + auto cp = core::make_smart_refctd_ptr<ICPUAnimationLibrary>(std::move(_keyframeStorageBinding),std::move(_timestampStorageBinding),m_keyframeCount,std::move(_animationStorageRange)); |
| 105 | + clone_common(cp.get()); |
| 106 | + cp->setAnimationNames(this); |
| 107 | + |
| 108 | + return cp; |
| 109 | + } |
| 110 | + |
| 111 | + virtual void convertToDummyObject(uint32_t referenceLevelsBelowToConvert=0u) override |
| 112 | + { |
| 113 | + convertToDummyObject_common(referenceLevelsBelowToConvert); |
| 114 | + |
| 115 | + if (referenceLevelsBelowToConvert) |
| 116 | + { |
| 117 | + m_keyframeStorageBinding.buffer->convertToDummyObject(referenceLevelsBelowToConvert-1u); |
| 118 | + m_timestampStorageBinding.buffer->convertToDummyObject(referenceLevelsBelowToConvert-1u); |
| 119 | + m_animationStorageRange.buffer->convertToDummyObject(referenceLevelsBelowToConvert-1u); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + _NBL_STATIC_INLINE_CONSTEXPR auto AssetType = ET_ANIMATION_LIBRARY; |
| 124 | + inline E_TYPE getAssetType() const override { return AssetType; } |
| 125 | + |
| 126 | + virtual size_t conservativeSizeEstimate() const override |
| 127 | + { |
| 128 | + size_t estimate = sizeof(SBufferBinding<ICPUBuffer>)*2ull; |
| 129 | + estimate += sizeof(SBufferRange<ICPUBuffer>); |
| 130 | + estimate += sizeof(uint32_t); |
| 131 | + estimate += m_stringPoolSize; |
| 132 | + estimate += m_nameToAnimation.size()*sizeof(std::pair<uint32_t,uint32_t>); |
| 133 | + // do we add other things to the size estimate? |
| 134 | + return estimate; |
| 135 | + } |
| 136 | + |
| 137 | + bool canBeRestoredFrom(const IAsset* _other) const override |
| 138 | + { |
| 139 | + auto other = static_cast<const ICPUAnimationLibrary*>(_other); |
| 140 | + if (m_keyframeCount != other->m_keyframeCount) |
| 141 | + return false; |
| 142 | + |
| 143 | + if (m_keyframeStorageBinding.offset != other->m_keyframeStorageBinding.offset) |
| 144 | + return false; |
| 145 | + if (m_keyframeStorageBinding.buffer->canBeRestoredFrom(other->m_keyframeStorageBinding.buffer.get())) |
| 146 | + return false; |
| 147 | + if (m_timestampStorageBinding.offset != other->m_timestampStorageBinding.offset) |
| 148 | + return false; |
| 149 | + if (m_timestampStorageBinding.buffer->canBeRestoredFrom(other->m_timestampStorageBinding.buffer.get())) |
| 150 | + return false; |
| 151 | + |
| 152 | + if (m_animationStorageRange.offset != other->m_animationStorageRange.offset) |
| 153 | + return false; |
| 154 | + if (m_animationStorageRange.size != other->m_animationStorageRange.size) |
| 155 | + return false; |
| 156 | + if ((!m_animationStorageRange.buffer) != (!other->m_animationStorageRange.buffer)) |
| 157 | + return false; |
| 158 | + if (m_animationStorageRange.buffer && !m_animationStorageRange.buffer->canBeRestoredFrom(other->m_animationStorageRange.buffer.get())) |
| 159 | + return false; |
| 160 | + |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + protected: |
| 165 | + void restoreFromDummy_impl(IAsset* _other, uint32_t _levelsBelow) override |
| 166 | + { |
| 167 | + auto* other = static_cast<ICPUSkeleton*>(_other); |
| 168 | + |
| 169 | + if (_levelsBelow) |
| 170 | + { |
| 171 | + --_levelsBelow; |
| 172 | + |
| 173 | + if (m_keyframeStorageBinding.buffer) |
| 174 | + restoreFromDummy_impl_call(m_keyframeStorageBinding.buffer.get(),other->m_keyframeStorageBinding.buffer.get(),_levelsBelow); |
| 175 | + if (m_timestampStorageBinding.buffer) |
| 176 | + restoreFromDummy_impl_call(m_timestampStorageBinding.buffer.get(),other->m_timestampStorageBinding.buffer.get(),_levelsBelow); |
| 177 | + |
| 178 | + if (m_animationStorageRange.buffer) |
| 179 | + restoreFromDummy_impl_call(m_animationStorageRange.buffer.get(),other->m_animationStorageRange.buffer.get(),_levelsBelow); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + bool isAnyDependencyDummy_impl(uint32_t _levelsBelow) const override |
| 184 | + { |
| 185 | + --_levelsBelow; |
| 186 | + if (m_keyframeStorageBinding.buffer && m_keyframeStorageBinding.buffer->isAnyDependencyDummy(_levelsBelow)) |
| 187 | + return true; |
| 188 | + if (m_timestampStorageBinding.buffer && m_timestampStorageBinding.buffer->isAnyDependencyDummy(_levelsBelow)) |
| 189 | + return true; |
| 190 | + |
| 191 | + return m_animationStorageRange.buffer && m_animationStorageRange.buffer->isAnyDependencyDummy(_levelsBelow); |
| 192 | + } |
| 193 | +}; |
| 194 | + |
| 195 | +} |
| 196 | +} |
| 197 | + |
| 198 | +#endif |
0 commit comments