BsAnimationManager.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /** Maps animation ID to a pose information structure, containing its global joint transforms. */
  24. UnorderedMap<UINT64, PoseInfo> poseInfos;
  25. /** Global joint transforms for all skeletons in the scene. */
  26. Vector<Matrix4> transforms;
  27. };
  28. /**
  29. * Keeps track of all active animations, queues animation thread tasks and synchronizes data between simulation, core
  30. * and animation threads.
  31. */
  32. class BS_CORE_EXPORT AnimationManager : public Module<AnimationManager>
  33. {
  34. public:
  35. AnimationManager();
  36. /** Pauses or resumes the animation evaluation. */
  37. void setPaused(bool paused);
  38. /**
  39. * Determines how often to evaluate animations. If rendering is not running at adequate framerate the animation
  40. * could end up being evaluated less times than specified here.
  41. *
  42. * @param[in] fps Number of frames per second to evaluate the animation. Default is 60.
  43. */
  44. void setUpdateRate(UINT32 fps);
  45. /**
  46. * Synchronizes animation data from the animation thread with the scene objects. Should be called before component
  47. * updates are sent.
  48. */
  49. void preUpdate();
  50. /**
  51. * Synchronizes animation data to the animation thread, advances animation time and queues new animation evaluation
  52. * task.
  53. */
  54. void postUpdate();
  55. /**
  56. * Gets skeleton poses required by the renderer to display all the animations. This will block the animation thread
  57. * if it has not yet finished, and it will also advance the read buffer index, meaning this shouldn't be called more
  58. * than once per frame. The returned data can be referenced, and is guaranteed to be valid for a single core-thread
  59. * frame.
  60. *
  61. * @note Core thread only.
  62. */
  63. const RendererAnimationData& getRendererData();
  64. private:
  65. friend class Animation;
  66. /**
  67. * Registers a new animation and returns a unique ID for it. Must be called whenever an Animation is constructed.
  68. */
  69. UINT64 registerAnimation(Animation* anim);
  70. /** Unregisters an animation with the specified ID. Must be called before an Animation is destroyed. */
  71. void unregisterAnimation(UINT64 id);
  72. /** Worker method ran on the animation thread that evaluates all animation at the provided time. */
  73. void evaluateAnimation();
  74. UINT64 mNextId;
  75. UnorderedMap<UINT64, Animation*> mAnimations;
  76. float mUpdateRate;
  77. float mAnimationTime;
  78. float mLastAnimationUpdateTime;
  79. float mNextAnimationUpdateTime;
  80. bool mPaused;
  81. bool mWorkerRunning;
  82. SPtr<Task> mAnimationWorker;
  83. // Animation thread
  84. Vector<SPtr<AnimationProxy>> mProxies;
  85. RendererAnimationData mAnimData[CoreThread::NUM_SYNC_BUFFERS];
  86. UINT32 mPoseReadBufferIdx;
  87. UINT32 mPoseWriteBufferIdx;
  88. std::atomic<INT32> mDataReadyCount;
  89. };
  90. /** Provides easier access to AnimationManager. */
  91. BS_CORE_EXPORT AnimationManager& gAnimation();
  92. /** @} */
  93. }