StateCache.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.Versioning;
  4. namespace Ryujinx.Graphics.Metal
  5. {
  6. [SupportedOSPlatform("macos")]
  7. abstract class StateCache<T, TDescriptor, THash> : IDisposable where T : IDisposable
  8. {
  9. private readonly Dictionary<THash, T> _cache = new();
  10. protected abstract THash GetHash(TDescriptor descriptor);
  11. protected abstract T CreateValue(TDescriptor descriptor);
  12. public void Dispose()
  13. {
  14. foreach (T value in _cache.Values)
  15. {
  16. value.Dispose();
  17. }
  18. GC.SuppressFinalize(this);
  19. }
  20. public T GetOrCreate(TDescriptor descriptor)
  21. {
  22. THash hash = GetHash(descriptor);
  23. if (_cache.TryGetValue(hash, out T value))
  24. {
  25. return value;
  26. }
  27. else
  28. {
  29. T newValue = CreateValue(descriptor);
  30. _cache.Add(hash, newValue);
  31. return newValue;
  32. }
  33. }
  34. }
  35. }