Buffer.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 BufferHandle Create()
  9. {
  10. return Handle.FromInt32<BufferHandle>(GL.GenBuffer());
  11. }
  12. public static BufferHandle Create(int size)
  13. {
  14. int handle = GL.GenBuffer();
  15. GL.BindBuffer(BufferTarget.CopyWriteBuffer, handle);
  16. GL.BufferData(BufferTarget.CopyWriteBuffer, size, IntPtr.Zero, BufferUsageHint.DynamicDraw);
  17. return Handle.FromInt32<BufferHandle>(handle);
  18. }
  19. public static void Copy(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
  20. {
  21. GL.BindBuffer(BufferTarget.CopyReadBuffer, source.ToInt32());
  22. GL.BindBuffer(BufferTarget.CopyWriteBuffer, destination.ToInt32());
  23. GL.CopyBufferSubData(
  24. BufferTarget.CopyReadBuffer,
  25. BufferTarget.CopyWriteBuffer,
  26. (IntPtr)srcOffset,
  27. (IntPtr)dstOffset,
  28. (IntPtr)size);
  29. }
  30. public static byte[] GetData(BufferHandle buffer, int offset, int size)
  31. {
  32. GL.BindBuffer(BufferTarget.CopyReadBuffer, buffer.ToInt32());
  33. byte[] data = new byte[size];
  34. GL.GetBufferSubData(BufferTarget.CopyReadBuffer, (IntPtr)offset, size, data);
  35. return data;
  36. }
  37. public static void Resize(BufferHandle handle, int size)
  38. {
  39. GL.BindBuffer(BufferTarget.CopyWriteBuffer, handle.ToInt32());
  40. GL.BufferData(BufferTarget.CopyWriteBuffer, size, IntPtr.Zero, BufferUsageHint.StreamCopy);
  41. }
  42. public static void SetData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  43. {
  44. GL.BindBuffer(BufferTarget.CopyWriteBuffer, buffer.ToInt32());
  45. unsafe
  46. {
  47. fixed (byte* ptr = data)
  48. {
  49. GL.BufferSubData(BufferTarget.CopyWriteBuffer, (IntPtr)offset, data.Length, (IntPtr)ptr);
  50. }
  51. }
  52. }
  53. public static void Delete(BufferHandle buffer)
  54. {
  55. GL.DeleteBuffer(buffer.ToInt32());
  56. }
  57. }
  58. }