Factory.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <System/Factory.h>
  9. #include <System/Solver.h>
  10. #include <System/Fabric.h>
  11. #include <System/Cloth.h>
  12. #include <System/SystemComponent.h>
  13. // NvCloth library includes
  14. #include <NvCloth/Range.h>
  15. #include <NvCloth/Factory.h>
  16. namespace NvCloth
  17. {
  18. namespace
  19. {
  20. AZ::u64 ClothIdCounter = 1;
  21. }
  22. void Factory::Init()
  23. {
  24. // Create a CPU NvCloth Factory
  25. if (!m_nvFactory)
  26. {
  27. m_nvFactory = NvFactoryUniquePtr(NvClothCreateFactoryCPU());
  28. AZ_Assert(m_nvFactory, "Failed to create CPU cloth factory");
  29. if (SystemComponent::CheckLastClothError())
  30. {
  31. AZ_Printf("Cloth", "NVIDIA NvCloth Gem using CPU for cloth simulation.\n");
  32. }
  33. else
  34. {
  35. AZ_Error("Cloth", false, "NvCloth library failed to create CPU factory.");
  36. }
  37. }
  38. }
  39. void Factory::Destroy()
  40. {
  41. m_nvFactory.reset();
  42. }
  43. AZStd::unique_ptr<Solver> Factory::CreateSolver(const AZStd::string& name)
  44. {
  45. if (name.empty())
  46. {
  47. AZ_Warning("NvCloth", false, "Factory failed to create solver because name passed is empty.");
  48. return nullptr;
  49. }
  50. NvSolverUniquePtr nvSolver(
  51. m_nvFactory->createSolver());
  52. if (!nvSolver)
  53. {
  54. AZ_Warning("NvCloth", false, "Factory failed to create solver %s.", name.c_str());
  55. return nullptr;
  56. }
  57. return AZStd::make_unique<Solver>(
  58. name,
  59. AZStd::move(nvSolver));
  60. }
  61. AZStd::unique_ptr<Fabric> Factory::CreateFabric(const FabricCookedData& fabricCookedData)
  62. {
  63. if (!fabricCookedData.m_id.IsValid())
  64. {
  65. AZ_Warning("NvCloth", false, "Factory failed to create fabric because the id of the fabric cooked data passed is not valid.");
  66. return nullptr;
  67. }
  68. NvFabricUniquePtr nvFabric(
  69. m_nvFactory->createFabric(
  70. fabricCookedData.m_internalData.m_numParticles,
  71. ToNvRange(fabricCookedData.m_internalData.m_phaseIndices),
  72. ToNvRange(fabricCookedData.m_internalData.m_sets),
  73. ToNvRange(fabricCookedData.m_internalData.m_restValues),
  74. ToNvRange(fabricCookedData.m_internalData.m_stiffnessValues),
  75. ToNvRange(fabricCookedData.m_internalData.m_indices),
  76. ToNvRange(fabricCookedData.m_internalData.m_anchors),
  77. ToNvRange(fabricCookedData.m_internalData.m_tetherLengths),
  78. ToNvRange(fabricCookedData.m_internalData.m_triangles)));
  79. if (!nvFabric)
  80. {
  81. AZ_Warning("NvCloth", false, "Factory failed to create fabric.");
  82. return nullptr;
  83. }
  84. return AZStd::make_unique<Fabric>(
  85. fabricCookedData,
  86. AZStd::move(nvFabric));
  87. }
  88. AZStd::unique_ptr<Cloth> Factory::CreateCloth(
  89. const AZStd::vector<SimParticleFormat>& initialParticles,
  90. Fabric* fabric)
  91. {
  92. if (initialParticles.empty())
  93. {
  94. AZ_Warning("NvCloth", false, "Factory failed to create cloth because no particles were provided.");
  95. return nullptr;
  96. }
  97. if (!fabric)
  98. {
  99. AZ_Warning("NvCloth", false, "Factory failed to create cloth because fabric provided is invalid.");
  100. return nullptr;
  101. }
  102. if (initialParticles.size() != fabric->m_cookedData.m_particles.size())
  103. {
  104. AZ_Warning("NvCloth", false, "Factory failed to create cloth because the number of initial particles provided (%d) didn't match the fabric's (%d).",
  105. initialParticles.size(), fabric->m_cookedData.m_particles.size());
  106. return nullptr;
  107. }
  108. NvClothUniquePtr nvCloth(
  109. m_nvFactory->createCloth(
  110. ToPxVec4NvRange(initialParticles),
  111. *fabric->m_nvFabric.get()));
  112. if (!nvCloth)
  113. {
  114. AZ_Warning("NvCloth", false, "Factory failed to create cloth.");
  115. return nullptr;
  116. }
  117. return AZStd::make_unique<Cloth>(
  118. ClothId(ClothIdCounter++),
  119. initialParticles,
  120. fabric,
  121. AZStd::move(nvCloth));
  122. }
  123. } // namespace NvCloth