PersistentBuffers.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using OpenTK.Graphics.OpenGL;
  4. using Ryujinx.Common.Logging;
  5. using Ryujinx.Graphics.GAL;
  6. using Ryujinx.Graphics.OpenGL.Image;
  7. namespace Ryujinx.Graphics.OpenGL
  8. {
  9. class PersistentBuffers : IDisposable
  10. {
  11. private PersistentBuffer _main = new PersistentBuffer();
  12. private PersistentBuffer _background = new PersistentBuffer();
  13. public PersistentBuffer Default => BackgroundContextWorker.InBackground ? _background : _main;
  14. public void Dispose()
  15. {
  16. _main?.Dispose();
  17. _background?.Dispose();
  18. }
  19. }
  20. class PersistentBuffer : IDisposable
  21. {
  22. private IntPtr _bufferMap;
  23. private int _copyBufferHandle;
  24. private int _copyBufferSize;
  25. private void EnsureBuffer(int requiredSize)
  26. {
  27. if (_copyBufferSize < requiredSize && _copyBufferHandle != 0)
  28. {
  29. GL.DeleteBuffer(_copyBufferHandle);
  30. _copyBufferHandle = 0;
  31. }
  32. if (_copyBufferHandle == 0)
  33. {
  34. _copyBufferHandle = GL.GenBuffer();
  35. _copyBufferSize = requiredSize;
  36. GL.BindBuffer(BufferTarget.CopyWriteBuffer, _copyBufferHandle);
  37. GL.BufferStorage(BufferTarget.CopyWriteBuffer, requiredSize, IntPtr.Zero, BufferStorageFlags.MapReadBit | BufferStorageFlags.MapPersistentBit);
  38. _bufferMap = GL.MapBufferRange(BufferTarget.CopyWriteBuffer, IntPtr.Zero, requiredSize, BufferAccessMask.MapReadBit | BufferAccessMask.MapPersistentBit);
  39. }
  40. }
  41. private void Sync()
  42. {
  43. GL.MemoryBarrier(MemoryBarrierFlags.ClientMappedBufferBarrierBit);
  44. IntPtr sync = GL.FenceSync(SyncCondition.SyncGpuCommandsComplete, WaitSyncFlags.None);
  45. WaitSyncStatus syncResult = GL.ClientWaitSync(sync, ClientWaitSyncFlags.SyncFlushCommandsBit, 1000000000);
  46. if (syncResult == WaitSyncStatus.TimeoutExpired)
  47. {
  48. Logger.Error?.PrintMsg(LogClass.Gpu, $"Failed to sync persistent buffer state within 1000ms. Continuing...");
  49. }
  50. GL.DeleteSync(sync);
  51. }
  52. public byte[] GetTextureData(TextureView view, int size)
  53. {
  54. EnsureBuffer(size);
  55. GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyBufferHandle);
  56. view.WriteToPbo(0, false);
  57. GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
  58. byte[] data = new byte[size];
  59. Sync();
  60. Marshal.Copy(_bufferMap, data, 0, size);
  61. return data;
  62. }
  63. public byte[] GetBufferData(BufferHandle buffer, int offset, int size)
  64. {
  65. EnsureBuffer(size);
  66. GL.BindBuffer(BufferTarget.CopyReadBuffer, buffer.ToInt32());
  67. GL.BindBuffer(BufferTarget.CopyWriteBuffer, _copyBufferHandle);
  68. GL.CopyBufferSubData(BufferTarget.CopyReadBuffer, BufferTarget.CopyWriteBuffer, (IntPtr)offset, IntPtr.Zero, size);
  69. GL.BindBuffer(BufferTarget.CopyWriteBuffer, 0);
  70. byte[] data = new byte[size];
  71. Sync();
  72. Marshal.Copy(_bufferMap, data, 0, size);
  73. return data;
  74. }
  75. public void Dispose()
  76. {
  77. if (_copyBufferHandle != 0)
  78. {
  79. GL.DeleteBuffer(_copyBufferHandle);
  80. }
  81. }
  82. }
  83. }