OGLRasterizer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using OpenTK.Graphics.OpenGL;
  2. using System;
  3. namespace Ryujinx.Graphics.Gal.OpenGL
  4. {
  5. class OGLRasterizer : IGalRasterizer
  6. {
  7. private int[] VertexBuffers;
  8. private OGLCachedResource<int> VboCache;
  9. private OGLCachedResource<int> IboCache;
  10. private struct IbInfo
  11. {
  12. public int Count;
  13. public int ElemSizeLog2;
  14. public DrawElementsType Type;
  15. }
  16. private IbInfo IndexBuffer;
  17. public OGLRasterizer()
  18. {
  19. VertexBuffers = new int[32];
  20. VboCache = new OGLCachedResource<int>(GL.DeleteBuffer);
  21. IboCache = new OGLCachedResource<int>(GL.DeleteBuffer);
  22. IndexBuffer = new IbInfo();
  23. }
  24. public void LockCaches()
  25. {
  26. VboCache.Lock();
  27. IboCache.Lock();
  28. }
  29. public void UnlockCaches()
  30. {
  31. VboCache.Unlock();
  32. IboCache.Unlock();
  33. }
  34. public void ClearBuffers(
  35. GalClearBufferFlags Flags,
  36. int Attachment,
  37. float Red, float Green, float Blue, float Alpha,
  38. float Depth,
  39. int Stencil)
  40. {
  41. GL.ColorMask(
  42. Flags.HasFlag(GalClearBufferFlags.ColorRed),
  43. Flags.HasFlag(GalClearBufferFlags.ColorGreen),
  44. Flags.HasFlag(GalClearBufferFlags.ColorBlue),
  45. Flags.HasFlag(GalClearBufferFlags.ColorAlpha));
  46. GL.ClearBuffer(ClearBuffer.Color, Attachment, new float[] { Red, Green, Blue, Alpha });
  47. if (Flags.HasFlag(GalClearBufferFlags.Depth))
  48. {
  49. GL.ClearBuffer(ClearBuffer.Depth, 0, ref Depth);
  50. }
  51. if (Flags.HasFlag(GalClearBufferFlags.Stencil))
  52. {
  53. GL.ClearBuffer(ClearBuffer.Stencil, 0, ref Stencil);
  54. }
  55. GL.ColorMask(true, true, true, true);
  56. }
  57. public bool IsVboCached(long Key, long DataSize)
  58. {
  59. return VboCache.TryGetSize(Key, out long Size) && Size == DataSize;
  60. }
  61. public bool IsIboCached(long Key, long DataSize)
  62. {
  63. return IboCache.TryGetSize(Key, out long Size) && Size == DataSize;
  64. }
  65. public void CreateVbo(long Key, int DataSize, IntPtr HostAddress)
  66. {
  67. int Handle = GL.GenBuffer();
  68. VboCache.AddOrUpdate(Key, Handle, (uint)DataSize);
  69. IntPtr Length = new IntPtr(DataSize);
  70. GL.BindBuffer(BufferTarget.ArrayBuffer, Handle);
  71. GL.BufferData(BufferTarget.ArrayBuffer, Length, HostAddress, BufferUsageHint.StreamDraw);
  72. }
  73. public void CreateIbo(long Key, int DataSize, IntPtr HostAddress)
  74. {
  75. int Handle = GL.GenBuffer();
  76. IboCache.AddOrUpdate(Key, Handle, (uint)DataSize);
  77. IntPtr Length = new IntPtr(DataSize);
  78. GL.BindBuffer(BufferTarget.ElementArrayBuffer, Handle);
  79. GL.BufferData(BufferTarget.ElementArrayBuffer, Length, HostAddress, BufferUsageHint.StreamDraw);
  80. }
  81. public void SetIndexArray(int Size, GalIndexFormat Format)
  82. {
  83. IndexBuffer.Type = OGLEnumConverter.GetDrawElementsType(Format);
  84. IndexBuffer.Count = Size >> (int)Format;
  85. IndexBuffer.ElemSizeLog2 = (int)Format;
  86. }
  87. public void DrawArrays(int First, int Count, GalPrimitiveType PrimType)
  88. {
  89. if (Count == 0)
  90. {
  91. return;
  92. }
  93. GL.DrawArrays(OGLEnumConverter.GetPrimitiveType(PrimType), First, Count);
  94. }
  95. public void DrawElements(long IboKey, int First, int VertexBase, GalPrimitiveType PrimType)
  96. {
  97. if (!IboCache.TryGetValue(IboKey, out int IboHandle))
  98. {
  99. return;
  100. }
  101. PrimitiveType Mode = OGLEnumConverter.GetPrimitiveType(PrimType);
  102. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IboHandle);
  103. First <<= IndexBuffer.ElemSizeLog2;
  104. if (VertexBase != 0)
  105. {
  106. IntPtr Indices = new IntPtr(First);
  107. GL.DrawElementsBaseVertex(Mode, IndexBuffer.Count, IndexBuffer.Type, Indices, VertexBase);
  108. }
  109. else
  110. {
  111. GL.DrawElements(Mode, IndexBuffer.Count, IndexBuffer.Type, First);
  112. }
  113. }
  114. public bool TryGetVbo(long VboKey, out int VboHandle)
  115. {
  116. return VboCache.TryGetValue(VboKey, out VboHandle);
  117. }
  118. }
  119. }