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