texture_cache.h 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  1. // SPDX-FileCopyrightText: 2021 yuzu emulator team
  2. // (https://github.com/skyline-emu/)
  3. // SPDX-License-Identifier: GPL-3.0-or-later Licensed under GPLv3
  4. // or any later version Refer to the license.txt file included.
  5. #pragma once
  6. #include <unordered_set>
  7. #include "common/alignment.h"
  8. #include "common/settings.h"
  9. #include "video_core/control/channel_state.h"
  10. #include "video_core/dirty_flags.h"
  11. #include "video_core/engines/kepler_compute.h"
  12. #include "video_core/texture_cache/image_view_base.h"
  13. #include "video_core/texture_cache/samples_helper.h"
  14. #include "video_core/texture_cache/texture_cache_base.h"
  15. #include "video_core/texture_cache/util.h"
  16. namespace VideoCommon {
  17. using Tegra::Texture::SwizzleSource;
  18. using Tegra::Texture::TextureType;
  19. using Tegra::Texture::TICEntry;
  20. using Tegra::Texture::TSCEntry;
  21. using VideoCore::Surface::GetFormatType;
  22. using VideoCore::Surface::IsCopyCompatible;
  23. using VideoCore::Surface::PixelFormat;
  24. using VideoCore::Surface::PixelFormatFromDepthFormat;
  25. using VideoCore::Surface::PixelFormatFromRenderTargetFormat;
  26. using VideoCore::Surface::SurfaceType;
  27. using namespace Common::Literals;
  28. template <class P>
  29. TextureCache<P>::TextureCache(Runtime& runtime_, VideoCore::RasterizerInterface& rasterizer_)
  30. : runtime{runtime_}, rasterizer{rasterizer_} {
  31. // Configure null sampler
  32. TSCEntry sampler_descriptor{};
  33. sampler_descriptor.min_filter.Assign(Tegra::Texture::TextureFilter::Linear);
  34. sampler_descriptor.mag_filter.Assign(Tegra::Texture::TextureFilter::Linear);
  35. sampler_descriptor.mipmap_filter.Assign(Tegra::Texture::TextureMipmapFilter::Linear);
  36. sampler_descriptor.cubemap_anisotropy.Assign(1);
  37. // Make sure the first index is reserved for the null resources
  38. // This way the null resource becomes a compile time constant
  39. void(slot_images.insert(NullImageParams{}));
  40. void(slot_image_views.insert(runtime, NullImageViewParams{}));
  41. void(slot_samplers.insert(runtime, sampler_descriptor));
  42. if constexpr (HAS_DEVICE_MEMORY_INFO) {
  43. const s64 device_memory = static_cast<s64>(runtime.GetDeviceLocalMemory());
  44. const s64 min_spacing_expected = device_memory - 1_GiB - 512_MiB;
  45. const s64 min_spacing_critical = device_memory - 1_GiB;
  46. const s64 mem_threshold = std::min(device_memory, TARGET_THRESHOLD);
  47. const s64 min_vacancy_expected = (6 * mem_threshold) / 10;
  48. const s64 min_vacancy_critical = (3 * mem_threshold) / 10;
  49. expected_memory = static_cast<u64>(
  50. std::max(std::min(device_memory - min_vacancy_expected, min_spacing_expected),
  51. DEFAULT_EXPECTED_MEMORY));
  52. critical_memory = static_cast<u64>(
  53. std::max(std::min(device_memory - min_vacancy_critical, min_spacing_critical),
  54. DEFAULT_CRITICAL_MEMORY));
  55. minimum_memory = static_cast<u64>((device_memory - mem_threshold) / 2);
  56. } else {
  57. expected_memory = DEFAULT_EXPECTED_MEMORY + 512_MiB;
  58. critical_memory = DEFAULT_CRITICAL_MEMORY + 1_GiB;
  59. minimum_memory = 0;
  60. }
  61. }
  62. template <class P>
  63. void TextureCache<P>::RunGarbageCollector() {
  64. bool high_priority_mode = total_used_memory >= expected_memory;
  65. bool aggressive_mode = total_used_memory >= critical_memory;
  66. const u64 ticks_to_destroy = aggressive_mode ? 10ULL : high_priority_mode ? 25ULL : 50ULL;
  67. size_t num_iterations = aggressive_mode ? 40 : (high_priority_mode ? 20 : 10);
  68. const auto clean_up = [this, &num_iterations, &high_priority_mode,
  69. &aggressive_mode](ImageId image_id) {
  70. if (num_iterations == 0) {
  71. return true;
  72. }
  73. --num_iterations;
  74. auto& image = slot_images[image_id];
  75. const bool must_download =
  76. image.IsSafeDownload() && False(image.flags & ImageFlagBits::BadOverlap);
  77. if (!high_priority_mode &&
  78. (must_download || True(image.flags & ImageFlagBits::CostlyLoad))) {
  79. return false;
  80. }
  81. if (must_download) {
  82. auto map = runtime.DownloadStagingBuffer(image.unswizzled_size_bytes);
  83. const auto copies = FullDownloadCopies(image.info);
  84. image.DownloadMemory(map, copies);
  85. runtime.Finish();
  86. SwizzleImage(*gpu_memory, image.gpu_addr, image.info, copies, map.mapped_span);
  87. }
  88. if (True(image.flags & ImageFlagBits::Tracked)) {
  89. UntrackImage(image, image_id);
  90. }
  91. UnregisterImage(image_id);
  92. DeleteImage(image_id, image.scale_tick > frame_tick + 5);
  93. if (total_used_memory < critical_memory) {
  94. if (aggressive_mode) {
  95. // Sink the aggresiveness.
  96. num_iterations >>= 2;
  97. aggressive_mode = false;
  98. return false;
  99. }
  100. if (high_priority_mode && total_used_memory < expected_memory) {
  101. num_iterations >>= 1;
  102. high_priority_mode = false;
  103. }
  104. }
  105. return false;
  106. };
  107. lru_cache.ForEachItemBelow(frame_tick - ticks_to_destroy, clean_up);
  108. }
  109. template <class P>
  110. void TextureCache<P>::TickFrame() {
  111. // If we can obtain the memory info, use it instead of the estimate.
  112. if (runtime.CanReportMemoryUsage()) {
  113. total_used_memory = runtime.GetDeviceMemoryUsage();
  114. }
  115. if (total_used_memory > minimum_memory) {
  116. RunGarbageCollector();
  117. }
  118. sentenced_images.Tick();
  119. sentenced_framebuffers.Tick();
  120. sentenced_image_view.Tick();
  121. runtime.TickFrame();
  122. critical_gc = 0;
  123. ++frame_tick;
  124. }
  125. template <class P>
  126. const typename P::ImageView& TextureCache<P>::GetImageView(ImageViewId id) const noexcept {
  127. return slot_image_views[id];
  128. }
  129. template <class P>
  130. typename P::ImageView& TextureCache<P>::GetImageView(ImageViewId id) noexcept {
  131. return slot_image_views[id];
  132. }
  133. template <class P>
  134. void TextureCache<P>::MarkModification(ImageId id) noexcept {
  135. MarkModification(slot_images[id]);
  136. }
  137. template <class P>
  138. template <bool has_blacklists>
  139. void TextureCache<P>::FillGraphicsImageViews(std::span<ImageViewInOut> views) {
  140. FillImageViews<has_blacklists>(channel_state->graphics_image_table,
  141. channel_state->graphics_image_view_ids, views);
  142. }
  143. template <class P>
  144. void TextureCache<P>::FillComputeImageViews(std::span<ImageViewInOut> views) {
  145. FillImageViews<true>(channel_state->compute_image_table, channel_state->compute_image_view_ids,
  146. views);
  147. }
  148. template <class P>
  149. typename P::Sampler* TextureCache<P>::GetGraphicsSampler(u32 index) {
  150. if (index > channel_state->graphics_sampler_table.Limit()) {
  151. LOG_DEBUG(HW_GPU, "Invalid sampler index={}", index);
  152. return &slot_samplers[NULL_SAMPLER_ID];
  153. }
  154. const auto [descriptor, is_new] = channel_state->graphics_sampler_table.Read(index);
  155. SamplerId& id = channel_state->graphics_sampler_ids[index];
  156. if (is_new) {
  157. id = FindSampler(descriptor);
  158. }
  159. return &slot_samplers[id];
  160. }
  161. template <class P>
  162. typename P::Sampler* TextureCache<P>::GetComputeSampler(u32 index) {
  163. if (index > channel_state->compute_sampler_table.Limit()) {
  164. LOG_DEBUG(HW_GPU, "Invalid sampler index={}", index);
  165. return &slot_samplers[NULL_SAMPLER_ID];
  166. }
  167. const auto [descriptor, is_new] = channel_state->compute_sampler_table.Read(index);
  168. SamplerId& id = channel_state->compute_sampler_ids[index];
  169. if (is_new) {
  170. id = FindSampler(descriptor);
  171. }
  172. return &slot_samplers[id];
  173. }
  174. template <class P>
  175. void TextureCache<P>::SynchronizeGraphicsDescriptors() {
  176. using SamplerIndex = Tegra::Engines::Maxwell3D::Regs::SamplerIndex;
  177. const bool linked_tsc = maxwell3d->regs.sampler_index == SamplerIndex::ViaHeaderIndex;
  178. const u32 tic_limit = maxwell3d->regs.tic.limit;
  179. const u32 tsc_limit = linked_tsc ? tic_limit : maxwell3d->regs.tsc.limit;
  180. if (channel_state->graphics_sampler_table.Synchornize(maxwell3d->regs.tsc.Address(),
  181. tsc_limit)) {
  182. channel_state->graphics_sampler_ids.resize(tsc_limit + 1, CORRUPT_ID);
  183. }
  184. if (channel_state->graphics_image_table.Synchornize(maxwell3d->regs.tic.Address(), tic_limit)) {
  185. channel_state->graphics_image_view_ids.resize(tic_limit + 1, CORRUPT_ID);
  186. }
  187. }
  188. template <class P>
  189. void TextureCache<P>::SynchronizeComputeDescriptors() {
  190. const bool linked_tsc = kepler_compute->launch_description.linked_tsc;
  191. const u32 tic_limit = kepler_compute->regs.tic.limit;
  192. const u32 tsc_limit = linked_tsc ? tic_limit : kepler_compute->regs.tsc.limit;
  193. const GPUVAddr tsc_gpu_addr = kepler_compute->regs.tsc.Address();
  194. if (channel_state->compute_sampler_table.Synchornize(tsc_gpu_addr, tsc_limit)) {
  195. channel_state->compute_sampler_ids.resize(tsc_limit + 1, CORRUPT_ID);
  196. }
  197. if (channel_state->compute_image_table.Synchornize(kepler_compute->regs.tic.Address(),
  198. tic_limit)) {
  199. channel_state->compute_image_view_ids.resize(tic_limit + 1, CORRUPT_ID);
  200. }
  201. }
  202. template <class P>
  203. bool TextureCache<P>::RescaleRenderTargets(bool is_clear) {
  204. auto& flags = maxwell3d->dirty.flags;
  205. u32 scale_rating = 0;
  206. bool rescaled = false;
  207. std::array<ImageId, NUM_RT> tmp_color_images{};
  208. ImageId tmp_depth_image{};
  209. do {
  210. flags[Dirty::RenderTargets] = false;
  211. has_deleted_images = false;
  212. // Render target control is used on all render targets, so force look ups when this one is
  213. // up
  214. const bool force = flags[Dirty::RenderTargetControl];
  215. flags[Dirty::RenderTargetControl] = false;
  216. scale_rating = 0;
  217. bool any_rescaled = false;
  218. bool can_rescale = true;
  219. const auto check_rescale = [&](ImageViewId view_id, ImageId& id_save) {
  220. if (view_id != NULL_IMAGE_VIEW_ID && view_id != ImageViewId{}) {
  221. const auto& view = slot_image_views[view_id];
  222. const auto image_id = view.image_id;
  223. id_save = image_id;
  224. auto& image = slot_images[image_id];
  225. can_rescale &= ImageCanRescale(image);
  226. any_rescaled |= True(image.flags & ImageFlagBits::Rescaled) ||
  227. GetFormatType(image.info.format) != SurfaceType::ColorTexture;
  228. scale_rating = std::max<u32>(scale_rating, image.scale_tick <= frame_tick
  229. ? image.scale_rating + 1U
  230. : image.scale_rating);
  231. } else {
  232. id_save = CORRUPT_ID;
  233. }
  234. };
  235. for (size_t index = 0; index < NUM_RT; ++index) {
  236. ImageViewId& color_buffer_id = render_targets.color_buffer_ids[index];
  237. if (flags[Dirty::ColorBuffer0 + index] || force) {
  238. flags[Dirty::ColorBuffer0 + index] = false;
  239. BindRenderTarget(&color_buffer_id, FindColorBuffer(index, is_clear));
  240. }
  241. check_rescale(color_buffer_id, tmp_color_images[index]);
  242. }
  243. if (flags[Dirty::ZetaBuffer] || force) {
  244. flags[Dirty::ZetaBuffer] = false;
  245. BindRenderTarget(&render_targets.depth_buffer_id, FindDepthBuffer(is_clear));
  246. }
  247. check_rescale(render_targets.depth_buffer_id, tmp_depth_image);
  248. if (can_rescale) {
  249. rescaled = any_rescaled || scale_rating >= 2;
  250. const auto scale_up = [this](ImageId image_id) {
  251. if (image_id != CORRUPT_ID) {
  252. Image& image = slot_images[image_id];
  253. ScaleUp(image);
  254. }
  255. };
  256. if (rescaled) {
  257. for (size_t index = 0; index < NUM_RT; ++index) {
  258. scale_up(tmp_color_images[index]);
  259. }
  260. scale_up(tmp_depth_image);
  261. scale_rating = 2;
  262. }
  263. } else {
  264. rescaled = false;
  265. const auto scale_down = [this](ImageId image_id) {
  266. if (image_id != CORRUPT_ID) {
  267. Image& image = slot_images[image_id];
  268. ScaleDown(image);
  269. }
  270. };
  271. for (size_t index = 0; index < NUM_RT; ++index) {
  272. scale_down(tmp_color_images[index]);
  273. }
  274. scale_down(tmp_depth_image);
  275. scale_rating = 1;
  276. }
  277. } while (has_deleted_images);
  278. const auto set_rating = [this, scale_rating](ImageId image_id) {
  279. if (image_id != CORRUPT_ID) {
  280. Image& image = slot_images[image_id];
  281. image.scale_rating = scale_rating;
  282. if (image.scale_tick <= frame_tick) {
  283. image.scale_tick = frame_tick + 1;
  284. }
  285. }
  286. };
  287. for (size_t index = 0; index < NUM_RT; ++index) {
  288. set_rating(tmp_color_images[index]);
  289. }
  290. set_rating(tmp_depth_image);
  291. return rescaled;
  292. }
  293. template <class P>
  294. void TextureCache<P>::UpdateRenderTargets(bool is_clear) {
  295. using namespace VideoCommon::Dirty;
  296. auto& flags = maxwell3d->dirty.flags;
  297. if (!flags[Dirty::RenderTargets]) {
  298. for (size_t index = 0; index < NUM_RT; ++index) {
  299. ImageViewId& color_buffer_id = render_targets.color_buffer_ids[index];
  300. PrepareImageView(color_buffer_id, true, is_clear && IsFullClear(color_buffer_id));
  301. }
  302. const ImageViewId depth_buffer_id = render_targets.depth_buffer_id;
  303. PrepareImageView(depth_buffer_id, true, is_clear && IsFullClear(depth_buffer_id));
  304. return;
  305. }
  306. const bool rescaled = RescaleRenderTargets(is_clear);
  307. if (is_rescaling != rescaled) {
  308. flags[Dirty::RescaleViewports] = true;
  309. flags[Dirty::RescaleScissors] = true;
  310. is_rescaling = rescaled;
  311. }
  312. for (size_t index = 0; index < NUM_RT; ++index) {
  313. ImageViewId& color_buffer_id = render_targets.color_buffer_ids[index];
  314. PrepareImageView(color_buffer_id, true, is_clear && IsFullClear(color_buffer_id));
  315. }
  316. const ImageViewId depth_buffer_id = render_targets.depth_buffer_id;
  317. PrepareImageView(depth_buffer_id, true, is_clear && IsFullClear(depth_buffer_id));
  318. for (size_t index = 0; index < NUM_RT; ++index) {
  319. render_targets.draw_buffers[index] = static_cast<u8>(maxwell3d->regs.rt_control.Map(index));
  320. }
  321. u32 up_scale = 1;
  322. u32 down_shift = 0;
  323. if (is_rescaling) {
  324. up_scale = Settings::values.resolution_info.up_scale;
  325. down_shift = Settings::values.resolution_info.down_shift;
  326. }
  327. render_targets.size = Extent2D{
  328. (maxwell3d->regs.render_area.width * up_scale) >> down_shift,
  329. (maxwell3d->regs.render_area.height * up_scale) >> down_shift,
  330. };
  331. flags[Dirty::DepthBiasGlobal] = true;
  332. }
  333. template <class P>
  334. typename P::Framebuffer* TextureCache<P>::GetFramebuffer() {
  335. return &slot_framebuffers[GetFramebufferId(render_targets)];
  336. }
  337. template <class P>
  338. template <bool has_blacklists>
  339. void TextureCache<P>::FillImageViews(DescriptorTable<TICEntry>& table,
  340. std::span<ImageViewId> cached_image_view_ids,
  341. std::span<ImageViewInOut> views) {
  342. bool has_blacklisted = false;
  343. do {
  344. has_deleted_images = false;
  345. if constexpr (has_blacklists) {
  346. has_blacklisted = false;
  347. }
  348. for (ImageViewInOut& view : views) {
  349. view.id = VisitImageView(table, cached_image_view_ids, view.index);
  350. if constexpr (has_blacklists) {
  351. if (view.blacklist && view.id != NULL_IMAGE_VIEW_ID) {
  352. const ImageViewBase& image_view{slot_image_views[view.id]};
  353. auto& image = slot_images[image_view.image_id];
  354. has_blacklisted |= ScaleDown(image);
  355. image.scale_rating = 0;
  356. }
  357. }
  358. }
  359. } while (has_deleted_images || (has_blacklists && has_blacklisted));
  360. }
  361. template <class P>
  362. ImageViewId TextureCache<P>::VisitImageView(DescriptorTable<TICEntry>& table,
  363. std::span<ImageViewId> cached_image_view_ids,
  364. u32 index) {
  365. if (index > table.Limit()) {
  366. LOG_DEBUG(HW_GPU, "Invalid image view index={}", index);
  367. return NULL_IMAGE_VIEW_ID;
  368. }
  369. const auto [descriptor, is_new] = table.Read(index);
  370. ImageViewId& image_view_id = cached_image_view_ids[index];
  371. if (is_new) {
  372. image_view_id = FindImageView(descriptor);
  373. }
  374. if (image_view_id != NULL_IMAGE_VIEW_ID) {
  375. PrepareImageView(image_view_id, false, false);
  376. }
  377. return image_view_id;
  378. }
  379. template <class P>
  380. FramebufferId TextureCache<P>::GetFramebufferId(const RenderTargets& key) {
  381. const auto [pair, is_new] = framebuffers.try_emplace(key);
  382. FramebufferId& framebuffer_id = pair->second;
  383. if (!is_new) {
  384. return framebuffer_id;
  385. }
  386. std::array<ImageView*, NUM_RT> color_buffers;
  387. std::ranges::transform(key.color_buffer_ids, color_buffers.begin(),
  388. [this](ImageViewId id) { return id ? &slot_image_views[id] : nullptr; });
  389. ImageView* const depth_buffer =
  390. key.depth_buffer_id ? &slot_image_views[key.depth_buffer_id] : nullptr;
  391. framebuffer_id = slot_framebuffers.insert(runtime, color_buffers, depth_buffer, key);
  392. return framebuffer_id;
  393. }
  394. template <class P>
  395. void TextureCache<P>::WriteMemory(VAddr cpu_addr, size_t size) {
  396. ForEachImageInRegion(cpu_addr, size, [this](ImageId image_id, Image& image) {
  397. if (True(image.flags & ImageFlagBits::CpuModified)) {
  398. return;
  399. }
  400. image.flags |= ImageFlagBits::CpuModified;
  401. if (True(image.flags & ImageFlagBits::Tracked)) {
  402. UntrackImage(image, image_id);
  403. }
  404. });
  405. }
  406. template <class P>
  407. void TextureCache<P>::DownloadMemory(VAddr cpu_addr, size_t size) {
  408. std::vector<ImageId> images;
  409. ForEachImageInRegion(cpu_addr, size, [this, &images](ImageId image_id, ImageBase& image) {
  410. if (!image.IsSafeDownload()) {
  411. return;
  412. }
  413. image.flags &= ~ImageFlagBits::GpuModified;
  414. images.push_back(image_id);
  415. });
  416. if (images.empty()) {
  417. return;
  418. }
  419. std::ranges::sort(images, [this](ImageId lhs, ImageId rhs) {
  420. return slot_images[lhs].modification_tick < slot_images[rhs].modification_tick;
  421. });
  422. for (const ImageId image_id : images) {
  423. Image& image = slot_images[image_id];
  424. auto map = runtime.DownloadStagingBuffer(image.unswizzled_size_bytes);
  425. const auto copies = FullDownloadCopies(image.info);
  426. image.DownloadMemory(map, copies);
  427. runtime.Finish();
  428. SwizzleImage(*gpu_memory, image.gpu_addr, image.info, copies, map.mapped_span);
  429. }
  430. }
  431. template <class P>
  432. void TextureCache<P>::UnmapMemory(VAddr cpu_addr, size_t size) {
  433. std::vector<ImageId> deleted_images;
  434. ForEachImageInRegion(cpu_addr, size, [&](ImageId id, Image&) { deleted_images.push_back(id); });
  435. for (const ImageId id : deleted_images) {
  436. Image& image = slot_images[id];
  437. if (True(image.flags & ImageFlagBits::Tracked)) {
  438. UntrackImage(image, id);
  439. }
  440. UnregisterImage(id);
  441. DeleteImage(id);
  442. }
  443. }
  444. template <class P>
  445. void TextureCache<P>::UnmapGPUMemory(size_t as_id, GPUVAddr gpu_addr, size_t size) {
  446. std::vector<ImageId> deleted_images;
  447. ForEachImageInRegionGPU(as_id, gpu_addr, size,
  448. [&](ImageId id, Image&) { deleted_images.push_back(id); });
  449. for (const ImageId id : deleted_images) {
  450. Image& image = slot_images[id];
  451. if (True(image.flags & ImageFlagBits::CpuModified)) {
  452. return;
  453. }
  454. image.flags |= ImageFlagBits::CpuModified;
  455. if (True(image.flags & ImageFlagBits::Tracked)) {
  456. UntrackImage(image, id);
  457. }
  458. /*
  459. if (True(image.flags & ImageFlagBits::Remapped)) {
  460. continue;
  461. }
  462. image.flags |= ImageFlagBits::Remapped;
  463. if (True(image.flags & ImageFlagBits::Tracked)) {
  464. UntrackImage(image, id);
  465. }
  466. */
  467. }
  468. }
  469. template <class P>
  470. void TextureCache<P>::BlitImage(const Tegra::Engines::Fermi2D::Surface& dst,
  471. const Tegra::Engines::Fermi2D::Surface& src,
  472. const Tegra::Engines::Fermi2D::Config& copy) {
  473. const BlitImages images = GetBlitImages(dst, src, copy);
  474. const ImageId dst_id = images.dst_id;
  475. const ImageId src_id = images.src_id;
  476. PrepareImage(src_id, false, false);
  477. PrepareImage(dst_id, true, false);
  478. Image& dst_image = slot_images[dst_id];
  479. Image& src_image = slot_images[src_id];
  480. bool is_src_rescaled = True(src_image.flags & ImageFlagBits::Rescaled);
  481. bool is_dst_rescaled = True(dst_image.flags & ImageFlagBits::Rescaled);
  482. const bool is_resolve = src_image.info.num_samples != 1 && dst_image.info.num_samples == 1;
  483. if (is_src_rescaled != is_dst_rescaled) {
  484. if (ImageCanRescale(src_image)) {
  485. ScaleUp(src_image);
  486. is_src_rescaled = True(src_image.flags & ImageFlagBits::Rescaled);
  487. if (is_resolve) {
  488. dst_image.info.rescaleable = true;
  489. for (const auto& alias : dst_image.aliased_images) {
  490. Image& other_image = slot_images[alias.id];
  491. other_image.info.rescaleable = true;
  492. }
  493. }
  494. }
  495. if (ImageCanRescale(dst_image)) {
  496. ScaleUp(dst_image);
  497. is_dst_rescaled = True(dst_image.flags & ImageFlagBits::Rescaled);
  498. }
  499. }
  500. if (is_resolve && (is_src_rescaled != is_dst_rescaled)) {
  501. // A resolve requires both images to be the same dimensions. Resize down if needed.
  502. ScaleDown(src_image);
  503. ScaleDown(dst_image);
  504. is_src_rescaled = True(src_image.flags & ImageFlagBits::Rescaled);
  505. is_dst_rescaled = True(dst_image.flags & ImageFlagBits::Rescaled);
  506. }
  507. const auto& resolution = Settings::values.resolution_info;
  508. const auto scale_region = [&](Region2D& region) {
  509. region.start.x = resolution.ScaleUp(region.start.x);
  510. region.start.y = resolution.ScaleUp(region.start.y);
  511. region.end.x = resolution.ScaleUp(region.end.x);
  512. region.end.y = resolution.ScaleUp(region.end.y);
  513. };
  514. // TODO: Deduplicate
  515. const std::optional src_base = src_image.TryFindBase(src.Address());
  516. const SubresourceRange src_range{.base = src_base.value(), .extent = {1, 1}};
  517. const ImageViewInfo src_view_info(ImageViewType::e2D, images.src_format, src_range);
  518. const auto [src_framebuffer_id, src_view_id] = RenderTargetFromImage(src_id, src_view_info);
  519. const auto [src_samples_x, src_samples_y] = SamplesLog2(src_image.info.num_samples);
  520. Region2D src_region{
  521. Offset2D{.x = copy.src_x0 >> src_samples_x, .y = copy.src_y0 >> src_samples_y},
  522. Offset2D{.x = copy.src_x1 >> src_samples_x, .y = copy.src_y1 >> src_samples_y},
  523. };
  524. if (is_src_rescaled) {
  525. scale_region(src_region);
  526. }
  527. const std::optional dst_base = dst_image.TryFindBase(dst.Address());
  528. const SubresourceRange dst_range{.base = dst_base.value(), .extent = {1, 1}};
  529. const ImageViewInfo dst_view_info(ImageViewType::e2D, images.dst_format, dst_range);
  530. const auto [dst_framebuffer_id, dst_view_id] = RenderTargetFromImage(dst_id, dst_view_info);
  531. const auto [dst_samples_x, dst_samples_y] = SamplesLog2(dst_image.info.num_samples);
  532. Region2D dst_region{
  533. Offset2D{.x = copy.dst_x0 >> dst_samples_x, .y = copy.dst_y0 >> dst_samples_y},
  534. Offset2D{.x = copy.dst_x1 >> dst_samples_x, .y = copy.dst_y1 >> dst_samples_y},
  535. };
  536. if (is_dst_rescaled) {
  537. scale_region(dst_region);
  538. }
  539. // Always call this after src_framebuffer_id was queried, as the address might be invalidated.
  540. Framebuffer* const dst_framebuffer = &slot_framebuffers[dst_framebuffer_id];
  541. if constexpr (FRAMEBUFFER_BLITS) {
  542. // OpenGL blits from framebuffers, not images
  543. Framebuffer* const src_framebuffer = &slot_framebuffers[src_framebuffer_id];
  544. runtime.BlitFramebuffer(dst_framebuffer, src_framebuffer, dst_region, src_region,
  545. copy.filter, copy.operation);
  546. } else {
  547. // Vulkan can blit images, but it lacks format reinterpretations
  548. // Provide a framebuffer in case it's necessary
  549. ImageView& dst_view = slot_image_views[dst_view_id];
  550. ImageView& src_view = slot_image_views[src_view_id];
  551. runtime.BlitImage(dst_framebuffer, dst_view, src_view, dst_region, src_region, copy.filter,
  552. copy.operation);
  553. }
  554. }
  555. template <class P>
  556. typename P::ImageView* TextureCache<P>::TryFindFramebufferImageView(VAddr cpu_addr) {
  557. // TODO: Properly implement this
  558. const auto it = page_table.find(cpu_addr >> YUZU_PAGEBITS);
  559. if (it == page_table.end()) {
  560. return nullptr;
  561. }
  562. const auto& image_map_ids = it->second;
  563. for (const ImageMapId map_id : image_map_ids) {
  564. const ImageMapView& map = slot_map_views[map_id];
  565. const ImageBase& image = slot_images[map.image_id];
  566. if (image.cpu_addr != cpu_addr) {
  567. continue;
  568. }
  569. if (image.image_view_ids.empty()) {
  570. continue;
  571. }
  572. return &slot_image_views[image.image_view_ids.at(0)];
  573. }
  574. return nullptr;
  575. }
  576. template <class P>
  577. bool TextureCache<P>::HasUncommittedFlushes() const noexcept {
  578. return !uncommitted_downloads.empty();
  579. }
  580. template <class P>
  581. bool TextureCache<P>::ShouldWaitAsyncFlushes() const noexcept {
  582. return !committed_downloads.empty() && !committed_downloads.front().empty();
  583. }
  584. template <class P>
  585. void TextureCache<P>::CommitAsyncFlushes() {
  586. // This is intentionally passing the value by copy
  587. committed_downloads.push(uncommitted_downloads);
  588. uncommitted_downloads.clear();
  589. }
  590. template <class P>
  591. void TextureCache<P>::PopAsyncFlushes() {
  592. if (committed_downloads.empty()) {
  593. return;
  594. }
  595. const std::span<const ImageId> download_ids = committed_downloads.front();
  596. if (download_ids.empty()) {
  597. committed_downloads.pop();
  598. return;
  599. }
  600. size_t total_size_bytes = 0;
  601. for (const ImageId image_id : download_ids) {
  602. total_size_bytes += slot_images[image_id].unswizzled_size_bytes;
  603. }
  604. auto download_map = runtime.DownloadStagingBuffer(total_size_bytes);
  605. const size_t original_offset = download_map.offset;
  606. for (const ImageId image_id : download_ids) {
  607. Image& image = slot_images[image_id];
  608. const auto copies = FullDownloadCopies(image.info);
  609. image.DownloadMemory(download_map, copies);
  610. download_map.offset += image.unswizzled_size_bytes;
  611. }
  612. // Wait for downloads to finish
  613. runtime.Finish();
  614. download_map.offset = original_offset;
  615. std::span<u8> download_span = download_map.mapped_span;
  616. for (const ImageId image_id : download_ids) {
  617. const ImageBase& image = slot_images[image_id];
  618. const auto copies = FullDownloadCopies(image.info);
  619. SwizzleImage(*gpu_memory, image.gpu_addr, image.info, copies, download_span);
  620. download_map.offset += image.unswizzled_size_bytes;
  621. download_span = download_span.subspan(image.unswizzled_size_bytes);
  622. }
  623. committed_downloads.pop();
  624. }
  625. template <class P>
  626. bool TextureCache<P>::IsRescaling() const noexcept {
  627. return is_rescaling;
  628. }
  629. template <class P>
  630. bool TextureCache<P>::IsRescaling(const ImageViewBase& image_view) const noexcept {
  631. if (image_view.type == ImageViewType::Buffer) {
  632. return false;
  633. }
  634. const ImageBase& image = slot_images[image_view.image_id];
  635. return True(image.flags & ImageFlagBits::Rescaled);
  636. }
  637. template <class P>
  638. bool TextureCache<P>::IsRegionGpuModified(VAddr addr, size_t size) {
  639. bool is_modified = false;
  640. ForEachImageInRegion(addr, size, [&is_modified](ImageId, ImageBase& image) {
  641. if (False(image.flags & ImageFlagBits::GpuModified)) {
  642. return false;
  643. }
  644. is_modified = true;
  645. return true;
  646. });
  647. return is_modified;
  648. }
  649. template <class P>
  650. void TextureCache<P>::RefreshContents(Image& image, ImageId image_id) {
  651. if (False(image.flags & ImageFlagBits::CpuModified)) {
  652. // Only upload modified images
  653. return;
  654. }
  655. image.flags &= ~ImageFlagBits::CpuModified;
  656. TrackImage(image, image_id);
  657. if (image.info.num_samples > 1) {
  658. LOG_WARNING(HW_GPU, "MSAA image uploads are not implemented");
  659. return;
  660. }
  661. auto staging = runtime.UploadStagingBuffer(MapSizeBytes(image));
  662. UploadImageContents(image, staging);
  663. runtime.InsertUploadMemoryBarrier();
  664. }
  665. template <class P>
  666. template <typename StagingBuffer>
  667. void TextureCache<P>::UploadImageContents(Image& image, StagingBuffer& staging) {
  668. const std::span<u8> mapped_span = staging.mapped_span;
  669. const GPUVAddr gpu_addr = image.gpu_addr;
  670. if (True(image.flags & ImageFlagBits::AcceleratedUpload)) {
  671. gpu_memory->ReadBlockUnsafe(gpu_addr, mapped_span.data(), mapped_span.size_bytes());
  672. const auto uploads = FullUploadSwizzles(image.info);
  673. runtime.AccelerateImageUpload(image, staging, uploads);
  674. } else if (True(image.flags & ImageFlagBits::Converted)) {
  675. std::vector<u8> unswizzled_data(image.unswizzled_size_bytes);
  676. auto copies = UnswizzleImage(*gpu_memory, gpu_addr, image.info, unswizzled_data);
  677. ConvertImage(unswizzled_data, image.info, mapped_span, copies);
  678. image.UploadMemory(staging, copies);
  679. } else {
  680. const auto copies = UnswizzleImage(*gpu_memory, gpu_addr, image.info, mapped_span);
  681. image.UploadMemory(staging, copies);
  682. }
  683. }
  684. template <class P>
  685. ImageViewId TextureCache<P>::FindImageView(const TICEntry& config) {
  686. if (!IsValidEntry(*gpu_memory, config)) {
  687. return NULL_IMAGE_VIEW_ID;
  688. }
  689. const auto [pair, is_new] = channel_state->image_views.try_emplace(config);
  690. ImageViewId& image_view_id = pair->second;
  691. if (is_new) {
  692. image_view_id = CreateImageView(config);
  693. }
  694. return image_view_id;
  695. }
  696. template <class P>
  697. ImageViewId TextureCache<P>::CreateImageView(const TICEntry& config) {
  698. const ImageInfo info(config);
  699. if (info.type == ImageType::Buffer) {
  700. const ImageViewInfo view_info(config, 0);
  701. return slot_image_views.insert(runtime, info, view_info, config.Address());
  702. }
  703. const u32 layer_offset = config.BaseLayer() * info.layer_stride;
  704. const GPUVAddr image_gpu_addr = config.Address() - layer_offset;
  705. const ImageId image_id = FindOrInsertImage(info, image_gpu_addr);
  706. if (!image_id) {
  707. return NULL_IMAGE_VIEW_ID;
  708. }
  709. ImageBase& image = slot_images[image_id];
  710. const SubresourceBase base = image.TryFindBase(config.Address()).value();
  711. ASSERT(base.level == 0);
  712. const ImageViewInfo view_info(config, base.layer);
  713. const ImageViewId image_view_id = FindOrEmplaceImageView(image_id, view_info);
  714. ImageViewBase& image_view = slot_image_views[image_view_id];
  715. image_view.flags |= ImageViewFlagBits::Strong;
  716. image.flags |= ImageFlagBits::Strong;
  717. return image_view_id;
  718. }
  719. template <class P>
  720. ImageId TextureCache<P>::FindOrInsertImage(const ImageInfo& info, GPUVAddr gpu_addr,
  721. RelaxedOptions options) {
  722. if (const ImageId image_id = FindImage(info, gpu_addr, options); image_id) {
  723. return image_id;
  724. }
  725. return InsertImage(info, gpu_addr, options);
  726. }
  727. template <class P>
  728. ImageId TextureCache<P>::FindImage(const ImageInfo& info, GPUVAddr gpu_addr,
  729. RelaxedOptions options) {
  730. std::optional<VAddr> cpu_addr = gpu_memory->GpuToCpuAddress(gpu_addr);
  731. if (!cpu_addr) {
  732. cpu_addr = gpu_memory->GpuToCpuAddress(gpu_addr, CalculateGuestSizeInBytes(info));
  733. if (!cpu_addr) {
  734. return ImageId{};
  735. }
  736. }
  737. const bool broken_views =
  738. runtime.HasBrokenTextureViewFormats() || True(options & RelaxedOptions::ForceBrokenViews);
  739. const bool native_bgr = runtime.HasNativeBgr();
  740. const bool flexible_formats = True(options & RelaxedOptions::Format);
  741. ImageId image_id{};
  742. boost::container::small_vector<ImageId, 1> image_ids;
  743. const auto lambda = [&](ImageId existing_image_id, ImageBase& existing_image) {
  744. if (True(existing_image.flags & ImageFlagBits::Remapped)) {
  745. return false;
  746. }
  747. if (info.type == ImageType::Linear || existing_image.info.type == ImageType::Linear)
  748. [[unlikely]] {
  749. const bool strict_size = False(options & RelaxedOptions::Size) &&
  750. True(existing_image.flags & ImageFlagBits::Strong);
  751. const ImageInfo& existing = existing_image.info;
  752. if (existing_image.gpu_addr == gpu_addr && existing.type == info.type &&
  753. existing.pitch == info.pitch &&
  754. IsPitchLinearSameSize(existing, info, strict_size) &&
  755. IsViewCompatible(existing.format, info.format, broken_views, native_bgr)) {
  756. image_id = existing_image_id;
  757. image_ids.push_back(existing_image_id);
  758. return !flexible_formats && existing.format == info.format;
  759. }
  760. } else if (IsSubresource(info, existing_image, gpu_addr, options, broken_views,
  761. native_bgr)) {
  762. image_id = existing_image_id;
  763. image_ids.push_back(existing_image_id);
  764. return !flexible_formats && existing_image.info.format == info.format;
  765. }
  766. return false;
  767. };
  768. ForEachImageInRegion(*cpu_addr, CalculateGuestSizeInBytes(info), lambda);
  769. if (image_ids.size() <= 1) [[likely]] {
  770. return image_id;
  771. }
  772. auto image_ids_compare = [this](ImageId a, ImageId b) {
  773. auto& image_a = slot_images[a];
  774. auto& image_b = slot_images[b];
  775. return image_a.modification_tick < image_b.modification_tick;
  776. };
  777. return *std::ranges::max_element(image_ids, image_ids_compare);
  778. }
  779. template <class P>
  780. bool TextureCache<P>::ImageCanRescale(ImageBase& image) {
  781. if (!image.info.rescaleable) {
  782. return false;
  783. }
  784. if (Settings::values.resolution_info.downscale && !image.info.downscaleable) {
  785. return false;
  786. }
  787. if (True(image.flags & (ImageFlagBits::Rescaled | ImageFlagBits::CheckingRescalable))) {
  788. return true;
  789. }
  790. if (True(image.flags & ImageFlagBits::IsRescalable)) {
  791. return true;
  792. }
  793. image.flags |= ImageFlagBits::CheckingRescalable;
  794. for (const auto& alias : image.aliased_images) {
  795. Image& other_image = slot_images[alias.id];
  796. if (!ImageCanRescale(other_image)) {
  797. image.flags &= ~ImageFlagBits::CheckingRescalable;
  798. return false;
  799. }
  800. }
  801. image.flags &= ~ImageFlagBits::CheckingRescalable;
  802. image.flags |= ImageFlagBits::IsRescalable;
  803. return true;
  804. }
  805. template <class P>
  806. void TextureCache<P>::InvalidateScale(Image& image) {
  807. if (image.scale_tick <= frame_tick) {
  808. image.scale_tick = frame_tick + 1;
  809. }
  810. const std::span<const ImageViewId> image_view_ids = image.image_view_ids;
  811. auto& dirty = maxwell3d->dirty.flags;
  812. dirty[Dirty::RenderTargets] = true;
  813. dirty[Dirty::ZetaBuffer] = true;
  814. for (size_t rt = 0; rt < NUM_RT; ++rt) {
  815. dirty[Dirty::ColorBuffer0 + rt] = true;
  816. }
  817. for (const ImageViewId image_view_id : image_view_ids) {
  818. std::ranges::replace(render_targets.color_buffer_ids, image_view_id, ImageViewId{});
  819. if (render_targets.depth_buffer_id == image_view_id) {
  820. render_targets.depth_buffer_id = ImageViewId{};
  821. }
  822. }
  823. RemoveImageViewReferences(image_view_ids);
  824. RemoveFramebuffers(image_view_ids);
  825. for (const ImageViewId image_view_id : image_view_ids) {
  826. sentenced_image_view.Push(std::move(slot_image_views[image_view_id]));
  827. slot_image_views.erase(image_view_id);
  828. }
  829. image.image_view_ids.clear();
  830. image.image_view_infos.clear();
  831. for (size_t c : active_channel_ids) {
  832. auto& channel_info = channel_storage[c];
  833. if constexpr (ENABLE_VALIDATION) {
  834. std::ranges::fill(channel_info.graphics_image_view_ids, CORRUPT_ID);
  835. std::ranges::fill(channel_info.compute_image_view_ids, CORRUPT_ID);
  836. }
  837. channel_info.graphics_image_table.Invalidate();
  838. channel_info.compute_image_table.Invalidate();
  839. }
  840. has_deleted_images = true;
  841. }
  842. template <class P>
  843. u64 TextureCache<P>::GetScaledImageSizeBytes(ImageBase& image) {
  844. const u64 scale_up = static_cast<u64>(Settings::values.resolution_info.up_scale *
  845. Settings::values.resolution_info.up_scale);
  846. const u64 down_shift = static_cast<u64>(Settings::values.resolution_info.down_shift +
  847. Settings::values.resolution_info.down_shift);
  848. const u64 image_size_bytes =
  849. static_cast<u64>(std::max(image.guest_size_bytes, image.unswizzled_size_bytes));
  850. const u64 tentative_size = (image_size_bytes * scale_up) >> down_shift;
  851. const u64 fitted_size = Common::AlignUp(tentative_size, 1024);
  852. return fitted_size;
  853. }
  854. template <class P>
  855. bool TextureCache<P>::ScaleUp(Image& image) {
  856. const bool has_copy = image.HasScaled();
  857. const bool rescaled = image.ScaleUp();
  858. if (!rescaled) {
  859. return false;
  860. }
  861. if (!has_copy) {
  862. total_used_memory += GetScaledImageSizeBytes(image);
  863. }
  864. InvalidateScale(image);
  865. return true;
  866. }
  867. template <class P>
  868. bool TextureCache<P>::ScaleDown(Image& image) {
  869. const bool rescaled = image.ScaleDown();
  870. if (!rescaled) {
  871. return false;
  872. }
  873. InvalidateScale(image);
  874. return true;
  875. }
  876. template <class P>
  877. ImageId TextureCache<P>::InsertImage(const ImageInfo& info, GPUVAddr gpu_addr,
  878. RelaxedOptions options) {
  879. std::optional<VAddr> cpu_addr = gpu_memory->GpuToCpuAddress(gpu_addr);
  880. if (!cpu_addr) {
  881. const auto size = CalculateGuestSizeInBytes(info);
  882. cpu_addr = gpu_memory->GpuToCpuAddress(gpu_addr, size);
  883. if (!cpu_addr) {
  884. const VAddr fake_addr = ~(1ULL << 40ULL) + virtual_invalid_space;
  885. virtual_invalid_space += Common::AlignUp(size, 32);
  886. cpu_addr = std::optional<VAddr>(fake_addr);
  887. }
  888. }
  889. ASSERT_MSG(cpu_addr, "Tried to insert an image to an invalid gpu_addr=0x{:x}", gpu_addr);
  890. const ImageId image_id = JoinImages(info, gpu_addr, *cpu_addr);
  891. const Image& image = slot_images[image_id];
  892. // Using "image.gpu_addr" instead of "gpu_addr" is important because it might be different
  893. const auto [it, is_new] = image_allocs_table.try_emplace(image.gpu_addr);
  894. if (is_new) {
  895. it->second = slot_image_allocs.insert();
  896. }
  897. slot_image_allocs[it->second].images.push_back(image_id);
  898. return image_id;
  899. }
  900. template <class P>
  901. ImageId TextureCache<P>::JoinImages(const ImageInfo& info, GPUVAddr gpu_addr, VAddr cpu_addr) {
  902. ImageInfo new_info = info;
  903. const size_t size_bytes = CalculateGuestSizeInBytes(new_info);
  904. const bool broken_views = runtime.HasBrokenTextureViewFormats();
  905. const bool native_bgr = runtime.HasNativeBgr();
  906. std::vector<ImageId> overlap_ids;
  907. std::unordered_set<ImageId> overlaps_found;
  908. std::vector<ImageId> left_aliased_ids;
  909. std::vector<ImageId> right_aliased_ids;
  910. std::unordered_set<ImageId> ignore_textures;
  911. std::vector<ImageId> bad_overlap_ids;
  912. std::vector<ImageId> all_siblings;
  913. const bool this_is_linear = info.type == ImageType::Linear;
  914. const auto region_check = [&](ImageId overlap_id, ImageBase& overlap) {
  915. if (True(overlap.flags & ImageFlagBits::Remapped)) {
  916. ignore_textures.insert(overlap_id);
  917. return;
  918. }
  919. const bool overlap_is_linear = overlap.info.type == ImageType::Linear;
  920. if (this_is_linear != overlap_is_linear) {
  921. return;
  922. }
  923. if (this_is_linear && overlap_is_linear) {
  924. if (info.pitch == overlap.info.pitch && gpu_addr == overlap.gpu_addr) {
  925. // Alias linear images with the same pitch
  926. left_aliased_ids.push_back(overlap_id);
  927. }
  928. return;
  929. }
  930. overlaps_found.insert(overlap_id);
  931. static constexpr bool strict_size = true;
  932. const std::optional<OverlapResult> solution = ResolveOverlap(
  933. new_info, gpu_addr, cpu_addr, overlap, strict_size, broken_views, native_bgr);
  934. if (solution) {
  935. gpu_addr = solution->gpu_addr;
  936. cpu_addr = solution->cpu_addr;
  937. new_info.resources = solution->resources;
  938. overlap_ids.push_back(overlap_id);
  939. all_siblings.push_back(overlap_id);
  940. return;
  941. }
  942. static constexpr auto options = RelaxedOptions::Size | RelaxedOptions::Format;
  943. const ImageBase new_image_base(new_info, gpu_addr, cpu_addr);
  944. if (IsSubresource(new_info, overlap, gpu_addr, options, broken_views, native_bgr)) {
  945. left_aliased_ids.push_back(overlap_id);
  946. overlap.flags |= ImageFlagBits::Alias;
  947. all_siblings.push_back(overlap_id);
  948. } else if (IsSubresource(overlap.info, new_image_base, overlap.gpu_addr, options,
  949. broken_views, native_bgr)) {
  950. right_aliased_ids.push_back(overlap_id);
  951. overlap.flags |= ImageFlagBits::Alias;
  952. all_siblings.push_back(overlap_id);
  953. } else {
  954. bad_overlap_ids.push_back(overlap_id);
  955. overlap.flags |= ImageFlagBits::BadOverlap;
  956. }
  957. };
  958. ForEachImageInRegion(cpu_addr, size_bytes, region_check);
  959. const auto region_check_gpu = [&](ImageId overlap_id, ImageBase& overlap) {
  960. if (!overlaps_found.contains(overlap_id)) {
  961. if (True(overlap.flags & ImageFlagBits::Remapped)) {
  962. ignore_textures.insert(overlap_id);
  963. }
  964. if (overlap.gpu_addr == gpu_addr && overlap.guest_size_bytes == size_bytes) {
  965. ignore_textures.insert(overlap_id);
  966. }
  967. }
  968. };
  969. ForEachSparseImageInRegion(gpu_addr, size_bytes, region_check_gpu);
  970. bool can_rescale = info.rescaleable;
  971. bool any_rescaled = false;
  972. for (const ImageId sibling_id : all_siblings) {
  973. if (!can_rescale) {
  974. break;
  975. }
  976. Image& sibling = slot_images[sibling_id];
  977. can_rescale &= ImageCanRescale(sibling);
  978. any_rescaled |= True(sibling.flags & ImageFlagBits::Rescaled);
  979. }
  980. can_rescale &= any_rescaled;
  981. if (can_rescale) {
  982. for (const ImageId sibling_id : all_siblings) {
  983. Image& sibling = slot_images[sibling_id];
  984. ScaleUp(sibling);
  985. }
  986. } else {
  987. for (const ImageId sibling_id : all_siblings) {
  988. Image& sibling = slot_images[sibling_id];
  989. ScaleDown(sibling);
  990. }
  991. }
  992. const ImageId new_image_id = slot_images.insert(runtime, new_info, gpu_addr, cpu_addr);
  993. Image& new_image = slot_images[new_image_id];
  994. if (!gpu_memory->IsContinousRange(new_image.gpu_addr, new_image.guest_size_bytes)) {
  995. new_image.flags |= ImageFlagBits::Sparse;
  996. }
  997. for (const ImageId overlap_id : ignore_textures) {
  998. Image& overlap = slot_images[overlap_id];
  999. if (True(overlap.flags & ImageFlagBits::GpuModified)) {
  1000. UNIMPLEMENTED();
  1001. }
  1002. if (True(overlap.flags & ImageFlagBits::Tracked)) {
  1003. UntrackImage(overlap, overlap_id);
  1004. }
  1005. UnregisterImage(overlap_id);
  1006. DeleteImage(overlap_id);
  1007. }
  1008. // TODO: Only upload what we need
  1009. RefreshContents(new_image, new_image_id);
  1010. if (can_rescale) {
  1011. ScaleUp(new_image);
  1012. } else {
  1013. ScaleDown(new_image);
  1014. }
  1015. for (const ImageId overlap_id : overlap_ids) {
  1016. Image& overlap = slot_images[overlap_id];
  1017. if (True(overlap.flags & ImageFlagBits::GpuModified)) {
  1018. new_image.flags |= ImageFlagBits::GpuModified;
  1019. }
  1020. if (overlap.info.num_samples != new_image.info.num_samples) {
  1021. LOG_WARNING(HW_GPU, "Copying between images with different samples is not implemented");
  1022. } else {
  1023. const auto& resolution = Settings::values.resolution_info;
  1024. const SubresourceBase base = new_image.TryFindBase(overlap.gpu_addr).value();
  1025. const u32 up_scale = can_rescale ? resolution.up_scale : 1;
  1026. const u32 down_shift = can_rescale ? resolution.down_shift : 0;
  1027. auto copies = MakeShrinkImageCopies(new_info, overlap.info, base, up_scale, down_shift);
  1028. runtime.CopyImage(new_image, overlap, std::move(copies));
  1029. }
  1030. if (True(overlap.flags & ImageFlagBits::Tracked)) {
  1031. UntrackImage(overlap, overlap_id);
  1032. }
  1033. UnregisterImage(overlap_id);
  1034. DeleteImage(overlap_id);
  1035. }
  1036. ImageBase& new_image_base = new_image;
  1037. for (const ImageId aliased_id : right_aliased_ids) {
  1038. ImageBase& aliased = slot_images[aliased_id];
  1039. AddImageAlias(new_image_base, aliased, new_image_id, aliased_id);
  1040. new_image.flags |= ImageFlagBits::Alias;
  1041. }
  1042. for (const ImageId aliased_id : left_aliased_ids) {
  1043. ImageBase& aliased = slot_images[aliased_id];
  1044. AddImageAlias(aliased, new_image_base, aliased_id, new_image_id);
  1045. new_image.flags |= ImageFlagBits::Alias;
  1046. }
  1047. for (const ImageId aliased_id : bad_overlap_ids) {
  1048. ImageBase& aliased = slot_images[aliased_id];
  1049. aliased.overlapping_images.push_back(new_image_id);
  1050. new_image.overlapping_images.push_back(aliased_id);
  1051. new_image.flags |= ImageFlagBits::BadOverlap;
  1052. }
  1053. RegisterImage(new_image_id);
  1054. return new_image_id;
  1055. }
  1056. template <class P>
  1057. typename TextureCache<P>::BlitImages TextureCache<P>::GetBlitImages(
  1058. const Tegra::Engines::Fermi2D::Surface& dst, const Tegra::Engines::Fermi2D::Surface& src,
  1059. const Tegra::Engines::Fermi2D::Config& copy) {
  1060. static constexpr auto FIND_OPTIONS = RelaxedOptions::Samples;
  1061. const GPUVAddr dst_addr = dst.Address();
  1062. const GPUVAddr src_addr = src.Address();
  1063. ImageInfo dst_info(dst);
  1064. ImageInfo src_info(src);
  1065. const bool can_be_depth_blit =
  1066. dst_info.format == src_info.format && copy.filter == Tegra::Engines::Fermi2D::Filter::Point;
  1067. ImageId dst_id;
  1068. ImageId src_id;
  1069. RelaxedOptions try_options = FIND_OPTIONS;
  1070. if (can_be_depth_blit) {
  1071. try_options |= RelaxedOptions::Format;
  1072. }
  1073. do {
  1074. has_deleted_images = false;
  1075. src_id = FindImage(src_info, src_addr, try_options);
  1076. dst_id = FindImage(dst_info, dst_addr, try_options);
  1077. const ImageBase* const src_image = src_id ? &slot_images[src_id] : nullptr;
  1078. if (src_image && src_image->info.num_samples > 1) {
  1079. RelaxedOptions find_options{FIND_OPTIONS | RelaxedOptions::ForceBrokenViews};
  1080. src_id = FindOrInsertImage(src_info, src_addr, find_options);
  1081. dst_id = FindOrInsertImage(dst_info, dst_addr, find_options);
  1082. if (has_deleted_images) {
  1083. continue;
  1084. }
  1085. break;
  1086. }
  1087. if (can_be_depth_blit) {
  1088. const ImageBase* const dst_image = dst_id ? &slot_images[dst_id] : nullptr;
  1089. DeduceBlitImages(dst_info, src_info, dst_image, src_image);
  1090. if (GetFormatType(dst_info.format) != GetFormatType(src_info.format)) {
  1091. continue;
  1092. }
  1093. }
  1094. if (!src_id) {
  1095. src_id = InsertImage(src_info, src_addr, RelaxedOptions{});
  1096. }
  1097. if (!dst_id) {
  1098. dst_id = InsertImage(dst_info, dst_addr, RelaxedOptions{});
  1099. }
  1100. } while (has_deleted_images);
  1101. const ImageBase& src_image = slot_images[src_id];
  1102. const ImageBase& dst_image = slot_images[dst_id];
  1103. const bool native_bgr = runtime.HasNativeBgr();
  1104. if (GetFormatType(dst_info.format) != GetFormatType(dst_image.info.format) ||
  1105. GetFormatType(src_info.format) != GetFormatType(src_image.info.format) ||
  1106. !VideoCore::Surface::IsViewCompatible(dst_info.format, dst_image.info.format, false,
  1107. native_bgr) ||
  1108. !VideoCore::Surface::IsViewCompatible(src_info.format, src_image.info.format, false,
  1109. native_bgr)) {
  1110. // Make sure the images match the expected format.
  1111. do {
  1112. has_deleted_images = false;
  1113. src_id = FindOrInsertImage(src_info, src_addr, RelaxedOptions{});
  1114. dst_id = FindOrInsertImage(dst_info, dst_addr, RelaxedOptions{});
  1115. } while (has_deleted_images);
  1116. }
  1117. return BlitImages{
  1118. .dst_id = dst_id,
  1119. .src_id = src_id,
  1120. .dst_format = dst_info.format,
  1121. .src_format = src_info.format,
  1122. };
  1123. }
  1124. template <class P>
  1125. SamplerId TextureCache<P>::FindSampler(const TSCEntry& config) {
  1126. if (std::ranges::all_of(config.raw, [](u64 value) { return value == 0; })) {
  1127. return NULL_SAMPLER_ID;
  1128. }
  1129. const auto [pair, is_new] = channel_state->samplers.try_emplace(config);
  1130. if (is_new) {
  1131. pair->second = slot_samplers.insert(runtime, config);
  1132. }
  1133. return pair->second;
  1134. }
  1135. template <class P>
  1136. ImageViewId TextureCache<P>::FindColorBuffer(size_t index, bool is_clear) {
  1137. const auto& regs = maxwell3d->regs;
  1138. if (index >= regs.rt_control.count) {
  1139. return ImageViewId{};
  1140. }
  1141. const auto& rt = regs.rt[index];
  1142. const GPUVAddr gpu_addr = rt.Address();
  1143. if (gpu_addr == 0) {
  1144. return ImageViewId{};
  1145. }
  1146. if (rt.format == Tegra::RenderTargetFormat::NONE) {
  1147. return ImageViewId{};
  1148. }
  1149. const ImageInfo info(regs, index);
  1150. return FindRenderTargetView(info, gpu_addr, is_clear);
  1151. }
  1152. template <class P>
  1153. ImageViewId TextureCache<P>::FindDepthBuffer(bool is_clear) {
  1154. const auto& regs = maxwell3d->regs;
  1155. if (!regs.zeta_enable) {
  1156. return ImageViewId{};
  1157. }
  1158. const GPUVAddr gpu_addr = regs.zeta.Address();
  1159. if (gpu_addr == 0) {
  1160. return ImageViewId{};
  1161. }
  1162. const ImageInfo info(regs);
  1163. return FindRenderTargetView(info, gpu_addr, is_clear);
  1164. }
  1165. template <class P>
  1166. ImageViewId TextureCache<P>::FindRenderTargetView(const ImageInfo& info, GPUVAddr gpu_addr,
  1167. bool is_clear) {
  1168. const auto options = is_clear ? RelaxedOptions::Samples : RelaxedOptions{};
  1169. ImageId image_id{};
  1170. bool delete_state = has_deleted_images;
  1171. do {
  1172. has_deleted_images = false;
  1173. image_id = FindOrInsertImage(info, gpu_addr, options);
  1174. delete_state |= has_deleted_images;
  1175. } while (has_deleted_images);
  1176. has_deleted_images = delete_state;
  1177. if (!image_id) {
  1178. return NULL_IMAGE_VIEW_ID;
  1179. }
  1180. Image& image = slot_images[image_id];
  1181. const ImageViewType view_type = RenderTargetImageViewType(info);
  1182. SubresourceBase base;
  1183. if (image.info.type == ImageType::Linear) {
  1184. base = SubresourceBase{.level = 0, .layer = 0};
  1185. } else {
  1186. base = image.TryFindBase(gpu_addr).value();
  1187. }
  1188. const s32 layers = image.info.type == ImageType::e3D ? info.size.depth : info.resources.layers;
  1189. const SubresourceRange range{
  1190. .base = base,
  1191. .extent = {.levels = 1, .layers = layers},
  1192. };
  1193. return FindOrEmplaceImageView(image_id, ImageViewInfo(view_type, info.format, range));
  1194. }
  1195. template <class P>
  1196. template <typename Func>
  1197. void TextureCache<P>::ForEachImageInRegion(VAddr cpu_addr, size_t size, Func&& func) {
  1198. using FuncReturn = typename std::invoke_result<Func, ImageId, Image&>::type;
  1199. static constexpr bool BOOL_BREAK = std::is_same_v<FuncReturn, bool>;
  1200. boost::container::small_vector<ImageId, 32> images;
  1201. boost::container::small_vector<ImageMapId, 32> maps;
  1202. ForEachCPUPage(cpu_addr, size, [this, &images, &maps, cpu_addr, size, func](u64 page) {
  1203. const auto it = page_table.find(page);
  1204. if (it == page_table.end()) {
  1205. if constexpr (BOOL_BREAK) {
  1206. return false;
  1207. } else {
  1208. return;
  1209. }
  1210. }
  1211. for (const ImageMapId map_id : it->second) {
  1212. ImageMapView& map = slot_map_views[map_id];
  1213. if (map.picked) {
  1214. continue;
  1215. }
  1216. if (!map.Overlaps(cpu_addr, size)) {
  1217. continue;
  1218. }
  1219. map.picked = true;
  1220. maps.push_back(map_id);
  1221. Image& image = slot_images[map.image_id];
  1222. if (True(image.flags & ImageFlagBits::Picked)) {
  1223. continue;
  1224. }
  1225. image.flags |= ImageFlagBits::Picked;
  1226. images.push_back(map.image_id);
  1227. if constexpr (BOOL_BREAK) {
  1228. if (func(map.image_id, image)) {
  1229. return true;
  1230. }
  1231. } else {
  1232. func(map.image_id, image);
  1233. }
  1234. }
  1235. if constexpr (BOOL_BREAK) {
  1236. return false;
  1237. }
  1238. });
  1239. for (const ImageId image_id : images) {
  1240. slot_images[image_id].flags &= ~ImageFlagBits::Picked;
  1241. }
  1242. for (const ImageMapId map_id : maps) {
  1243. slot_map_views[map_id].picked = false;
  1244. }
  1245. }
  1246. template <class P>
  1247. template <typename Func>
  1248. void TextureCache<P>::ForEachImageInRegionGPU(size_t as_id, GPUVAddr gpu_addr, size_t size,
  1249. Func&& func) {
  1250. using FuncReturn = typename std::invoke_result<Func, ImageId, Image&>::type;
  1251. static constexpr bool BOOL_BREAK = std::is_same_v<FuncReturn, bool>;
  1252. boost::container::small_vector<ImageId, 8> images;
  1253. auto storage_id = getStorageID(as_id);
  1254. if (!storage_id) {
  1255. return;
  1256. }
  1257. auto& gpu_page_table = gpu_page_table_storage[*storage_id];
  1258. ForEachGPUPage(gpu_addr, size, [this, gpu_page_table, &images, gpu_addr, size, func](u64 page) {
  1259. const auto it = gpu_page_table.find(page);
  1260. if (it == gpu_page_table.end()) {
  1261. if constexpr (BOOL_BREAK) {
  1262. return false;
  1263. } else {
  1264. return;
  1265. }
  1266. }
  1267. for (const ImageId image_id : it->second) {
  1268. Image& image = slot_images[image_id];
  1269. if (True(image.flags & ImageFlagBits::Picked)) {
  1270. continue;
  1271. }
  1272. if (!image.OverlapsGPU(gpu_addr, size)) {
  1273. continue;
  1274. }
  1275. image.flags |= ImageFlagBits::Picked;
  1276. images.push_back(image_id);
  1277. if constexpr (BOOL_BREAK) {
  1278. if (func(image_id, image)) {
  1279. return true;
  1280. }
  1281. } else {
  1282. func(image_id, image);
  1283. }
  1284. }
  1285. if constexpr (BOOL_BREAK) {
  1286. return false;
  1287. }
  1288. });
  1289. for (const ImageId image_id : images) {
  1290. slot_images[image_id].flags &= ~ImageFlagBits::Picked;
  1291. }
  1292. }
  1293. template <class P>
  1294. template <typename Func>
  1295. void TextureCache<P>::ForEachSparseImageInRegion(GPUVAddr gpu_addr, size_t size, Func&& func) {
  1296. using FuncReturn = typename std::invoke_result<Func, ImageId, Image&>::type;
  1297. static constexpr bool BOOL_BREAK = std::is_same_v<FuncReturn, bool>;
  1298. boost::container::small_vector<ImageId, 8> images;
  1299. ForEachGPUPage(gpu_addr, size, [this, &images, gpu_addr, size, func](u64 page) {
  1300. const auto it = sparse_page_table.find(page);
  1301. if (it == sparse_page_table.end()) {
  1302. if constexpr (BOOL_BREAK) {
  1303. return false;
  1304. } else {
  1305. return;
  1306. }
  1307. }
  1308. for (const ImageId image_id : it->second) {
  1309. Image& image = slot_images[image_id];
  1310. if (True(image.flags & ImageFlagBits::Picked)) {
  1311. continue;
  1312. }
  1313. if (!image.OverlapsGPU(gpu_addr, size)) {
  1314. continue;
  1315. }
  1316. image.flags |= ImageFlagBits::Picked;
  1317. images.push_back(image_id);
  1318. if constexpr (BOOL_BREAK) {
  1319. if (func(image_id, image)) {
  1320. return true;
  1321. }
  1322. } else {
  1323. func(image_id, image);
  1324. }
  1325. }
  1326. if constexpr (BOOL_BREAK) {
  1327. return false;
  1328. }
  1329. });
  1330. for (const ImageId image_id : images) {
  1331. slot_images[image_id].flags &= ~ImageFlagBits::Picked;
  1332. }
  1333. }
  1334. template <class P>
  1335. template <typename Func>
  1336. void TextureCache<P>::ForEachSparseSegment(ImageBase& image, Func&& func) {
  1337. using FuncReturn = typename std::invoke_result<Func, GPUVAddr, VAddr, size_t>::type;
  1338. static constexpr bool RETURNS_BOOL = std::is_same_v<FuncReturn, bool>;
  1339. const auto segments = gpu_memory->GetSubmappedRange(image.gpu_addr, image.guest_size_bytes);
  1340. for (const auto& [gpu_addr, size] : segments) {
  1341. std::optional<VAddr> cpu_addr = gpu_memory->GpuToCpuAddress(gpu_addr);
  1342. ASSERT(cpu_addr);
  1343. if constexpr (RETURNS_BOOL) {
  1344. if (func(gpu_addr, *cpu_addr, size)) {
  1345. break;
  1346. }
  1347. } else {
  1348. func(gpu_addr, *cpu_addr, size);
  1349. }
  1350. }
  1351. }
  1352. template <class P>
  1353. ImageViewId TextureCache<P>::FindOrEmplaceImageView(ImageId image_id, const ImageViewInfo& info) {
  1354. Image& image = slot_images[image_id];
  1355. if (const ImageViewId image_view_id = image.FindView(info); image_view_id) {
  1356. return image_view_id;
  1357. }
  1358. const ImageViewId image_view_id =
  1359. slot_image_views.insert(runtime, info, image_id, image, slot_images);
  1360. image.InsertView(info, image_view_id);
  1361. return image_view_id;
  1362. }
  1363. template <class P>
  1364. void TextureCache<P>::RegisterImage(ImageId image_id) {
  1365. ImageBase& image = slot_images[image_id];
  1366. ASSERT_MSG(False(image.flags & ImageFlagBits::Registered),
  1367. "Trying to register an already registered image");
  1368. image.flags |= ImageFlagBits::Registered;
  1369. u64 tentative_size = std::max(image.guest_size_bytes, image.unswizzled_size_bytes);
  1370. if ((IsPixelFormatASTC(image.info.format) &&
  1371. True(image.flags & ImageFlagBits::AcceleratedUpload)) ||
  1372. True(image.flags & ImageFlagBits::Converted)) {
  1373. tentative_size = EstimatedDecompressedSize(tentative_size, image.info.format);
  1374. }
  1375. total_used_memory += Common::AlignUp(tentative_size, 1024);
  1376. if (total_used_memory > critical_memory && critical_gc < GC_EMERGENCY_COUNTS) {
  1377. RunGarbageCollector();
  1378. critical_gc++;
  1379. }
  1380. image.lru_index = lru_cache.Insert(image_id, frame_tick);
  1381. ForEachGPUPage(image.gpu_addr, image.guest_size_bytes, [this, image_id](u64 page) {
  1382. (*channel_state->gpu_page_table)[page].push_back(image_id);
  1383. });
  1384. if (False(image.flags & ImageFlagBits::Sparse)) {
  1385. auto map_id =
  1386. slot_map_views.insert(image.gpu_addr, image.cpu_addr, image.guest_size_bytes, image_id);
  1387. ForEachCPUPage(image.cpu_addr, image.guest_size_bytes,
  1388. [this, map_id](u64 page) { page_table[page].push_back(map_id); });
  1389. image.map_view_id = map_id;
  1390. return;
  1391. }
  1392. std::vector<ImageViewId> sparse_maps{};
  1393. ForEachSparseSegment(
  1394. image, [this, image_id, &sparse_maps](GPUVAddr gpu_addr, VAddr cpu_addr, size_t size) {
  1395. auto map_id = slot_map_views.insert(gpu_addr, cpu_addr, size, image_id);
  1396. ForEachCPUPage(cpu_addr, size,
  1397. [this, map_id](u64 page) { page_table[page].push_back(map_id); });
  1398. sparse_maps.push_back(map_id);
  1399. });
  1400. sparse_views.emplace(image_id, std::move(sparse_maps));
  1401. ForEachGPUPage(image.gpu_addr, image.guest_size_bytes,
  1402. [this, image_id](u64 page) { sparse_page_table[page].push_back(image_id); });
  1403. }
  1404. template <class P>
  1405. void TextureCache<P>::UnregisterImage(ImageId image_id) {
  1406. Image& image = slot_images[image_id];
  1407. ASSERT_MSG(True(image.flags & ImageFlagBits::Registered),
  1408. "Trying to unregister an already registered image");
  1409. image.flags &= ~ImageFlagBits::Registered;
  1410. image.flags &= ~ImageFlagBits::BadOverlap;
  1411. lru_cache.Free(image.lru_index);
  1412. const auto& clear_page_table =
  1413. [this, image_id](u64 page,
  1414. std::unordered_map<u64, std::vector<ImageId>, Common::IdentityHash<u64>>&
  1415. selected_page_table) {
  1416. const auto page_it = selected_page_table.find(page);
  1417. if (page_it == selected_page_table.end()) {
  1418. ASSERT_MSG(false, "Unregistering unregistered page=0x{:x}", page << YUZU_PAGEBITS);
  1419. return;
  1420. }
  1421. std::vector<ImageId>& image_ids = page_it->second;
  1422. const auto vector_it = std::ranges::find(image_ids, image_id);
  1423. if (vector_it == image_ids.end()) {
  1424. ASSERT_MSG(false, "Unregistering unregistered image in page=0x{:x}",
  1425. page << YUZU_PAGEBITS);
  1426. return;
  1427. }
  1428. image_ids.erase(vector_it);
  1429. };
  1430. ForEachGPUPage(image.gpu_addr, image.guest_size_bytes, [this, &clear_page_table](u64 page) {
  1431. clear_page_table(page, (*channel_state->gpu_page_table));
  1432. });
  1433. if (False(image.flags & ImageFlagBits::Sparse)) {
  1434. const auto map_id = image.map_view_id;
  1435. ForEachCPUPage(image.cpu_addr, image.guest_size_bytes, [this, map_id](u64 page) {
  1436. const auto page_it = page_table.find(page);
  1437. if (page_it == page_table.end()) {
  1438. ASSERT_MSG(false, "Unregistering unregistered page=0x{:x}", page << YUZU_PAGEBITS);
  1439. return;
  1440. }
  1441. std::vector<ImageMapId>& image_map_ids = page_it->second;
  1442. const auto vector_it = std::ranges::find(image_map_ids, map_id);
  1443. if (vector_it == image_map_ids.end()) {
  1444. ASSERT_MSG(false, "Unregistering unregistered image in page=0x{:x}",
  1445. page << YUZU_PAGEBITS);
  1446. return;
  1447. }
  1448. image_map_ids.erase(vector_it);
  1449. });
  1450. slot_map_views.erase(map_id);
  1451. return;
  1452. }
  1453. ForEachGPUPage(image.gpu_addr, image.guest_size_bytes, [this, &clear_page_table](u64 page) {
  1454. clear_page_table(page, sparse_page_table);
  1455. });
  1456. auto it = sparse_views.find(image_id);
  1457. ASSERT(it != sparse_views.end());
  1458. auto& sparse_maps = it->second;
  1459. for (auto& map_view_id : sparse_maps) {
  1460. const auto& map_range = slot_map_views[map_view_id];
  1461. const VAddr cpu_addr = map_range.cpu_addr;
  1462. const std::size_t size = map_range.size;
  1463. ForEachCPUPage(cpu_addr, size, [this, image_id](u64 page) {
  1464. const auto page_it = page_table.find(page);
  1465. if (page_it == page_table.end()) {
  1466. ASSERT_MSG(false, "Unregistering unregistered page=0x{:x}", page << YUZU_PAGEBITS);
  1467. return;
  1468. }
  1469. std::vector<ImageMapId>& image_map_ids = page_it->second;
  1470. auto vector_it = image_map_ids.begin();
  1471. while (vector_it != image_map_ids.end()) {
  1472. ImageMapView& map = slot_map_views[*vector_it];
  1473. if (map.image_id != image_id) {
  1474. vector_it++;
  1475. continue;
  1476. }
  1477. if (!map.picked) {
  1478. map.picked = true;
  1479. }
  1480. vector_it = image_map_ids.erase(vector_it);
  1481. }
  1482. });
  1483. slot_map_views.erase(map_view_id);
  1484. }
  1485. sparse_views.erase(it);
  1486. }
  1487. template <class P>
  1488. void TextureCache<P>::TrackImage(ImageBase& image, ImageId image_id) {
  1489. ASSERT(False(image.flags & ImageFlagBits::Tracked));
  1490. image.flags |= ImageFlagBits::Tracked;
  1491. if (False(image.flags & ImageFlagBits::Sparse)) {
  1492. rasterizer.UpdatePagesCachedCount(image.cpu_addr, image.guest_size_bytes, 1);
  1493. return;
  1494. }
  1495. if (True(image.flags & ImageFlagBits::Registered)) {
  1496. auto it = sparse_views.find(image_id);
  1497. ASSERT(it != sparse_views.end());
  1498. auto& sparse_maps = it->second;
  1499. for (auto& map_view_id : sparse_maps) {
  1500. const auto& map = slot_map_views[map_view_id];
  1501. const VAddr cpu_addr = map.cpu_addr;
  1502. const std::size_t size = map.size;
  1503. rasterizer.UpdatePagesCachedCount(cpu_addr, size, 1);
  1504. }
  1505. return;
  1506. }
  1507. ForEachSparseSegment(image,
  1508. [this]([[maybe_unused]] GPUVAddr gpu_addr, VAddr cpu_addr, size_t size) {
  1509. rasterizer.UpdatePagesCachedCount(cpu_addr, size, 1);
  1510. });
  1511. }
  1512. template <class P>
  1513. void TextureCache<P>::UntrackImage(ImageBase& image, ImageId image_id) {
  1514. ASSERT(True(image.flags & ImageFlagBits::Tracked));
  1515. image.flags &= ~ImageFlagBits::Tracked;
  1516. if (False(image.flags & ImageFlagBits::Sparse)) {
  1517. rasterizer.UpdatePagesCachedCount(image.cpu_addr, image.guest_size_bytes, -1);
  1518. return;
  1519. }
  1520. ASSERT(True(image.flags & ImageFlagBits::Registered));
  1521. auto it = sparse_views.find(image_id);
  1522. ASSERT(it != sparse_views.end());
  1523. auto& sparse_maps = it->second;
  1524. for (auto& map_view_id : sparse_maps) {
  1525. const auto& map = slot_map_views[map_view_id];
  1526. const VAddr cpu_addr = map.cpu_addr;
  1527. const std::size_t size = map.size;
  1528. rasterizer.UpdatePagesCachedCount(cpu_addr, size, -1);
  1529. }
  1530. }
  1531. template <class P>
  1532. void TextureCache<P>::DeleteImage(ImageId image_id, bool immediate_delete) {
  1533. ImageBase& image = slot_images[image_id];
  1534. if (image.HasScaled()) {
  1535. total_used_memory -= GetScaledImageSizeBytes(image);
  1536. }
  1537. u64 tentative_size = std::max(image.guest_size_bytes, image.unswizzled_size_bytes);
  1538. if ((IsPixelFormatASTC(image.info.format) &&
  1539. True(image.flags & ImageFlagBits::AcceleratedUpload)) ||
  1540. True(image.flags & ImageFlagBits::Converted)) {
  1541. tentative_size = EstimatedDecompressedSize(tentative_size, image.info.format);
  1542. }
  1543. total_used_memory -= Common::AlignUp(tentative_size, 1024);
  1544. const GPUVAddr gpu_addr = image.gpu_addr;
  1545. const auto alloc_it = image_allocs_table.find(gpu_addr);
  1546. if (alloc_it == image_allocs_table.end()) {
  1547. ASSERT_MSG(false, "Trying to delete an image alloc that does not exist in address 0x{:x}",
  1548. gpu_addr);
  1549. return;
  1550. }
  1551. const ImageAllocId alloc_id = alloc_it->second;
  1552. std::vector<ImageId>& alloc_images = slot_image_allocs[alloc_id].images;
  1553. const auto alloc_image_it = std::ranges::find(alloc_images, image_id);
  1554. if (alloc_image_it == alloc_images.end()) {
  1555. ASSERT_MSG(false, "Trying to delete an image that does not exist");
  1556. return;
  1557. }
  1558. ASSERT_MSG(False(image.flags & ImageFlagBits::Tracked), "Image was not untracked");
  1559. ASSERT_MSG(False(image.flags & ImageFlagBits::Registered), "Image was not unregistered");
  1560. // Mark render targets as dirty
  1561. auto& dirty = maxwell3d->dirty.flags;
  1562. dirty[Dirty::RenderTargets] = true;
  1563. dirty[Dirty::ZetaBuffer] = true;
  1564. for (size_t rt = 0; rt < NUM_RT; ++rt) {
  1565. dirty[Dirty::ColorBuffer0 + rt] = true;
  1566. }
  1567. const std::span<const ImageViewId> image_view_ids = image.image_view_ids;
  1568. for (const ImageViewId image_view_id : image_view_ids) {
  1569. std::ranges::replace(render_targets.color_buffer_ids, image_view_id, ImageViewId{});
  1570. if (render_targets.depth_buffer_id == image_view_id) {
  1571. render_targets.depth_buffer_id = ImageViewId{};
  1572. }
  1573. }
  1574. RemoveImageViewReferences(image_view_ids);
  1575. RemoveFramebuffers(image_view_ids);
  1576. for (const AliasedImage& alias : image.aliased_images) {
  1577. ImageBase& other_image = slot_images[alias.id];
  1578. [[maybe_unused]] const size_t num_removed_aliases =
  1579. std::erase_if(other_image.aliased_images, [image_id](const AliasedImage& other_alias) {
  1580. return other_alias.id == image_id;
  1581. });
  1582. other_image.CheckAliasState();
  1583. ASSERT_MSG(num_removed_aliases == 1, "Invalid number of removed aliases: {}",
  1584. num_removed_aliases);
  1585. }
  1586. for (const ImageId overlap_id : image.overlapping_images) {
  1587. ImageBase& other_image = slot_images[overlap_id];
  1588. [[maybe_unused]] const size_t num_removed_overlaps = std::erase_if(
  1589. other_image.overlapping_images,
  1590. [image_id](const ImageId other_overlap_id) { return other_overlap_id == image_id; });
  1591. other_image.CheckBadOverlapState();
  1592. ASSERT_MSG(num_removed_overlaps == 1, "Invalid number of removed overlapps: {}",
  1593. num_removed_overlaps);
  1594. }
  1595. for (const ImageViewId image_view_id : image_view_ids) {
  1596. if (!immediate_delete) {
  1597. sentenced_image_view.Push(std::move(slot_image_views[image_view_id]));
  1598. }
  1599. slot_image_views.erase(image_view_id);
  1600. }
  1601. if (!immediate_delete) {
  1602. sentenced_images.Push(std::move(slot_images[image_id]));
  1603. }
  1604. slot_images.erase(image_id);
  1605. alloc_images.erase(alloc_image_it);
  1606. if (alloc_images.empty()) {
  1607. image_allocs_table.erase(alloc_it);
  1608. }
  1609. for (size_t c : active_channel_ids) {
  1610. auto& channel_info = channel_storage[c];
  1611. if constexpr (ENABLE_VALIDATION) {
  1612. std::ranges::fill(channel_info.graphics_image_view_ids, CORRUPT_ID);
  1613. std::ranges::fill(channel_info.compute_image_view_ids, CORRUPT_ID);
  1614. }
  1615. channel_info.graphics_image_table.Invalidate();
  1616. channel_info.compute_image_table.Invalidate();
  1617. }
  1618. has_deleted_images = true;
  1619. }
  1620. template <class P>
  1621. void TextureCache<P>::RemoveImageViewReferences(std::span<const ImageViewId> removed_views) {
  1622. for (size_t c : active_channel_ids) {
  1623. auto& channel_info = channel_storage[c];
  1624. auto it = channel_info.image_views.begin();
  1625. while (it != channel_info.image_views.end()) {
  1626. const auto found = std::ranges::find(removed_views, it->second);
  1627. if (found != removed_views.end()) {
  1628. it = channel_info.image_views.erase(it);
  1629. } else {
  1630. ++it;
  1631. }
  1632. }
  1633. }
  1634. }
  1635. template <class P>
  1636. void TextureCache<P>::RemoveFramebuffers(std::span<const ImageViewId> removed_views) {
  1637. auto it = framebuffers.begin();
  1638. while (it != framebuffers.end()) {
  1639. if (it->first.Contains(removed_views)) {
  1640. auto framebuffer_id = it->second;
  1641. ASSERT(framebuffer_id);
  1642. sentenced_framebuffers.Push(std::move(slot_framebuffers[framebuffer_id]));
  1643. it = framebuffers.erase(it);
  1644. } else {
  1645. ++it;
  1646. }
  1647. }
  1648. }
  1649. template <class P>
  1650. void TextureCache<P>::MarkModification(ImageBase& image) noexcept {
  1651. image.flags |= ImageFlagBits::GpuModified;
  1652. image.modification_tick = ++modification_tick;
  1653. }
  1654. template <class P>
  1655. void TextureCache<P>::SynchronizeAliases(ImageId image_id) {
  1656. boost::container::small_vector<const AliasedImage*, 1> aliased_images;
  1657. Image& image = slot_images[image_id];
  1658. bool any_rescaled = True(image.flags & ImageFlagBits::Rescaled);
  1659. u64 most_recent_tick = image.modification_tick;
  1660. for (const AliasedImage& aliased : image.aliased_images) {
  1661. ImageBase& aliased_image = slot_images[aliased.id];
  1662. if (image.modification_tick < aliased_image.modification_tick) {
  1663. most_recent_tick = std::max(most_recent_tick, aliased_image.modification_tick);
  1664. aliased_images.push_back(&aliased);
  1665. any_rescaled |= True(aliased_image.flags & ImageFlagBits::Rescaled);
  1666. if (True(aliased_image.flags & ImageFlagBits::GpuModified)) {
  1667. image.flags |= ImageFlagBits::GpuModified;
  1668. }
  1669. }
  1670. }
  1671. if (aliased_images.empty()) {
  1672. return;
  1673. }
  1674. const bool can_rescale = ImageCanRescale(image);
  1675. if (any_rescaled) {
  1676. if (can_rescale) {
  1677. ScaleUp(image);
  1678. } else {
  1679. ScaleDown(image);
  1680. }
  1681. }
  1682. image.modification_tick = most_recent_tick;
  1683. std::ranges::sort(aliased_images, [this](const AliasedImage* lhs, const AliasedImage* rhs) {
  1684. const ImageBase& lhs_image = slot_images[lhs->id];
  1685. const ImageBase& rhs_image = slot_images[rhs->id];
  1686. return lhs_image.modification_tick < rhs_image.modification_tick;
  1687. });
  1688. const auto& resolution = Settings::values.resolution_info;
  1689. for (const AliasedImage* const aliased : aliased_images) {
  1690. if (!resolution.active || !any_rescaled) {
  1691. CopyImage(image_id, aliased->id, aliased->copies);
  1692. continue;
  1693. }
  1694. Image& aliased_image = slot_images[aliased->id];
  1695. if (!can_rescale) {
  1696. ScaleDown(aliased_image);
  1697. CopyImage(image_id, aliased->id, aliased->copies);
  1698. continue;
  1699. }
  1700. ScaleUp(aliased_image);
  1701. CopyImage(image_id, aliased->id, aliased->copies);
  1702. }
  1703. }
  1704. template <class P>
  1705. void TextureCache<P>::PrepareImage(ImageId image_id, bool is_modification, bool invalidate) {
  1706. Image& image = slot_images[image_id];
  1707. if (invalidate) {
  1708. image.flags &= ~(ImageFlagBits::CpuModified | ImageFlagBits::GpuModified);
  1709. if (False(image.flags & ImageFlagBits::Tracked)) {
  1710. TrackImage(image, image_id);
  1711. }
  1712. } else {
  1713. RefreshContents(image, image_id);
  1714. SynchronizeAliases(image_id);
  1715. }
  1716. if (is_modification) {
  1717. MarkModification(image);
  1718. }
  1719. lru_cache.Touch(image.lru_index, frame_tick);
  1720. }
  1721. template <class P>
  1722. void TextureCache<P>::PrepareImageView(ImageViewId image_view_id, bool is_modification,
  1723. bool invalidate) {
  1724. if (!image_view_id) {
  1725. return;
  1726. }
  1727. const ImageViewBase& image_view = slot_image_views[image_view_id];
  1728. if (image_view.IsBuffer()) {
  1729. return;
  1730. }
  1731. PrepareImage(image_view.image_id, is_modification, invalidate);
  1732. }
  1733. template <class P>
  1734. void TextureCache<P>::CopyImage(ImageId dst_id, ImageId src_id, std::vector<ImageCopy> copies) {
  1735. Image& dst = slot_images[dst_id];
  1736. Image& src = slot_images[src_id];
  1737. const bool is_rescaled = True(src.flags & ImageFlagBits::Rescaled);
  1738. if (is_rescaled) {
  1739. ASSERT(True(dst.flags & ImageFlagBits::Rescaled));
  1740. const bool both_2d{src.info.type == ImageType::e2D && dst.info.type == ImageType::e2D};
  1741. const auto& resolution = Settings::values.resolution_info;
  1742. for (auto& copy : copies) {
  1743. copy.src_offset.x = resolution.ScaleUp(copy.src_offset.x);
  1744. copy.dst_offset.x = resolution.ScaleUp(copy.dst_offset.x);
  1745. copy.extent.width = resolution.ScaleUp(copy.extent.width);
  1746. if (both_2d) {
  1747. copy.src_offset.y = resolution.ScaleUp(copy.src_offset.y);
  1748. copy.dst_offset.y = resolution.ScaleUp(copy.dst_offset.y);
  1749. copy.extent.height = resolution.ScaleUp(copy.extent.height);
  1750. }
  1751. }
  1752. }
  1753. const auto dst_format_type = GetFormatType(dst.info.format);
  1754. const auto src_format_type = GetFormatType(src.info.format);
  1755. if (src_format_type == dst_format_type) {
  1756. if constexpr (HAS_EMULATED_COPIES) {
  1757. if (!runtime.CanImageBeCopied(dst, src)) {
  1758. return runtime.EmulateCopyImage(dst, src, copies);
  1759. }
  1760. }
  1761. return runtime.CopyImage(dst, src, copies);
  1762. }
  1763. UNIMPLEMENTED_IF(dst.info.type != ImageType::e2D);
  1764. UNIMPLEMENTED_IF(src.info.type != ImageType::e2D);
  1765. if (runtime.ShouldReinterpret(dst, src)) {
  1766. return runtime.ReinterpretImage(dst, src, copies);
  1767. }
  1768. for (const ImageCopy& copy : copies) {
  1769. UNIMPLEMENTED_IF(copy.dst_subresource.num_layers != 1);
  1770. UNIMPLEMENTED_IF(copy.src_subresource.num_layers != 1);
  1771. UNIMPLEMENTED_IF(copy.src_offset != Offset3D{});
  1772. UNIMPLEMENTED_IF(copy.dst_offset != Offset3D{});
  1773. const SubresourceBase dst_base{
  1774. .level = copy.dst_subresource.base_level,
  1775. .layer = copy.dst_subresource.base_layer,
  1776. };
  1777. const SubresourceBase src_base{
  1778. .level = copy.src_subresource.base_level,
  1779. .layer = copy.src_subresource.base_layer,
  1780. };
  1781. const SubresourceExtent dst_extent{.levels = 1, .layers = 1};
  1782. const SubresourceExtent src_extent{.levels = 1, .layers = 1};
  1783. const SubresourceRange dst_range{.base = dst_base, .extent = dst_extent};
  1784. const SubresourceRange src_range{.base = src_base, .extent = src_extent};
  1785. PixelFormat dst_format = dst.info.format;
  1786. if (GetFormatType(src.info.format) == SurfaceType::DepthStencil &&
  1787. GetFormatType(dst_format) == SurfaceType::ColorTexture &&
  1788. BytesPerBlock(dst_format) == 4) {
  1789. dst_format = PixelFormat::A8B8G8R8_UNORM;
  1790. }
  1791. const ImageViewInfo dst_view_info(ImageViewType::e2D, dst_format, dst_range);
  1792. const ImageViewInfo src_view_info(ImageViewType::e2D, src.info.format, src_range);
  1793. const auto [dst_framebuffer_id, dst_view_id] = RenderTargetFromImage(dst_id, dst_view_info);
  1794. Framebuffer* const dst_framebuffer = &slot_framebuffers[dst_framebuffer_id];
  1795. const ImageViewId src_view_id = FindOrEmplaceImageView(src_id, src_view_info);
  1796. ImageView& dst_view = slot_image_views[dst_view_id];
  1797. ImageView& src_view = slot_image_views[src_view_id];
  1798. [[maybe_unused]] const Extent3D expected_size{
  1799. .width = std::min(dst_view.size.width, src_view.size.width),
  1800. .height = std::min(dst_view.size.height, src_view.size.height),
  1801. .depth = std::min(dst_view.size.depth, src_view.size.depth),
  1802. };
  1803. const Extent3D scaled_extent = [is_rescaled, expected_size]() {
  1804. if (!is_rescaled) {
  1805. return expected_size;
  1806. }
  1807. const auto& resolution = Settings::values.resolution_info;
  1808. return Extent3D{
  1809. .width = resolution.ScaleUp(expected_size.width),
  1810. .height = resolution.ScaleUp(expected_size.height),
  1811. .depth = expected_size.depth,
  1812. };
  1813. }();
  1814. UNIMPLEMENTED_IF(copy.extent != scaled_extent);
  1815. runtime.ConvertImage(dst_framebuffer, dst_view, src_view);
  1816. }
  1817. }
  1818. template <class P>
  1819. void TextureCache<P>::BindRenderTarget(ImageViewId* old_id, ImageViewId new_id) {
  1820. if (*old_id == new_id) {
  1821. return;
  1822. }
  1823. if (new_id) {
  1824. const ImageViewBase& old_view = slot_image_views[new_id];
  1825. if (True(old_view.flags & ImageViewFlagBits::PreemtiveDownload)) {
  1826. uncommitted_downloads.push_back(old_view.image_id);
  1827. }
  1828. }
  1829. *old_id = new_id;
  1830. }
  1831. template <class P>
  1832. std::pair<FramebufferId, ImageViewId> TextureCache<P>::RenderTargetFromImage(
  1833. ImageId image_id, const ImageViewInfo& view_info) {
  1834. const ImageViewId view_id = FindOrEmplaceImageView(image_id, view_info);
  1835. const ImageBase& image = slot_images[image_id];
  1836. const bool is_rescaled = True(image.flags & ImageFlagBits::Rescaled);
  1837. const bool is_color = GetFormatType(image.info.format) == SurfaceType::ColorTexture;
  1838. const ImageViewId color_view_id = is_color ? view_id : ImageViewId{};
  1839. const ImageViewId depth_view_id = is_color ? ImageViewId{} : view_id;
  1840. Extent3D extent = MipSize(image.info.size, view_info.range.base.level);
  1841. if (is_rescaled) {
  1842. const auto& resolution = Settings::values.resolution_info;
  1843. extent.width = resolution.ScaleUp(extent.width);
  1844. if (image.info.type == ImageType::e2D) {
  1845. extent.height = resolution.ScaleUp(extent.height);
  1846. }
  1847. }
  1848. const u32 num_samples = image.info.num_samples;
  1849. const auto [samples_x, samples_y] = SamplesLog2(num_samples);
  1850. const FramebufferId framebuffer_id = GetFramebufferId(RenderTargets{
  1851. .color_buffer_ids = {color_view_id},
  1852. .depth_buffer_id = depth_view_id,
  1853. .size = {extent.width >> samples_x, extent.height >> samples_y},
  1854. });
  1855. return {framebuffer_id, view_id};
  1856. }
  1857. template <class P>
  1858. bool TextureCache<P>::IsFullClear(ImageViewId id) {
  1859. if (!id) {
  1860. return true;
  1861. }
  1862. const ImageViewBase& image_view = slot_image_views[id];
  1863. const ImageBase& image = slot_images[image_view.image_id];
  1864. const Extent3D size = image_view.size;
  1865. const auto& regs = maxwell3d->regs;
  1866. const auto& scissor = regs.scissor_test[0];
  1867. if (image.info.resources.levels > 1 || image.info.resources.layers > 1) {
  1868. // Images with multiple resources can't be cleared in a single call
  1869. return false;
  1870. }
  1871. if (regs.clear_flags.scissor == 0) {
  1872. // If scissor testing is disabled, the clear is always full
  1873. return true;
  1874. }
  1875. // Make sure the clear covers all texels in the subresource
  1876. return scissor.min_x == 0 && scissor.min_y == 0 && scissor.max_x >= size.width &&
  1877. scissor.max_y >= size.height;
  1878. }
  1879. template <class P>
  1880. void TextureCache<P>::CreateChannel(struct Tegra::Control::ChannelState& channel) {
  1881. VideoCommon::ChannelSetupCaches<TextureCacheChannelInfo>::CreateChannel(channel);
  1882. const auto it = channel_map.find(channel.bind_id);
  1883. auto* this_state = &channel_storage[it->second];
  1884. const auto& this_as_ref = address_spaces[channel.memory_manager->GetID()];
  1885. this_state->gpu_page_table = &gpu_page_table_storage[this_as_ref.storage_id];
  1886. }
  1887. /// Bind a channel for execution.
  1888. template <class P>
  1889. void TextureCache<P>::OnGPUASRegister([[maybe_unused]] size_t map_id) {
  1890. gpu_page_table_storage.emplace_back();
  1891. }
  1892. } // namespace VideoCommon