Solver.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Jobs/Job.h>
  10. #include <AzCore/Jobs/JobCompletion.h>
  11. #include <AzCore/RTTI/RTTI.h>
  12. #include <AzCore/std/containers/unordered_set.h>
  13. #include <NvCloth/ISolver.h>
  14. #include <System/NvTypes.h>
  15. namespace NvCloth
  16. {
  17. class Cloth;
  18. //! Implementation of the ISolver interface.
  19. //!
  20. //! When enabled, it runs the simulation on all its cloths and sends
  21. //! notifications before and after the simulation has been executed.
  22. class Solver
  23. : public ISolver
  24. {
  25. public:
  26. AZ_RTTI(Solver, "{111055FC-F590-4BCD-A7B9-D96B1C44E3E8}", ISolver);
  27. Solver(const AZStd::string& name, NvSolverUniquePtr nvSolver);
  28. ~Solver();
  29. void AddCloth(Cloth* cloth);
  30. void RemoveCloth(Cloth* cloth);
  31. size_t GetNumCloths() const;
  32. // ISolver overrides ...
  33. const AZStd::string& GetName() const override;
  34. void Enable(bool value) override;
  35. bool IsEnabled() const override;
  36. void SetUserSimulated(bool value) override;
  37. bool IsUserSimulated() const override;
  38. void StartSimulation(float deltaTime) override;
  39. void FinishSimulation() override;
  40. void SetInterCollisionDistance(float distance) override;
  41. void SetInterCollisionStiffness(float stiffness) override;
  42. void SetInterCollisionIterations(AZ::u32 iterations) override;
  43. private:
  44. using Cloths = AZStd::vector<Cloth*>;
  45. class ClothsPreSimulationJob
  46. : public AZ::Job
  47. {
  48. public:
  49. AZ_CLASS_ALLOCATOR(ClothsPreSimulationJob, AZ::ThreadPoolAllocator);
  50. ClothsPreSimulationJob(const Cloths* cloths, float deltaTime,
  51. AZ::Job* continuationJob, AZ::JobContext* context = nullptr);
  52. void Process() override;
  53. private:
  54. // List of cloths to do the pre-simulation work for.
  55. const Cloths* m_cloths = nullptr;
  56. // The job to run after all pre-simulation jobs are completed.
  57. AZ::Job* m_continuationJob = nullptr;
  58. // Delta time for the current simulation pass.
  59. float m_deltaTime = 0.0f;
  60. };
  61. class ClothsSimulationJob
  62. : public AZ::Job
  63. {
  64. public:
  65. AZ_CLASS_ALLOCATOR(Solver::ClothsSimulationJob, AZ::ThreadPoolAllocator)
  66. ClothsSimulationJob(nv::cloth::Solver* solver, float deltaTime,
  67. AZ::Job* continuationJob, AZ::JobContext* context = nullptr);
  68. void Process() override;
  69. private:
  70. // NvCloth solver object to simulate.
  71. nv::cloth::Solver* m_solver = nullptr;
  72. // The job to run after all simulation jobs are completed.
  73. AZ::Job* m_continuationJob = nullptr;
  74. // Delta time for the current simulation pass.
  75. float m_deltaTime = 0.0f;
  76. };
  77. class ClothsPostSimulationJob
  78. : public AZ::Job
  79. {
  80. public:
  81. AZ_CLASS_ALLOCATOR(ClothsPostSimulationJob, AZ::ThreadPoolAllocator);
  82. ClothsPostSimulationJob(const Cloths* cloths, float deltaTime,
  83. AZ::Job* continuationJob, AZ::JobContext* context = nullptr);
  84. void Process() override;
  85. private:
  86. // List of cloths to do the post-simulation work for.
  87. const Cloths* m_cloths = nullptr;
  88. // The job to run after all post-simulation jobs are completed.
  89. AZ::Job* m_continuationJob = nullptr;
  90. // Delta time for the current simulation pass.
  91. float m_deltaTime = 0.0f;
  92. };
  93. void RemoveClothInternal(Cloths::iterator clothIt);
  94. // Name of the solver.
  95. AZStd::string m_name;
  96. // NvCloth solver object.
  97. NvSolverUniquePtr m_nvSolver;
  98. // When enabled the solver will be simulated and its events signaled.
  99. bool m_enabled = true;
  100. // When user-simulated the user will have the responsibility of calling Simulate function.
  101. bool m_userSimulated = false;
  102. // List of Cloth instances added to this solver.
  103. Cloths m_cloths;
  104. // Stored delta time during the simulation.
  105. float m_deltaTime = 0.0f;
  106. // Flag indicating if the simulation jobs are currently running.
  107. bool m_isSimulating = false;
  108. // Simulation synchronization job
  109. AZ::JobCompletion m_simulationCompletion;
  110. };
  111. } // namespace NvCloth