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. }