BsAnimationManager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "Utility/BsModule.h"
  6. #include "CoreThread/BsCoreThread.h"
  7. #include "Math/BsConvexVolume.h"
  8. #include "RenderAPI/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 EvaluatedAnimationData
  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. * Evaluates animations for all animated objects, and returns the evaluated skeleton bone poses and morph shape
  63. * meshes that can be passed along to the renderer.
  64. *
  65. * @param[in] async If true the method returns immediately while the animation gets evaluated in the
  66. * background. The returned evaluated data will be the data from the previous frame.
  67. * Therefore note that this introduces a one frame latency on the animation. If the
  68. * latency is not acceptable set this to false, at a potential performance impact.
  69. * @return Evaluated animation data for this frame (if @p async is false), or the previous
  70. * frame (if @p async is true). Note that the system re-uses the returned buffers,
  71. * and the returned buffer should stop being used after every second call to update().
  72. * This is enough to have one buffer be processed by the core thread, one queued
  73. * for future rendering and one that's being written to.
  74. */
  75. const EvaluatedAnimationData* update(bool async = true);
  76. private:
  77. friend class Animation;
  78. /** Possible states the worker thread can be in, used for synchronization. */
  79. enum class WorkerState
  80. {
  81. Inactive,
  82. Started,
  83. DataReady
  84. };
  85. /**
  86. * Registers a new animation and returns a unique ID for it. Must be called whenever an Animation is constructed.
  87. */
  88. UINT64 registerAnimation(Animation* anim);
  89. /** Unregisters an animation with the specified ID. Must be called before an Animation is destroyed. */
  90. void unregisterAnimation(UINT64 id);
  91. /** Worker method ran on the animation thread that evaluates all animation at the provided time. */
  92. void evaluateAnimation();
  93. /**
  94. * Evaluates animation for a single object and writes the result in the currently active write buffer.
  95. *
  96. * @param[in] anim Proxy representing the animation to evaluate.
  97. * @param[in] boneIdx Index in the output buffer in which to write evaluated bone information. This will be
  98. * automatically advanced by the number of written bone transforms.
  99. */
  100. void evaluateAnimation(AnimationProxy* anim, UINT32& boneIdx);
  101. UINT64 mNextId;
  102. UnorderedMap<UINT64, Animation*> mAnimations;
  103. float mUpdateRate;
  104. float mAnimationTime;
  105. float mLastAnimationUpdateTime;
  106. float mNextAnimationUpdateTime;
  107. bool mPaused;
  108. SPtr<VertexDataDesc> mBlendShapeVertexDesc;
  109. // Animation thread
  110. Vector<SPtr<AnimationProxy>> mProxies;
  111. Vector<ConvexVolume> mCullFrustums;
  112. EvaluatedAnimationData mAnimData[CoreThread::NUM_SYNC_BUFFERS + 1];
  113. UINT32 mPoseReadBufferIdx;
  114. UINT32 mPoseWriteBufferIdx;
  115. Signal mWorkerDoneSignal;
  116. Mutex mMutex;
  117. UINT32 mNumActiveWorkers = 0;
  118. bool mSwapBuffers = false;
  119. };
  120. /** Provides easier access to AnimationManager. */
  121. BS_CORE_EXPORT AnimationManager& gAnimation();
  122. /** @} */
  123. }