BsAnimationManager.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsAnimationManager.h"
  4. #include "BsAnimation.h"
  5. #include "BsAnimationClip.h"
  6. #include "BsTaskScheduler.h"
  7. #include "BsTime.h"
  8. namespace BansheeEngine
  9. {
  10. AnimationManager::AnimationManager()
  11. : mNextId(1), mUpdateRate(1.0f / 60.0f), mAnimationTime(0.0f), mLastAnimationUpdateTime(0.0f)
  12. , mNextAnimationUpdateTime(0.0f), mPaused(false), mWorkerRunning(false), mPoseReadBufferIdx(0)
  13. , mPoseWriteBufferIdx(0), mDataReadyCount(0)
  14. {
  15. mAnimationWorker = Task::create("Animation", std::bind(&AnimationManager::evaluateAnimation, this));
  16. }
  17. void AnimationManager::setPaused(bool paused)
  18. {
  19. mPaused = paused;
  20. }
  21. void AnimationManager::setUpdateRate(UINT32 fps)
  22. {
  23. if (fps == 0)
  24. fps = 1;
  25. mUpdateRate = 1.0f / fps;
  26. }
  27. void AnimationManager::preUpdate()
  28. {
  29. if (mPaused || !mWorkerRunning)
  30. return;
  31. mAnimationWorker->wait();
  32. // Make sure we don't load obsolete skeletal pose and other evaluation ouputs written by the animation thread
  33. std::atomic_thread_fence(std::memory_order_acquire);
  34. // Trigger events
  35. for (auto& anim : mAnimations)
  36. anim.second->triggerEvents(mAnimationTime, gTime().getFrameDelta());
  37. // TODO - Write TRS animation results to relevant SceneObjects
  38. // TODO - Transfer generic curve evaluated data back to Animation
  39. mWorkerRunning = false;
  40. }
  41. void AnimationManager::postUpdate()
  42. {
  43. if (mPaused)
  44. return;
  45. mAnimationTime += gTime().getFrameDelta();
  46. if (mAnimationTime < mNextAnimationUpdateTime)
  47. return;
  48. mNextAnimationUpdateTime = Math::floor(mAnimationTime / mUpdateRate) * mUpdateRate + mUpdateRate;
  49. float timeDelta = mAnimationTime - mLastAnimationUpdateTime;
  50. mLastAnimationUpdateTime = mAnimationTime;
  51. // Update poses in the currently active buffer. Multi-buffering allows the core thread to safely read the
  52. // poses without worrying about them being overwritten by another call to postUpdate, as long as the simulation
  53. // thread doesn't go more than (CoreThread::NUM_SYNC_BUFFERS - 1) frames ahead.
  54. mProxies.clear();
  55. for (auto& anim : mAnimations)
  56. {
  57. anim.second->updateAnimProxy(timeDelta);
  58. mProxies.push_back(anim.second->mAnimProxy);
  59. }
  60. // Make sure thread finishes writing all changes to the anim proxies as they will be read by the animation thread
  61. std::atomic_thread_fence(std::memory_order_release);
  62. // Note: Animation thread will trigger about the same time as the core thread. The core thread will need to wait
  63. // until animation thread finishes, which might end up blocking it (and losing the multi-threading performance).
  64. // Consider delaying displayed animation for a single frame or pre-calculating animations (by advancing time the
  65. // previous frame) for non-dirty animations.
  66. TaskScheduler::instance().addTask(mAnimationWorker);
  67. mWorkerRunning = true;
  68. }
  69. void AnimationManager::evaluateAnimation()
  70. {
  71. // Make sure we don't load obsolete anim proxy data written by the simulation thread
  72. std::atomic_thread_fence(std::memory_order_acquire);
  73. // No need for locking, as we are sure that only postUpdate() writes to the proxy buffer, and increments the write
  74. // buffer index. And it's called sequentially ensuring previous call to evaluate finishes.
  75. UINT32 totalNumBones = 0;
  76. for (auto& anim : mProxies)
  77. {
  78. if (anim->skeleton != nullptr)
  79. totalNumBones += anim->skeleton->getNumBones();
  80. }
  81. RendererAnimationData& renderData = mAnimData[mPoseWriteBufferIdx];
  82. mPoseWriteBufferIdx = (mPoseWriteBufferIdx + 1) % CoreThread::NUM_SYNC_BUFFERS;
  83. renderData.poseInfos.clear();
  84. renderData.transforms.resize(totalNumBones);
  85. UINT32 curBoneIdx = 0;
  86. for(auto& anim : mProxies)
  87. {
  88. if (anim->skeleton != nullptr)
  89. {
  90. UINT32 numBones = anim->skeleton->getNumBones();
  91. RendererAnimationData::PoseInfo info;
  92. info.animId = anim->id;
  93. info.startIdx = curBoneIdx;
  94. info.numBones = numBones;
  95. Matrix4* boneDst = renderData.transforms.data() + curBoneIdx;
  96. anim->skeleton->getPose(boneDst, anim->localPose, anim->layers, anim->numLayers);
  97. renderData.poseInfos[anim->id] = info;
  98. curBoneIdx += numBones;
  99. }
  100. else
  101. {
  102. // Note: No blending for non-skeletal animations, just use first animation
  103. if(anim->numLayers > 0 && anim->layers[0].numStates > 0)
  104. {
  105. const AnimationState& state = anim->layers[0].states[0];
  106. {
  107. UINT32 numCurves = (UINT32)state.curves->position.size();
  108. for(UINT32 i = 0; i < numCurves; i++)
  109. {
  110. const TAnimationCurve<Vector3>& curve = state.curves->position[i].curve;
  111. anim->localPose.positions[i] = curve.evaluate(state.time, state.positionCaches[i], state.loop);
  112. }
  113. }
  114. {
  115. UINT32 numCurves = (UINT32)state.curves->rotation.size();
  116. for (UINT32 i = 0; i < numCurves; i++)
  117. {
  118. const TAnimationCurve<Quaternion>& curve = state.curves->rotation[i].curve;
  119. anim->localPose.rotations[i] = curve.evaluate(state.time, state.rotationCaches[i], state.loop);
  120. }
  121. }
  122. {
  123. UINT32 numCurves = (UINT32)state.curves->scale.size();
  124. for (UINT32 i = 0; i < numCurves; i++)
  125. {
  126. const TAnimationCurve<Vector3>& curve = state.curves->scale[i].curve;
  127. anim->localPose.scales[i] = curve.evaluate(state.time, state.scaleCaches[i], state.loop);
  128. }
  129. }
  130. }
  131. }
  132. // Note: No blending for generic animations, just use first animation
  133. if (anim->numLayers > 0 && anim->layers[0].numStates > 0)
  134. {
  135. const AnimationState& state = anim->layers[0].states[0];
  136. {
  137. UINT32 numCurves = (UINT32)state.curves->generic.size();
  138. for (UINT32 i = 0; i < numCurves; i++)
  139. {
  140. const TAnimationCurve<float>& curve = state.curves->generic[i].curve;
  141. anim->genericCurveOutputs[i] = curve.evaluate(state.time, state.genericCaches[i], state.loop);
  142. }
  143. }
  144. }
  145. }
  146. mDataReadyCount.fetch_add(1, std::memory_order_relaxed);
  147. // Make sure the thread finishes writing skeletal pose and other evaluation outputs as they will be read by sim and
  148. // core threads
  149. std::atomic_thread_fence(std::memory_order_release);
  150. }
  151. const RendererAnimationData& AnimationManager::getRendererData()
  152. {
  153. mAnimationWorker->wait();
  154. // Make sure we don't load obsolete skeletal pose and other evaluation ouputs written by the animation thread
  155. std::atomic_thread_fence(std::memory_order_acquire);
  156. INT32 dataReadyCount = mDataReadyCount.load(std::memory_order_relaxed);
  157. assert(dataReadyCount <= CoreThread::NUM_SYNC_BUFFERS);
  158. if (dataReadyCount <= 0)
  159. {
  160. static RendererAnimationData dummy;
  161. return dummy;
  162. }
  163. const RendererAnimationData& output = mAnimData[mPoseReadBufferIdx];
  164. mPoseReadBufferIdx = (mPoseReadBufferIdx + 1) % CoreThread::NUM_SYNC_BUFFERS;
  165. mDataReadyCount.fetch_add(-1, std::memory_order_relaxed);
  166. return output;
  167. }
  168. UINT64 AnimationManager::registerAnimation(Animation* anim)
  169. {
  170. mAnimations[mNextId] = anim;
  171. return mNextId++;
  172. }
  173. void AnimationManager::unregisterAnimation(UINT64 animId)
  174. {
  175. mAnimations.erase(animId);
  176. }
  177. AnimationManager& gAnimation()
  178. {
  179. return AnimationManager::instance();
  180. }
  181. }