PersistentBuffers.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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)
  53. {
  54. int size = 0;
  55. for (int level = 0; level < view.Info.Levels; level++)
  56. {
  57. size += view.Info.GetMipSize(level);
  58. }
  59. EnsureBuffer(size);
  60. GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyBufferHandle);
  61. view.WriteToPbo(0, false);
  62. GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
  63. byte[] data = new byte[size];
  64. Sync();
  65. Marshal.Copy(_bufferMap, data, 0, size);
  66. return data;
  67. }
  68. public byte[] GetBufferData(BufferHandle buffer, int offset, int size)
  69. {
  70. EnsureBuffer(size);
  71. GL.BindBuffer(BufferTarget.CopyReadBuffer, buffer.ToInt32());
  72. GL.BindBuffer(BufferTarget.CopyWriteBuffer, _copyBufferHandle);
  73. GL.CopyBufferSubData(BufferTarget.CopyReadBuffer, BufferTarget.CopyWriteBuffer, (IntPtr)offset, IntPtr.Zero, size);
  74. GL.BindBuffer(BufferTarget.CopyWriteBuffer, 0);
  75. byte[] data = new byte[size];
  76. Sync();
  77. Marshal.Copy(_bufferMap, data, 0, size);
  78. return data;
  79. }
  80. public void Dispose()
  81. {
  82. if (_copyBufferHandle != 0)
  83. {
  84. GL.DeleteBuffer(_copyBufferHandle);
  85. }
  86. }
  87. }
  88. }