TextureBindingsArrayCache.cs 47 KB

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