OGLRasterizer.cs 4.6 KB

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