TexturePoolCache.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections.Generic;
  2. namespace Ryujinx.Graphics.Gpu.Image
  3. {
  4. class TexturePoolCache
  5. {
  6. private const int MaxCapacity = 4;
  7. private GpuContext _context;
  8. private TextureManager _textureManager;
  9. private LinkedList<TexturePool> _pools;
  10. public TexturePoolCache(GpuContext context, TextureManager textureManager)
  11. {
  12. _context = context;
  13. _textureManager = textureManager;
  14. _pools = new LinkedList<TexturePool>();
  15. }
  16. public TexturePool FindOrCreate(ulong address, int maximumId)
  17. {
  18. TexturePool pool;
  19. // First we try to find the pool.
  20. for (LinkedListNode<TexturePool> node = _pools.First; node != null; node = node.Next)
  21. {
  22. pool = node.Value;
  23. if (pool.Address == address)
  24. {
  25. if (pool.CacheNode != _pools.Last)
  26. {
  27. _pools.Remove(pool.CacheNode);
  28. pool.CacheNode = _pools.AddLast(pool);
  29. }
  30. return pool;
  31. }
  32. }
  33. // If not found, create a new one.
  34. pool = new TexturePool(_context, _textureManager, address, maximumId);
  35. pool.CacheNode = _pools.AddLast(pool);
  36. if (_pools.Count > MaxCapacity)
  37. {
  38. TexturePool oldestPool = _pools.First.Value;
  39. _pools.RemoveFirst();
  40. oldestPool.Dispose();
  41. oldestPool.CacheNode = null;
  42. }
  43. return pool;
  44. }
  45. public void InvalidateRange(ulong address, ulong size)
  46. {
  47. for (LinkedListNode<TexturePool> node = _pools.First; node != null; node = node.Next)
  48. {
  49. TexturePool pool = node.Value;
  50. pool.InvalidateRange(address, size);
  51. }
  52. }
  53. }
  54. }