StreamingImageAsset.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/Image/StreamingImageAsset.h>
  9. #include <Atom/RPI.Reflect/Allocators.h>
  10. #include <AzCore/Asset/AssetSerializer.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. namespace AZ
  13. {
  14. namespace RPI
  15. {
  16. AZ_CLASS_ALLOCATOR_IMPL(StreamingImageAsset, StreamingImageAssetAllocator)
  17. static bool ConvertOldVersions(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  18. {
  19. if (classElement.GetVersion() < 3)
  20. {
  21. auto crc32 = AZ::Crc32("m_tags");
  22. auto* vectorElement = classElement.FindSubElement(crc32);
  23. if (vectorElement)
  24. {
  25. // Get the old data
  26. AZStd::vector<AZ::Name> oldData;
  27. if (classElement.GetChildData(crc32, oldData))
  28. {
  29. // Convert the vector with the new allocator
  30. vectorElement->Convert(context, AZ::AzTypeInfo<StreamingImageAsset::TagList>::Uuid());
  31. for (const auto& element : oldData)
  32. {
  33. // Re add the elements
  34. vectorElement->AddElementWithData<AZ::Name>(context, "element", element);
  35. }
  36. }
  37. }
  38. }
  39. return true;
  40. }
  41. void StreamingImageAsset::Reflect(ReflectContext* context)
  42. {
  43. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  44. {
  45. // Need to register the old type with the Serializer so we can read it in order to convert it
  46. serializeContext->RegisterGenericType<AZStd::vector<AZ::Name>>();
  47. serializeContext->Class<MipChain>()
  48. ->Field("m_mipOffset", &MipChain::m_mipOffset)
  49. ->Field("m_mipCount", &MipChain::m_mipCount)
  50. ->Field("m_asset", &MipChain::m_asset)
  51. ;
  52. serializeContext->Class<StreamingImageAsset, ImageAsset>()
  53. ->Version(3, &ConvertOldVersions) // Added m_averageColor field
  54. ->Field("m_mipLevelToChainIndex", &StreamingImageAsset::m_mipLevelToChainIndex)
  55. ->Field("m_mipChains", &StreamingImageAsset::m_mipChains)
  56. ->Field("m_flags", &StreamingImageAsset::m_flags)
  57. ->Field("m_tailMipChain", &StreamingImageAsset::m_tailMipChain)
  58. ->Field("m_totalImageDataSize", &StreamingImageAsset::m_totalImageDataSize)
  59. ->Field("m_averageColor", &StreamingImageAsset::m_averageColor)
  60. ->Field("m_tags", &StreamingImageAsset::m_tags)
  61. ;
  62. }
  63. }
  64. const Data::Asset<ImageMipChainAsset>& StreamingImageAsset::GetMipChainAsset(size_t mipChainIndex) const
  65. {
  66. return m_mipChains[mipChainIndex].m_asset;
  67. }
  68. const ImageMipChainAsset& StreamingImageAsset::GetTailMipChain() const
  69. {
  70. return m_tailMipChain;
  71. }
  72. size_t StreamingImageAsset::GetMipChainCount() const
  73. {
  74. return m_mipChains.size();
  75. }
  76. size_t StreamingImageAsset::GetMipChainIndex(size_t mipLevel) const
  77. {
  78. if (mipLevel >= m_imageDescriptor.m_mipLevels)
  79. {
  80. AZ_Assert(false, "Input mipLevel doesn't exist");
  81. mipLevel = m_imageDescriptor.m_mipLevels - 1;
  82. }
  83. return m_mipLevelToChainIndex[mipLevel];
  84. }
  85. size_t StreamingImageAsset::GetMipLevel(size_t mipChainIndex) const
  86. {
  87. return m_mipChains[mipChainIndex].m_mipOffset;
  88. }
  89. size_t StreamingImageAsset::GetMipCount(size_t mipChainIndex) const
  90. {
  91. return m_mipChains[mipChainIndex].m_mipCount;
  92. }
  93. const Data::AssetId& StreamingImageAsset::GetPoolAssetId() const
  94. {
  95. return m_poolAssetId;
  96. }
  97. StreamingImageFlags StreamingImageAsset::GetFlags() const
  98. {
  99. return m_flags;
  100. }
  101. size_t StreamingImageAsset::GetTotalImageDataSize() const
  102. {
  103. return m_totalImageDataSize;
  104. }
  105. Color StreamingImageAsset::GetAverageColor() const
  106. {
  107. if (m_averageColor.IsFinite())
  108. {
  109. return m_averageColor;
  110. }
  111. else
  112. {
  113. AZ_Warning(
  114. "Streaming Image", false,
  115. "Non-finite average color, it probably was never initialized. Returning black.");
  116. return Color(0.0f);
  117. }
  118. }
  119. RHI::ImageDescriptor StreamingImageAsset::GetImageDescriptorForMipLevel(AZ::u32 mipLevel) const
  120. {
  121. RHI::ImageDescriptor imageDescriptor = GetImageDescriptor();
  122. // Retrieve the layout for the particular mip level.
  123. // The levels get stored in a series of ImageMipChainAssets, which keep a
  124. // record of an offset so that you can calculate the sub-image index
  125. // based on the actual mip level.
  126. const ImageMipChainAsset* mipChainAsset = GetImageMipChainAsset(mipLevel);
  127. if (mipChainAsset)
  128. {
  129. auto mipChainIndex = GetMipChainIndex(mipLevel);
  130. auto mipChainOffset = aznumeric_cast<AZ::u32>(GetMipLevel(mipChainIndex));
  131. auto layout = mipChainAsset->GetSubImageLayout(mipLevel - mipChainOffset);
  132. imageDescriptor.m_size = layout.m_size;
  133. }
  134. else
  135. {
  136. AZ_Warning("Streaming Image", false, "Mip level index (%u) out of bounds, only %u levels available for asset %s",
  137. mipLevel, imageDescriptor.m_mipLevels, m_assetId.ToString<AZStd::string>().c_str());
  138. return RHI::ImageDescriptor();
  139. }
  140. return imageDescriptor;
  141. }
  142. const StreamingImageAsset::TagList& StreamingImageAsset::GetTags() const
  143. {
  144. return m_tags;
  145. }
  146. void StreamingImageAsset::RemoveFrontMipchains(size_t mipChainLevel)
  147. {
  148. mipChainLevel = AZStd::min(mipChainLevel, m_mipChains.size() - 1);
  149. if (mipChainLevel == 0)
  150. {
  151. return;
  152. }
  153. AZ::u16 mipmapShift = m_mipChains[mipChainLevel].m_mipOffset;
  154. AZStd::move(m_mipLevelToChainIndex.begin() + mipmapShift, m_mipLevelToChainIndex.end(), m_mipLevelToChainIndex.begin());
  155. AZ_Assert(m_mipLevelToChainIndex.front() == mipChainLevel, "unmatching mipchain index");
  156. for (AZ::u16& chainIndex : m_mipLevelToChainIndex)
  157. {
  158. chainIndex -= aznumeric_cast<AZ::u16>(mipChainLevel);
  159. }
  160. AZStd::move(m_mipChains.begin() + mipChainLevel, m_mipChains.end(), m_mipChains.begin());
  161. m_mipChains.resize(m_mipChains.size() - mipChainLevel);
  162. for (auto& mipChain : m_mipChains)
  163. {
  164. // Assert that the offset does not become negative after subtraction:
  165. AZ_Assert(mipChain.m_mipOffset >= mipmapShift, "unexpected mipoffset");
  166. mipChain.m_mipOffset -= mipmapShift;
  167. }
  168. m_imageDescriptor.m_mipLevels -= mipmapShift;
  169. m_imageDescriptor.m_size = m_imageDescriptor.m_size.GetReducedMip(mipmapShift);
  170. }
  171. AZStd::span<const uint8_t> StreamingImageAsset::GetSubImageData(uint32_t mip, uint32_t slice)
  172. {
  173. auto mipChainIndex = GetMipChainIndex(mip);
  174. const ImageMipChainAsset* mipChainAsset = GetImageMipChainAsset(mip);
  175. if (mipChainAsset == nullptr)
  176. {
  177. MipChain& mipChain = m_mipChains[mipChainIndex];
  178. if (mipChain.m_asset.QueueLoad())
  179. {
  180. mipChain.m_asset.BlockUntilLoadComplete();
  181. }
  182. mipChainAsset = GetImageMipChainAsset(mip);
  183. }
  184. if (mipChainAsset == nullptr)
  185. {
  186. AZ_Warning("Streaming Image", false, "MipChain asset wasn't loaded for assetId %s",
  187. m_assetId.ToString<AZStd::string>().c_str());
  188. return AZStd::span<const uint8_t>();
  189. }
  190. auto mipChainOffset = aznumeric_cast<AZ::u32>(GetMipLevel(mipChainIndex));
  191. return mipChainAsset->GetSubImageData(mip - mipChainOffset, slice);
  192. }
  193. const ImageMipChainAsset* StreamingImageAsset::GetImageMipChainAsset(AZ::u32 mipLevel) const
  194. {
  195. if (mipLevel >= m_mipLevelToChainIndex.size())
  196. {
  197. return nullptr;
  198. }
  199. size_t mipChainIndex = m_mipLevelToChainIndex[mipLevel];
  200. const MipChain& mipChain = m_mipChains[mipChainIndex];
  201. const ImageMipChainAsset* mipChainAsset = nullptr;
  202. // Use m_tailMipChain if it's the last mip chain
  203. if (mipChainIndex == m_mipChains.size() - 1)
  204. {
  205. mipChainAsset = &m_tailMipChain;
  206. }
  207. else if (mipChain.m_asset.IsReady())
  208. {
  209. mipChainAsset = mipChain.m_asset.Get();
  210. }
  211. return mipChainAsset;
  212. }
  213. bool StreamingImageAsset::HasFullMipChainAssets() const
  214. {
  215. for (const auto& mipChain : m_mipChains)
  216. {
  217. if (mipChain.m_asset.GetId().IsValid() && !mipChain.m_asset.GetData())
  218. {
  219. // if the asset id is valid but the asset doesn't contain asset data, return false
  220. return false;
  221. }
  222. }
  223. return true;
  224. }
  225. }
  226. }