Buffer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using System;
  4. namespace Ryujinx.Graphics.OpenGL
  5. {
  6. static class Buffer
  7. {
  8. public static void Clear(BufferHandle destination, int offset, int size, uint value)
  9. {
  10. GL.BindBuffer(BufferTarget.CopyWriteBuffer, destination.ToInt32());
  11. unsafe
  12. {
  13. uint* valueArr = stackalloc uint[1];
  14. valueArr[0] = value;
  15. GL.ClearBufferSubData(
  16. BufferTarget.CopyWriteBuffer,
  17. PixelInternalFormat.Rgba8ui,
  18. (IntPtr)offset,
  19. (IntPtr)size,
  20. PixelFormat.RgbaInteger,
  21. PixelType.UnsignedByte,
  22. (IntPtr)valueArr);
  23. }
  24. }
  25. public static BufferHandle Create()
  26. {
  27. return Handle.FromInt32<BufferHandle>(GL.GenBuffer());
  28. }
  29. public static BufferHandle Create(int size)
  30. {
  31. int handle = GL.GenBuffer();
  32. GL.BindBuffer(BufferTarget.CopyWriteBuffer, handle);
  33. GL.BufferData(BufferTarget.CopyWriteBuffer, size, IntPtr.Zero, BufferUsageHint.DynamicDraw);
  34. return Handle.FromInt32<BufferHandle>(handle);
  35. }
  36. public static void Copy(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
  37. {
  38. GL.BindBuffer(BufferTarget.CopyReadBuffer, source.ToInt32());
  39. GL.BindBuffer(BufferTarget.CopyWriteBuffer, destination.ToInt32());
  40. GL.CopyBufferSubData(
  41. BufferTarget.CopyReadBuffer,
  42. BufferTarget.CopyWriteBuffer,
  43. (IntPtr)srcOffset,
  44. (IntPtr)dstOffset,
  45. (IntPtr)size);
  46. }
  47. public static unsafe PinnedSpan<byte> GetData(OpenGLRenderer renderer, BufferHandle buffer, int offset, int size)
  48. {
  49. // Data in the persistent buffer and host array is guaranteed to be available
  50. // until the next time the host thread requests data.
  51. if (HwCapabilities.UsePersistentBufferForFlush)
  52. {
  53. return PinnedSpan<byte>.UnsafeFromSpan(renderer.PersistentBuffers.Default.GetBufferData(buffer, offset, size));
  54. }
  55. else
  56. {
  57. IntPtr target = renderer.PersistentBuffers.Default.GetHostArray(size);
  58. GL.BindBuffer(BufferTarget.CopyReadBuffer, buffer.ToInt32());
  59. GL.GetBufferSubData(BufferTarget.CopyReadBuffer, (IntPtr)offset, size, target);
  60. return new PinnedSpan<byte>(target.ToPointer(), size);
  61. }
  62. }
  63. public static void Resize(BufferHandle handle, int size)
  64. {
  65. GL.BindBuffer(BufferTarget.CopyWriteBuffer, handle.ToInt32());
  66. GL.BufferData(BufferTarget.CopyWriteBuffer, size, IntPtr.Zero, BufferUsageHint.StreamCopy);
  67. }
  68. public static void SetData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  69. {
  70. GL.BindBuffer(BufferTarget.CopyWriteBuffer, buffer.ToInt32());
  71. unsafe
  72. {
  73. fixed (byte* ptr = data)
  74. {
  75. GL.BufferSubData(BufferTarget.CopyWriteBuffer, (IntPtr)offset, data.Length, (IntPtr)ptr);
  76. }
  77. }
  78. }
  79. public static void Delete(BufferHandle buffer)
  80. {
  81. GL.DeleteBuffer(buffer.ToInt32());
  82. }
  83. }
  84. }