3
0

SkinnedMeshOutputStreamManager.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <SkinnedMesh/SkinnedMeshOutputStreamManager.h>
  9. #include <AzCore/Console/IConsole.h>
  10. #include <Atom/Feature/SkinnedMesh/SkinnedMeshVertexStreams.h>
  11. #include <Atom/Feature/SkinnedMesh/SkinnedMeshFeatureProcessorBus.h>
  12. #include <Atom/RHI/RHISystemInterface.h>
  13. #include <Atom/RPI.Reflect/Buffer/BufferAssetCreator.h>
  14. #include <Atom_Feature_Traits_Platform.h>
  15. #include <numeric>
  16. namespace AZ
  17. {
  18. namespace Render
  19. {
  20. SkinnedMeshOutputStreamManager::SkinnedMeshOutputStreamManager()
  21. {
  22. Init();
  23. }
  24. SkinnedMeshOutputStreamManager::~SkinnedMeshOutputStreamManager()
  25. {
  26. m_bufferAsset = {};
  27. }
  28. void SkinnedMeshOutputStreamManager::CalculateAlignment()
  29. {
  30. m_alignment = 1;
  31. for (uint8_t outputStreamIndex = 0; outputStreamIndex < static_cast<uint8_t>(SkinnedMeshOutputVertexStreams::NumVertexStreams); ++outputStreamIndex)
  32. {
  33. // Using the least common multiple enables resource views to be typed and ensures they can get an offset in bytes that is a multiple of an element count
  34. const SkinnedMeshOutputVertexStreamInfo& outputStreamInfo = SkinnedMeshVertexStreamPropertyInterface::Get()->GetOutputStreamInfo(static_cast<SkinnedMeshOutputVertexStreams>(outputStreamIndex));
  35. m_alignment = std::lcm(m_alignment, outputStreamInfo.m_elementSize);
  36. }
  37. }
  38. void SkinnedMeshOutputStreamManager::CreateBufferAsset()
  39. {
  40. RHI::FreeListAllocator::Descriptor allocatorDescriptor;
  41. allocatorDescriptor.m_alignmentInBytes = m_alignment;
  42. allocatorDescriptor.m_capacityInBytes = m_sizeInBytes;
  43. allocatorDescriptor.m_policy = RHI::FreeListAllocatorPolicy::BestFit;
  44. allocatorDescriptor.m_garbageCollectLatency = 0;
  45. m_freeListAllocator.Init(allocatorDescriptor);
  46. // Create the actual buffer
  47. RPI::BufferAssetCreator creator;
  48. Uuid uuid = Uuid::CreateRandom();
  49. creator.Begin(uuid);
  50. creator.SetBufferName("SkinnedMeshOutputStream");
  51. creator.SetPoolAsset(SkinnedMeshVertexStreamPropertyInterface::Get()->GetOutputStreamResourcePool());
  52. RHI::BufferDescriptor bufferDescriptor;
  53. bufferDescriptor.m_bindFlags = RHI::BufferBindFlags::InputAssembly | RHI::BufferBindFlags::ShaderReadWrite;
  54. bufferDescriptor.m_byteCount = m_sizeInBytes;
  55. bufferDescriptor.m_alignment = m_alignment;
  56. creator.SetBuffer(nullptr, 0, bufferDescriptor);
  57. RHI::BufferViewDescriptor viewDescriptor;
  58. viewDescriptor.m_elementFormat = RHI::Format::Unknown;
  59. viewDescriptor.m_elementSize = sizeof(float);
  60. viewDescriptor.m_elementCount = aznumeric_cast<uint32_t>(m_sizeInBytes) / viewDescriptor.m_elementSize;
  61. viewDescriptor.m_elementOffset = 0;
  62. creator.SetBufferViewDescriptor(viewDescriptor);
  63. creator.End(m_bufferAsset);
  64. }
  65. // default value of 256mb supports roughly 42 character instances at 100,000 vertices per character x 64 bytes per vertex (12 byte position + 12 byte previous frame position + 12 byte normal + 16 byte tangent + 12 byte bitangent)
  66. // This includes only the output of the skinning compute shader, not the input buffers or bone transforms
  67. AZ_CVAR(
  68. int,
  69. r_skinnedMeshInstanceMemoryPoolSize,
  70. AZ_TRAIT_DEFAULT_SKINNING_MEMORY_SIZE,
  71. nullptr,
  72. AZ::ConsoleFunctorFlags::NeedsReload,
  73. "The amount of memory in Mb available for all actor skinning data. Note that this must only be set once at application startup"
  74. );
  75. void SkinnedMeshOutputStreamManager::Init()
  76. {
  77. }
  78. void SkinnedMeshOutputStreamManager::EnsureInit()
  79. {
  80. if (!m_needsInit)
  81. {
  82. return;
  83. }
  84. m_needsInit = false;
  85. AZ::u64 sizeInMb{};
  86. if (auto console = AZ::Interface<AZ::IConsole>::Get(); console != nullptr)
  87. {
  88. console->GetCvarValue("r_skinnedMeshInstanceMemoryPoolSize", sizeInMb);
  89. }
  90. m_sizeInBytes = sizeInMb * (1024u * 1024u);
  91. RHI::Device* device = AZ::RHI::RHISystemInterface::Get()->GetDevice();
  92. AZ_Assert(device != nullptr, "Invalid RHI device");
  93. const size_t maxBufferSize = static_cast<size_t>(device->GetLimits().m_maxBufferSize);
  94. AZ_Warning("SkinnedMeshOutputStreamManager", m_sizeInBytes <= maxBufferSize,
  95. "Amount of memory available for actor skinning data capped from the requested %zu bytes to %zu bytes due to max buffer size limit.",
  96. m_sizeInBytes, maxBufferSize);
  97. m_sizeInBytes = AZStd::min(m_sizeInBytes, maxBufferSize);
  98. CalculateAlignment();
  99. CreateBufferAsset();
  100. SystemTickBus::Handler::BusConnect();
  101. }
  102. AZStd::intrusive_ptr<SkinnedMeshOutputStreamAllocation> SkinnedMeshOutputStreamManager::Allocate(size_t byteCount)
  103. {
  104. RHI::VirtualAddress result;
  105. {
  106. AZStd::lock_guard<AZStd::mutex> lock(m_allocatorMutex);
  107. EnsureInit();
  108. result = m_freeListAllocator.Allocate(byteCount, m_alignment);
  109. }
  110. if (result.IsValid())
  111. {
  112. return aznew SkinnedMeshOutputStreamAllocation(result);
  113. }
  114. return nullptr;
  115. }
  116. void SkinnedMeshOutputStreamManager::DeAllocate(RHI::VirtualAddress allocation)
  117. {
  118. if (allocation.IsValid())
  119. {
  120. {
  121. AZStd::lock_guard<AZStd::mutex> lock(m_allocatorMutex);
  122. m_freeListAllocator.DeAllocate(allocation);
  123. }
  124. m_memoryWasFreed = true;
  125. m_broadcastMemoryAvailableEvent = true;
  126. }
  127. }
  128. void SkinnedMeshOutputStreamManager::DeAllocateNoSignal(RHI::VirtualAddress allocation)
  129. {
  130. if (allocation.IsValid())
  131. {
  132. {
  133. AZStd::lock_guard<AZStd::mutex> lock(m_allocatorMutex);
  134. m_freeListAllocator.DeAllocate(allocation);
  135. }
  136. m_memoryWasFreed = true;
  137. }
  138. }
  139. Data::Asset<RPI::BufferAsset> SkinnedMeshOutputStreamManager::GetBufferAsset()
  140. {
  141. EnsureInit();
  142. return m_bufferAsset;
  143. }
  144. Data::Instance<RPI::Buffer> SkinnedMeshOutputStreamManager::GetBuffer()
  145. {
  146. EnsureInit();
  147. if (!m_buffer)
  148. {
  149. m_buffer = RPI::Buffer::FindOrCreate(m_bufferAsset);
  150. }
  151. return m_buffer;
  152. }
  153. void SkinnedMeshOutputStreamManager::OnSystemTick()
  154. {
  155. GarbageCollect();
  156. }
  157. void SkinnedMeshOutputStreamManager::GarbageCollect()
  158. {
  159. if (m_memoryWasFreed)
  160. {
  161. m_memoryWasFreed = false;
  162. {
  163. AZStd::lock_guard<AZStd::mutex> lock(m_allocatorMutex);
  164. m_freeListAllocator.GarbageCollect();
  165. }
  166. if (m_broadcastMemoryAvailableEvent)
  167. {
  168. SkinnedMeshOutputStreamNotificationBus::Broadcast(&SkinnedMeshOutputStreamNotificationBus::Events::OnSkinnedMeshOutputStreamMemoryAvailable);
  169. m_broadcastMemoryAvailableEvent = false;
  170. }
  171. }
  172. }
  173. }// namespace Render
  174. }// namespace AZ