MeshInstanceManager.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/std/parallel/mutex.h>
  10. #include <Mesh/MeshInstanceGroupKey.h>
  11. #include <Mesh/MeshInstanceGroupList.h>
  12. namespace AZ::Render
  13. {
  14. //! The MeshInstanceManager tracks the mesh/material combinations that can be instanced
  15. class MeshInstanceManager
  16. {
  17. public:
  18. using Handle = MeshInstanceGroupList::WeakHandle;
  19. using InsertResult = MeshInstanceGroupList::InsertResult;
  20. using ParallelRanges = MeshInstanceGroupList::ParallelRanges;
  21. //! Increase the ref-count for an instance group if one already exists for the given key, or add a new instance group if it doesn't exist.
  22. //! Returns an InsertResult with a weak handle to the data and the ref-count for the instance group after adding this instance.
  23. InsertResult AddInstance(MeshInstanceGroupKey meshInstanceGroupData);
  24. //! Decrease the ref-count for an instance group, and remove it if the ref-count drops to 0.
  25. //! The MeshInstanceManager keeps a copy of the MeshInstanceGroupKey alongside the data, so
  26. //! removing by handle is just as performance as removing by key.
  27. void RemoveInstance(MeshInstanceGroupKey meshInstanceGroupData);
  28. //! Decrease the ref-count for an instance group, and remove it if the ref-count drops to 0.
  29. //! The MeshInstanceManager keeps a copy of the MeshInstanceGroupKey alongside the data, so
  30. //! removing by handle is just as performance as removing by key.
  31. void RemoveInstance(Handle handle);
  32. //! Get the total number of instance groups being managed by the MeshInstanceManager
  33. uint32_t GetInstanceGroupCount() const;
  34. //! Constant O(1) access to a MeshInstanceGroup via its handle
  35. MeshInstanceGroupData& operator[](Handle handle);
  36. //! Get begin and end iterators for each page in the MeshInstanceGroup, which can be processed in parallel
  37. ParallelRanges GetParallelRanges();
  38. private:
  39. MeshInstanceGroupList m_instanceData;
  40. // Adding and removing entries in a MeshInstanceList is not threadsafe, and should be guarded with a mutex
  41. AZStd::mutex m_instanceDataMutex;
  42. AZStd::vector<uint32_t> m_createDrawPacketQueue;
  43. };
  44. } // namespace AZ::Render