OGLRasterizer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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,
  38. float Green,
  39. float Blue,
  40. float Alpha,
  41. float Depth,
  42. int Stencil)
  43. {
  44. GL.ColorMask(
  45. Attachment,
  46. Flags.HasFlag(GalClearBufferFlags.ColorRed),
  47. Flags.HasFlag(GalClearBufferFlags.ColorGreen),
  48. Flags.HasFlag(GalClearBufferFlags.ColorBlue),
  49. Flags.HasFlag(GalClearBufferFlags.ColorAlpha));
  50. GL.ClearBuffer(ClearBuffer.Color, Attachment, new float[] { Red, Green, Blue, Alpha });
  51. GL.ColorMask(Attachment, true, true, true, true);
  52. GL.DepthMask(true);
  53. if (Flags.HasFlag(GalClearBufferFlags.Depth))
  54. {
  55. GL.ClearBuffer(ClearBuffer.Depth, 0, ref Depth);
  56. }
  57. if (Flags.HasFlag(GalClearBufferFlags.Stencil))
  58. {
  59. GL.ClearBuffer(ClearBuffer.Stencil, 0, ref Stencil);
  60. }
  61. }
  62. public bool IsVboCached(long Key, long DataSize)
  63. {
  64. return VboCache.TryGetSize(Key, out long Size) && Size == DataSize;
  65. }
  66. public bool IsIboCached(long Key, long DataSize)
  67. {
  68. return IboCache.TryGetSize(Key, out long Size) && Size == DataSize;
  69. }
  70. public void CreateVbo(long Key, int DataSize, IntPtr HostAddress)
  71. {
  72. int Handle = GL.GenBuffer();
  73. VboCache.AddOrUpdate(Key, Handle, (uint)DataSize);
  74. IntPtr Length = new IntPtr(DataSize);
  75. GL.BindBuffer(BufferTarget.ArrayBuffer, Handle);
  76. GL.BufferData(BufferTarget.ArrayBuffer, Length, HostAddress, BufferUsageHint.StreamDraw);
  77. }
  78. public void CreateIbo(long Key, int DataSize, IntPtr HostAddress)
  79. {
  80. int Handle = GL.GenBuffer();
  81. IboCache.AddOrUpdate(Key, Handle, (uint)DataSize);
  82. IntPtr Length = new IntPtr(DataSize);
  83. GL.BindBuffer(BufferTarget.ElementArrayBuffer, Handle);
  84. GL.BufferData(BufferTarget.ElementArrayBuffer, Length, HostAddress, BufferUsageHint.StreamDraw);
  85. }
  86. public void CreateIbo(long Key, int DataSize, byte[] Buffer)
  87. {
  88. int Handle = GL.GenBuffer();
  89. IboCache.AddOrUpdate(Key, Handle, (uint)DataSize);
  90. IntPtr Length = new IntPtr(Buffer.Length);
  91. GL.BindBuffer(BufferTarget.ElementArrayBuffer, Handle);
  92. GL.BufferData(BufferTarget.ElementArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  93. }
  94. public void SetIndexArray(int Size, GalIndexFormat Format)
  95. {
  96. IndexBuffer.Type = OGLEnumConverter.GetDrawElementsType(Format);
  97. IndexBuffer.Count = Size >> (int)Format;
  98. IndexBuffer.ElemSizeLog2 = (int)Format;
  99. }
  100. public void DrawArrays(int First, int Count, GalPrimitiveType PrimType)
  101. {
  102. if (Count == 0)
  103. {
  104. return;
  105. }
  106. if (PrimType == GalPrimitiveType.Quads)
  107. {
  108. for (int Offset = 0; Offset < Count; Offset += 4)
  109. {
  110. GL.DrawArrays(PrimitiveType.TriangleFan, First + Offset, 4);
  111. }
  112. }
  113. else if (PrimType == GalPrimitiveType.QuadStrip)
  114. {
  115. GL.DrawArrays(PrimitiveType.TriangleFan, First, 4);
  116. for (int Offset = 2; Offset < Count; Offset += 2)
  117. {
  118. GL.DrawArrays(PrimitiveType.TriangleFan, First + Offset, 4);
  119. }
  120. }
  121. else
  122. {
  123. GL.DrawArrays(OGLEnumConverter.GetPrimitiveType(PrimType), First, Count);
  124. }
  125. }
  126. public void DrawElements(long IboKey, int First, int VertexBase, GalPrimitiveType PrimType)
  127. {
  128. if (!IboCache.TryGetValue(IboKey, out int IboHandle))
  129. {
  130. return;
  131. }
  132. PrimitiveType Mode = OGLEnumConverter.GetPrimitiveType(PrimType);
  133. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IboHandle);
  134. First <<= IndexBuffer.ElemSizeLog2;
  135. if (VertexBase != 0)
  136. {
  137. IntPtr Indices = new IntPtr(First);
  138. GL.DrawElementsBaseVertex(Mode, IndexBuffer.Count, IndexBuffer.Type, Indices, VertexBase);
  139. }
  140. else
  141. {
  142. GL.DrawElements(Mode, IndexBuffer.Count, IndexBuffer.Type, First);
  143. }
  144. }
  145. public bool TryGetVbo(long VboKey, out int VboHandle)
  146. {
  147. return VboCache.TryGetValue(VboKey, out VboHandle);
  148. }
  149. }
  150. }