OGLRasterizer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using OpenTK.Graphics.OpenGL;
  2. using System;
  3. namespace Ryujinx.Graphics.Gal.OpenGL
  4. {
  5. public 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. float Red, float Green, float Blue, float Alpha,
  37. float Depth,
  38. int Stencil)
  39. {
  40. ClearBufferMask Mask = ClearBufferMask.ColorBufferBit;
  41. GL.ColorMask(
  42. Flags.HasFlag(GalClearBufferFlags.ColorRed),
  43. Flags.HasFlag(GalClearBufferFlags.ColorGreen),
  44. Flags.HasFlag(GalClearBufferFlags.ColorBlue),
  45. Flags.HasFlag(GalClearBufferFlags.ColorAlpha));
  46. if (Flags.HasFlag(GalClearBufferFlags.Depth))
  47. {
  48. Mask |= ClearBufferMask.DepthBufferBit;
  49. }
  50. if (Flags.HasFlag(GalClearBufferFlags.Stencil))
  51. {
  52. Mask |= ClearBufferMask.StencilBufferBit;
  53. }
  54. GL.ClearColor(Red, Green, Blue, Alpha);
  55. GL.ClearDepth(Depth);
  56. GL.ClearStencil(Stencil);
  57. GL.Clear(Mask);
  58. GL.ColorMask(true, true, true, true);
  59. }
  60. public bool IsVboCached(long Key, long DataSize)
  61. {
  62. return VboCache.TryGetSize(Key, out long Size) && Size == DataSize;
  63. }
  64. public bool IsIboCached(long Key, long DataSize)
  65. {
  66. return IboCache.TryGetSize(Key, out long Size) && Size == DataSize;
  67. }
  68. public void CreateVbo(long Key, int DataSize, IntPtr HostAddress)
  69. {
  70. int Handle = GL.GenBuffer();
  71. VboCache.AddOrUpdate(Key, Handle, (uint)DataSize);
  72. IntPtr Length = new IntPtr(DataSize);
  73. GL.BindBuffer(BufferTarget.ArrayBuffer, Handle);
  74. GL.BufferData(BufferTarget.ArrayBuffer, Length, HostAddress, BufferUsageHint.StreamDraw);
  75. }
  76. public void CreateIbo(long Key, int DataSize, IntPtr HostAddress)
  77. {
  78. int Handle = GL.GenBuffer();
  79. IboCache.AddOrUpdate(Key, Handle, (uint)DataSize);
  80. IntPtr Length = new IntPtr(DataSize);
  81. GL.BindBuffer(BufferTarget.ElementArrayBuffer, Handle);
  82. GL.BufferData(BufferTarget.ElementArrayBuffer, Length, HostAddress, BufferUsageHint.StreamDraw);
  83. }
  84. public void SetIndexArray(int Size, GalIndexFormat Format)
  85. {
  86. IndexBuffer.Type = OGLEnumConverter.GetDrawElementsType(Format);
  87. IndexBuffer.Count = Size >> (int)Format;
  88. IndexBuffer.ElemSizeLog2 = (int)Format;
  89. }
  90. public void DrawArrays(int First, int PrimCount, GalPrimitiveType PrimType)
  91. {
  92. if (PrimCount == 0)
  93. {
  94. return;
  95. }
  96. GL.DrawArrays(OGLEnumConverter.GetPrimitiveType(PrimType), First, PrimCount);
  97. }
  98. public void DrawElements(long IboKey, int First, int VertexBase, GalPrimitiveType PrimType)
  99. {
  100. if (!IboCache.TryGetValue(IboKey, out int IboHandle))
  101. {
  102. return;
  103. }
  104. PrimitiveType Mode = OGLEnumConverter.GetPrimitiveType(PrimType);
  105. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IboHandle);
  106. First <<= IndexBuffer.ElemSizeLog2;
  107. if (VertexBase != 0)
  108. {
  109. IntPtr Indices = new IntPtr(First);
  110. GL.DrawElementsBaseVertex(Mode, IndexBuffer.Count, IndexBuffer.Type, Indices, VertexBase);
  111. }
  112. else
  113. {
  114. GL.DrawElements(Mode, IndexBuffer.Count, IndexBuffer.Type, First);
  115. }
  116. }
  117. public bool TryGetVbo(long VboKey, out int VboHandle)
  118. {
  119. return VboCache.TryGetValue(VboKey, out VboHandle);
  120. }
  121. }
  122. }