3
0

MeshInstanceGroupList.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/Feature/Mesh/MeshFeatureProcessor.h>
  11. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  12. namespace AZ::Render
  13. {
  14. bool MeshInstanceGroupData::UpdateDrawPacket(const RPI::Scene& parentScene, bool forceUpdate)
  15. {
  16. if (m_drawPacket.Update(parentScene, forceUpdate))
  17. {
  18. // Clear any cached draw packets, since they need to be re-created
  19. m_perViewDrawPackets.clear();
  20. for (auto modelDataInstance : m_associatedInstances)
  21. {
  22. modelDataInstance->HandleDrawPacketUpdate();
  23. }
  24. return true;
  25. }
  26. return false;
  27. }
  28. bool MeshInstanceGroupData::UpdateShaderOptionFlags()
  29. {
  30. uint32_t newShaderOptionFlagMask = 0; // defaults to 0 so no shader options are specified.
  31. uint32_t newShaderOptionFlags = m_shaderOptionFlags;
  32. if (m_associatedInstances.size() > 0)
  33. {
  34. newShaderOptionFlags = (*m_associatedInstances.begin())->GetCullable().m_shaderOptionFlags;
  35. }
  36. for (auto modelDataInstance : m_associatedInstances)
  37. {
  38. // If the shader option flag of different intances are different, the mask for the flag is 0, which means the flag is unspecified.
  39. newShaderOptionFlagMask = ~(newShaderOptionFlags ^ modelDataInstance->GetCullable().m_shaderOptionFlags);
  40. // if the option flag has same value, keep the value.
  41. newShaderOptionFlags &= modelDataInstance->GetCullable().m_shaderOptionFlags;
  42. }
  43. // return ture if the shader option flags or mask changed.
  44. if (newShaderOptionFlags != m_shaderOptionFlags || newShaderOptionFlagMask != m_shaderOptionFlagMask)
  45. {
  46. m_shaderOptionFlags = newShaderOptionFlags;
  47. m_shaderOptionFlagMask = newShaderOptionFlagMask;
  48. return true;
  49. }
  50. return false;
  51. }
  52. void MeshInstanceGroupData::AddAssociatedInstance(ModelDataInstance* instance)
  53. {
  54. AZStd::scoped_lock<AZStd::mutex> scopedLock(m_eventLock);
  55. m_associatedInstances.insert(instance);
  56. }
  57. void MeshInstanceGroupData::RemoveAssociatedInstance(ModelDataInstance* instance)
  58. {
  59. AZStd::scoped_lock<AZStd::mutex> scopedLock(m_eventLock);
  60. m_associatedInstances.erase(instance);
  61. }
  62. MeshInstanceGroupList::InsertResult MeshInstanceGroupList::Add(const MeshInstanceGroupKey& key)
  63. {
  64. // It is not safe to have multiple threads Add and/or Remove at the same time
  65. m_instanceDataConcurrencyChecker.soft_lock();
  66. typename DataMap::iterator it = m_dataMap.find(key);
  67. if (it == m_dataMap.end())
  68. {
  69. // Add the data map entry containing the handle and reference count
  70. IndexMapEntry entry;
  71. entry.m_handle = m_instanceGroupData.emplace();
  72. entry.m_count = 1;
  73. it = m_dataMap.emplace(AZStd::make_pair(key, AZStd::move(entry))).first;
  74. }
  75. else
  76. {
  77. // Data is already known, update the reference count and return the index
  78. it->second.m_count++;
  79. }
  80. // Keep track of the count from the map in the data itself
  81. it->second.m_handle->m_count = it->second.m_count;
  82. m_instanceDataConcurrencyChecker.soft_unlock();
  83. return MeshInstanceGroupList::InsertResult{ it->second.m_handle.GetWeakHandle(), it->second.m_count, static_cast<uint32_t>(m_instanceGroupData.GetPageIndex(it->second.m_handle))};
  84. }
  85. void MeshInstanceGroupList::Remove(const MeshInstanceGroupKey& key)
  86. {
  87. // It is not safe to have multiple threads Add and/or Remove at the same time
  88. m_instanceDataConcurrencyChecker.soft_lock();
  89. typename DataMap::iterator it = m_dataMap.find(key);
  90. if (it == m_dataMap.end())
  91. {
  92. AZ_Assert(false, "Unable to find key in the DataMap");
  93. m_instanceDataConcurrencyChecker.soft_unlock();
  94. return;
  95. }
  96. it->second.m_count--;
  97. if (it->second.m_count == 0)
  98. {
  99. // Remove it from the data map
  100. // The owning handle will go out of scope, which will erase it from the underlying array as well
  101. m_dataMap.erase(it);
  102. }
  103. m_instanceDataConcurrencyChecker.soft_unlock();
  104. }
  105. uint32_t MeshInstanceGroupList::GetInstanceGroupCount() const
  106. {
  107. return static_cast<uint32_t>(m_instanceGroupData.size());
  108. }
  109. auto MeshInstanceGroupList::GetParallelRanges() -> ParallelRanges
  110. {
  111. return m_instanceGroupData.GetParallelRanges();
  112. }
  113. MeshInstanceGroupData& MeshInstanceGroupList::operator[](WeakHandle handle)
  114. {
  115. return *handle;
  116. }
  117. const MeshInstanceGroupData& MeshInstanceGroupList::operator[](WeakHandle handle) const
  118. {
  119. return *handle;
  120. }
  121. } // namespace AZ::Render