DecalTextureArray.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 "DecalTextureArray.h"
  9. #include <Atom/RPI.Public/Image/ImageSystemInterface.h>
  10. #include <Atom/RPI.Reflect/Image/StreamingImageAssetCreator.h>
  11. #include <Atom/RPI.Public/Image/StreamingImagePool.h>
  12. #include <Atom/RPI.Reflect/Image/ImageMipChainAsset.h>
  13. #include <Atom/RPI.Reflect/Image/ImageMipChainAssetCreator.h>
  14. #include <Atom/RPI.Reflect/Image/StreamingImageAssetHandler.h>
  15. #include <Atom/RPI.Public/Image/StreamingImage.h>
  16. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  17. #include <Atom/RPI.Public/Material/Material.h>
  18. #include <Atom/RPI.Reflect/Image/StreamingImageAsset.h>
  19. #include <AzCore/Name/NameDictionary.h>
  20. namespace AZ
  21. {
  22. namespace Render
  23. {
  24. namespace
  25. {
  26. static const AZ::Name& GetMapName(const DecalMapType mapType)
  27. {
  28. switch (mapType)
  29. {
  30. case DecalMapType_Diffuse:
  31. return AZ_NAME_LITERAL("baseColor.textureMap");
  32. case DecalMapType_Normal:
  33. return AZ_NAME_LITERAL("normal.textureMap");
  34. default:
  35. return AZ_NAME_LITERAL("");
  36. }
  37. }
  38. static AZ::Data::AssetId GetImagePoolId()
  39. {
  40. const Data::Instance<RPI::StreamingImagePool>& imagePool = RPI::ImageSystemInterface::Get()->GetSystemStreamingPool();
  41. return imagePool->GetAssetId();
  42. }
  43. static AZ::Data::Asset<AZ::RPI::MaterialAsset> QueueLoad(const AZ::Data::AssetId id)
  44. {
  45. auto asset = AZ::Data::AssetManager::Instance().GetAsset<AZ::RPI::MaterialAsset>(
  46. id, AZ::Data::AssetLoadBehavior::QueueLoad);
  47. return asset;
  48. }
  49. // Extract exactly which texture asset we need to load from the given material and map type (diffuse, normal, etc).
  50. static AZ::Data::Asset<AZ::RPI::StreamingImageAsset> GetStreamingImageAsset(AZ::RPI::MaterialAsset& materialAsset, const AZ::Name& propertyName)
  51. {
  52. if (!materialAsset.IsReady())
  53. {
  54. AZ_Warning("DecalTextureArray", false, "GetStreamingImageAsset() called with material property: %s, was passed a MaterialAsset that was not ready for use", propertyName.GetCStr());
  55. return {};
  56. }
  57. const AZ::RPI::MaterialPropertiesLayout* materialLayout = materialAsset.GetMaterialPropertiesLayout();
  58. const AZ::RPI::MaterialPropertyIndex propertyIndex = materialLayout->FindPropertyIndex(propertyName);
  59. if (propertyIndex.IsNull())
  60. {
  61. AZ_Warning("DecalTextureArray", false, "Unable to find material property with the name: %s", propertyName.GetCStr());
  62. return {};
  63. }
  64. const auto& propertyValues = materialAsset.GetPropertyValues();
  65. const AZ::RPI::MaterialPropertyValue& propertyValue = propertyValues[propertyIndex.GetIndex()];
  66. auto imageAsset = propertyValue.GetValue<Data::Asset<RPI::ImageAsset>>();
  67. const auto& assetId = imageAsset.GetId();
  68. if (assetId.IsValid())
  69. {
  70. imageAsset.QueueLoad();
  71. // [GFX TODO][ATOM-14271] - DecalTextureArrayFeatureProcessor should use async loading
  72. imageAsset.BlockUntilLoadComplete();
  73. }
  74. else
  75. {
  76. AZ_Warning("DecalTextureArray", false, "Material property: %s does not have a valid asset Id", propertyName.GetCStr());
  77. return {};
  78. }
  79. return Data::static_pointer_cast<AZ::RPI::StreamingImageAsset>(imageAsset);
  80. }
  81. static AZ::Data::Asset<AZ::RPI::StreamingImageAsset> GetStreamingImageAsset(const AZ::Data::Asset<Data::AssetData> materialAssetData, const AZ::Name& propertyName)
  82. {
  83. AZ_Assert(materialAssetData->IsReady(), "GetStreamingImageAsset() called with AssetData that is not ready.");
  84. AZ::RPI::MaterialAsset* materialAsset = materialAssetData.GetAs<AZ::RPI::MaterialAsset>();
  85. return GetStreamingImageAsset(*materialAsset, propertyName);
  86. }
  87. }
  88. int DecalTextureArray::FindMaterial(const AZ::Data::AssetId materialAssetId) const
  89. {
  90. int iter = m_materials.begin();
  91. while (iter != -1)
  92. {
  93. if (m_materials[iter].m_materialAssetId == materialAssetId)
  94. {
  95. return iter;
  96. }
  97. iter = m_materials.next(iter);
  98. }
  99. return -1;
  100. }
  101. int DecalTextureArray::AddMaterial(const AZ::Data::AssetId materialAssetId)
  102. {
  103. AZ_Error("DecalTextureArray", FindMaterial(materialAssetId) == -1, "Adding material when it already exists in the array");
  104. // Invalidate the existing texture array, as we need to repack it taking into account the new material.
  105. AZStd::fill(m_textureArrayPacked.begin(), m_textureArrayPacked.end(), nullptr);
  106. MaterialData materialData;
  107. materialData.m_materialAssetId = materialAssetId;
  108. int iter = m_materials.push_front(materialData);
  109. return iter;
  110. }
  111. void DecalTextureArray::RemoveMaterial(const int index)
  112. {
  113. m_materials[index] = {};
  114. m_materials.erase(index);
  115. }
  116. AZ::Data::AssetId DecalTextureArray::GetMaterialAssetId(const int index) const
  117. {
  118. return m_materials[index].m_materialAssetId;
  119. }
  120. RHI::Size DecalTextureArray::GetImageDimensions(const DecalMapType mapType) const
  121. {
  122. AZ_Assert(m_materials.size() > 0, "GetImageDimensions() cannot be called until at least one material has been added");
  123. const int iter = m_materials.begin();
  124. // All textures in a texture array must have the same size, so just pick the first
  125. const MaterialData& firstMaterial = m_materials[iter];
  126. const auto& baseColorAsset = GetStreamingImageAsset(firstMaterial.m_materialAssetData, GetMapName(mapType));
  127. return baseColorAsset->GetImageDescriptor().m_size;
  128. }
  129. const AZ::Data::Instance<AZ::RPI::StreamingImage>& DecalTextureArray::GetPackedTexture(const DecalMapType mapType) const
  130. {
  131. return m_textureArrayPacked[mapType];
  132. }
  133. bool DecalTextureArray::IsValidDecalMaterial(AZ::RPI::MaterialAsset& materialAsset)
  134. {
  135. return GetStreamingImageAsset(materialAsset, GetMapName(DecalMapType_Diffuse)).IsReady();
  136. }
  137. AZ::Data::Asset<AZ::RPI::ImageMipChainAsset> DecalTextureArray::BuildPackedMipChainAsset(const DecalMapType mapType, const size_t numTexturesToCreate)
  138. {
  139. RPI::ImageMipChainAssetCreator assetCreator;
  140. const uint32_t mipLevels = GetNumMipLevels(mapType);
  141. assetCreator.Begin(Data::AssetId(AZ::Uuid::CreateRandom()), aznumeric_cast<uint16_t>(mipLevels), aznumeric_cast<uint16_t>(numTexturesToCreate));
  142. for (uint32_t mipLevel = 0; mipLevel < mipLevels; ++mipLevel)
  143. {
  144. const auto& layout = GetLayout(mapType, mipLevel);
  145. assetCreator.BeginMip(layout);
  146. for (int i = 0; i < m_materials.array_size(); ++i)
  147. {
  148. const auto imageData = GetRawImageData(GetMapName(mapType), i, mipLevel);
  149. assetCreator.AddSubImage(imageData.data(), imageData.size());
  150. }
  151. assetCreator.EndMip();
  152. }
  153. Data::Asset<RPI::ImageMipChainAsset> asset;
  154. assetCreator.End(asset);
  155. return AZStd::move(asset);
  156. }
  157. RHI::ImageDescriptor DecalTextureArray::CreatePackedImageDescriptor(
  158. const DecalMapType mapType, const uint16_t arraySize, const uint16_t mipLevels) const
  159. {
  160. const RHI::Size imageDimensions = GetImageDimensions(mapType);
  161. RHI::ImageDescriptor imageDescriptor = RHI::ImageDescriptor::Create2DArray(
  162. RHI::ImageBindFlags::ShaderRead, imageDimensions.m_width, imageDimensions.m_height, arraySize, GetFormat(mapType));
  163. imageDescriptor.m_mipLevels = mipLevels;
  164. return imageDescriptor;
  165. }
  166. void DecalTextureArray::Pack()
  167. {
  168. if (!NeedsPacking())
  169. return;
  170. if (!AreAllAssetsReady())
  171. {
  172. QueueAssetLoads();
  173. return;
  174. }
  175. const size_t numTexturesToCreate = m_materials.array_size();
  176. for (int i = 0; i < DecalMapType_Num; ++i)
  177. {
  178. const DecalMapType mapType = aznumeric_cast<DecalMapType>(i);
  179. if (!AreAllTextureMapsPresent(mapType))
  180. {
  181. AZ_Warning("DecalTextureArray", false, "Missing decal texture maps for %s. Please make sure all maps of this type are present.\n", GetMapName(mapType).GetCStr());
  182. m_textureArrayPacked[i] = nullptr;
  183. continue;
  184. }
  185. const auto mipChainAsset = BuildPackedMipChainAsset(mapType, numTexturesToCreate);
  186. RHI::ImageViewDescriptor imageViewDescriptor;
  187. imageViewDescriptor.m_isArray = true;
  188. RPI::StreamingImageAssetCreator assetCreator;
  189. assetCreator.Begin(Data::AssetId(Uuid::CreateRandom()));
  190. assetCreator.SetPoolAssetId(GetImagePoolId());
  191. assetCreator.SetFlags(RPI::StreamingImageFlags::None);
  192. assetCreator.SetImageDescriptor(
  193. CreatePackedImageDescriptor(mapType, aznumeric_cast<uint16_t>(numTexturesToCreate), GetNumMipLevels(mapType)));
  194. assetCreator.SetImageViewDescriptor(imageViewDescriptor);
  195. assetCreator.AddMipChainAsset(*mipChainAsset);
  196. Data::Asset<RPI::StreamingImageAsset> packedAsset;
  197. const bool createdOk = assetCreator.End(packedAsset);
  198. AZ_Error("TextureArrayData", createdOk, "Pack() call failed.");
  199. m_textureArrayPacked[i] = createdOk ? RPI::StreamingImage::FindOrCreate(packedAsset) : nullptr;
  200. }
  201. // Free unused memory
  202. ClearAssets();
  203. }
  204. size_t DecalTextureArray::NumMaterials() const
  205. {
  206. return m_materials.size();
  207. }
  208. void DecalTextureArray::OnAssetReady(Data::Asset<Data::AssetData> asset)
  209. {
  210. AZ::Data::AssetBus::MultiHandler::BusDisconnect(asset.GetId());
  211. m_assetsCurrentlyLoading.erase(asset.GetId());
  212. if (m_assetsCurrentlyLoading.empty())
  213. {
  214. Pack();
  215. }
  216. }
  217. uint16_t DecalTextureArray::GetNumMipLevels(const DecalMapType mapType) const
  218. {
  219. AZ_Assert(m_materials.size() > 0, "GetNumMipLevels() cannot be called until at least one material has been added");
  220. // All decals in a texture array must have the same number of mips, so just pick the first
  221. const int iter = m_materials.begin();
  222. const MaterialData& firstMaterial = m_materials[iter];
  223. const auto& imageAsset = GetStreamingImageAsset(firstMaterial.m_materialAssetData, GetMapName(mapType));
  224. return imageAsset->GetImageDescriptor().m_mipLevels;
  225. }
  226. RHI::ImageSubresourceLayout DecalTextureArray::GetLayout(const DecalMapType mapType, int mip) const
  227. {
  228. AZ_Assert(m_materials.size() > 0, "GetLayout() cannot be called unless at least one material has been added");
  229. const int iter = m_materials.begin();
  230. const auto& descriptor =
  231. GetStreamingImageAsset(m_materials[iter].m_materialAssetData, GetMapName(mapType))->GetImageDescriptor();
  232. RHI::Size mipSize = descriptor.m_size;
  233. mipSize.m_width >>= mip;
  234. mipSize.m_height >>= mip;
  235. return AZ::RHI::GetImageSubresourceLayout(mipSize, descriptor.m_format);
  236. }
  237. AZStd::span<const uint8_t> DecalTextureArray::GetRawImageData(const AZ::Name& mapName, int arrayLevel, const int mip) const
  238. {
  239. // We always want to provide valid data to the AssetCreator for each texture.
  240. // If this spot in the array is empty, just provide some random image as filler.
  241. // (No decals will be indexing this spot anyway)
  242. const bool isEmptySlot = !m_materials[arrayLevel].m_materialAssetData.GetId().IsValid();
  243. if (isEmptySlot)
  244. {
  245. arrayLevel = m_materials.begin();
  246. }
  247. const auto image = GetStreamingImageAsset(m_materials[arrayLevel].m_materialAssetData, mapName);
  248. if (!image)
  249. {
  250. return {};
  251. }
  252. AZ_Assert(
  253. mip < image->GetImageDescriptor().m_mipLevels,
  254. "It is expected that all decals in a texture array must have the same number of mips which may not be the case here. "
  255. "Please ensure that all the materials within m_materials are pointing to textures with same mips.");
  256. const auto srcData = image->GetSubImageData(mip, 0);
  257. return srcData;
  258. }
  259. AZ::RHI::Format DecalTextureArray::GetFormat(const DecalMapType mapType) const
  260. {
  261. AZ_Assert(m_materials.size() > 0, "GetFormat() can only be called after at least one material has been added.");
  262. const int iter = m_materials.begin();
  263. const auto& baseColorAsset = GetStreamingImageAsset(m_materials[iter].m_materialAssetData, GetMapName(mapType));
  264. return baseColorAsset->GetImageDescriptor().m_format;
  265. }
  266. bool DecalTextureArray::AreAllAssetsReady() const
  267. {
  268. int iter = m_materials.begin();
  269. while (iter != -1)
  270. {
  271. if (!IsAssetReady(m_materials[iter]))
  272. return false;
  273. iter = m_materials.next(iter);
  274. }
  275. return true;
  276. }
  277. bool DecalTextureArray::IsAssetReady(const MaterialData& materialData) const
  278. {
  279. const auto& id = materialData.m_materialAssetData.GetId();
  280. return id.IsValid() && materialData.m_materialAssetData.IsReady();
  281. }
  282. bool DecalTextureArray::AreAllTextureMapsPresent(const DecalMapType mapType) const
  283. {
  284. int iter = m_materials.begin();
  285. while (iter != -1)
  286. {
  287. if (!IsTextureMapPresentInMaterial(m_materials[iter], mapType))
  288. {
  289. return false;
  290. }
  291. iter = m_materials.next(iter);
  292. }
  293. return true;
  294. }
  295. bool DecalTextureArray::IsTextureMapPresentInMaterial(const MaterialData& materialData, const DecalMapType mapType) const
  296. {
  297. return GetStreamingImageAsset(materialData.m_materialAssetData, GetMapName(mapType)).IsReady();
  298. }
  299. void DecalTextureArray::ClearAssets()
  300. {
  301. int iter = m_materials.begin();
  302. while (iter != -1)
  303. {
  304. ClearAsset(m_materials[iter]);
  305. iter = m_materials.next(iter);
  306. }
  307. }
  308. void DecalTextureArray::ClearAsset(MaterialData& materialData)
  309. {
  310. materialData.m_materialAssetData = {};
  311. }
  312. void DecalTextureArray::QueueAssetLoad(MaterialData& materialData)
  313. {
  314. if (materialData.m_materialAssetData.IsReady())
  315. return;
  316. m_assetsCurrentlyLoading.emplace(materialData.m_materialAssetId);
  317. materialData.m_materialAssetData = QueueLoad(materialData.m_materialAssetId);
  318. AZ::Data::AssetBus::MultiHandler::BusConnect(materialData.m_materialAssetId);
  319. }
  320. void DecalTextureArray::QueueAssetLoads()
  321. {
  322. int iter = m_materials.begin();
  323. while (iter != -1)
  324. {
  325. QueueAssetLoad(m_materials[iter]);
  326. iter = m_materials.next(iter);
  327. }
  328. }
  329. bool DecalTextureArray::NeedsPacking() const
  330. {
  331. if (m_materials.size() == 0)
  332. return false;
  333. // We pack all diffuse/normal/etc in one go, so just check to see if the diffusemaps need packing
  334. return m_textureArrayPacked[DecalMapType_Diffuse] == nullptr;
  335. }
  336. }
  337. } // namespace AZ