BsAnimationManager.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 bs
  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 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. /** Possible states the worker thread can be in, used for synchronization. */
  89. enum class WorkerState
  90. {
  91. Inactive,
  92. Started,
  93. DataReady
  94. };
  95. /**
  96. * Registers a new animation and returns a unique ID for it. Must be called whenever an Animation is constructed.
  97. */
  98. UINT64 registerAnimation(Animation* anim);
  99. /** Unregisters an animation with the specified ID. Must be called before an Animation is destroyed. */
  100. void unregisterAnimation(UINT64 id);
  101. /** Worker method ran on the animation thread that evaluates all animation at the provided time. */
  102. void evaluateAnimation();
  103. UINT64 mNextId;
  104. UnorderedMap<UINT64, Animation*> mAnimations;
  105. float mUpdateRate;
  106. float mAnimationTime;
  107. float mLastAnimationUpdateTime;
  108. float mNextAnimationUpdateTime;
  109. bool mPaused;
  110. bool mWorkerStarted;
  111. SPtr<Task> mAnimationWorker;
  112. SPtr<VertexDataDesc> mBlendShapeVertexDesc;
  113. // Animation thread
  114. Vector<SPtr<AnimationProxy>> mProxies;
  115. Vector<ConvexVolume> mCullFrustums;
  116. RendererAnimationData mAnimData[CoreThread::NUM_SYNC_BUFFERS];
  117. UINT32 mPoseReadBufferIdx;
  118. UINT32 mPoseWriteBufferIdx;
  119. std::atomic<INT32> mDataReadyCount; // Anim <-> Core thread sync
  120. std::atomic<WorkerState> mWorkerState; // Anim <-> Sim thread sync
  121. bool mDataReady;
  122. };
  123. /** Provides easier access to AnimationManager. */
  124. BS_CORE_EXPORT AnimationManager& gAnimation();
  125. /** @} */
  126. }