Buffer.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 byte[] GetData(BufferHandle buffer, int offset, int size)
  48. {
  49. GL.BindBuffer(BufferTarget.CopyReadBuffer, buffer.ToInt32());
  50. byte[] data = new byte[size];
  51. GL.GetBufferSubData(BufferTarget.CopyReadBuffer, (IntPtr)offset, size, data);
  52. return data;
  53. }
  54. public static void Resize(BufferHandle handle, int size)
  55. {
  56. GL.BindBuffer(BufferTarget.CopyWriteBuffer, handle.ToInt32());
  57. GL.BufferData(BufferTarget.CopyWriteBuffer, size, IntPtr.Zero, BufferUsageHint.StreamCopy);
  58. }
  59. public static void SetData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  60. {
  61. GL.BindBuffer(BufferTarget.CopyWriteBuffer, buffer.ToInt32());
  62. unsafe
  63. {
  64. fixed (byte* ptr = data)
  65. {
  66. GL.BufferSubData(BufferTarget.CopyWriteBuffer, (IntPtr)offset, data.Length, (IntPtr)ptr);
  67. }
  68. }
  69. }
  70. public static void Delete(BufferHandle buffer)
  71. {
  72. GL.DeleteBuffer(buffer.ToInt32());
  73. }
  74. }
  75. }