TextureBindingsArrayCache.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Engine.Types;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using Ryujinx.Graphics.Shader;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. namespace Ryujinx.Graphics.Gpu.Image
  10. {
  11. /// <summary>
  12. /// Texture bindings array cache.
  13. /// </summary>
  14. class TextureBindingsArrayCache
  15. {
  16. /// <summary>
  17. /// Minimum timestamp delta until texture array can be removed from the cache.
  18. /// </summary>
  19. private const int MinDeltaForRemoval = 20000;
  20. private readonly GpuContext _context;
  21. private readonly GpuChannel _channel;
  22. /// <summary>
  23. /// Array cache entry key.
  24. /// </summary>
  25. private readonly struct CacheEntryFromPoolKey : IEquatable<CacheEntryFromPoolKey>
  26. {
  27. /// <summary>
  28. /// Whether the entry is for an image.
  29. /// </summary>
  30. public readonly bool IsImage;
  31. /// <summary>
  32. /// Whether the entry is for a sampler.
  33. /// </summary>
  34. public readonly bool IsSampler;
  35. /// <summary>
  36. /// Texture or image target type.
  37. /// </summary>
  38. public readonly Target Target;
  39. /// <summary>
  40. /// Number of entries of the array.
  41. /// </summary>
  42. public readonly int ArrayLength;
  43. private readonly TexturePool _texturePool;
  44. private readonly SamplerPool _samplerPool;
  45. /// <summary>
  46. /// Creates a new array cache entry.
  47. /// </summary>
  48. /// <param name="isImage">Whether the entry is for an image</param>
  49. /// <param name="bindingInfo">Binding information for the array</param>
  50. /// <param name="texturePool">Texture pool where the array textures are located</param>
  51. /// <param name="samplerPool">Sampler pool where the array samplers are located</param>
  52. public CacheEntryFromPoolKey(bool isImage, TextureBindingInfo bindingInfo, TexturePool texturePool, SamplerPool samplerPool)
  53. {
  54. IsImage = isImage;
  55. IsSampler = bindingInfo.IsSamplerOnly;
  56. Target = bindingInfo.Target;
  57. ArrayLength = bindingInfo.ArrayLength;
  58. _texturePool = texturePool;
  59. _samplerPool = samplerPool;
  60. }
  61. /// <summary>
  62. /// Checks if the pool matches the cached pool.
  63. /// </summary>
  64. /// <param name="texturePool">Texture or sampler pool instance</param>
  65. /// <returns>True if the pool matches, false otherwise</returns>
  66. public bool MatchesPool<T>(IPool<T> pool)
  67. {
  68. return _texturePool == pool || _samplerPool == pool;
  69. }
  70. /// <summary>
  71. /// Checks if the texture and sampler pools matches the cached pools.
  72. /// </summary>
  73. /// <param name="texturePool">Texture pool instance</param>
  74. /// <param name="samplerPool">Sampler pool instance</param>
  75. /// <returns>True if the pools match, false otherwise</returns>
  76. private bool MatchesPools(TexturePool texturePool, SamplerPool samplerPool)
  77. {
  78. return _texturePool == texturePool && _samplerPool == samplerPool;
  79. }
  80. public bool Equals(CacheEntryFromPoolKey other)
  81. {
  82. return IsImage == other.IsImage &&
  83. IsSampler == other.IsSampler &&
  84. Target == other.Target &&
  85. ArrayLength == other.ArrayLength &&
  86. MatchesPools(other._texturePool, other._samplerPool);
  87. }
  88. public override bool Equals(object obj)
  89. {
  90. return obj is CacheEntryFromBufferKey other && Equals(other);
  91. }
  92. public override int GetHashCode()
  93. {
  94. return HashCode.Combine(_texturePool, _samplerPool, IsSampler);
  95. }
  96. }
  97. /// <summary>
  98. /// Array cache entry key.
  99. /// </summary>
  100. private readonly struct CacheEntryFromBufferKey : IEquatable<CacheEntryFromBufferKey>
  101. {
  102. /// <summary>
  103. /// Whether the entry is for an image.
  104. /// </summary>
  105. public readonly bool IsImage;
  106. /// <summary>
  107. /// Texture or image target type.
  108. /// </summary>
  109. public readonly Target Target;
  110. /// <summary>
  111. /// Word offset of the first handle on the constant buffer.
  112. /// </summary>
  113. public readonly int HandleIndex;
  114. /// <summary>
  115. /// Number of entries of the array.
  116. /// </summary>
  117. public readonly int ArrayLength;
  118. private readonly TexturePool _texturePool;
  119. private readonly SamplerPool _samplerPool;
  120. private readonly BufferBounds _textureBufferBounds;
  121. /// <summary>
  122. /// Creates a new array cache entry.
  123. /// </summary>
  124. /// <param name="isImage">Whether the entry is for an image</param>
  125. /// <param name="bindingInfo">Binding information for the array</param>
  126. /// <param name="texturePool">Texture pool where the array textures are located</param>
  127. /// <param name="samplerPool">Sampler pool where the array samplers are located</param>
  128. /// <param name="textureBufferBounds">Constant buffer bounds with the texture handles</param>
  129. public CacheEntryFromBufferKey(
  130. bool isImage,
  131. TextureBindingInfo bindingInfo,
  132. TexturePool texturePool,
  133. SamplerPool samplerPool,
  134. ref BufferBounds textureBufferBounds)
  135. {
  136. IsImage = isImage;
  137. Target = bindingInfo.Target;
  138. HandleIndex = bindingInfo.Handle;
  139. ArrayLength = bindingInfo.ArrayLength;
  140. _texturePool = texturePool;
  141. _samplerPool = samplerPool;
  142. _textureBufferBounds = textureBufferBounds;
  143. }
  144. /// <summary>
  145. /// Checks if the texture and sampler pools matches the cached pools.
  146. /// </summary>
  147. /// <param name="texturePool">Texture pool instance</param>
  148. /// <param name="samplerPool">Sampler pool instance</param>
  149. /// <returns>True if the pools match, false otherwise</returns>
  150. private bool MatchesPools(TexturePool texturePool, SamplerPool samplerPool)
  151. {
  152. return _texturePool == texturePool && _samplerPool == samplerPool;
  153. }
  154. /// <summary>
  155. /// Checks if the cached constant buffer address and size matches.
  156. /// </summary>
  157. /// <param name="textureBufferBounds">New buffer address and size</param>
  158. /// <returns>True if the address and size matches, false otherwise</returns>
  159. private bool MatchesBufferBounds(BufferBounds textureBufferBounds)
  160. {
  161. return _textureBufferBounds.Equals(textureBufferBounds);
  162. }
  163. public bool Equals(CacheEntryFromBufferKey other)
  164. {
  165. return IsImage == other.IsImage &&
  166. Target == other.Target &&
  167. HandleIndex == other.HandleIndex &&
  168. ArrayLength == other.ArrayLength &&
  169. MatchesPools(other._texturePool, other._samplerPool) &&
  170. MatchesBufferBounds(other._textureBufferBounds);
  171. }
  172. public override bool Equals(object obj)
  173. {
  174. return obj is CacheEntryFromBufferKey other && Equals(other);
  175. }
  176. public override int GetHashCode()
  177. {
  178. return _textureBufferBounds.Range.GetHashCode();
  179. }
  180. }
  181. /// <summary>
  182. /// Array cache entry from pool.
  183. /// </summary>
  184. private class CacheEntry
  185. {
  186. /// <summary>
  187. /// All cached textures, along with their invalidated sequence number as value.
  188. /// </summary>
  189. public readonly Dictionary<Texture, int> Textures;
  190. /// <summary>
  191. /// Backend texture array if the entry is for a texture, otherwise null.
  192. /// </summary>
  193. public readonly ITextureArray TextureArray;
  194. /// <summary>
  195. /// Backend image array if the entry is for an image, otherwise null.
  196. /// </summary>
  197. public readonly IImageArray ImageArray;
  198. /// <summary>
  199. /// Texture pool where the array textures are located.
  200. /// </summary>
  201. protected readonly TexturePool TexturePool;
  202. /// <summary>
  203. /// Sampler pool where the array samplers are located.
  204. /// </summary>
  205. protected readonly SamplerPool SamplerPool;
  206. private int _texturePoolSequence;
  207. private int _samplerPoolSequence;
  208. /// <summary>
  209. /// Creates a new array cache entry.
  210. /// </summary>
  211. /// <param name="texturePool">Texture pool where the array textures are located</param>
  212. /// <param name="samplerPool">Sampler pool where the array samplers are located</param>
  213. private CacheEntry(TexturePool texturePool, SamplerPool samplerPool)
  214. {
  215. Textures = new Dictionary<Texture, int>();
  216. TexturePool = texturePool;
  217. SamplerPool = samplerPool;
  218. }
  219. /// <summary>
  220. /// Creates a new array cache entry.
  221. /// </summary>
  222. /// <param name="array">Backend texture array</param>
  223. /// <param name="texturePool">Texture pool where the array textures are located</param>
  224. /// <param name="samplerPool">Sampler pool where the array samplers are located</param>
  225. public CacheEntry(ITextureArray array, TexturePool texturePool, SamplerPool samplerPool) : this(texturePool, samplerPool)
  226. {
  227. TextureArray = array;
  228. }
  229. /// <summary>
  230. /// Creates a new array cache entry.
  231. /// </summary>
  232. /// <param name="array">Backend image array</param>
  233. /// <param name="texturePool">Texture pool where the array textures are located</param>
  234. /// <param name="samplerPool">Sampler pool where the array samplers are located</param>
  235. public CacheEntry(IImageArray array, TexturePool texturePool, SamplerPool samplerPool) : this(texturePool, samplerPool)
  236. {
  237. ImageArray = array;
  238. }
  239. /// <summary>
  240. /// Synchronizes memory for all textures in the array.
  241. /// </summary>
  242. /// <param name="isStore">Indicates if the texture may be modified by the access</param>
  243. /// <param name="blacklistScale">Indicates if the texture should be blacklisted for scaling</param>
  244. public void SynchronizeMemory(bool isStore, bool blacklistScale)
  245. {
  246. foreach (Texture texture in Textures.Keys)
  247. {
  248. texture.SynchronizeMemory();
  249. if (isStore)
  250. {
  251. texture.SignalModified();
  252. }
  253. if (blacklistScale && texture.ScaleMode != TextureScaleMode.Blacklisted)
  254. {
  255. // Scaling textures used on arrays is currently not supported.
  256. texture.BlacklistScale();
  257. }
  258. }
  259. }
  260. /// <summary>
  261. /// Clears all cached texture instances.
  262. /// </summary>
  263. public virtual void Reset()
  264. {
  265. Textures.Clear();
  266. }
  267. /// <summary>
  268. /// Checks if any texture has been deleted since the last call to this method.
  269. /// </summary>
  270. /// <returns>True if one or more textures have been deleted, false otherwise</returns>
  271. public bool ValidateTextures()
  272. {
  273. foreach ((Texture texture, int invalidatedSequence) in Textures)
  274. {
  275. if (texture.InvalidatedSequence != invalidatedSequence)
  276. {
  277. return false;
  278. }
  279. }
  280. return true;
  281. }
  282. /// <summary>
  283. /// Checks if the cached texture or sampler pool has been modified since the last call to this method.
  284. /// </summary>
  285. /// <returns>True if any used entries of the pool might have been modified, false otherwise</returns>
  286. public bool TexturePoolModified()
  287. {
  288. return TexturePool.WasModified(ref _texturePoolSequence);
  289. }
  290. /// <summary>
  291. /// Checks if the cached texture or sampler pool has been modified since the last call to this method.
  292. /// </summary>
  293. /// <returns>True if any used entries of the pool might have been modified, false otherwise</returns>
  294. public bool SamplerPoolModified()
  295. {
  296. return SamplerPool != null && SamplerPool.WasModified(ref _samplerPoolSequence);
  297. }
  298. }
  299. /// <summary>
  300. /// Array cache entry from constant buffer.
  301. /// </summary>
  302. private class CacheEntryFromBuffer : CacheEntry
  303. {
  304. /// <summary>
  305. /// Key for this entry on the cache.
  306. /// </summary>
  307. public readonly CacheEntryFromBufferKey Key;
  308. /// <summary>
  309. /// Linked list node used on the texture bindings array cache.
  310. /// </summary>
  311. public LinkedListNode<CacheEntryFromBuffer> CacheNode;
  312. /// <summary>
  313. /// Timestamp set on the last use of the array by the cache.
  314. /// </summary>
  315. public int CacheTimestamp;
  316. /// <summary>
  317. /// All pool texture IDs along with their textures.
  318. /// </summary>
  319. public readonly Dictionary<int, (Texture, TextureDescriptor)> TextureIds;
  320. /// <summary>
  321. /// All pool sampler IDs along with their samplers.
  322. /// </summary>
  323. public readonly Dictionary<int, (Sampler, SamplerDescriptor)> SamplerIds;
  324. private int[] _cachedTextureBuffer;
  325. private int[] _cachedSamplerBuffer;
  326. private int _lastSequenceNumber;
  327. /// <summary>
  328. /// Creates a new array cache entry.
  329. /// </summary>
  330. /// <param name="key">Key for this entry on the cache</param>
  331. /// <param name="array">Backend texture array</param>
  332. /// <param name="texturePool">Texture pool where the array textures are located</param>
  333. /// <param name="samplerPool">Sampler pool where the array samplers are located</param>
  334. public CacheEntryFromBuffer(ref CacheEntryFromBufferKey key, ITextureArray array, TexturePool texturePool, SamplerPool samplerPool) : base(array, texturePool, samplerPool)
  335. {
  336. Key = key;
  337. _lastSequenceNumber = -1;
  338. TextureIds = new Dictionary<int, (Texture, TextureDescriptor)>();
  339. SamplerIds = new Dictionary<int, (Sampler, SamplerDescriptor)>();
  340. }
  341. /// <summary>
  342. /// Creates a new array cache entry.
  343. /// </summary>
  344. /// <param name="key">Key for this entry on the cache</param>
  345. /// <param name="array">Backend image array</param>
  346. /// <param name="texturePool">Texture pool where the array textures are located</param>
  347. /// <param name="samplerPool">Sampler pool where the array samplers are located</param>
  348. public CacheEntryFromBuffer(ref CacheEntryFromBufferKey key, IImageArray array, TexturePool texturePool, SamplerPool samplerPool) : base(array, texturePool, samplerPool)
  349. {
  350. Key = key;
  351. _lastSequenceNumber = -1;
  352. TextureIds = new Dictionary<int, (Texture, TextureDescriptor)>();
  353. SamplerIds = new Dictionary<int, (Sampler, SamplerDescriptor)>();
  354. }
  355. /// <inheritdoc/>
  356. public override void Reset()
  357. {
  358. base.Reset();
  359. TextureIds.Clear();
  360. SamplerIds.Clear();
  361. }
  362. /// <summary>
  363. /// Updates the cached constant buffer data.
  364. /// </summary>
  365. /// <param name="cachedTextureBuffer">Constant buffer data with the texture handles (and sampler handles, if they are combined)</param>
  366. /// <param name="cachedSamplerBuffer">Constant buffer data with the sampler handles</param>
  367. /// <param name="separateSamplerBuffer">Whether <paramref name="cachedTextureBuffer"/> and <paramref name="cachedSamplerBuffer"/> comes from different buffers</param>
  368. public void UpdateData(ReadOnlySpan<int> cachedTextureBuffer, ReadOnlySpan<int> cachedSamplerBuffer, bool separateSamplerBuffer)
  369. {
  370. _cachedTextureBuffer = cachedTextureBuffer.ToArray();
  371. _cachedSamplerBuffer = separateSamplerBuffer ? cachedSamplerBuffer.ToArray() : _cachedTextureBuffer;
  372. }
  373. /// <summary>
  374. /// Checks if the sequence number matches the one used on the last call to this method.
  375. /// </summary>
  376. /// <param name="currentSequenceNumber">Current sequence number</param>
  377. /// <returns>True if the sequence numbers match, false otherwise</returns>
  378. public bool MatchesSequenceNumber(int currentSequenceNumber)
  379. {
  380. if (_lastSequenceNumber == currentSequenceNumber)
  381. {
  382. return true;
  383. }
  384. _lastSequenceNumber = currentSequenceNumber;
  385. return false;
  386. }
  387. /// <summary>
  388. /// Checks if the buffer data matches the cached data.
  389. /// </summary>
  390. /// <param name="cachedTextureBuffer">New texture buffer data</param>
  391. /// <param name="cachedSamplerBuffer">New sampler buffer data</param>
  392. /// <param name="separateSamplerBuffer">Whether <paramref name="cachedTextureBuffer"/> and <paramref name="cachedSamplerBuffer"/> comes from different buffers</param>
  393. /// <param name="samplerWordOffset">Word offset of the sampler constant buffer handle that is used</param>
  394. /// <returns>True if the data matches, false otherwise</returns>
  395. public bool MatchesBufferData(
  396. ReadOnlySpan<int> cachedTextureBuffer,
  397. ReadOnlySpan<int> cachedSamplerBuffer,
  398. bool separateSamplerBuffer,
  399. int samplerWordOffset)
  400. {
  401. if (_cachedTextureBuffer != null && cachedTextureBuffer.Length > _cachedTextureBuffer.Length)
  402. {
  403. cachedTextureBuffer = cachedTextureBuffer[.._cachedTextureBuffer.Length];
  404. }
  405. if (!_cachedTextureBuffer.AsSpan().SequenceEqual(cachedTextureBuffer))
  406. {
  407. return false;
  408. }
  409. if (separateSamplerBuffer)
  410. {
  411. if (_cachedSamplerBuffer == null ||
  412. _cachedSamplerBuffer.Length <= samplerWordOffset ||
  413. cachedSamplerBuffer.Length <= samplerWordOffset)
  414. {
  415. return false;
  416. }
  417. int oldValue = _cachedSamplerBuffer[samplerWordOffset];
  418. int newValue = cachedSamplerBuffer[samplerWordOffset];
  419. return oldValue == newValue;
  420. }
  421. return true;
  422. }
  423. /// <summary>
  424. /// Checks if the cached texture or sampler pool has been modified since the last call to this method.
  425. /// </summary>
  426. /// <returns>True if any used entries of the pools might have been modified, false otherwise</returns>
  427. public bool PoolsModified()
  428. {
  429. bool texturePoolModified = TexturePoolModified();
  430. bool samplerPoolModified = SamplerPoolModified();
  431. // If both pools were not modified since the last check, we have nothing else to check.
  432. if (!texturePoolModified && !samplerPoolModified)
  433. {
  434. return false;
  435. }
  436. // If the pools were modified, let's check if any of the entries we care about changed.
  437. // Check if any of our cached textures changed on the pool.
  438. foreach ((int textureId, (Texture texture, TextureDescriptor descriptor)) in TextureIds)
  439. {
  440. if (TexturePool.GetCachedItem(textureId) != texture ||
  441. (texture == null && TexturePool.IsValidId(textureId) && !TexturePool.GetDescriptorRef(textureId).Equals(descriptor)))
  442. {
  443. return true;
  444. }
  445. }
  446. // Check if any of our cached samplers changed on the pool.
  447. if (SamplerPool != null)
  448. {
  449. foreach ((int samplerId, (Sampler sampler, SamplerDescriptor descriptor)) in SamplerIds)
  450. {
  451. if (SamplerPool.GetCachedItem(samplerId) != sampler ||
  452. (sampler == null && SamplerPool.IsValidId(samplerId) && !SamplerPool.GetDescriptorRef(samplerId).Equals(descriptor)))
  453. {
  454. return true;
  455. }
  456. }
  457. }
  458. return false;
  459. }
  460. }
  461. private readonly Dictionary<CacheEntryFromBufferKey, CacheEntryFromBuffer> _cacheFromBuffer;
  462. private readonly Dictionary<CacheEntryFromPoolKey, CacheEntry> _cacheFromPool;
  463. private readonly LinkedList<CacheEntryFromBuffer> _lruCache;
  464. private int _currentTimestamp;
  465. /// <summary>
  466. /// Creates a new instance of the texture bindings array cache.
  467. /// </summary>
  468. /// <param name="context">GPU context</param>
  469. /// <param name="channel">GPU channel</param>
  470. public TextureBindingsArrayCache(GpuContext context, GpuChannel channel)
  471. {
  472. _context = context;
  473. _channel = channel;
  474. _cacheFromBuffer = new Dictionary<CacheEntryFromBufferKey, CacheEntryFromBuffer>();
  475. _cacheFromPool = new Dictionary<CacheEntryFromPoolKey, CacheEntry>();
  476. _lruCache = new LinkedList<CacheEntryFromBuffer>();
  477. }
  478. /// <summary>
  479. /// Updates a texture array bindings and textures.
  480. /// </summary>
  481. /// <param name="texturePool">Texture pool</param>
  482. /// <param name="samplerPool">Sampler pool</param>
  483. /// <param name="stage">Shader stage where the array is used</param>
  484. /// <param name="stageIndex">Shader stage index where the array is used</param>
  485. /// <param name="textureBufferIndex">Texture constant buffer index</param>
  486. /// <param name="samplerIndex">Sampler handles source</param>
  487. /// <param name="bindingInfo">Array binding information</param>
  488. public void UpdateTextureArray(
  489. TexturePool texturePool,
  490. SamplerPool samplerPool,
  491. ShaderStage stage,
  492. int stageIndex,
  493. int textureBufferIndex,
  494. SamplerIndex samplerIndex,
  495. in TextureBindingInfo bindingInfo)
  496. {
  497. Update(texturePool, samplerPool, stage, stageIndex, textureBufferIndex, isImage: false, samplerIndex, bindingInfo);
  498. }
  499. /// <summary>
  500. /// Updates a image array bindings and textures.
  501. /// </summary>
  502. /// <param name="texturePool">Texture pool</param>
  503. /// <param name="stage">Shader stage where the array is used</param>
  504. /// <param name="stageIndex">Shader stage index where the array is used</param>
  505. /// <param name="textureBufferIndex">Texture constant buffer index</param>
  506. /// <param name="bindingInfo">Array binding information</param>
  507. public void UpdateImageArray(TexturePool texturePool, ShaderStage stage, int stageIndex, int textureBufferIndex, in TextureBindingInfo bindingInfo)
  508. {
  509. Update(texturePool, null, stage, stageIndex, textureBufferIndex, isImage: true, SamplerIndex.ViaHeaderIndex, bindingInfo);
  510. }
  511. /// <summary>
  512. /// Updates a texture or image array bindings and textures.
  513. /// </summary>
  514. /// <param name="texturePool">Texture pool</param>
  515. /// <param name="samplerPool">Sampler pool</param>
  516. /// <param name="stage">Shader stage where the array is used</param>
  517. /// <param name="stageIndex">Shader stage index where the array is used</param>
  518. /// <param name="textureBufferIndex">Texture constant buffer index</param>
  519. /// <param name="isImage">Whether the array is a image or texture array</param>
  520. /// <param name="samplerIndex">Sampler handles source</param>
  521. /// <param name="bindingInfo">Array binding information</param>
  522. private void Update(
  523. TexturePool texturePool,
  524. SamplerPool samplerPool,
  525. ShaderStage stage,
  526. int stageIndex,
  527. int textureBufferIndex,
  528. bool isImage,
  529. SamplerIndex samplerIndex,
  530. in TextureBindingInfo bindingInfo)
  531. {
  532. if (IsDirectHandleType(bindingInfo.Handle))
  533. {
  534. UpdateFromPool(texturePool, samplerPool, stage, isImage, bindingInfo);
  535. }
  536. else
  537. {
  538. UpdateFromBuffer(texturePool, samplerPool, stage, stageIndex, textureBufferIndex, isImage, samplerIndex, bindingInfo);
  539. }
  540. }
  541. /// <summary>
  542. /// Updates a texture or image array bindings and textures from a texture or sampler pool.
  543. /// </summary>
  544. /// <param name="texturePool">Texture pool</param>
  545. /// <param name="samplerPool">Sampler pool</param>
  546. /// <param name="stage">Shader stage where the array is used</param>
  547. /// <param name="isImage">Whether the array is a image or texture array</param>
  548. /// <param name="bindingInfo">Array binding information</param>
  549. private void UpdateFromPool(TexturePool texturePool, SamplerPool samplerPool, ShaderStage stage, bool isImage, in TextureBindingInfo bindingInfo)
  550. {
  551. CacheEntry entry = GetOrAddEntry(texturePool, samplerPool, bindingInfo, isImage, out bool isNewEntry);
  552. bool isSampler = bindingInfo.IsSamplerOnly;
  553. bool poolModified = isSampler ? entry.SamplerPoolModified() : entry.TexturePoolModified();
  554. bool isStore = bindingInfo.Flags.HasFlag(TextureUsageFlags.ImageStore);
  555. bool resScaleUnsupported = bindingInfo.Flags.HasFlag(TextureUsageFlags.ResScaleUnsupported);
  556. if (!poolModified && !isNewEntry && entry.ValidateTextures())
  557. {
  558. entry.SynchronizeMemory(isStore, resScaleUnsupported);
  559. if (isImage)
  560. {
  561. SetImageArray(stage, bindingInfo, entry.ImageArray);
  562. }
  563. else
  564. {
  565. SetTextureArray(stage, bindingInfo, entry.TextureArray);
  566. }
  567. return;
  568. }
  569. if (!isNewEntry)
  570. {
  571. entry.Reset();
  572. }
  573. int length = (isSampler ? samplerPool.MaximumId : texturePool.MaximumId) + 1;
  574. length = Math.Min(length, bindingInfo.ArrayLength);
  575. ISampler[] samplers = isImage ? null : new ISampler[bindingInfo.ArrayLength];
  576. ITexture[] textures = new ITexture[bindingInfo.ArrayLength];
  577. for (int index = 0; index < length; index++)
  578. {
  579. Texture texture = null;
  580. Sampler sampler = null;
  581. if (isSampler)
  582. {
  583. sampler = samplerPool?.Get(index);
  584. }
  585. else
  586. {
  587. ref readonly TextureDescriptor descriptor = ref texturePool.GetForBinding(index, bindingInfo.FormatInfo, out texture);
  588. if (texture != null)
  589. {
  590. entry.Textures[texture] = texture.InvalidatedSequence;
  591. if (isStore)
  592. {
  593. texture.SignalModified();
  594. }
  595. if (resScaleUnsupported && texture.ScaleMode != TextureScaleMode.Blacklisted)
  596. {
  597. // Scaling textures used on arrays is currently not supported.
  598. texture.BlacklistScale();
  599. }
  600. }
  601. }
  602. ITexture hostTexture = texture?.GetTargetTexture(bindingInfo.Target);
  603. ISampler hostSampler = sampler?.GetHostSampler(texture);
  604. if (hostTexture != null && texture.Target == Target.TextureBuffer)
  605. {
  606. // Ensure that the buffer texture is using the correct buffer as storage.
  607. // Buffers are frequently re-created to accommodate larger data, so we need to re-bind
  608. // to ensure we're not using a old buffer that was already deleted.
  609. if (isImage)
  610. {
  611. _channel.BufferManager.SetBufferTextureStorage(stage, entry.ImageArray, hostTexture, texture.Range, bindingInfo, index);
  612. }
  613. else
  614. {
  615. _channel.BufferManager.SetBufferTextureStorage(stage, entry.TextureArray, hostTexture, texture.Range, bindingInfo, index);
  616. }
  617. }
  618. else if (isImage)
  619. {
  620. textures[index] = hostTexture;
  621. }
  622. else
  623. {
  624. samplers[index] = hostSampler;
  625. textures[index] = hostTexture;
  626. }
  627. }
  628. if (isImage)
  629. {
  630. entry.ImageArray.SetImages(0, textures);
  631. SetImageArray(stage, bindingInfo, entry.ImageArray);
  632. }
  633. else
  634. {
  635. entry.TextureArray.SetSamplers(0, samplers);
  636. entry.TextureArray.SetTextures(0, textures);
  637. SetTextureArray(stage, bindingInfo, entry.TextureArray);
  638. }
  639. }
  640. /// <summary>
  641. /// Updates a texture or image array bindings and textures from constant buffer handles.
  642. /// </summary>
  643. /// <param name="texturePool">Texture pool</param>
  644. /// <param name="samplerPool">Sampler pool</param>
  645. /// <param name="stage">Shader stage where the array is used</param>
  646. /// <param name="stageIndex">Shader stage index where the array is used</param>
  647. /// <param name="textureBufferIndex">Texture constant buffer index</param>
  648. /// <param name="isImage">Whether the array is a image or texture array</param>
  649. /// <param name="samplerIndex">Sampler handles source</param>
  650. /// <param name="bindingInfo">Array binding information</param>
  651. private void UpdateFromBuffer(
  652. TexturePool texturePool,
  653. SamplerPool samplerPool,
  654. ShaderStage stage,
  655. int stageIndex,
  656. int textureBufferIndex,
  657. bool isImage,
  658. SamplerIndex samplerIndex,
  659. in TextureBindingInfo bindingInfo)
  660. {
  661. (textureBufferIndex, int samplerBufferIndex) = TextureHandle.UnpackSlots(bindingInfo.CbufSlot, textureBufferIndex);
  662. bool separateSamplerBuffer = textureBufferIndex != samplerBufferIndex;
  663. bool isCompute = stage == ShaderStage.Compute;
  664. ref BufferBounds textureBufferBounds = ref _channel.BufferManager.GetUniformBufferBounds(isCompute, stageIndex, textureBufferIndex);
  665. ref BufferBounds samplerBufferBounds = ref _channel.BufferManager.GetUniformBufferBounds(isCompute, stageIndex, samplerBufferIndex);
  666. CacheEntryFromBuffer entry = GetOrAddEntry(
  667. texturePool,
  668. samplerPool,
  669. bindingInfo,
  670. isImage,
  671. ref textureBufferBounds,
  672. out bool isNewEntry);
  673. bool poolsModified = entry.PoolsModified();
  674. bool isStore = bindingInfo.Flags.HasFlag(TextureUsageFlags.ImageStore);
  675. bool resScaleUnsupported = bindingInfo.Flags.HasFlag(TextureUsageFlags.ResScaleUnsupported);
  676. ReadOnlySpan<int> cachedTextureBuffer;
  677. ReadOnlySpan<int> cachedSamplerBuffer;
  678. if (!poolsModified && !isNewEntry && entry.ValidateTextures())
  679. {
  680. if (entry.MatchesSequenceNumber(_context.SequenceNumber))
  681. {
  682. entry.SynchronizeMemory(isStore, resScaleUnsupported);
  683. if (isImage)
  684. {
  685. SetImageArray(stage, bindingInfo, entry.ImageArray);
  686. }
  687. else
  688. {
  689. SetTextureArray(stage, bindingInfo, entry.TextureArray);
  690. }
  691. return;
  692. }
  693. cachedTextureBuffer = MemoryMarshal.Cast<byte, int>(_channel.MemoryManager.Physical.GetSpan(textureBufferBounds.Range));
  694. if (separateSamplerBuffer)
  695. {
  696. cachedSamplerBuffer = MemoryMarshal.Cast<byte, int>(_channel.MemoryManager.Physical.GetSpan(samplerBufferBounds.Range));
  697. }
  698. else
  699. {
  700. cachedSamplerBuffer = cachedTextureBuffer;
  701. }
  702. (_, int samplerWordOffset, _) = TextureHandle.UnpackOffsets(bindingInfo.Handle);
  703. if (entry.MatchesBufferData(cachedTextureBuffer, cachedSamplerBuffer, separateSamplerBuffer, samplerWordOffset))
  704. {
  705. entry.SynchronizeMemory(isStore, resScaleUnsupported);
  706. if (isImage)
  707. {
  708. SetImageArray(stage, bindingInfo, entry.ImageArray);
  709. }
  710. else
  711. {
  712. SetTextureArray(stage, bindingInfo, entry.TextureArray);
  713. }
  714. return;
  715. }
  716. }
  717. else
  718. {
  719. cachedTextureBuffer = MemoryMarshal.Cast<byte, int>(_channel.MemoryManager.Physical.GetSpan(textureBufferBounds.Range));
  720. if (separateSamplerBuffer)
  721. {
  722. cachedSamplerBuffer = MemoryMarshal.Cast<byte, int>(_channel.MemoryManager.Physical.GetSpan(samplerBufferBounds.Range));
  723. }
  724. else
  725. {
  726. cachedSamplerBuffer = cachedTextureBuffer;
  727. }
  728. }
  729. if (!isNewEntry)
  730. {
  731. entry.Reset();
  732. }
  733. entry.UpdateData(cachedTextureBuffer, cachedSamplerBuffer, separateSamplerBuffer);
  734. ISampler[] samplers = isImage ? null : new ISampler[bindingInfo.ArrayLength];
  735. ITexture[] textures = new ITexture[bindingInfo.ArrayLength];
  736. for (int index = 0; index < bindingInfo.ArrayLength; index++)
  737. {
  738. int handleIndex = bindingInfo.Handle + index * (Constants.TextureHandleSizeInBytes / sizeof(int));
  739. int packedId = TextureHandle.ReadPackedId(handleIndex, cachedTextureBuffer, cachedSamplerBuffer);
  740. int textureId = TextureHandle.UnpackTextureId(packedId);
  741. int samplerId;
  742. if (samplerIndex == SamplerIndex.ViaHeaderIndex)
  743. {
  744. samplerId = textureId;
  745. }
  746. else
  747. {
  748. samplerId = TextureHandle.UnpackSamplerId(packedId);
  749. }
  750. ref readonly TextureDescriptor descriptor = ref texturePool.GetForBinding(textureId, bindingInfo.FormatInfo, out Texture texture);
  751. if (texture != null)
  752. {
  753. entry.Textures[texture] = texture.InvalidatedSequence;
  754. if (isStore)
  755. {
  756. texture.SignalModified();
  757. }
  758. if (resScaleUnsupported && texture.ScaleMode != TextureScaleMode.Blacklisted)
  759. {
  760. // Scaling textures used on arrays is currently not supported.
  761. texture.BlacklistScale();
  762. }
  763. }
  764. entry.TextureIds[textureId] = (texture, descriptor);
  765. ITexture hostTexture = texture?.GetTargetTexture(bindingInfo.Target);
  766. ISampler hostSampler = null;
  767. if (!isImage && bindingInfo.Target != Target.TextureBuffer)
  768. {
  769. Sampler sampler = samplerPool?.Get(samplerId);
  770. entry.SamplerIds[samplerId] = (sampler, samplerPool?.GetDescriptorRef(samplerId) ?? default);
  771. hostSampler = sampler?.GetHostSampler(texture);
  772. }
  773. if (hostTexture != null && texture.Target == Target.TextureBuffer)
  774. {
  775. // Ensure that the buffer texture is using the correct buffer as storage.
  776. // Buffers are frequently re-created to accommodate larger data, so we need to re-bind
  777. // to ensure we're not using a old buffer that was already deleted.
  778. if (isImage)
  779. {
  780. _channel.BufferManager.SetBufferTextureStorage(stage, entry.ImageArray, hostTexture, texture.Range, bindingInfo, index);
  781. }
  782. else
  783. {
  784. _channel.BufferManager.SetBufferTextureStorage(stage, entry.TextureArray, hostTexture, texture.Range, bindingInfo, index);
  785. }
  786. }
  787. else if (isImage)
  788. {
  789. textures[index] = hostTexture;
  790. }
  791. else
  792. {
  793. samplers[index] = hostSampler;
  794. textures[index] = hostTexture;
  795. }
  796. }
  797. if (isImage)
  798. {
  799. entry.ImageArray.SetImages(0, textures);
  800. SetImageArray(stage, bindingInfo, entry.ImageArray);
  801. }
  802. else
  803. {
  804. entry.TextureArray.SetSamplers(0, samplers);
  805. entry.TextureArray.SetTextures(0, textures);
  806. SetTextureArray(stage, bindingInfo, entry.TextureArray);
  807. }
  808. }
  809. /// <summary>
  810. /// Updates a texture array binding on the host.
  811. /// </summary>
  812. /// <param name="stage">Shader stage where the array is used</param>
  813. /// <param name="bindingInfo">Array binding information</param>
  814. /// <param name="array">Texture array</param>
  815. private void SetTextureArray(ShaderStage stage, in TextureBindingInfo bindingInfo, ITextureArray array)
  816. {
  817. if (bindingInfo.Set >= _context.Capabilities.ExtraSetBaseIndex && _context.Capabilities.MaximumExtraSets != 0)
  818. {
  819. _context.Renderer.Pipeline.SetTextureArraySeparate(stage, bindingInfo.Set, array);
  820. }
  821. else
  822. {
  823. _context.Renderer.Pipeline.SetTextureArray(stage, bindingInfo.Binding, array);
  824. }
  825. }
  826. /// <summary>
  827. /// Updates a image array binding on the host.
  828. /// </summary>
  829. /// <param name="stage">Shader stage where the array is used</param>
  830. /// <param name="bindingInfo">Array binding information</param>
  831. /// <param name="array">Image array</param>
  832. private void SetImageArray(ShaderStage stage, in TextureBindingInfo bindingInfo, IImageArray array)
  833. {
  834. if (bindingInfo.Set >= _context.Capabilities.ExtraSetBaseIndex && _context.Capabilities.MaximumExtraSets != 0)
  835. {
  836. _context.Renderer.Pipeline.SetImageArraySeparate(stage, bindingInfo.Set, array);
  837. }
  838. else
  839. {
  840. _context.Renderer.Pipeline.SetImageArray(stage, bindingInfo.Binding, array);
  841. }
  842. }
  843. /// <summary>
  844. /// Gets a cached texture entry from pool, or creates a new one if not found.
  845. /// </summary>
  846. /// <param name="texturePool">Texture pool</param>
  847. /// <param name="samplerPool">Sampler pool</param>
  848. /// <param name="bindingInfo">Array binding information</param>
  849. /// <param name="isImage">Whether the array is a image or texture array</param>
  850. /// <param name="isNew">Whether a new entry was created, or an existing one was returned</param>
  851. /// <returns>Cache entry</returns>
  852. private CacheEntry GetOrAddEntry(
  853. TexturePool texturePool,
  854. SamplerPool samplerPool,
  855. in TextureBindingInfo bindingInfo,
  856. bool isImage,
  857. out bool isNew)
  858. {
  859. CacheEntryFromPoolKey key = new CacheEntryFromPoolKey(isImage, bindingInfo, texturePool, samplerPool);
  860. isNew = !_cacheFromPool.TryGetValue(key, out CacheEntry entry);
  861. if (isNew)
  862. {
  863. int arrayLength = bindingInfo.ArrayLength;
  864. if (isImage)
  865. {
  866. IImageArray array = _context.Renderer.CreateImageArray(arrayLength, bindingInfo.Target == Target.TextureBuffer);
  867. _cacheFromPool.Add(key, entry = new CacheEntry(array, texturePool, samplerPool));
  868. }
  869. else
  870. {
  871. ITextureArray array = _context.Renderer.CreateTextureArray(arrayLength, bindingInfo.Target == Target.TextureBuffer);
  872. _cacheFromPool.Add(key, entry = new CacheEntry(array, texturePool, samplerPool));
  873. }
  874. }
  875. return entry;
  876. }
  877. /// <summary>
  878. /// Gets a cached texture entry from constant buffer, or creates a new one if not found.
  879. /// </summary>
  880. /// <param name="texturePool">Texture pool</param>
  881. /// <param name="samplerPool">Sampler pool</param>
  882. /// <param name="bindingInfo">Array binding information</param>
  883. /// <param name="isImage">Whether the array is a image or texture array</param>
  884. /// <param name="textureBufferBounds">Constant buffer bounds with the texture handles</param>
  885. /// <param name="isNew">Whether a new entry was created, or an existing one was returned</param>
  886. /// <returns>Cache entry</returns>
  887. private CacheEntryFromBuffer GetOrAddEntry(
  888. TexturePool texturePool,
  889. SamplerPool samplerPool,
  890. in TextureBindingInfo bindingInfo,
  891. bool isImage,
  892. ref BufferBounds textureBufferBounds,
  893. out bool isNew)
  894. {
  895. CacheEntryFromBufferKey key = new CacheEntryFromBufferKey(
  896. isImage,
  897. bindingInfo,
  898. texturePool,
  899. samplerPool,
  900. ref textureBufferBounds);
  901. isNew = !_cacheFromBuffer.TryGetValue(key, out CacheEntryFromBuffer entry);
  902. if (isNew)
  903. {
  904. int arrayLength = bindingInfo.ArrayLength;
  905. if (isImage)
  906. {
  907. IImageArray array = _context.Renderer.CreateImageArray(arrayLength, bindingInfo.Target == Target.TextureBuffer);
  908. _cacheFromBuffer.Add(key, entry = new CacheEntryFromBuffer(ref key, array, texturePool, samplerPool));
  909. }
  910. else
  911. {
  912. ITextureArray array = _context.Renderer.CreateTextureArray(arrayLength, bindingInfo.Target == Target.TextureBuffer);
  913. _cacheFromBuffer.Add(key, entry = new CacheEntryFromBuffer(ref key, array, texturePool, samplerPool));
  914. }
  915. }
  916. if (entry.CacheNode != null)
  917. {
  918. _lruCache.Remove(entry.CacheNode);
  919. _lruCache.AddLast(entry.CacheNode);
  920. }
  921. else
  922. {
  923. entry.CacheNode = _lruCache.AddLast(entry);
  924. }
  925. entry.CacheTimestamp = ++_currentTimestamp;
  926. RemoveLeastUsedEntries();
  927. return entry;
  928. }
  929. /// <summary>
  930. /// Remove entries from the cache that have not been used for some time.
  931. /// </summary>
  932. private void RemoveLeastUsedEntries()
  933. {
  934. LinkedListNode<CacheEntryFromBuffer> nextNode = _lruCache.First;
  935. while (nextNode != null && _currentTimestamp - nextNode.Value.CacheTimestamp >= MinDeltaForRemoval)
  936. {
  937. LinkedListNode<CacheEntryFromBuffer> toRemove = nextNode;
  938. nextNode = nextNode.Next;
  939. _cacheFromBuffer.Remove(toRemove.Value.Key);
  940. _lruCache.Remove(toRemove);
  941. if (toRemove.Value.Key.IsImage)
  942. {
  943. toRemove.Value.ImageArray.Dispose();
  944. }
  945. else
  946. {
  947. toRemove.Value.TextureArray.Dispose();
  948. }
  949. }
  950. }
  951. /// <summary>
  952. /// Removes all cached texture arrays matching the specified texture pool.
  953. /// </summary>
  954. /// <param name="pool">Texture pool</param>
  955. public void RemoveAllWithPool<T>(IPool<T> pool)
  956. {
  957. List<CacheEntryFromPoolKey> keysToRemove = null;
  958. foreach ((CacheEntryFromPoolKey key, CacheEntry entry) in _cacheFromPool)
  959. {
  960. if (key.MatchesPool(pool))
  961. {
  962. (keysToRemove ??= new()).Add(key);
  963. if (key.IsImage)
  964. {
  965. entry.ImageArray.Dispose();
  966. }
  967. else
  968. {
  969. entry.TextureArray.Dispose();
  970. }
  971. }
  972. }
  973. if (keysToRemove != null)
  974. {
  975. foreach (CacheEntryFromPoolKey key in keysToRemove)
  976. {
  977. _cacheFromPool.Remove(key);
  978. }
  979. }
  980. }
  981. /// <summary>
  982. /// Checks if a handle indicates the binding should have all its textures sourced directly from a pool.
  983. /// </summary>
  984. /// <param name="handle">Handle to check</param>
  985. /// <returns>True if the handle represents direct pool access, false otherwise</returns>
  986. private static bool IsDirectHandleType(int handle)
  987. {
  988. (_, _, TextureHandleType type) = TextureHandle.UnpackOffsets(handle);
  989. return type == TextureHandleType.Direct;
  990. }
  991. }
  992. }