OGLRasterizer.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 static Dictionary<GalVertexAttribSize, int> AttribElements =
  9. new Dictionary<GalVertexAttribSize, int>()
  10. {
  11. { GalVertexAttribSize._32_32_32_32, 4 },
  12. { GalVertexAttribSize._32_32_32, 3 },
  13. { GalVertexAttribSize._16_16_16_16, 4 },
  14. { GalVertexAttribSize._32_32, 2 },
  15. { GalVertexAttribSize._16_16_16, 3 },
  16. { GalVertexAttribSize._8_8_8_8, 4 },
  17. { GalVertexAttribSize._16_16, 2 },
  18. { GalVertexAttribSize._32, 1 },
  19. { GalVertexAttribSize._8_8_8, 3 },
  20. { GalVertexAttribSize._8_8, 2 },
  21. { GalVertexAttribSize._16, 1 },
  22. { GalVertexAttribSize._8, 1 },
  23. { GalVertexAttribSize._10_10_10_2, 4 },
  24. { GalVertexAttribSize._11_11_10, 3 }
  25. };
  26. private static Dictionary<GalVertexAttribSize, VertexAttribPointerType> AttribTypes =
  27. new Dictionary<GalVertexAttribSize, VertexAttribPointerType>()
  28. {
  29. { GalVertexAttribSize._32_32_32_32, VertexAttribPointerType.Int },
  30. { GalVertexAttribSize._32_32_32, VertexAttribPointerType.Int },
  31. { GalVertexAttribSize._16_16_16_16, VertexAttribPointerType.Short },
  32. { GalVertexAttribSize._32_32, VertexAttribPointerType.Int },
  33. { GalVertexAttribSize._16_16_16, VertexAttribPointerType.Short },
  34. { GalVertexAttribSize._8_8_8_8, VertexAttribPointerType.Byte },
  35. { GalVertexAttribSize._16_16, VertexAttribPointerType.Short },
  36. { GalVertexAttribSize._32, VertexAttribPointerType.Int },
  37. { GalVertexAttribSize._8_8_8, VertexAttribPointerType.Byte },
  38. { GalVertexAttribSize._8_8, VertexAttribPointerType.Byte },
  39. { GalVertexAttribSize._16, VertexAttribPointerType.Short },
  40. { GalVertexAttribSize._8, VertexAttribPointerType.Byte },
  41. { GalVertexAttribSize._10_10_10_2, VertexAttribPointerType.Int }, //?
  42. { GalVertexAttribSize._11_11_10, VertexAttribPointerType.Int } //?
  43. };
  44. private int VaoHandle;
  45. private int[] VertexBuffers;
  46. private OGLCachedResource<int> VboCache;
  47. private OGLCachedResource<int> IboCache;
  48. private struct IbInfo
  49. {
  50. public int Count;
  51. public DrawElementsType Type;
  52. }
  53. private IbInfo IndexBuffer;
  54. public OGLRasterizer()
  55. {
  56. VertexBuffers = new int[32];
  57. VboCache = new OGLCachedResource<int>(GL.DeleteBuffer);
  58. IboCache = new OGLCachedResource<int>(GL.DeleteBuffer);
  59. IndexBuffer = new IbInfo();
  60. }
  61. public void ClearBuffers(int RtIndex, GalClearBufferFlags Flags)
  62. {
  63. ClearBufferMask Mask = 0;
  64. //TODO: Use glColorMask to clear just the specified channels.
  65. if (Flags.HasFlag(GalClearBufferFlags.ColorRed) &&
  66. Flags.HasFlag(GalClearBufferFlags.ColorGreen) &&
  67. Flags.HasFlag(GalClearBufferFlags.ColorBlue) &&
  68. Flags.HasFlag(GalClearBufferFlags.ColorAlpha))
  69. {
  70. Mask = ClearBufferMask.ColorBufferBit;
  71. }
  72. if (Flags.HasFlag(GalClearBufferFlags.Depth))
  73. {
  74. Mask |= ClearBufferMask.DepthBufferBit;
  75. }
  76. if (Flags.HasFlag(GalClearBufferFlags.Stencil))
  77. {
  78. Mask |= ClearBufferMask.StencilBufferBit;
  79. }
  80. GL.Clear(Mask);
  81. }
  82. public bool IsVboCached(long Key, long DataSize)
  83. {
  84. return VboCache.TryGetSize(Key, out long Size) && Size == DataSize;
  85. }
  86. public bool IsIboCached(long Key, long DataSize)
  87. {
  88. return IboCache.TryGetSize(Key, out long Size) && Size == DataSize;
  89. }
  90. public void CreateVbo(long Key, byte[] Buffer)
  91. {
  92. int Handle = GL.GenBuffer();
  93. VboCache.AddOrUpdate(Key, Handle, (uint)Buffer.Length);
  94. IntPtr Length = new IntPtr(Buffer.Length);
  95. GL.BindBuffer(BufferTarget.ArrayBuffer, Handle);
  96. GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  97. }
  98. public void CreateIbo(long Key, byte[] Buffer)
  99. {
  100. int Handle = GL.GenBuffer();
  101. IboCache.AddOrUpdate(Key, Handle, (uint)Buffer.Length);
  102. IntPtr Length = new IntPtr(Buffer.Length);
  103. GL.BindBuffer(BufferTarget.ElementArrayBuffer, Handle);
  104. GL.BufferData(BufferTarget.ElementArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  105. }
  106. public void SetVertexArray(int VbIndex, int Stride, long VboKey, GalVertexAttrib[] Attribs)
  107. {
  108. if (!VboCache.TryGetValue(VboKey, out int VboHandle))
  109. {
  110. return;
  111. }
  112. if (VaoHandle == 0)
  113. {
  114. VaoHandle = GL.GenVertexArray();
  115. }
  116. GL.BindVertexArray(VaoHandle);
  117. foreach (GalVertexAttrib Attrib in Attribs)
  118. {
  119. GL.EnableVertexAttribArray(Attrib.Index);
  120. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  121. bool Unsigned =
  122. Attrib.Type == GalVertexAttribType.Unorm ||
  123. Attrib.Type == GalVertexAttribType.Uint ||
  124. Attrib.Type == GalVertexAttribType.Uscaled;
  125. bool Normalize =
  126. Attrib.Type == GalVertexAttribType.Snorm ||
  127. Attrib.Type == GalVertexAttribType.Unorm;
  128. VertexAttribPointerType Type = 0;
  129. if (Attrib.Type == GalVertexAttribType.Float)
  130. {
  131. Type = VertexAttribPointerType.Float;
  132. }
  133. else
  134. {
  135. Type = AttribTypes[Attrib.Size] + (Unsigned ? 1 : 0);
  136. }
  137. int Size = AttribElements[Attrib.Size];
  138. int Offset = Attrib.Offset;
  139. GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Stride, Offset);
  140. }
  141. }
  142. public void SetIndexArray(long Key, int Size, GalIndexFormat Format)
  143. {
  144. IndexBuffer.Type = OGLEnumConverter.GetDrawElementsType(Format);
  145. IndexBuffer.Count = Size >> (int)Format;
  146. }
  147. public void DrawArrays(int First, int PrimCount, GalPrimitiveType PrimType)
  148. {
  149. if (PrimCount == 0)
  150. {
  151. return;
  152. }
  153. GL.BindVertexArray(VaoHandle);
  154. GL.DrawArrays(OGLEnumConverter.GetPrimitiveType(PrimType), First, PrimCount);
  155. }
  156. public void DrawElements(long IboKey, int First, GalPrimitiveType PrimType)
  157. {
  158. if (!IboCache.TryGetValue(IboKey, out int IboHandle))
  159. {
  160. return;
  161. }
  162. PrimitiveType Mode = OGLEnumConverter.GetPrimitiveType(PrimType);
  163. GL.BindVertexArray(VaoHandle);
  164. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IboHandle);
  165. GL.DrawElements(Mode, IndexBuffer.Count, IndexBuffer.Type, First);
  166. }
  167. }
  168. }