ModelAsset.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <Assets/ModelAsset.h>
  9. #include <AzNetworking/Serialization/NetworkInputSerializer.h>
  10. #include <AzNetworking/Serialization/NetworkOutputSerializer.h>
  11. namespace MachineLearning
  12. {
  13. bool ModelAsset::Serialize(AzNetworking::ISerializer& serializer)
  14. {
  15. return serializer.Serialize(m_name, "Name")
  16. && serializer.Serialize(m_activationCount, "activationCount")
  17. && serializer.Serialize(m_layers, "layers");
  18. }
  19. AZStd::size_t ModelAsset::EstimateSerializeSize() const
  20. {
  21. const AZStd::size_t padding = 64; // 64 bytes of extra padding just in case
  22. AZStd::size_t estimatedSize = padding
  23. + sizeof(AZStd::size_t)
  24. + m_name.size()
  25. + sizeof(m_activationCount)
  26. + sizeof(AZStd::size_t);
  27. for (const Layer& layer : m_layers)
  28. {
  29. estimatedSize += layer.EstimateSerializeSize();
  30. }
  31. return estimatedSize;
  32. }
  33. ModelAssetHandler::ModelAssetHandler()
  34. : AzFramework::GenericAssetHandler<ModelAsset>(ModelAsset::DisplayName, ModelAsset::Group, ModelAsset::Extension)
  35. {
  36. }
  37. AZ::Data::AssetHandler::LoadResult ModelAssetHandler::LoadAssetData
  38. (
  39. const AZ::Data::Asset<AZ::Data::AssetData>& asset,
  40. AZStd::shared_ptr<AZ::Data::AssetDataStream> stream,
  41. [[maybe_unused]]const AZ::Data::AssetFilterCB& assetLoadFilterCB
  42. )
  43. {
  44. ModelAsset* assetData = asset.GetAs<ModelAsset>();
  45. AZ_Assert(assetData, "Asset is of the wrong type.");
  46. const AZ::IO::SizeType length = stream->GetLength();
  47. AZStd::vector<uint8_t> serializeBuffer;
  48. serializeBuffer.resize(length);
  49. stream->Read(length, serializeBuffer.data());
  50. AzNetworking::NetworkOutputSerializer serializer(serializeBuffer.data(), static_cast<uint32_t>(serializeBuffer.size()));
  51. if (assetData->Serialize(serializer))
  52. {
  53. return AZ::Data::AssetHandler::LoadResult::LoadComplete;
  54. }
  55. return AZ::Data::AssetHandler::LoadResult::Error;
  56. }
  57. bool ModelAssetHandler::SaveAssetData(const AZ::Data::Asset<AZ::Data::AssetData>& asset, AZ::IO::GenericStream* stream)
  58. {
  59. ModelAsset* assetData = asset.GetAs<ModelAsset>();
  60. AZ_Assert(assetData, "Asset is of the wrong type.");
  61. AZStd::vector<uint8_t> serializeBuffer;
  62. serializeBuffer.resize(assetData->EstimateSerializeSize());
  63. AzNetworking::NetworkInputSerializer serializer(serializeBuffer.data(), static_cast<uint32_t>(serializeBuffer.size()));
  64. if (assetData->Serialize(serializer))
  65. {
  66. stream->Write(serializer.GetSize(), serializeBuffer.data());
  67. return true;
  68. }
  69. return false;
  70. }
  71. }