OglConstBuffer.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using OpenTK.Graphics.OpenGL;
  2. using System;
  3. namespace Ryujinx.Graphics.Gal.OpenGL
  4. {
  5. class OglConstBuffer : IGalConstBuffer
  6. {
  7. private const long MaxConstBufferCacheSize = 64 * 1024 * 1024;
  8. private OglCachedResource<OglStreamBuffer> _cache;
  9. public OglConstBuffer()
  10. {
  11. _cache = new OglCachedResource<OglStreamBuffer>(DeleteBuffer, MaxConstBufferCacheSize);
  12. }
  13. public void LockCache()
  14. {
  15. _cache.Lock();
  16. }
  17. public void UnlockCache()
  18. {
  19. _cache.Unlock();
  20. }
  21. public void Create(long key, long size)
  22. {
  23. OglStreamBuffer buffer = new OglStreamBuffer(BufferTarget.UniformBuffer, size);
  24. _cache.AddOrUpdate(key, buffer, size);
  25. }
  26. public bool IsCached(long key, long size)
  27. {
  28. return _cache.TryGetSize(key, out long cachedSize) && cachedSize == size;
  29. }
  30. public void SetData(long key, long size, IntPtr hostAddress)
  31. {
  32. if (_cache.TryGetValue(key, out OglStreamBuffer buffer))
  33. {
  34. buffer.SetData(size, hostAddress);
  35. }
  36. }
  37. public void SetData(long key, byte[] data)
  38. {
  39. if (_cache.TryGetValue(key, out OglStreamBuffer buffer))
  40. {
  41. buffer.SetData(data);
  42. }
  43. }
  44. public bool TryGetUbo(long key, out int uboHandle)
  45. {
  46. if (_cache.TryGetValue(key, out OglStreamBuffer buffer))
  47. {
  48. uboHandle = buffer.Handle;
  49. return true;
  50. }
  51. uboHandle = 0;
  52. return false;
  53. }
  54. private static void DeleteBuffer(OglStreamBuffer buffer)
  55. {
  56. buffer.Dispose();
  57. }
  58. }
  59. }