MeshInstanceGroupList.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <Mesh/MeshInstanceGroupList.h>
  9. #include <AzCore/std/numeric.h>
  10. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  11. namespace AZ::Render
  12. {
  13. MeshInstanceGroupList::InsertResult MeshInstanceGroupList::Add(const MeshInstanceGroupKey& key)
  14. {
  15. // It is not safe to have multiple threads Add and/or Remove at the same time
  16. m_instanceDataConcurrencyChecker.soft_lock();
  17. typename DataMap::iterator it = m_dataMap.find(key);
  18. if (it == m_dataMap.end())
  19. {
  20. // Add the data map entry containing the handle and reference count
  21. IndexMapEntry entry;
  22. entry.m_handle = m_instanceGroupData.emplace();
  23. entry.m_count = 1;
  24. it = m_dataMap.emplace(AZStd::make_pair(key, AZStd::move(entry))).first;
  25. }
  26. else
  27. {
  28. // Data is already known, update the reference count and return the index
  29. it->second.m_count++;
  30. }
  31. // Keep track of the count from the map in the data itself
  32. it->second.m_handle->m_count = it->second.m_count;
  33. m_instanceDataConcurrencyChecker.soft_unlock();
  34. return MeshInstanceGroupList::InsertResult{ it->second.m_handle.GetWeakHandle(), it->second.m_count, static_cast<uint32_t>(m_instanceGroupData.GetPageIndex(it->second.m_handle))};
  35. }
  36. void MeshInstanceGroupList::Remove(const MeshInstanceGroupKey& key)
  37. {
  38. // It is not safe to have multiple threads Add and/or Remove at the same time
  39. m_instanceDataConcurrencyChecker.soft_lock();
  40. typename DataMap::iterator it = m_dataMap.find(key);
  41. if (it == m_dataMap.end())
  42. {
  43. AZ_Assert(false, "Unable to find key in the DataMap");
  44. m_instanceDataConcurrencyChecker.soft_unlock();
  45. return;
  46. }
  47. it->second.m_count--;
  48. if (it->second.m_count == 0)
  49. {
  50. // Remove it from the data map
  51. // The owning handle will go out of scope, which will erase it from the underlying array as well
  52. m_dataMap.erase(it);
  53. }
  54. m_instanceDataConcurrencyChecker.soft_unlock();
  55. }
  56. uint32_t MeshInstanceGroupList::GetInstanceGroupCount() const
  57. {
  58. return static_cast<uint32_t>(m_instanceGroupData.size());
  59. }
  60. auto MeshInstanceGroupList::GetParallelRanges() -> ParallelRanges
  61. {
  62. return m_instanceGroupData.GetParallelRanges();
  63. }
  64. MeshInstanceGroupData& MeshInstanceGroupList::operator[](WeakHandle handle)
  65. {
  66. return *handle;
  67. }
  68. const MeshInstanceGroupData& MeshInstanceGroupList::operator[](WeakHandle handle) const
  69. {
  70. return *handle;
  71. }
  72. } // namespace AZ::Render