3
0

SystemCommon.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/Math/Vector2.h>
  10. #include <AzCore/Math/Vector3.h>
  11. #include <AzCore/Math/Transform.h>
  12. #include <AzCore/Math/Quaternion.h>
  13. #include <AzCore/Memory/ChildAllocatorSchema.h>
  14. #include <AzCore/Memory/SystemAllocator.h>
  15. #include <AzCore/std/smart_ptr/unique_ptr.h>
  16. #include <AzCore/std/smart_ptr/intrusive_ptr.h>
  17. #include <MCore/Source/Vector.h>
  18. #include <MCore/Source/MemoryObject.h>
  19. #include <EMotionFX/Source/Transform.h>
  20. namespace AZStd
  21. {
  22. /**
  23. * Intrusive ptr for EMotionFX-owned objects (uses EMotionFX's internal ref-counting MCore::Destroy()).
  24. */
  25. template<>
  26. struct IntrusivePtrCountPolicy<MCore::MemoryObject>
  27. {
  28. static AZ_FORCE_INLINE void add_ref(MCore::MemoryObject* ptr)
  29. {
  30. ptr->IncreaseReferenceCount();
  31. }
  32. static AZ_FORCE_INLINE void release(MCore::MemoryObject* ptr)
  33. {
  34. MCore::Destroy(ptr); // Calls DecreaseReferenceCount.
  35. }
  36. };
  37. }
  38. namespace EMotionFX
  39. {
  40. namespace Integration
  41. {
  42. /**
  43. * System allocator to be used for all EMotionFX and EMotionFXAnimation gem persistent allocations.
  44. */
  45. AZ_CHILD_ALLOCATOR_WITH_NAME(EMotionFXAllocator, "EMotionFXAllocator", "{00AEC34F-4A00-4ECB-BC9C-7221E76337D6}", AZ::SystemAllocator);
  46. /**
  47. * Intrusive ptr for EMotionFX-owned objects.
  48. * Uses EMotionFX's internal ref-counting.
  49. */
  50. template <typename ObjectType>
  51. class EMotionFXPtr
  52. {
  53. public:
  54. /// Use only to initialize a new EMotionFXPtr<> given an EMotionFX SDK object not currently owned by an EMotionFXPtr<>.
  55. /// This is generally only appropriate for use when an EMotionFX object has just been constructed.
  56. static EMotionFXPtr<ObjectType> MakeFromNew(ObjectType* object)
  57. {
  58. AZ_Assert(object, "CreateFromNew called with invalid object.");
  59. AZ_Assert(object && object->GetReferenceCount() == 1, "Newly constructed EMotion FX objects are expected to have a referene count initialized to 1.");
  60. // EMotionFX initializes objects with a ref count already at 1. So for newly-constructed objects that we're
  61. // managing through smart pointers, it's not necessary to increment ref count during initial acquisition.
  62. EMotionFXPtr<ObjectType> ptr;
  63. ptr.m_ptr = object;
  64. return ptr;
  65. }
  66. AZ_FORCE_INLINE explicit EMotionFXPtr(ObjectType* object = nullptr)
  67. {
  68. *this = object;
  69. }
  70. AZ_FORCE_INLINE EMotionFXPtr(const EMotionFXPtr<ObjectType>& rhs)
  71. {
  72. *this = rhs;
  73. }
  74. AZ_FORCE_INLINE ~EMotionFXPtr()
  75. {
  76. if (m_ptr)
  77. {
  78. MCore::Destroy(m_ptr); // Calls DecreaseReferenceCount.
  79. }
  80. }
  81. AZ_FORCE_INLINE void reset(ObjectType* object = nullptr)
  82. {
  83. *this = object;
  84. }
  85. AZ_FORCE_INLINE void operator=(ObjectType* object)
  86. {
  87. if (m_ptr)
  88. {
  89. MCore::Destroy(m_ptr); // Calls DecreaseReferenceCount.
  90. m_ptr = nullptr;
  91. }
  92. m_ptr = object;
  93. if (m_ptr)
  94. {
  95. m_ptr->IncreaseReferenceCount();
  96. }
  97. }
  98. AZ_FORCE_INLINE void operator=(const EMotionFXPtr<ObjectType>& rhs)
  99. {
  100. reset(rhs ? rhs.get() : nullptr);
  101. }
  102. AZ_FORCE_INLINE ObjectType* operator ->() const
  103. {
  104. AZ_Assert(m_ptr, "Attempting to dereference a null EMotion FX object pointer.");
  105. return m_ptr;
  106. }
  107. AZ_FORCE_INLINE ObjectType* get() const
  108. {
  109. return m_ptr;
  110. }
  111. AZ_FORCE_INLINE operator bool() const
  112. {
  113. return m_ptr != nullptr;
  114. }
  115. AZ_FORCE_INLINE bool operator==(const EMotionFXPtr<ObjectType>& rhs) const
  116. {
  117. return (m_ptr == rhs.m_ptr);
  118. }
  119. AZ_FORCE_INLINE bool operator==(const ObjectType* rhs) const
  120. {
  121. return (m_ptr == rhs);
  122. }
  123. AZ_FORCE_INLINE bool operator!=(const EMotionFXPtr<ObjectType>& rhs) const
  124. {
  125. return !(m_ptr == rhs.m_ptr);
  126. }
  127. AZ_FORCE_INLINE bool operator!=(const ObjectType* rhs) const
  128. {
  129. return !(m_ptr == rhs);
  130. }
  131. private:
  132. ObjectType* m_ptr = nullptr;
  133. };
  134. /**
  135. * EMotionFX memory hooks
  136. */
  137. AZ_FORCE_INLINE void* EMotionFXAlloc(size_t numBytes, AZ::u16 categoryID, AZ::u16 blockID, const char* filename, AZ::u32 lineNr)
  138. {
  139. (void)categoryID;
  140. (void)blockID;
  141. (void)lineNr;
  142. return AZ::AllocatorInstance<EMotionFXAllocator>::Get().Allocate(numBytes, 8, 0, "EMotionFX", filename, lineNr);
  143. }
  144. AZ_FORCE_INLINE void* EMotionFXRealloc(void* memory, size_t numBytes, AZ::u16 categoryID, AZ::u16 blockID, const char* filename, AZ::u32 lineNr)
  145. {
  146. (void)categoryID;
  147. (void)blockID;
  148. (void)filename;
  149. (void)lineNr;
  150. return AZ::AllocatorInstance<EMotionFXAllocator>::Get().ReAllocate(memory, numBytes, 8);
  151. }
  152. AZ_FORCE_INLINE void EMotionFXFree(void* memory)
  153. {
  154. AZ::AllocatorInstance<EMotionFXAllocator>::Get().DeAllocate(memory);
  155. }
  156. } //namespace Integration
  157. } // namespace EMotionFX