TexturePoolCache.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 LinkedList<TexturePool> _pools;
  9. public TexturePoolCache(GpuContext context)
  10. {
  11. _context = context;
  12. _pools = new LinkedList<TexturePool>();
  13. }
  14. public TexturePool FindOrCreate(ulong address, int maximumId)
  15. {
  16. TexturePool pool;
  17. // First we try to find the pool.
  18. for (LinkedListNode<TexturePool> node = _pools.First; node != null; node = node.Next)
  19. {
  20. pool = node.Value;
  21. if (pool.Address == address)
  22. {
  23. if (pool.CacheNode != _pools.Last)
  24. {
  25. _pools.Remove(pool.CacheNode);
  26. pool.CacheNode = _pools.AddLast(pool);
  27. }
  28. return pool;
  29. }
  30. }
  31. // If not found, create a new one.
  32. pool = new TexturePool(_context, address, maximumId);
  33. pool.CacheNode = _pools.AddLast(pool);
  34. if (_pools.Count > MaxCapacity)
  35. {
  36. TexturePool oldestPool = _pools.First.Value;
  37. _pools.RemoveFirst();
  38. oldestPool.Dispose();
  39. oldestPool.CacheNode = null;
  40. }
  41. return pool;
  42. }
  43. public void InvalidateRange(ulong address, ulong size)
  44. {
  45. for (LinkedListNode<TexturePool> node = _pools.First; node != null; node = node.Next)
  46. {
  47. TexturePool pool = node.Value;
  48. pool.InvalidateRange(address, size);
  49. }
  50. }
  51. }
  52. }