BsAnimationManager.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsModule.h"
  6. #include "BsCoreThread.h"
  7. namespace BansheeEngine
  8. {
  9. struct AnimationProxy;
  10. /** @addtogroup Animation-Internal
  11. * @{
  12. */
  13. /** Contains skeleton poses for all animations evaluated on a single frame. */
  14. struct RendererAnimationData
  15. {
  16. /** Contains data about a calculated skeleton pose. */
  17. struct PoseInfo
  18. {
  19. UINT64 animId;
  20. UINT32 startIdx;
  21. UINT32 numBones;
  22. };
  23. UnorderedMap<UINT64, PoseInfo> poseInfos;
  24. Vector<Matrix4> transforms;
  25. };
  26. /**
  27. * Keeps track of all active animations, queues animation thread tasks and synchronizes data between simulation, core
  28. * and animation threads.
  29. */
  30. class AnimationManager : public Module<AnimationManager>
  31. {
  32. public:
  33. AnimationManager();
  34. /** Pauses or resumes the animation evaluation. */
  35. void setPaused(bool paused);
  36. /**
  37. * Determines how often to evaluate animations. If rendering is not running at adequate framerate the animation
  38. * could end up being evaluated less times than specified here.
  39. *
  40. * @param[in] fps Number of frames per second to evaluate the animation. Default is 60.
  41. */
  42. void setUpdateRate(UINT32 fps);
  43. /**
  44. * Synchronizes animation data from the animation thread with the scene objects. Should be called before component
  45. * updates are sent.
  46. */
  47. void preUpdate();
  48. /**
  49. * Synchronizes animation data to the animation thread, advances animation time and queues new animation evaluation
  50. * task.
  51. */
  52. void postUpdate();
  53. /**
  54. * Gets skeleton poses required by the renderer to display all the animations. This will block the animation thread
  55. * if it has not yet finished, and it will also advance the read buffer index, meaning this shouldn't be called more
  56. * than once per frame.
  57. *
  58. * @note Core thread only.
  59. */
  60. const RendererAnimationData& getRendererData();
  61. private:
  62. friend class Animation;
  63. /**
  64. * Registers a new animation and returns a unique ID for it. Must be called whenever an Animation is constructed.
  65. */
  66. UINT64 registerAnimation(Animation* anim);
  67. /** Unregisters an animation with the specified ID. Must be called before an Animation is destroyed. */
  68. void unregisterAnimation(UINT64 id);
  69. /** Worker method ran on the animation thread that evaluates all animation at the provided time. */
  70. void evaluateAnimation();
  71. UINT64 mNextId;
  72. UnorderedMap<UINT64, Animation*> mAnimations;
  73. float mUpdateRate;
  74. float mAnimationTime;
  75. float mLastAnimationUpdateTime;
  76. float mNextAnimationUpdateTime;
  77. bool mPaused;
  78. bool mWorkerRunning;
  79. SPtr<Task> mAnimationWorker;
  80. // Animation thread
  81. Vector<SPtr<AnimationProxy>> mProxies;
  82. RendererAnimationData mAnimData[CoreThread::NUM_SYNC_BUFFERS];
  83. UINT32 mPoseReadBufferIdx;
  84. UINT32 mPoseWriteBufferIdx;
  85. std::atomic<INT32> mDataReadyCount;
  86. };
  87. /** @} */
  88. }