StreamingImageController.cpp 18 KB

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