OGLRasterizer.cs 6.2 KB

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