Texture.cs 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Common.Memory;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.Gpu.Memory;
  5. using Ryujinx.Graphics.Texture;
  6. using Ryujinx.Graphics.Texture.Astc;
  7. using Ryujinx.Memory;
  8. using Ryujinx.Memory.Range;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Numerics;
  14. namespace Ryujinx.Graphics.Gpu.Image
  15. {
  16. /// <summary>
  17. /// Represents a cached GPU texture.
  18. /// </summary>
  19. class Texture : IMultiRangeItem, IDisposable
  20. {
  21. // How many updates we need before switching to the byte-by-byte comparison
  22. // modification check method.
  23. // This method uses much more memory so we want to avoid it if possible.
  24. private const int ByteComparisonSwitchThreshold = 4;
  25. // Tuning for blacklisting textures from scaling when their data is updated from CPU.
  26. // Each write adds the weight, each GPU modification subtracts 1.
  27. // Exceeding the threshold blacklists the texture.
  28. private const int ScaledSetWeight = 10;
  29. private const int ScaledSetThreshold = 30;
  30. private const int MinLevelsForForceAnisotropy = 5;
  31. private struct TexturePoolOwner
  32. {
  33. public TexturePool Pool;
  34. public int ID;
  35. public ulong GpuAddress;
  36. }
  37. private GpuContext _context;
  38. private PhysicalMemory _physicalMemory;
  39. private SizeInfo _sizeInfo;
  40. /// <summary>
  41. /// Texture format.
  42. /// </summary>
  43. public Format Format => Info.FormatInfo.Format;
  44. /// <summary>
  45. /// Texture target.
  46. /// </summary>
  47. public Target Target { get; private set; }
  48. /// <summary>
  49. /// Texture width.
  50. /// </summary>
  51. public int Width { get; private set; }
  52. /// <summary>
  53. /// Texture height.
  54. /// </summary>
  55. public int Height { get; private set; }
  56. /// <summary>
  57. /// Texture information.
  58. /// </summary>
  59. public TextureInfo Info { get; private set; }
  60. /// <summary>
  61. /// Set when anisotropic filtering can be forced on the given texture.
  62. /// </summary>
  63. public bool CanForceAnisotropy { get; private set; }
  64. /// <summary>
  65. /// Host scale factor.
  66. /// </summary>
  67. public float ScaleFactor { get; private set; }
  68. /// <summary>
  69. /// Upscaling mode. Informs if a texture is scaled, or is eligible for scaling.
  70. /// </summary>
  71. public TextureScaleMode ScaleMode { get; private set; }
  72. /// <summary>
  73. /// Group that this texture belongs to. Manages read/write memory tracking.
  74. /// </summary>
  75. public TextureGroup Group { get; private set; }
  76. /// <summary>
  77. /// Set when a texture's GPU VA has ever been partially or fully unmapped.
  78. /// This indicates that the range must be fully checked when matching the texture.
  79. /// </summary>
  80. public bool ChangedMapping { get; private set; }
  81. /// <summary>
  82. /// True if the data for this texture must always be flushed when an overlap appears.
  83. /// This is useful if SetData is called directly on this texture, but the data is meant for a future texture.
  84. /// </summary>
  85. public bool AlwaysFlushOnOverlap { get; private set; }
  86. /// <summary>
  87. /// Increments when the host texture is swapped, or when the texture is removed from all pools.
  88. /// </summary>
  89. public int InvalidatedSequence { get; private set; }
  90. private int _depth;
  91. private int _layers;
  92. public int FirstLayer { get; private set; }
  93. public int FirstLevel { get; private set; }
  94. private bool _hasData;
  95. private bool _dirty = true;
  96. private int _updateCount;
  97. private byte[] _currentData;
  98. private bool _modifiedStale = true;
  99. private ITexture _arrayViewTexture;
  100. private Target _arrayViewTarget;
  101. private ITexture _flushHostTexture;
  102. private ITexture _setHostTexture;
  103. private int _scaledSetScore;
  104. private Texture _viewStorage;
  105. private List<Texture> _views;
  106. /// <summary>
  107. /// Host texture.
  108. /// </summary>
  109. public ITexture HostTexture { get; private set; }
  110. /// <summary>
  111. /// Intrusive linked list node used on the auto deletion texture cache.
  112. /// </summary>
  113. public LinkedListNode<Texture> CacheNode { get; set; }
  114. /// <summary>
  115. /// Entry for this texture in the short duration cache, if present.
  116. /// </summary>
  117. public ShortTextureCacheEntry ShortCacheEntry { get; set; }
  118. /// <summary>
  119. /// Whether this texture has ever been referenced by a pool.
  120. /// </summary>
  121. public bool HadPoolOwner { get; private set; }
  122. /// <summary>
  123. /// Physical memory ranges where the texture data is located.
  124. /// </summary>
  125. public MultiRange Range { get; private set; }
  126. /// <summary>
  127. /// Layer size in bytes.
  128. /// </summary>
  129. public int LayerSize => _sizeInfo.LayerSize;
  130. /// <summary>
  131. /// Texture size in bytes.
  132. /// </summary>
  133. public ulong Size => (ulong)_sizeInfo.TotalSize;
  134. /// <summary>
  135. /// Whether or not the texture belongs is a view.
  136. /// </summary>
  137. public bool IsView => _viewStorage != this;
  138. /// <summary>
  139. /// Whether or not this texture has views.
  140. /// </summary>
  141. public bool HasViews => _views.Count > 0;
  142. private int _referenceCount;
  143. private List<TexturePoolOwner> _poolOwners;
  144. /// <summary>
  145. /// Constructs a new instance of the cached GPU texture.
  146. /// </summary>
  147. /// <param name="context">GPU context that the texture belongs to</param>
  148. /// <param name="physicalMemory">Physical memory where the texture is mapped</param>
  149. /// <param name="info">Texture information</param>
  150. /// <param name="sizeInfo">Size information of the texture</param>
  151. /// <param name="range">Physical memory ranges where the texture data is located</param>
  152. /// <param name="firstLayer">The first layer of the texture, or 0 if the texture has no parent</param>
  153. /// <param name="firstLevel">The first mipmap level of the texture, or 0 if the texture has no parent</param>
  154. /// <param name="scaleFactor">The floating point scale factor to initialize with</param>
  155. /// <param name="scaleMode">The scale mode to initialize with</param>
  156. private Texture(
  157. GpuContext context,
  158. PhysicalMemory physicalMemory,
  159. TextureInfo info,
  160. SizeInfo sizeInfo,
  161. MultiRange range,
  162. int firstLayer,
  163. int firstLevel,
  164. float scaleFactor,
  165. TextureScaleMode scaleMode)
  166. {
  167. InitializeTexture(context, physicalMemory, info, sizeInfo, range);
  168. FirstLayer = firstLayer;
  169. FirstLevel = firstLevel;
  170. ScaleFactor = scaleFactor;
  171. ScaleMode = scaleMode;
  172. InitializeData(true);
  173. }
  174. /// <summary>
  175. /// Constructs a new instance of the cached GPU texture.
  176. /// </summary>
  177. /// <param name="context">GPU context that the texture belongs to</param>
  178. /// <param name="physicalMemory">Physical memory where the texture is mapped</param>
  179. /// <param name="info">Texture information</param>
  180. /// <param name="sizeInfo">Size information of the texture</param>
  181. /// <param name="range">Physical memory ranges where the texture data is located</param>
  182. /// <param name="scaleMode">The scale mode to initialize with. If scaled, the texture's data is loaded immediately and scaled up</param>
  183. public Texture(
  184. GpuContext context,
  185. PhysicalMemory physicalMemory,
  186. TextureInfo info,
  187. SizeInfo sizeInfo,
  188. MultiRange range,
  189. TextureScaleMode scaleMode)
  190. {
  191. ScaleFactor = 1f; // Texture is first loaded at scale 1x.
  192. ScaleMode = scaleMode;
  193. InitializeTexture(context, physicalMemory, info, sizeInfo, range);
  194. }
  195. /// <summary>
  196. /// Common texture initialization method.
  197. /// This sets the context, info and sizeInfo fields.
  198. /// Other fields are initialized with their default values.
  199. /// </summary>
  200. /// <param name="context">GPU context that the texture belongs to</param>
  201. /// <param name="physicalMemory">Physical memory where the texture is mapped</param>
  202. /// <param name="info">Texture information</param>
  203. /// <param name="sizeInfo">Size information of the texture</param>
  204. /// <param name="range">Physical memory ranges where the texture data is located</param>
  205. private void InitializeTexture(
  206. GpuContext context,
  207. PhysicalMemory physicalMemory,
  208. TextureInfo info,
  209. SizeInfo sizeInfo,
  210. MultiRange range)
  211. {
  212. _context = context;
  213. _physicalMemory = physicalMemory;
  214. _sizeInfo = sizeInfo;
  215. Range = range;
  216. SetInfo(info);
  217. _viewStorage = this;
  218. _views = new List<Texture>();
  219. _poolOwners = new List<TexturePoolOwner>();
  220. }
  221. /// <summary>
  222. /// Initializes the data for a texture. Can optionally initialize the texture with or without data.
  223. /// If the texture is a view, it will initialize memory tracking to be non-dirty.
  224. /// </summary>
  225. /// <param name="isView">True if the texture is a view, false otherwise</param>
  226. /// <param name="withData">True if the texture is to be initialized with data</param>
  227. public void InitializeData(bool isView, bool withData = false)
  228. {
  229. withData |= Group != null && Group.FlushIncompatibleOverlapsIfNeeded();
  230. if (withData)
  231. {
  232. Debug.Assert(!isView);
  233. TextureCreateInfo createInfo = TextureCache.GetCreateInfo(Info, _context.Capabilities, ScaleFactor);
  234. HostTexture = _context.Renderer.CreateTexture(createInfo);
  235. SynchronizeMemory(); // Load the data.
  236. if (ScaleMode == TextureScaleMode.Scaled)
  237. {
  238. SetScale(GraphicsConfig.ResScale); // Scale the data up.
  239. }
  240. }
  241. else
  242. {
  243. _hasData = true;
  244. if (!isView)
  245. {
  246. // Don't update this texture the next time we synchronize.
  247. CheckModified(true);
  248. if (ScaleMode == TextureScaleMode.Scaled)
  249. {
  250. // Don't need to start at 1x as there is no data to scale, just go straight to the target scale.
  251. ScaleFactor = GraphicsConfig.ResScale;
  252. }
  253. TextureCreateInfo createInfo = TextureCache.GetCreateInfo(Info, _context.Capabilities, ScaleFactor);
  254. HostTexture = _context.Renderer.CreateTexture(createInfo);
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// Initialize a new texture group with this texture as storage.
  260. /// </summary>
  261. /// <param name="hasLayerViews">True if the texture will have layer views</param>
  262. /// <param name="hasMipViews">True if the texture will have mip views</param>
  263. /// <param name="incompatibleOverlaps">Groups that overlap with this one but are incompatible</param>
  264. public void InitializeGroup(bool hasLayerViews, bool hasMipViews, List<TextureIncompatibleOverlap> incompatibleOverlaps)
  265. {
  266. Group = new TextureGroup(_context, _physicalMemory, this, incompatibleOverlaps);
  267. Group.Initialize(ref _sizeInfo, hasLayerViews, hasMipViews);
  268. }
  269. /// <summary>
  270. /// Create a texture view from this texture.
  271. /// A texture view is defined as a child texture, from a sub-range of their parent texture.
  272. /// For example, the initial layer and mipmap level of the view can be defined, so the texture
  273. /// will start at the given layer/level of the parent texture.
  274. /// </summary>
  275. /// <param name="info">Child texture information</param>
  276. /// <param name="sizeInfo">Child texture size information</param>
  277. /// <param name="range">Physical memory ranges where the texture data is located</param>
  278. /// <param name="firstLayer">Start layer of the child texture on the parent texture</param>
  279. /// <param name="firstLevel">Start mipmap level of the child texture on the parent texture</param>
  280. /// <returns>The child texture</returns>
  281. public Texture CreateView(TextureInfo info, SizeInfo sizeInfo, MultiRange range, int firstLayer, int firstLevel)
  282. {
  283. Texture texture = new(
  284. _context,
  285. _physicalMemory,
  286. info,
  287. sizeInfo,
  288. range,
  289. FirstLayer + firstLayer,
  290. FirstLevel + firstLevel,
  291. ScaleFactor,
  292. ScaleMode);
  293. TextureCreateInfo createInfo = TextureCache.GetCreateInfo(info, _context.Capabilities, ScaleFactor);
  294. texture.HostTexture = HostTexture.CreateView(createInfo, firstLayer, firstLevel);
  295. _viewStorage.AddView(texture);
  296. return texture;
  297. }
  298. /// <summary>
  299. /// Adds a child texture to this texture.
  300. /// </summary>
  301. /// <param name="texture">The child texture</param>
  302. private void AddView(Texture texture)
  303. {
  304. IncrementReferenceCount();
  305. _views.Add(texture);
  306. texture._viewStorage = this;
  307. Group.UpdateViews(_views, texture);
  308. if (texture.Group != null && texture.Group != Group)
  309. {
  310. if (texture.Group.Storage == texture)
  311. {
  312. // This texture's group is no longer used.
  313. Group.Inherit(texture.Group);
  314. texture.Group.Dispose();
  315. }
  316. }
  317. texture.Group = Group;
  318. }
  319. /// <summary>
  320. /// Removes a child texture from this texture.
  321. /// </summary>
  322. /// <param name="texture">The child texture</param>
  323. private void RemoveView(Texture texture)
  324. {
  325. _views.Remove(texture);
  326. Group.RemoveView(texture);
  327. texture._viewStorage = texture;
  328. DecrementReferenceCount();
  329. }
  330. /// <summary>
  331. /// Replaces the texture's physical memory range. This forces tracking to regenerate.
  332. /// </summary>
  333. /// <param name="range">New physical memory range backing the texture</param>
  334. public void ReplaceRange(MultiRange range)
  335. {
  336. Range = range;
  337. Group.RangeChanged();
  338. }
  339. /// <summary>
  340. /// Create a copy dependency to a texture that is view compatible with this one.
  341. /// When either texture is modified, the texture data will be copied to the other to keep them in sync.
  342. /// This is essentially an emulated view, useful for handling multiple view parents or format incompatibility.
  343. /// This also forces a copy on creation, to or from the given texture to get them in sync immediately.
  344. /// </summary>
  345. /// <param name="contained">The view compatible texture to create a dependency to</param>
  346. /// <param name="layer">The base layer of the given texture relative to this one</param>
  347. /// <param name="level">The base level of the given texture relative to this one</param>
  348. /// <param name="copyTo">True if this texture is first copied to the given one, false for the opposite direction</param>
  349. public void CreateCopyDependency(Texture contained, int layer, int level, bool copyTo)
  350. {
  351. if (contained.Group == Group)
  352. {
  353. return;
  354. }
  355. Group.CreateCopyDependency(contained, FirstLayer + layer, FirstLevel + level, copyTo);
  356. }
  357. /// <summary>
  358. /// Registers when a texture has had its data set after being scaled, and
  359. /// determines if it should be blacklisted from scaling to improve performance.
  360. /// </summary>
  361. /// <returns>True if setting data for a scaled texture is allowed, false if the texture has been blacklisted</returns>
  362. private bool AllowScaledSetData()
  363. {
  364. _scaledSetScore += ScaledSetWeight;
  365. if (_scaledSetScore >= ScaledSetThreshold)
  366. {
  367. BlacklistScale();
  368. return false;
  369. }
  370. return true;
  371. }
  372. /// <summary>
  373. /// Blacklists this texture from being scaled. Resets its scale to 1 if needed.
  374. /// </summary>
  375. public void BlacklistScale()
  376. {
  377. ScaleMode = TextureScaleMode.Blacklisted;
  378. SetScale(1f);
  379. }
  380. /// <summary>
  381. /// Propagates the scale between this texture and another to ensure they have the same scale.
  382. /// If one texture is blacklisted from scaling, the other will become blacklisted too.
  383. /// </summary>
  384. /// <param name="other">The other texture</param>
  385. public void PropagateScale(Texture other)
  386. {
  387. if (other.ScaleMode == TextureScaleMode.Blacklisted || ScaleMode == TextureScaleMode.Blacklisted)
  388. {
  389. BlacklistScale();
  390. other.BlacklistScale();
  391. }
  392. else
  393. {
  394. // Prefer the configured scale if present. If not, prefer the max.
  395. float targetScale = GraphicsConfig.ResScale;
  396. float sharedScale = (ScaleFactor == targetScale || other.ScaleFactor == targetScale) ? targetScale : Math.Max(ScaleFactor, other.ScaleFactor);
  397. SetScale(sharedScale);
  398. other.SetScale(sharedScale);
  399. }
  400. }
  401. /// <summary>
  402. /// Copy the host texture to a scaled one. If a texture is not provided, create it with the given scale.
  403. /// </summary>
  404. /// <param name="scale">Scale factor</param>
  405. /// <param name="copy">True if the data should be copied to the texture, false otherwise</param>
  406. /// <param name="storage">Texture to use instead of creating one</param>
  407. /// <returns>A host texture containing a scaled version of this texture</returns>
  408. private ITexture GetScaledHostTexture(float scale, bool copy, ITexture storage = null)
  409. {
  410. if (storage == null)
  411. {
  412. TextureCreateInfo createInfo = TextureCache.GetCreateInfo(Info, _context.Capabilities, scale);
  413. storage = _context.Renderer.CreateTexture(createInfo);
  414. }
  415. if (copy)
  416. {
  417. HostTexture.CopyTo(storage, new Extents2D(0, 0, HostTexture.Width, HostTexture.Height), new Extents2D(0, 0, storage.Width, storage.Height), true);
  418. }
  419. return storage;
  420. }
  421. /// <summary>
  422. /// Sets the Scale Factor on this texture, and immediately recreates it at the correct size.
  423. /// When a texture is resized, a scaled copy is performed from the old texture to the new one, to ensure no data is lost.
  424. /// If scale is equivalent, this only propagates the blacklisted/scaled mode.
  425. /// If called on a view, its storage is resized instead.
  426. /// When resizing storage, all texture views are recreated.
  427. /// </summary>
  428. /// <param name="scale">The new scale factor for this texture</param>
  429. public void SetScale(float scale)
  430. {
  431. bool unscaled = ScaleMode == TextureScaleMode.Blacklisted || (ScaleMode == TextureScaleMode.Undesired && scale == 1);
  432. TextureScaleMode newScaleMode = unscaled ? ScaleMode : TextureScaleMode.Scaled;
  433. if (_viewStorage != this)
  434. {
  435. _viewStorage.ScaleMode = newScaleMode;
  436. _viewStorage.SetScale(scale);
  437. return;
  438. }
  439. if (ScaleFactor != scale)
  440. {
  441. Logger.Debug?.Print(LogClass.Gpu, $"Rescaling {Info.Width}x{Info.Height} {Info.FormatInfo.Format} to ({ScaleFactor} to {scale}). ");
  442. ScaleFactor = scale;
  443. ITexture newStorage = GetScaledHostTexture(ScaleFactor, true);
  444. Logger.Debug?.Print(LogClass.Gpu, $" Copy performed: {HostTexture.Width}x{HostTexture.Height} to {newStorage.Width}x{newStorage.Height}");
  445. ReplaceStorage(newStorage);
  446. // All views must be recreated against the new storage.
  447. foreach (var view in _views)
  448. {
  449. Logger.Debug?.Print(LogClass.Gpu, $" Recreating view {Info.Width}x{Info.Height} {Info.FormatInfo.Format}.");
  450. view.ScaleFactor = scale;
  451. TextureCreateInfo viewCreateInfo = TextureCache.GetCreateInfo(view.Info, _context.Capabilities, scale);
  452. ITexture newView = HostTexture.CreateView(viewCreateInfo, view.FirstLayer - FirstLayer, view.FirstLevel - FirstLevel);
  453. view.ReplaceStorage(newView);
  454. view.ScaleMode = newScaleMode;
  455. }
  456. }
  457. if (ScaleMode != newScaleMode)
  458. {
  459. ScaleMode = newScaleMode;
  460. foreach (var view in _views)
  461. {
  462. view.ScaleMode = newScaleMode;
  463. }
  464. }
  465. }
  466. /// <summary>
  467. /// Checks if the memory for this texture was modified, and returns true if it was.
  468. /// The modified flags are optionally consumed as a result.
  469. /// </summary>
  470. /// <param name="consume">True to consume the dirty flags and reprotect, false to leave them as is</param>
  471. /// <returns>True if the texture was modified, false otherwise.</returns>
  472. public bool CheckModified(bool consume)
  473. {
  474. return Group.CheckDirty(this, consume);
  475. }
  476. /// <summary>
  477. /// Discards all data for this texture.
  478. /// This clears all dirty flags and pending copies from other textures.
  479. /// It should be used if the texture data will be fully overwritten by the next use.
  480. /// </summary>
  481. public void DiscardData()
  482. {
  483. Group.DiscardData(this);
  484. _dirty = false;
  485. }
  486. /// <summary>
  487. /// Synchronizes guest and host memory.
  488. /// This will overwrite the texture data with the texture data on the guest memory, if a CPU
  489. /// modification is detected.
  490. /// Be aware that this can cause texture data written by the GPU to be lost, this is just a
  491. /// one way copy (from CPU owned to GPU owned memory).
  492. /// </summary>
  493. public void SynchronizeMemory()
  494. {
  495. if (Target == Target.TextureBuffer)
  496. {
  497. return;
  498. }
  499. if (!_dirty)
  500. {
  501. return;
  502. }
  503. _dirty = false;
  504. if (_hasData)
  505. {
  506. Group.SynchronizeMemory(this);
  507. }
  508. else
  509. {
  510. Group.CheckDirty(this, true);
  511. SynchronizeFull();
  512. }
  513. }
  514. /// <summary>
  515. /// Signal that this texture is dirty, indicating that the texture group must be checked.
  516. /// </summary>
  517. public void SignalGroupDirty()
  518. {
  519. _dirty = true;
  520. }
  521. /// <summary>
  522. /// Signal that the modified state is dirty, indicating that the texture group should be notified when it changes.
  523. /// </summary>
  524. public void SignalModifiedDirty()
  525. {
  526. _modifiedStale = true;
  527. }
  528. /// <summary>
  529. /// Fully synchronizes guest and host memory.
  530. /// This will replace the entire texture with the data present in guest memory.
  531. /// </summary>
  532. public void SynchronizeFull()
  533. {
  534. ReadOnlySpan<byte> data = _physicalMemory.GetSpan(Range);
  535. // If the host does not support ASTC compression, we need to do the decompression.
  536. // The decompression is slow, so we want to avoid it as much as possible.
  537. // This does a byte-by-byte check and skips the update if the data is equal in this case.
  538. // This improves the speed on applications that overwrites ASTC data without changing anything.
  539. if (Info.FormatInfo.Format.IsAstc() && !_context.Capabilities.SupportsAstcCompression)
  540. {
  541. if (_updateCount < ByteComparisonSwitchThreshold)
  542. {
  543. _updateCount++;
  544. }
  545. else
  546. {
  547. bool dataMatches = _currentData != null && data.SequenceEqual(_currentData);
  548. if (dataMatches)
  549. {
  550. return;
  551. }
  552. _currentData = data.ToArray();
  553. }
  554. }
  555. SpanOrArray<byte> result = ConvertToHostCompatibleFormat(data);
  556. if (ScaleFactor != 1f && AllowScaledSetData())
  557. {
  558. // If needed, create a texture to load from 1x scale.
  559. ITexture texture = _setHostTexture = GetScaledHostTexture(1f, false, _setHostTexture);
  560. texture.SetData(result);
  561. texture.CopyTo(HostTexture, new Extents2D(0, 0, texture.Width, texture.Height), new Extents2D(0, 0, HostTexture.Width, HostTexture.Height), true);
  562. }
  563. else
  564. {
  565. HostTexture.SetData(result);
  566. }
  567. _hasData = true;
  568. }
  569. /// <summary>
  570. /// Uploads new texture data to the host GPU.
  571. /// </summary>
  572. /// <param name="data">New data</param>
  573. public void SetData(SpanOrArray<byte> data)
  574. {
  575. BlacklistScale();
  576. Group.CheckDirty(this, true);
  577. AlwaysFlushOnOverlap = true;
  578. HostTexture.SetData(data);
  579. _hasData = true;
  580. }
  581. /// <summary>
  582. /// Uploads new texture data to the host GPU for a specific layer/level.
  583. /// </summary>
  584. /// <param name="data">New data</param>
  585. /// <param name="layer">Target layer</param>
  586. /// <param name="level">Target level</param>
  587. public void SetData(SpanOrArray<byte> data, int layer, int level)
  588. {
  589. BlacklistScale();
  590. HostTexture.SetData(data, layer, level);
  591. _currentData = null;
  592. _hasData = true;
  593. }
  594. /// <summary>
  595. /// Uploads new texture data to the host GPU for a specific layer/level and 2D sub-region.
  596. /// </summary>
  597. /// <param name="data">New data</param>
  598. /// <param name="layer">Target layer</param>
  599. /// <param name="level">Target level</param>
  600. /// <param name="region">Target sub-region of the texture to update</param>
  601. public void SetData(ReadOnlySpan<byte> data, int layer, int level, Rectangle<int> region)
  602. {
  603. BlacklistScale();
  604. HostTexture.SetData(data, layer, level, region);
  605. _currentData = null;
  606. _hasData = true;
  607. }
  608. /// <summary>
  609. /// Converts texture data to a format and layout that is supported by the host GPU.
  610. /// </summary>
  611. /// <param name="data">Data to be converted</param>
  612. /// <param name="level">Mip level to convert</param>
  613. /// <param name="single">True to convert a single slice</param>
  614. /// <returns>Converted data</returns>
  615. public SpanOrArray<byte> ConvertToHostCompatibleFormat(ReadOnlySpan<byte> data, int level = 0, bool single = false)
  616. {
  617. int width = Info.Width;
  618. int height = Info.Height;
  619. int depth = _depth;
  620. int layers = single ? 1 : _layers;
  621. int levels = single ? 1 : (Info.Levels - level);
  622. width = Math.Max(width >> level, 1);
  623. height = Math.Max(height >> level, 1);
  624. depth = Math.Max(depth >> level, 1);
  625. int sliceDepth = single ? 1 : depth;
  626. SpanOrArray<byte> result;
  627. if (Info.IsLinear)
  628. {
  629. result = LayoutConverter.ConvertLinearStridedToLinear(
  630. width,
  631. height,
  632. Info.FormatInfo.BlockWidth,
  633. Info.FormatInfo.BlockHeight,
  634. Info.Stride,
  635. Info.Stride,
  636. Info.FormatInfo.BytesPerPixel,
  637. data);
  638. }
  639. else
  640. {
  641. result = LayoutConverter.ConvertBlockLinearToLinear(
  642. width,
  643. height,
  644. depth,
  645. sliceDepth,
  646. levels,
  647. layers,
  648. Info.FormatInfo.BlockWidth,
  649. Info.FormatInfo.BlockHeight,
  650. Info.FormatInfo.BytesPerPixel,
  651. Info.GobBlocksInY,
  652. Info.GobBlocksInZ,
  653. Info.GobBlocksInTileX,
  654. _sizeInfo,
  655. data);
  656. }
  657. // Handle compressed cases not supported by the host:
  658. // - ASTC is usually not supported on desktop cards.
  659. // - BC4/BC5 is not supported on 3D textures.
  660. if (!_context.Capabilities.SupportsAstcCompression && Format.IsAstc())
  661. {
  662. if (!AstcDecoder.TryDecodeToRgba8P(
  663. result.ToArray(),
  664. Info.FormatInfo.BlockWidth,
  665. Info.FormatInfo.BlockHeight,
  666. width,
  667. height,
  668. sliceDepth,
  669. levels,
  670. layers,
  671. out byte[] decoded))
  672. {
  673. string texInfo = $"{Info.Target} {Info.FormatInfo.Format} {Info.Width}x{Info.Height}x{Info.DepthOrLayers} levels {Info.Levels}";
  674. Logger.Debug?.Print(LogClass.Gpu, $"Invalid ASTC texture at 0x{Info.GpuAddress:X} ({texInfo}).");
  675. }
  676. if (GraphicsConfig.EnableTextureRecompression)
  677. {
  678. decoded = BCnEncoder.EncodeBC7(decoded, width, height, sliceDepth, levels, layers);
  679. }
  680. result = decoded;
  681. }
  682. else if (!_context.Capabilities.SupportsEtc2Compression && Format.IsEtc2())
  683. {
  684. switch (Format)
  685. {
  686. case Format.Etc2RgbaSrgb:
  687. case Format.Etc2RgbaUnorm:
  688. result = ETC2Decoder.DecodeRgba(result, width, height, sliceDepth, levels, layers);
  689. break;
  690. case Format.Etc2RgbPtaSrgb:
  691. case Format.Etc2RgbPtaUnorm:
  692. result = ETC2Decoder.DecodePta(result, width, height, sliceDepth, levels, layers);
  693. break;
  694. case Format.Etc2RgbSrgb:
  695. case Format.Etc2RgbUnorm:
  696. result = ETC2Decoder.DecodeRgb(result, width, height, sliceDepth, levels, layers);
  697. break;
  698. }
  699. }
  700. else if (!TextureCompatibility.HostSupportsBcFormat(Format, Target, _context.Capabilities))
  701. {
  702. switch (Format)
  703. {
  704. case Format.Bc1RgbaSrgb:
  705. case Format.Bc1RgbaUnorm:
  706. result = BCnDecoder.DecodeBC1(result, width, height, sliceDepth, levels, layers);
  707. break;
  708. case Format.Bc2Srgb:
  709. case Format.Bc2Unorm:
  710. result = BCnDecoder.DecodeBC2(result, width, height, sliceDepth, levels, layers);
  711. break;
  712. case Format.Bc3Srgb:
  713. case Format.Bc3Unorm:
  714. result = BCnDecoder.DecodeBC3(result, width, height, sliceDepth, levels, layers);
  715. break;
  716. case Format.Bc4Snorm:
  717. case Format.Bc4Unorm:
  718. result = BCnDecoder.DecodeBC4(result, width, height, sliceDepth, levels, layers, Format == Format.Bc4Snorm);
  719. break;
  720. case Format.Bc5Snorm:
  721. case Format.Bc5Unorm:
  722. result = BCnDecoder.DecodeBC5(result, width, height, sliceDepth, levels, layers, Format == Format.Bc5Snorm);
  723. break;
  724. case Format.Bc6HSfloat:
  725. case Format.Bc6HUfloat:
  726. result = BCnDecoder.DecodeBC6(result, width, height, sliceDepth, levels, layers, Format == Format.Bc6HSfloat);
  727. break;
  728. case Format.Bc7Srgb:
  729. case Format.Bc7Unorm:
  730. result = BCnDecoder.DecodeBC7(result, width, height, sliceDepth, levels, layers);
  731. break;
  732. }
  733. }
  734. else if (!_context.Capabilities.SupportsR4G4Format && Format == Format.R4G4Unorm)
  735. {
  736. result = PixelConverter.ConvertR4G4ToR4G4B4A4(result, width);
  737. if (!_context.Capabilities.SupportsR4G4B4A4Format)
  738. {
  739. result = PixelConverter.ConvertR4G4B4A4ToR8G8B8A8(result, width);
  740. }
  741. }
  742. else if (Format == Format.R4G4B4A4Unorm)
  743. {
  744. if (!_context.Capabilities.SupportsR4G4B4A4Format)
  745. {
  746. result = PixelConverter.ConvertR4G4B4A4ToR8G8B8A8(result, width);
  747. }
  748. }
  749. else if (!_context.Capabilities.Supports5BitComponentFormat && Format.Is16BitPacked())
  750. {
  751. switch (Format)
  752. {
  753. case Format.B5G6R5Unorm:
  754. case Format.R5G6B5Unorm:
  755. result = PixelConverter.ConvertR5G6B5ToR8G8B8A8(result, width);
  756. break;
  757. case Format.B5G5R5A1Unorm:
  758. case Format.R5G5B5X1Unorm:
  759. case Format.R5G5B5A1Unorm:
  760. result = PixelConverter.ConvertR5G5B5ToR8G8B8A8(result, width, Format == Format.R5G5B5X1Unorm);
  761. break;
  762. case Format.A1B5G5R5Unorm:
  763. result = PixelConverter.ConvertA1B5G5R5ToR8G8B8A8(result, width);
  764. break;
  765. case Format.R4G4B4A4Unorm:
  766. result = PixelConverter.ConvertR4G4B4A4ToR8G8B8A8(result, width);
  767. break;
  768. }
  769. }
  770. return result;
  771. }
  772. /// <summary>
  773. /// Converts texture data from a format and layout that is supported by the host GPU, back into the intended format on the guest GPU.
  774. /// </summary>
  775. /// <param name="output">Optional output span to convert into</param>
  776. /// <param name="data">Data to be converted</param>
  777. /// <param name="level">Mip level to convert</param>
  778. /// <param name="single">True to convert a single slice</param>
  779. /// <returns>Converted data</returns>
  780. public ReadOnlySpan<byte> ConvertFromHostCompatibleFormat(Span<byte> output, ReadOnlySpan<byte> data, int level = 0, bool single = false)
  781. {
  782. if (Target != Target.TextureBuffer)
  783. {
  784. int width = Info.Width;
  785. int height = Info.Height;
  786. int depth = _depth;
  787. int layers = single ? 1 : _layers;
  788. int levels = single ? 1 : (Info.Levels - level);
  789. width = Math.Max(width >> level, 1);
  790. height = Math.Max(height >> level, 1);
  791. depth = Math.Max(depth >> level, 1);
  792. if (Info.IsLinear)
  793. {
  794. data = LayoutConverter.ConvertLinearToLinearStrided(
  795. output,
  796. Info.Width,
  797. Info.Height,
  798. Info.FormatInfo.BlockWidth,
  799. Info.FormatInfo.BlockHeight,
  800. Info.Stride,
  801. Info.FormatInfo.BytesPerPixel,
  802. data);
  803. }
  804. else
  805. {
  806. data = LayoutConverter.ConvertLinearToBlockLinear(
  807. output,
  808. width,
  809. height,
  810. depth,
  811. single ? 1 : depth,
  812. levels,
  813. layers,
  814. Info.FormatInfo.BlockWidth,
  815. Info.FormatInfo.BlockHeight,
  816. Info.FormatInfo.BytesPerPixel,
  817. Info.GobBlocksInY,
  818. Info.GobBlocksInZ,
  819. Info.GobBlocksInTileX,
  820. _sizeInfo,
  821. data);
  822. }
  823. }
  824. return data;
  825. }
  826. /// <summary>
  827. /// Flushes the texture data.
  828. /// This causes the texture data to be written back to guest memory.
  829. /// If the texture was written by the GPU, this includes all modification made by the GPU
  830. /// up to this point.
  831. /// Be aware that this is an expensive operation, avoid calling it unless strictly needed.
  832. /// This may cause data corruption if the memory is already being used for something else on the CPU side.
  833. /// </summary>
  834. /// <param name="tracked">Whether or not the flush triggers write tracking. If it doesn't, the texture will not be blacklisted for scaling either.</param>
  835. /// <returns>True if data was flushed, false otherwise</returns>
  836. public bool FlushModified(bool tracked = true)
  837. {
  838. return TextureCompatibility.CanTextureFlush(Info, _context.Capabilities) && Group.FlushModified(this, tracked);
  839. }
  840. /// <summary>
  841. /// Flushes the texture data.
  842. /// This causes the texture data to be written back to guest memory.
  843. /// If the texture was written by the GPU, this includes all modification made by the GPU
  844. /// up to this point.
  845. /// Be aware that this is an expensive operation, avoid calling it unless strictly needed.
  846. /// This may cause data corruption if the memory is already being used for something else on the CPU side.
  847. /// </summary>
  848. /// <param name="tracked">Whether or not the flush triggers write tracking. If it doesn't, the texture will not be blacklisted for scaling either.</param>
  849. public void Flush(bool tracked)
  850. {
  851. if (TextureCompatibility.CanTextureFlush(Info, _context.Capabilities))
  852. {
  853. FlushTextureDataToGuest(tracked);
  854. }
  855. }
  856. /// <summary>
  857. /// Gets a host texture to use for flushing the texture, at 1x resolution.
  858. /// If the HostTexture is already at 1x resolution, it is returned directly.
  859. /// </summary>
  860. /// <returns>The host texture to flush</returns>
  861. public ITexture GetFlushTexture()
  862. {
  863. ITexture texture = HostTexture;
  864. if (ScaleFactor != 1f)
  865. {
  866. // If needed, create a texture to flush back to host at 1x scale.
  867. texture = _flushHostTexture = GetScaledHostTexture(1f, true, _flushHostTexture);
  868. }
  869. return texture;
  870. }
  871. /// <summary>
  872. /// Gets data from the host GPU, and flushes it all to guest memory.
  873. /// </summary>
  874. /// <remarks>
  875. /// This method should be used to retrieve data that was modified by the host GPU.
  876. /// This is not cheap, avoid doing that unless strictly needed.
  877. /// When possible, the data is written directly into guest memory, rather than copied.
  878. /// </remarks>
  879. /// <param name="tracked">True if writing the texture data is tracked, false otherwise</param>
  880. /// <param name="texture">The specific host texture to flush. Defaults to this texture</param>
  881. public void FlushTextureDataToGuest(bool tracked, ITexture texture = null)
  882. {
  883. using WritableRegion region = _physicalMemory.GetWritableRegion(Range, tracked);
  884. GetTextureDataFromGpu(region.Memory.Span, tracked, texture);
  885. }
  886. /// <summary>
  887. /// Gets data from the host GPU.
  888. /// </summary>
  889. /// <remarks>
  890. /// This method should be used to retrieve data that was modified by the host GPU.
  891. /// This is not cheap, avoid doing that unless strictly needed.
  892. /// </remarks>
  893. /// <param name="output">An output span to place the texture data into</param>
  894. /// <param name="blacklist">True if the texture should be blacklisted, false otherwise</param>
  895. /// <param name="texture">The specific host texture to flush. Defaults to this texture</param>
  896. private void GetTextureDataFromGpu(Span<byte> output, bool blacklist, ITexture texture = null)
  897. {
  898. PinnedSpan<byte> data;
  899. if (texture != null)
  900. {
  901. data = texture.GetData();
  902. }
  903. else
  904. {
  905. if (blacklist)
  906. {
  907. BlacklistScale();
  908. data = HostTexture.GetData();
  909. }
  910. else if (ScaleFactor != 1f)
  911. {
  912. float scale = ScaleFactor;
  913. SetScale(1f);
  914. data = HostTexture.GetData();
  915. SetScale(scale);
  916. }
  917. else
  918. {
  919. data = HostTexture.GetData();
  920. }
  921. }
  922. ConvertFromHostCompatibleFormat(output, data.Get());
  923. data.Dispose();
  924. }
  925. /// <summary>
  926. /// Gets data from the host GPU for a single slice.
  927. /// </summary>
  928. /// <remarks>
  929. /// This method should be used to retrieve data that was modified by the host GPU.
  930. /// This is not cheap, avoid doing that unless strictly needed.
  931. /// </remarks>
  932. /// <param name="output">An output span to place the texture data into. If empty, one is generated</param>
  933. /// <param name="layer">The layer of the texture to flush</param>
  934. /// <param name="level">The level of the texture to flush</param>
  935. /// <param name="blacklist">True if the texture should be blacklisted, false otherwise</param>
  936. /// <param name="texture">The specific host texture to flush. Defaults to this texture</param>
  937. public void GetTextureDataSliceFromGpu(Span<byte> output, int layer, int level, bool blacklist, ITexture texture = null)
  938. {
  939. PinnedSpan<byte> data;
  940. if (texture != null)
  941. {
  942. data = texture.GetData(layer, level);
  943. }
  944. else
  945. {
  946. if (blacklist)
  947. {
  948. BlacklistScale();
  949. data = HostTexture.GetData(layer, level);
  950. }
  951. else if (ScaleFactor != 1f)
  952. {
  953. float scale = ScaleFactor;
  954. SetScale(1f);
  955. data = HostTexture.GetData(layer, level);
  956. SetScale(scale);
  957. }
  958. else
  959. {
  960. data = HostTexture.GetData(layer, level);
  961. }
  962. }
  963. ConvertFromHostCompatibleFormat(output, data.Get(), level, true);
  964. data.Dispose();
  965. }
  966. /// <summary>
  967. /// This performs a strict comparison, used to check if this texture is equal to the one supplied.
  968. /// </summary>
  969. /// <param name="info">Texture information to compare against</param>
  970. /// <param name="flags">Comparison flags</param>
  971. /// <returns>A value indicating how well this texture matches the given info</returns>
  972. public TextureMatchQuality IsExactMatch(TextureInfo info, TextureSearchFlags flags)
  973. {
  974. bool forSampler = (flags & TextureSearchFlags.ForSampler) != 0;
  975. TextureMatchQuality matchQuality = TextureCompatibility.FormatMatches(Info, info, forSampler, (flags & TextureSearchFlags.DepthAlias) != 0);
  976. if (matchQuality == TextureMatchQuality.NoMatch)
  977. {
  978. return matchQuality;
  979. }
  980. if (!TextureCompatibility.LayoutMatches(Info, info))
  981. {
  982. return TextureMatchQuality.NoMatch;
  983. }
  984. if (!TextureCompatibility.SizeMatches(Info, info, forSampler))
  985. {
  986. return TextureMatchQuality.NoMatch;
  987. }
  988. if ((flags & TextureSearchFlags.ForSampler) != 0)
  989. {
  990. if (!TextureCompatibility.SamplerParamsMatches(Info, info))
  991. {
  992. return TextureMatchQuality.NoMatch;
  993. }
  994. }
  995. if ((flags & TextureSearchFlags.ForCopy) != 0)
  996. {
  997. bool msTargetCompatible = Info.Target == Target.Texture2DMultisample && info.Target == Target.Texture2D;
  998. if (!msTargetCompatible && !TextureCompatibility.TargetAndSamplesCompatible(Info, info))
  999. {
  1000. return TextureMatchQuality.NoMatch;
  1001. }
  1002. }
  1003. else if (!TextureCompatibility.TargetAndSamplesCompatible(Info, info))
  1004. {
  1005. return TextureMatchQuality.NoMatch;
  1006. }
  1007. return Info.Levels == info.Levels ? matchQuality : TextureMatchQuality.NoMatch;
  1008. }
  1009. /// <summary>
  1010. /// Check if it's possible to create a view, with the given parameters, from this texture.
  1011. /// </summary>
  1012. /// <param name="info">Texture view information</param>
  1013. /// <param name="range">Texture view physical memory ranges</param>
  1014. /// <param name="exactSize">Indicates if the texture sizes must be exactly equal, or width is allowed to differ</param>
  1015. /// <param name="layerSize">Layer size on the given texture</param>
  1016. /// <param name="caps">Host GPU capabilities</param>
  1017. /// <param name="firstLayer">Texture view initial layer on this texture</param>
  1018. /// <param name="firstLevel">Texture view first mipmap level on this texture</param>
  1019. /// <param name="flags">Texture search flags</param>
  1020. /// <returns>The level of compatiblilty a view with the given parameters created from this texture has</returns>
  1021. public TextureViewCompatibility IsViewCompatible(
  1022. TextureInfo info,
  1023. MultiRange range,
  1024. bool exactSize,
  1025. int layerSize,
  1026. Capabilities caps,
  1027. out int firstLayer,
  1028. out int firstLevel,
  1029. TextureSearchFlags flags = TextureSearchFlags.None)
  1030. {
  1031. TextureViewCompatibility result = TextureViewCompatibility.Full;
  1032. result = TextureCompatibility.PropagateViewCompatibility(result, TextureCompatibility.ViewFormatCompatible(Info, info, caps, flags));
  1033. if (result != TextureViewCompatibility.Incompatible)
  1034. {
  1035. result = TextureCompatibility.PropagateViewCompatibility(result, TextureCompatibility.ViewTargetCompatible(Info, info, ref caps));
  1036. bool bothMs = Info.Target.IsMultisample() && info.Target.IsMultisample();
  1037. if (bothMs && (Info.SamplesInX != info.SamplesInX || Info.SamplesInY != info.SamplesInY))
  1038. {
  1039. result = TextureViewCompatibility.Incompatible;
  1040. }
  1041. if (result == TextureViewCompatibility.Full && Info.FormatInfo.Format != info.FormatInfo.Format && !_context.Capabilities.SupportsMismatchingViewFormat)
  1042. {
  1043. // AMD and Intel have a bug where the view format is always ignored;
  1044. // they use the parent format instead.
  1045. // Create a copy dependency to avoid this issue.
  1046. result = TextureViewCompatibility.CopyOnly;
  1047. }
  1048. }
  1049. firstLayer = 0;
  1050. firstLevel = 0;
  1051. if (result == TextureViewCompatibility.Incompatible)
  1052. {
  1053. return TextureViewCompatibility.Incompatible;
  1054. }
  1055. int offset = Range.FindOffset(range);
  1056. if (offset < 0 || !_sizeInfo.FindView(offset, out firstLayer, out firstLevel))
  1057. {
  1058. return TextureViewCompatibility.LayoutIncompatible;
  1059. }
  1060. if (!TextureCompatibility.ViewLayoutCompatible(Info, info, firstLevel))
  1061. {
  1062. return TextureViewCompatibility.LayoutIncompatible;
  1063. }
  1064. if (info.GetSlices() > 1 && LayerSize != layerSize)
  1065. {
  1066. return TextureViewCompatibility.LayoutIncompatible;
  1067. }
  1068. result = TextureCompatibility.PropagateViewCompatibility(result, TextureCompatibility.ViewSizeMatches(Info, info, exactSize, firstLevel));
  1069. result = TextureCompatibility.PropagateViewCompatibility(result, TextureCompatibility.ViewSubImagesInBounds(Info, info, firstLayer, firstLevel));
  1070. return result;
  1071. }
  1072. /// <summary>
  1073. /// Gets a texture of the specified target type from this texture.
  1074. /// This can be used to get an array texture from a non-array texture and vice-versa.
  1075. /// If this texture and the requested targets are equal, then this texture Host texture is returned directly.
  1076. /// </summary>
  1077. /// <param name="target">The desired target type</param>
  1078. /// <returns>A view of this texture with the requested target, or null if the target is invalid for this texture</returns>
  1079. public ITexture GetTargetTexture(Target target)
  1080. {
  1081. if (target == Target)
  1082. {
  1083. return HostTexture;
  1084. }
  1085. if (_arrayViewTexture == null && IsSameDimensionsTarget(target))
  1086. {
  1087. FormatInfo formatInfo = TextureCompatibility.ToHostCompatibleFormat(Info, _context.Capabilities);
  1088. TextureCreateInfo createInfo = new(
  1089. Info.Width,
  1090. Info.Height,
  1091. target == Target.CubemapArray ? 6 : 1,
  1092. Info.Levels,
  1093. Info.Samples,
  1094. formatInfo.BlockWidth,
  1095. formatInfo.BlockHeight,
  1096. formatInfo.BytesPerPixel,
  1097. formatInfo.Format,
  1098. Info.DepthStencilMode,
  1099. target,
  1100. Info.SwizzleR,
  1101. Info.SwizzleG,
  1102. Info.SwizzleB,
  1103. Info.SwizzleA);
  1104. ITexture viewTexture = HostTexture.CreateView(createInfo, 0, 0);
  1105. _arrayViewTexture = viewTexture;
  1106. _arrayViewTarget = target;
  1107. return viewTexture;
  1108. }
  1109. else if (_arrayViewTarget == target)
  1110. {
  1111. return _arrayViewTexture;
  1112. }
  1113. return null;
  1114. }
  1115. /// <summary>
  1116. /// Determine if this texture can have anisotropic filtering forced.
  1117. /// Filtered textures that we might want to force anisotropy on should have a lot of mip levels.
  1118. /// </summary>
  1119. /// <returns>True if anisotropic filtering can be forced, false otherwise</returns>
  1120. private bool CanTextureForceAnisotropy()
  1121. {
  1122. if (!(Target == Target.Texture2D || Target == Target.Texture2DArray))
  1123. {
  1124. return false;
  1125. }
  1126. int maxSize = Math.Max(Info.Width, Info.Height);
  1127. int maxLevels = BitOperations.Log2((uint)maxSize) + 1;
  1128. return Info.Levels >= Math.Min(MinLevelsForForceAnisotropy, maxLevels);
  1129. }
  1130. /// <summary>
  1131. /// Check if this texture and the specified target have the same number of dimensions.
  1132. /// For the purposes of this comparison, 2D and 2D Multisample textures are not considered to have
  1133. /// the same number of dimensions. Same for Cubemap and 3D textures.
  1134. /// </summary>
  1135. /// <param name="target">The target to compare with</param>
  1136. /// <returns>True if both targets have the same number of dimensions, false otherwise</returns>
  1137. private bool IsSameDimensionsTarget(Target target)
  1138. {
  1139. switch (Info.Target)
  1140. {
  1141. case Target.Texture1D:
  1142. case Target.Texture1DArray:
  1143. return target == Target.Texture1D || target == Target.Texture1DArray;
  1144. case Target.Texture2D:
  1145. case Target.Texture2DArray:
  1146. return target == Target.Texture2D || target == Target.Texture2DArray;
  1147. case Target.Cubemap:
  1148. case Target.CubemapArray:
  1149. return target == Target.Cubemap || target == Target.CubemapArray;
  1150. case Target.Texture2DMultisample:
  1151. case Target.Texture2DMultisampleArray:
  1152. return target == Target.Texture2DMultisample || target == Target.Texture2DMultisampleArray;
  1153. case Target.Texture3D:
  1154. return target == Target.Texture3D;
  1155. default:
  1156. return false;
  1157. }
  1158. }
  1159. /// <summary>
  1160. /// Replaces view texture information.
  1161. /// This should only be used for child textures with a parent.
  1162. /// </summary>
  1163. /// <param name="parent">The parent texture</param>
  1164. /// <param name="info">The new view texture information</param>
  1165. /// <param name="hostTexture">The new host texture</param>
  1166. /// <param name="firstLayer">The first layer of the view</param>
  1167. /// <param name="firstLevel">The first level of the view</param>
  1168. public void ReplaceView(Texture parent, TextureInfo info, ITexture hostTexture, int firstLayer, int firstLevel)
  1169. {
  1170. IncrementReferenceCount();
  1171. parent._viewStorage.SynchronizeMemory();
  1172. // If this texture has views, they must be given to the new parent.
  1173. if (_views.Count > 0)
  1174. {
  1175. Texture[] viewCopy = _views.ToArray();
  1176. foreach (Texture view in viewCopy)
  1177. {
  1178. TextureCreateInfo createInfo = TextureCache.GetCreateInfo(view.Info, _context.Capabilities, ScaleFactor);
  1179. ITexture newView = parent.HostTexture.CreateView(createInfo, view.FirstLayer + firstLayer, view.FirstLevel + firstLevel);
  1180. view.ReplaceView(parent, view.Info, newView, view.FirstLayer + firstLayer, view.FirstLevel + firstLevel);
  1181. }
  1182. }
  1183. ReplaceStorage(hostTexture);
  1184. if (_viewStorage != this)
  1185. {
  1186. _viewStorage.RemoveView(this);
  1187. }
  1188. FirstLayer = parent.FirstLayer + firstLayer;
  1189. FirstLevel = parent.FirstLevel + firstLevel;
  1190. parent._viewStorage.AddView(this);
  1191. SetInfo(info);
  1192. DecrementReferenceCount();
  1193. }
  1194. /// <summary>
  1195. /// Sets the internal texture information structure.
  1196. /// </summary>
  1197. /// <param name="info">The new texture information</param>
  1198. private void SetInfo(TextureInfo info)
  1199. {
  1200. Info = info;
  1201. Target = info.Target;
  1202. Width = info.Width;
  1203. Height = info.Height;
  1204. CanForceAnisotropy = CanTextureForceAnisotropy();
  1205. _depth = info.GetDepth();
  1206. _layers = info.GetLayers();
  1207. }
  1208. /// <summary>
  1209. /// Signals that the texture has been modified.
  1210. /// </summary>
  1211. public void SignalModified()
  1212. {
  1213. _scaledSetScore = Math.Max(0, _scaledSetScore - 1);
  1214. if (_modifiedStale || Group.HasCopyDependencies)
  1215. {
  1216. _modifiedStale = false;
  1217. Group.SignalModified(this);
  1218. }
  1219. _physicalMemory.TextureCache.Lift(this);
  1220. }
  1221. /// <summary>
  1222. /// Signals that a texture has been bound, or has been unbound.
  1223. /// During this time, lazy copies will not clear the dirty flag.
  1224. /// </summary>
  1225. /// <param name="bound">True if the texture has been bound, false if it has been unbound</param>
  1226. public void SignalModifying(bool bound)
  1227. {
  1228. if (bound)
  1229. {
  1230. _scaledSetScore = Math.Max(0, _scaledSetScore - 1);
  1231. }
  1232. if (_modifiedStale || Group.HasCopyDependencies || Group.HasFlushBuffer)
  1233. {
  1234. _modifiedStale = false;
  1235. Group.SignalModifying(this, bound);
  1236. }
  1237. _physicalMemory.TextureCache.Lift(this);
  1238. if (bound)
  1239. {
  1240. IncrementReferenceCount();
  1241. }
  1242. else
  1243. {
  1244. DecrementReferenceCount();
  1245. }
  1246. }
  1247. /// <summary>
  1248. /// Replaces the host texture, while disposing of the old one if needed.
  1249. /// </summary>
  1250. /// <param name="hostTexture">The new host texture</param>
  1251. private void ReplaceStorage(ITexture hostTexture)
  1252. {
  1253. DisposeTextures();
  1254. HostTexture = hostTexture;
  1255. }
  1256. /// <summary>
  1257. /// Determine if any of this texture's data overlaps with another.
  1258. /// </summary>
  1259. /// <param name="texture">The texture to check against</param>
  1260. /// <param name="compatibility">The view compatibility of the two textures</param>
  1261. /// <returns>True if any slice of the textures overlap, false otherwise</returns>
  1262. public bool DataOverlaps(Texture texture, TextureViewCompatibility compatibility)
  1263. {
  1264. if (compatibility == TextureViewCompatibility.LayoutIncompatible && Info.GobBlocksInZ > 1 && Info.GobBlocksInZ == texture.Info.GobBlocksInZ)
  1265. {
  1266. // Allow overlapping slices of layout compatible 3D textures with matching GobBlocksInZ, as they are interleaved.
  1267. return false;
  1268. }
  1269. if (texture._sizeInfo.AllOffsets.Length == 1 && _sizeInfo.AllOffsets.Length == 1)
  1270. {
  1271. return Range.OverlapsWith(texture.Range);
  1272. }
  1273. MultiRange otherRange = texture.Range;
  1274. IEnumerable<MultiRange> regions = _sizeInfo.AllRegions().Select((region) => Range.Slice((ulong)region.Offset, (ulong)region.Size));
  1275. IEnumerable<MultiRange> otherRegions = texture._sizeInfo.AllRegions().Select((region) => otherRange.Slice((ulong)region.Offset, (ulong)region.Size));
  1276. foreach (MultiRange region in regions)
  1277. {
  1278. foreach (MultiRange otherRegion in otherRegions)
  1279. {
  1280. if (region.OverlapsWith(otherRegion))
  1281. {
  1282. return true;
  1283. }
  1284. }
  1285. }
  1286. return false;
  1287. }
  1288. /// <summary>
  1289. /// Increments the texture reference count.
  1290. /// </summary>
  1291. public void IncrementReferenceCount()
  1292. {
  1293. _referenceCount++;
  1294. }
  1295. /// <summary>
  1296. /// Increments the reference count and records the given texture pool and ID as a pool owner.
  1297. /// </summary>
  1298. /// <param name="pool">The texture pool this texture has been added to</param>
  1299. /// <param name="id">The ID of the reference to this texture in the pool</param>
  1300. /// <param name="gpuVa">GPU VA of the pool reference</param>
  1301. public void IncrementReferenceCount(TexturePool pool, int id, ulong gpuVa)
  1302. {
  1303. HadPoolOwner = true;
  1304. lock (_poolOwners)
  1305. {
  1306. _poolOwners.Add(new TexturePoolOwner { Pool = pool, ID = id, GpuAddress = gpuVa });
  1307. }
  1308. _referenceCount++;
  1309. if (ShortCacheEntry != null)
  1310. {
  1311. _physicalMemory.TextureCache.RemoveShortCache(this);
  1312. }
  1313. }
  1314. /// <summary>
  1315. /// Indicates that the texture has one reference left, and will delete on reference decrement.
  1316. /// </summary>
  1317. /// <returns>True if there is one reference remaining, false otherwise</returns>
  1318. public bool HasOneReference()
  1319. {
  1320. return _referenceCount == 1;
  1321. }
  1322. /// <summary>
  1323. /// Decrements the texture reference count.
  1324. /// When the reference count hits zero, the texture may be deleted and can't be used anymore.
  1325. /// </summary>
  1326. /// <returns>True if the texture is now referenceless, false otherwise</returns>
  1327. public bool DecrementReferenceCount()
  1328. {
  1329. int newRefCount = --_referenceCount;
  1330. if (newRefCount == 0)
  1331. {
  1332. if (_viewStorage != this)
  1333. {
  1334. _viewStorage.RemoveView(this);
  1335. }
  1336. _physicalMemory.TextureCache.RemoveTextureFromCache(this);
  1337. }
  1338. Debug.Assert(newRefCount >= 0);
  1339. DeleteIfNotUsed();
  1340. return newRefCount <= 0;
  1341. }
  1342. /// <summary>
  1343. /// Decrements the texture reference count, also removing an associated pool owner reference.
  1344. /// When the reference count hits zero, the texture may be deleted and can't be used anymore.
  1345. /// </summary>
  1346. /// <param name="pool">The texture pool this texture is being removed from</param>
  1347. /// <param name="id">The ID of the reference to this texture in the pool</param>
  1348. /// <returns>True if the texture is now referenceless, false otherwise</returns>
  1349. public bool DecrementReferenceCount(TexturePool pool, int id = -1)
  1350. {
  1351. lock (_poolOwners)
  1352. {
  1353. int references = _poolOwners.RemoveAll(entry => entry.Pool == pool && entry.ID == id || id == -1);
  1354. if (references == 0)
  1355. {
  1356. // This reference has already been removed.
  1357. return _referenceCount <= 0;
  1358. }
  1359. Debug.Assert(references == 1);
  1360. }
  1361. return DecrementReferenceCount();
  1362. }
  1363. /// <summary>
  1364. /// Forcibly remove this texture from all pools that reference it.
  1365. /// </summary>
  1366. /// <param name="deferred">Indicates if the removal is being done from another thread.</param>
  1367. public void RemoveFromPools(bool deferred)
  1368. {
  1369. lock (_poolOwners)
  1370. {
  1371. foreach (var owner in _poolOwners)
  1372. {
  1373. owner.Pool.ForceRemove(this, owner.ID, deferred);
  1374. }
  1375. _poolOwners.Clear();
  1376. }
  1377. if (ShortCacheEntry != null && !ShortCacheEntry.IsAutoDelete && _context.IsGpuThread())
  1378. {
  1379. // If this is called from another thread (unmapped), the short cache will
  1380. // have to remove this texture on a future tick.
  1381. _physicalMemory.TextureCache.RemoveShortCache(this);
  1382. }
  1383. InvalidatedSequence++;
  1384. }
  1385. /// <summary>
  1386. /// Queue updating texture mappings on the pool. Happens from another thread.
  1387. /// </summary>
  1388. public void UpdatePoolMappings()
  1389. {
  1390. ChangedMapping = true;
  1391. lock (_poolOwners)
  1392. {
  1393. ulong address = 0;
  1394. foreach (var owner in _poolOwners)
  1395. {
  1396. if (address == 0 || address == owner.GpuAddress)
  1397. {
  1398. address = owner.GpuAddress;
  1399. owner.Pool.QueueUpdateMapping(this, owner.ID);
  1400. }
  1401. else
  1402. {
  1403. // If there is a different GPU VA mapping, prefer the first and delete the others.
  1404. owner.Pool.ForceRemove(this, owner.ID, true);
  1405. }
  1406. }
  1407. _poolOwners.Clear();
  1408. }
  1409. InvalidatedSequence++;
  1410. }
  1411. /// <summary>
  1412. /// Delete the texture if it is not used anymore.
  1413. /// The texture is considered unused when the reference count is zero,
  1414. /// and it has no child views.
  1415. /// </summary>
  1416. private void DeleteIfNotUsed()
  1417. {
  1418. // We can delete the texture as long it is not being used
  1419. // in any cache (the reference count is 0 in this case), and
  1420. // also all views that may be created from this texture were
  1421. // already deleted (views count is 0).
  1422. if (_referenceCount == 0 && _views.Count == 0)
  1423. {
  1424. Dispose();
  1425. }
  1426. }
  1427. /// <summary>
  1428. /// Performs texture disposal, deleting the texture.
  1429. /// </summary>
  1430. private void DisposeTextures()
  1431. {
  1432. InvalidatedSequence++;
  1433. _currentData = null;
  1434. HostTexture.Release();
  1435. _arrayViewTexture?.Release();
  1436. _arrayViewTexture = null;
  1437. _flushHostTexture?.Release();
  1438. _flushHostTexture = null;
  1439. _setHostTexture?.Release();
  1440. _setHostTexture = null;
  1441. }
  1442. /// <summary>
  1443. /// Called when the memory for this texture has been unmapped.
  1444. /// Calls are from non-gpu threads.
  1445. /// </summary>
  1446. /// <param name="unmapRange">The range of memory being unmapped</param>
  1447. public void Unmapped(MultiRange unmapRange)
  1448. {
  1449. ChangedMapping = true;
  1450. if (Group.Storage == this)
  1451. {
  1452. Group.Unmapped();
  1453. Group.ClearModified(unmapRange);
  1454. }
  1455. }
  1456. /// <summary>
  1457. /// Performs texture disposal, deleting the texture.
  1458. /// </summary>
  1459. public void Dispose()
  1460. {
  1461. DisposeTextures();
  1462. if (Group.Storage == this)
  1463. {
  1464. Group.Dispose();
  1465. }
  1466. }
  1467. }
  1468. }