ModelLodAsset.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <Atom/RPI.Reflect/Model/ModelLodAsset.h>
  9. #include <AzCore/Asset/AssetSerializer.h>
  10. namespace AZ
  11. {
  12. namespace RPI
  13. {
  14. const char* ModelLodAsset::DisplayName = "ModelLodAsset";
  15. const char* ModelLodAsset::Group = "Model";
  16. const char* ModelLodAsset::Extension = "azlod";
  17. void ModelLodAsset::Reflect(AZ::ReflectContext* context)
  18. {
  19. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  20. {
  21. serializeContext->Class<ModelLodAsset>()
  22. ->Version(0)
  23. ->Field("Meshes", &ModelLodAsset::m_meshes)
  24. ->Field("Aabb", &ModelLodAsset::m_aabb)
  25. ->Field("StreamBuffers", &ModelLodAsset::m_streamBuffers)
  26. ->Field("IndexBufferView", &ModelLodAsset::m_indexBuffer)
  27. ;
  28. }
  29. Mesh::Reflect(context);
  30. }
  31. void ModelLodAsset::Mesh::Reflect(AZ::ReflectContext* context)
  32. {
  33. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  34. {
  35. serializeContext->Class<ModelLodAsset::Mesh>()
  36. ->Version(1)
  37. ->Field("Name", &ModelLodAsset::Mesh::m_name)
  38. ->Field("AABB", &ModelLodAsset::Mesh::m_aabb)
  39. ->Field("MaterialSlotId", &ModelLodAsset::Mesh::m_materialSlotId)
  40. ->Field("IndexBufferAssetView", &ModelLodAsset::Mesh::m_indexBufferAssetView)
  41. ->Field("StreamBufferInfo", &ModelLodAsset::Mesh::m_streamBufferInfo)
  42. ;
  43. }
  44. StreamBufferInfo::Reflect(context);
  45. }
  46. void ModelLodAsset::Mesh::StreamBufferInfo::Reflect(AZ::ReflectContext* context)
  47. {
  48. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  49. {
  50. serializeContext->Class<ModelLodAsset::Mesh::StreamBufferInfo>()
  51. ->Version(1)
  52. ->Field("Semantic", &ModelLodAsset::Mesh::StreamBufferInfo::m_semantic)
  53. ->Field("CustomName", &ModelLodAsset::Mesh::StreamBufferInfo::m_customName)
  54. ->Field("BufferAssetView", &ModelLodAsset::Mesh::StreamBufferInfo::m_bufferAssetView)
  55. ;
  56. }
  57. }
  58. uint32_t ModelLodAsset::Mesh::GetVertexCount() const
  59. {
  60. // Index 0 here is not special. All stream buffer views owned by this mesh should
  61. // view the same number of vertices. It doesn't make sense to be viewing 30 positions
  62. // but only 20 normals since we're using an index buffer model.
  63. return m_streamBufferInfo[0].m_bufferAssetView.GetBufferViewDescriptor().m_elementCount;
  64. }
  65. uint32_t ModelLodAsset::Mesh::GetIndexCount() const
  66. {
  67. return m_indexBufferAssetView.GetBufferViewDescriptor().m_elementCount;
  68. }
  69. ModelMaterialSlot::StableId ModelLodAsset::Mesh::GetMaterialSlotId() const
  70. {
  71. return m_materialSlotId;
  72. }
  73. const AZ::Name& ModelLodAsset::Mesh::GetName() const
  74. {
  75. return m_name;
  76. }
  77. const AZ::Aabb& ModelLodAsset::Mesh::GetAabb() const
  78. {
  79. return m_aabb;
  80. }
  81. const BufferAssetView& ModelLodAsset::Mesh::GetIndexBufferAssetView() const
  82. {
  83. return m_indexBufferAssetView;
  84. }
  85. AZStd::span<const ModelLodAsset::Mesh::StreamBufferInfo> ModelLodAsset::Mesh::GetStreamBufferInfoList() const
  86. {
  87. return AZStd::span<const ModelLodAsset::Mesh::StreamBufferInfo>(m_streamBufferInfo);
  88. }
  89. void ModelLodAsset::AddMesh(const Mesh& mesh)
  90. {
  91. m_meshes.push_back(mesh);
  92. Aabb meshAabb = mesh.GetAabb();
  93. m_aabb.AddAabb(meshAabb);
  94. }
  95. AZStd::span<const ModelLodAsset::Mesh> ModelLodAsset::GetMeshes() const
  96. {
  97. return AZStd::span<const ModelLodAsset::Mesh>(m_meshes);
  98. }
  99. const AZ::Aabb& ModelLodAsset::GetAabb() const
  100. {
  101. return m_aabb;
  102. }
  103. const BufferAssetView* ModelLodAsset::GetSemanticBufferAssetView(const AZ::Name& semantic, uint32_t meshIndex) const
  104. {
  105. AZ_Assert(meshIndex < m_meshes.size(), "Mesh index out of range");
  106. return m_meshes[meshIndex].GetSemanticBufferAssetView(semantic);
  107. }
  108. void ModelLodAsset::LoadBufferAssets()
  109. {
  110. m_indexBuffer.QueueLoad();
  111. for (auto& streamBuffer : m_streamBuffers)
  112. {
  113. streamBuffer.QueueLoad();
  114. }
  115. m_indexBuffer.BlockUntilLoadComplete();
  116. for (auto& streamBuffer : m_streamBuffers)
  117. {
  118. streamBuffer.BlockUntilLoadComplete();
  119. }
  120. // update buffer asset references in meshes
  121. for (auto& mesh : m_meshes)
  122. {
  123. mesh.LoadBufferAssets();
  124. }
  125. }
  126. void ModelLodAsset::ReleaseBufferAssets()
  127. {
  128. m_indexBuffer.Release();
  129. for (auto& streamBuffer : m_streamBuffers)
  130. {
  131. streamBuffer.Release();
  132. }
  133. for (auto& mesh : m_meshes)
  134. {
  135. mesh.ReleaseBufferAssets();
  136. }
  137. }
  138. void ModelLodAsset::Mesh::LoadBufferAssets()
  139. {
  140. m_indexBufferAssetView.LoadBufferAsset();
  141. for (auto& bufferInfo : m_streamBufferInfo)
  142. {
  143. bufferInfo.m_bufferAssetView.LoadBufferAsset();
  144. }
  145. }
  146. void ModelLodAsset::Mesh::ReleaseBufferAssets()
  147. {
  148. m_indexBufferAssetView.ReleaseBufferAsset();
  149. for (auto& bufferInfo : m_streamBufferInfo)
  150. {
  151. bufferInfo.m_bufferAssetView.ReleaseBufferAsset();
  152. }
  153. }
  154. const BufferAssetView* ModelLodAsset::Mesh::GetSemanticBufferAssetView(const AZ::Name& semantic) const
  155. {
  156. const AZStd::span<const ModelLodAsset::Mesh::StreamBufferInfo>& streamBufferList = GetStreamBufferInfoList();
  157. for (const ModelLodAsset::Mesh::StreamBufferInfo& streamBufferInfo : streamBufferList)
  158. {
  159. if (streamBufferInfo.m_semantic.m_name == semantic)
  160. {
  161. return &streamBufferInfo.m_bufferAssetView;
  162. }
  163. }
  164. return nullptr;
  165. }
  166. void ModelLodAsset::SetReady()
  167. {
  168. m_status = Data::AssetData::AssetStatus::Ready;
  169. }
  170. } // namespace RPI
  171. } // namespace AZ