StreamingImageController.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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.Public/Image/StreamingImageController.h>
  9. #include <Atom/RPI.Public/Image/StreamingImageContext.h>
  10. #include <Atom/RPI.Public/Image/StreamingImage.h>
  11. #include <AzCore/Jobs/Job.h>
  12. #include <AzCore/Time/ITime.h>
  13. ATOM_RPI_PUBLIC_API AZ_DECLARE_BUDGET(RPI);
  14. #define ENABLE_STREAMING_DEBUG_TRACE 0
  15. namespace AZ
  16. {
  17. namespace RPI
  18. {
  19. #if ENABLE_STREAMING_DEBUG_TRACE
  20. #define StreamingDebugOutput(window, ...) AZ_TracePrintf(window, __VA_ARGS__);
  21. #else
  22. #define StreamingDebugOutput(window, ...)
  23. #endif
  24. AZStd::unique_ptr<StreamingImageController> StreamingImageController::Create(RHI::StreamingImagePool& pool)
  25. {
  26. AZStd::unique_ptr<StreamingImageController> controller = AZStd::make_unique<StreamingImageController>();
  27. controller->m_pool = &pool;
  28. controller->m_pool->SetLowMemoryCallback(AZStd::bind(&StreamingImageController::ReleaseMemory, controller.get(), AZStd::placeholders::_1));
  29. return controller;
  30. }
  31. void StreamingImageController::AttachImage(StreamingImage* image)
  32. {
  33. AZ_PROFILE_FUNCTION(RPI);
  34. AZ_Assert(image, "Image must not be null");
  35. {
  36. AZStd::lock_guard<AZStd::mutex> lock(m_contextAccessMutex);
  37. StreamingImageContextPtr context = CreateContext();
  38. AZ_Assert(context, "A valid context must be returned from AttachImageInternal.");
  39. m_contexts.push_back(*context);
  40. context->m_streamingImage = image;
  41. image->m_streamingController = this;
  42. image->m_streamingContext = AZStd::move(context);
  43. }
  44. {
  45. AZStd::lock_guard<AZStd::recursive_mutex> lock(m_imageListAccessMutex);
  46. m_streamableImages.insert(image);
  47. }
  48. ReinsertImageToLists(image);
  49. }
  50. void StreamingImageController::DetachImage(StreamingImage* image)
  51. {
  52. AZ_Assert(image, "Image must not be null.");
  53. // Remove image from the list first before clearing the image streaming context
  54. // since the compare functions may use the image's StreamingImageContext
  55. {
  56. AZStd::lock_guard<AZStd::recursive_mutex> lock(m_imageListAccessMutex);
  57. m_streamableImages.erase(image);
  58. m_expandingImages.erase(image);
  59. m_expandableImages.erase(image);
  60. m_evictableImages.erase(image);
  61. }
  62. const StreamingImageContextPtr& context = image->m_streamingContext;
  63. AZ_Assert(context, "Image streaming context must not be null.");
  64. {
  65. AZStd::lock_guard<AZStd::mutex> lock(m_contextAccessMutex);
  66. m_contexts.erase(*context);
  67. }
  68. context->m_queuedForMipExpand = false;
  69. context->m_streamingImage = nullptr;
  70. image->m_streamingController = nullptr;
  71. image->m_streamingContext = nullptr;
  72. }
  73. void StreamingImageController::ReinsertImageToLists(StreamingImage* image, AZStd::optional<size_t> updatedTimestamp)
  74. {
  75. AZStd::lock_guard<AZStd::recursive_mutex> lock(m_imageListAccessMutex);
  76. m_expandableImages.erase(image);
  77. m_evictableImages.erase(image);
  78. if (updatedTimestamp)
  79. {
  80. StreamingImageContext* context = image->m_streamingContext.get();
  81. context->m_lastAccessTimestamp = updatedTimestamp.value();
  82. }
  83. if (!image->IsExpanding())
  84. {
  85. image->m_streamingContext->UpdateMipStats();
  86. if (NeedExpand(image))
  87. {
  88. m_expandableImages.insert(image);
  89. }
  90. if (image->IsTrimmable())
  91. {
  92. m_evictableImages.insert(image);
  93. }
  94. }
  95. }
  96. void StreamingImageController::EndExpandImage(StreamingImage* image)
  97. {
  98. // remove unused mips in case global mip bias was changed during expanding
  99. EvictUnusedMips(image);
  100. image->m_streamingContext->m_queuedForMipExpand = false;
  101. ReinsertImageToLists(image);
  102. }
  103. void StreamingImageController::Update()
  104. {
  105. AZ_PROFILE_FUNCTION(RPI);
  106. // Limit the amount of upload image per update to avoid internal queue being too long
  107. const uint32_t c_jobCount = 30;
  108. uint32_t jobCount = 0;
  109. // if the memory was low, cancel all expanding images
  110. if (m_lastLowMemory)
  111. {
  112. // clear the gpu expand queue
  113. {
  114. AZStd::lock_guard<AZStd::mutex> mipExpandlock(m_mipExpandMutex);
  115. m_mipExpandQueue = AZStd::queue<StreamingImageContextPtr>();
  116. }
  117. // clear the expanding images
  118. {
  119. AZStd::lock_guard<AZStd::recursive_mutex> imageListAccesslock(m_imageListAccessMutex);
  120. for (auto image:m_expandingImages)
  121. {
  122. image->CancelExpanding();
  123. EndExpandImage(image);
  124. }
  125. m_expandingImages.clear();
  126. }
  127. }
  128. // Finalize the mip expansion events generated from the controller. This is done once per update. Anytime
  129. // a new mip chain asset is ready, the streaming image will notify the controller, which will then queue
  130. // the request.
  131. {
  132. AZStd::lock_guard<AZStd::mutex> mipExpandlock(m_mipExpandMutex);
  133. while (m_mipExpandQueue.size() > 0)
  134. {
  135. StreamingImageContextPtr context = m_mipExpandQueue.front();
  136. if (context->m_queuedForMipExpand)
  137. {
  138. if (StreamingImage* image = context->TryGetImage())
  139. {
  140. StreamingDebugOutput("StreamingImageController", "ExpandMipChain for [%s]\n", image->GetRHIImage()->GetName().GetCStr());
  141. [[maybe_unused]] const RHI::ResultCode resultCode = image->ExpandMipChain();
  142. AZ_Warning("StreamingImageController", resultCode == RHI::ResultCode::Success, "Failed to expand mip chain for streaming image.");
  143. if (!image->IsExpanding())
  144. {
  145. EndExpandImage(image);
  146. m_expandingImages.erase(image);
  147. StreamingDebugOutput("StreamingImageController", "Image [%s] expanded mip level to %d\n",
  148. image->GetRHIImage()->GetName().GetCStr(), image->m_imageAsset->GetMipChainIndex(image->m_mipChainState.m_residencyTarget));
  149. }
  150. }
  151. }
  152. m_mipExpandQueue.pop();
  153. jobCount++;
  154. if (jobCount >= c_jobCount || m_lastLowMemory > 0)
  155. {
  156. break;
  157. }
  158. }
  159. }
  160. // reset low memory state if the memory is dropping since last low memory state
  161. if (m_lastLowMemory > GetPoolMemoryUsage())
  162. {
  163. m_lastLowMemory = 0;
  164. }
  165. // Try to expand if it's not in low memory state
  166. jobCount = 0;
  167. while (jobCount < c_jobCount && m_lastLowMemory == 0)
  168. {
  169. if (ExpandOneMipChain())
  170. {
  171. jobCount++;
  172. }
  173. else
  174. {
  175. break;
  176. }
  177. }
  178. ++m_timestamp;
  179. }
  180. size_t StreamingImageController::GetTimestamp() const
  181. {
  182. return m_timestamp;
  183. }
  184. void StreamingImageController::OnSetTargetMip(StreamingImage* image, uint16_t mipLevelTarget)
  185. {
  186. StreamingImageContext* context = image->m_streamingContext.get();
  187. context->m_mipLevelTarget = mipLevelTarget;
  188. // update image priority and re-insert the image
  189. if (!context->m_queuedForMipExpand)
  190. {
  191. EvictUnusedMips(image);
  192. }
  193. // reinsert the image since the priority might be changed after mip target changed
  194. ReinsertImageToLists(image, m_timestamp);
  195. }
  196. bool StreamingImageController::ExpandPriorityComparator::operator()(const StreamingImage* lhs, const StreamingImage* rhs) const
  197. {
  198. // use the resident mip size and missing mip count to decide the expand priority
  199. auto lhsMipSize = lhs->m_streamingContext->m_residentMipSize;
  200. auto rhsMipSize = rhs->m_streamingContext->m_residentMipSize;
  201. if (lhsMipSize == rhsMipSize)
  202. {
  203. auto lhsMissingMips = lhs->m_streamingContext->m_missingMips;
  204. auto rhsMissingMips = rhs->m_streamingContext->m_missingMips;
  205. if (lhsMissingMips == rhsMissingMips)
  206. {
  207. auto lhsTimestamp = lhs->m_streamingContext->GetLastAccessTimestamp();
  208. auto rhsTimestamp = rhs->m_streamingContext->GetLastAccessTimestamp();
  209. if (lhsTimestamp == rhsTimestamp)
  210. {
  211. // we need this to avoid same key in the AZStd::set
  212. return lhs < rhs;
  213. }
  214. // latest accessed image has higher priority
  215. return lhsTimestamp > rhsTimestamp;
  216. }
  217. else
  218. {
  219. // image with more missing mips has higher priority
  220. return lhsMissingMips > rhsMissingMips;
  221. }
  222. }
  223. // image has smaller resolution has higher priority
  224. return lhsMipSize < rhsMipSize;
  225. }
  226. bool StreamingImageController::EvictPriorityComparator::operator()(const StreamingImage* lhs, const StreamingImage* rhs) const
  227. {
  228. auto lhsEvictableMips = lhs->m_streamingContext->m_evictableMips;
  229. auto rhsEvictableMips = rhs->m_streamingContext->m_evictableMips;
  230. if (lhsEvictableMips == rhsEvictableMips)
  231. {
  232. auto lhsTimestamp = lhs->m_streamingContext->GetLastAccessTimestamp();
  233. auto rhsTimestamp = rhs->m_streamingContext->GetLastAccessTimestamp();
  234. if (lhsTimestamp == rhsTimestamp)
  235. {
  236. // we need this to avoid same key in the AZStd::set
  237. return lhs < rhs;
  238. }
  239. // Last visited image will be evicted later
  240. return lhsTimestamp < rhsTimestamp;
  241. }
  242. // images with higher evictable mip count will be evict first
  243. return lhsEvictableMips > rhsEvictableMips;
  244. }
  245. void StreamingImageController::OnMipChainAssetReady(StreamingImage* image)
  246. {
  247. StreamingImageContext* context = image->m_streamingContext.get();
  248. const bool queuedForMipExpand = context->m_queuedForMipExpand.exchange(true);
  249. // If the image was already queued for expand, it will take care of all mip chain assets that are ready.
  250. // So it's unnecessary to queue again.
  251. if (!queuedForMipExpand)
  252. {
  253. AZStd::lock_guard<AZStd::mutex> lock(m_mipExpandMutex);
  254. m_mipExpandQueue.push(context);
  255. }
  256. }
  257. uint32_t StreamingImageController::GetStreamableImageCount() const
  258. {
  259. return aznumeric_cast<uint32_t>(m_streamableImages.size());
  260. }
  261. uint32_t StreamingImageController::GetExpandingImageCount() const
  262. {
  263. return aznumeric_cast<uint32_t>(m_expandingImages.size());
  264. }
  265. void StreamingImageController::SetMipBias(int16_t mipBias)
  266. {
  267. if (m_globalMipBias == mipBias)
  268. {
  269. return;
  270. }
  271. m_globalMipBias = mipBias;
  272. // we need go through all the streamable image to update their streaming context and regenerate the lists
  273. AZStd::lock_guard<AZStd::recursive_mutex> lock(m_imageListAccessMutex);
  274. m_expandableImages.clear();
  275. m_evictableImages.clear();
  276. for (auto image : m_streamableImages)
  277. {
  278. EvictUnusedMips(image);
  279. image->m_streamingContext->UpdateMipStats();
  280. if (!image->IsExpanding())
  281. {
  282. if (NeedExpand(image))
  283. {
  284. m_expandableImages.insert(image);
  285. }
  286. if (image->IsTrimmable())
  287. {
  288. m_evictableImages.insert(image);
  289. }
  290. }
  291. }
  292. }
  293. int16_t StreamingImageController::GetMipBias() const
  294. {
  295. return m_globalMipBias;
  296. }
  297. StreamingImageContextPtr StreamingImageController::CreateContext()
  298. {
  299. return aznew StreamingImageContext();
  300. }
  301. void StreamingImageController::ResetLowMemoryState()
  302. {
  303. m_lastLowMemory = 0;
  304. }
  305. bool StreamingImageController::EvictOneMipChain()
  306. {
  307. AZStd::lock_guard<AZStd::recursive_mutex> lock(m_imageListAccessMutex);
  308. for (auto itr = m_evictableImages.begin(); itr != m_evictableImages.end(); itr++)
  309. {
  310. StreamingImage* image = *itr;
  311. RHI::ResultCode success = image->TrimOneMipChain();
  312. if (success == RHI::ResultCode::Success)
  313. {
  314. // update the image's priority and re-insert the image
  315. ReinsertImageToLists(image);
  316. StreamingDebugOutput(
  317. "StreamingImageController",
  318. "Image [%s] has one mipchain released; Current resident mip: %d\n",
  319. image->GetRHIImage()->GetName().GetCStr(),
  320. image->GetRHIImage()->GetResidentMipLevel());
  321. return true;
  322. }
  323. else
  324. {
  325. AZ_Assert(false, "failed to evict an evictable image!");
  326. }
  327. }
  328. return false;
  329. }
  330. bool StreamingImageController::NeedExpand(const StreamingImage* image) const
  331. {
  332. uint16_t targetMip = GetImageTargetMip(image);
  333. // only need expand if the current expanding target is smaller than final target
  334. return image->m_mipChainState.m_streamingTarget > image->m_imageAsset->GetMipChainIndex(targetMip);
  335. }
  336. bool StreamingImageController::ExpandOneMipChain()
  337. {
  338. AZStd::lock_guard<AZStd::recursive_mutex> lock(m_imageListAccessMutex);
  339. if (m_expandableImages.size() == 0)
  340. {
  341. return false;
  342. }
  343. auto itr = m_expandableImages.begin();
  344. StreamingImage* image = *itr;
  345. image->QueueExpandToNextMipChainLevel();
  346. if (image->IsExpanding())
  347. {
  348. StreamingDebugOutput("StreamingImageController", "Image [%s] is expanding mip level to %d\n",
  349. image->GetRHIImage()->GetName().GetCStr(), image->m_imageAsset->GetMipChainIndex(image->m_mipChainState.m_streamingTarget));
  350. m_expandingImages.insert(image);
  351. ReinsertImageToLists(image);
  352. }
  353. return true;
  354. }
  355. uint16_t StreamingImageController::GetImageTargetMip(const StreamingImage* image) const
  356. {
  357. int16_t targetMip = image->m_streamingContext->GetTargetMip() + m_globalMipBias;
  358. targetMip = AZStd::clamp(targetMip, (int16_t)0, aznumeric_cast<int16_t>(image->GetRHIImage()->GetDescriptor().m_mipLevels-1));
  359. return aznumeric_cast<uint16_t>(targetMip);
  360. }
  361. bool StreamingImageController::IsMemoryLow() const
  362. {
  363. return m_lastLowMemory != 0;
  364. }
  365. bool StreamingImageController::EvictUnusedMips(StreamingImage* image)
  366. {
  367. uint16_t targetMip = GetImageTargetMip(image);
  368. size_t targetMipChain = image->m_imageAsset->GetMipChainIndex(targetMip);
  369. if (image->m_mipChainState.m_streamingTarget < targetMipChain)
  370. {
  371. RHI::ResultCode success = image->TrimToMipChainLevel(targetMipChain);
  372. StreamingDebugOutput("StreamingImageController", "Image [%s] mip level was evicted to %d\n",
  373. image->GetRHIImage()->GetName().GetCStr(), image->GetResidentMipLevel());
  374. return success == RHI::ResultCode::Success;
  375. }
  376. return true;
  377. }
  378. bool StreamingImageController::ReleaseMemory(size_t targetMemoryUsage)
  379. {
  380. StreamingDebugOutput("StreamingImageController", "Handle low memory\n");
  381. size_t currentResident = GetPoolMemoryUsage();
  382. while (currentResident > targetMemoryUsage)
  383. {
  384. // Evict some mips
  385. bool evicted = EvictOneMipChain();
  386. if (!evicted)
  387. {
  388. // nothing to be evicted anymore
  389. m_lastLowMemory = currentResident;
  390. return false;
  391. }
  392. currentResident = GetPoolMemoryUsage();
  393. }
  394. m_lastLowMemory = currentResident;
  395. return true;
  396. }
  397. size_t StreamingImageController::GetPoolMemoryUsage()
  398. {
  399. size_t totalResident = m_pool->GetHeapMemoryUsage(RHI::HeapMemoryLevel::Device).m_usedResidentInBytes.load();
  400. return totalResident;
  401. }
  402. }
  403. }