BsAnimationManager.h 4.2 KB

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