BsAnimationManager.h 3.5 KB

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