OGLRasterizer.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using OpenTK.Graphics.OpenGL;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Gal.OpenGL
  5. {
  6. class OGLRasterizer
  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. //OpenGL doesn't support clearing just a single color channel,
  65. //so we can't just clear all channels...
  66. if (Flags.HasFlag(GalClearBufferFlags.ColorRed) &&
  67. Flags.HasFlag(GalClearBufferFlags.ColorGreen) &&
  68. Flags.HasFlag(GalClearBufferFlags.ColorBlue) &&
  69. Flags.HasFlag(GalClearBufferFlags.ColorAlpha))
  70. {
  71. Mask = ClearBufferMask.ColorBufferBit;
  72. }
  73. if (Flags.HasFlag(GalClearBufferFlags.Depth))
  74. {
  75. Mask |= ClearBufferMask.DepthBufferBit;
  76. }
  77. if (Flags.HasFlag(GalClearBufferFlags.Stencil))
  78. {
  79. Mask |= ClearBufferMask.StencilBufferBit;
  80. }
  81. GL.Clear(Mask);
  82. }
  83. public bool IsVboCached(long Tag, long DataSize)
  84. {
  85. return VboCache.TryGetSize(Tag, out long Size) && Size == DataSize;
  86. }
  87. public bool IsIboCached(long Tag, long DataSize)
  88. {
  89. return IboCache.TryGetSize(Tag, out long Size) && Size == DataSize;
  90. }
  91. public void CreateVbo(long Tag, byte[] Buffer)
  92. {
  93. int Handle = GL.GenBuffer();
  94. VboCache.AddOrUpdate(Tag, Handle, (uint)Buffer.Length);
  95. IntPtr Length = new IntPtr(Buffer.Length);
  96. GL.BindBuffer(BufferTarget.ArrayBuffer, Handle);
  97. GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  98. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  99. }
  100. public void CreateIbo(long Tag, byte[] Buffer)
  101. {
  102. int Handle = GL.GenBuffer();
  103. IboCache.AddOrUpdate(Tag, Handle, (uint)Buffer.Length);
  104. IntPtr Length = new IntPtr(Buffer.Length);
  105. GL.BindBuffer(BufferTarget.ElementArrayBuffer, Handle);
  106. GL.BufferData(BufferTarget.ElementArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  107. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  108. }
  109. public void SetVertexArray(int VbIndex, int Stride, long VboTag, GalVertexAttrib[] Attribs)
  110. {
  111. if (!VboCache.TryGetValue(VboTag, out int VboHandle))
  112. {
  113. return;
  114. }
  115. if (VaoHandle == 0)
  116. {
  117. VaoHandle = GL.GenVertexArray();
  118. }
  119. GL.BindVertexArray(VaoHandle);
  120. foreach (GalVertexAttrib Attrib in Attribs)
  121. {
  122. GL.EnableVertexAttribArray(Attrib.Index);
  123. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  124. bool Unsigned =
  125. Attrib.Type == GalVertexAttribType.Unorm ||
  126. Attrib.Type == GalVertexAttribType.Uint ||
  127. Attrib.Type == GalVertexAttribType.Uscaled;
  128. bool Normalize =
  129. Attrib.Type == GalVertexAttribType.Snorm ||
  130. Attrib.Type == GalVertexAttribType.Unorm;
  131. VertexAttribPointerType Type = 0;
  132. if (Attrib.Type == GalVertexAttribType.Float)
  133. {
  134. Type = VertexAttribPointerType.Float;
  135. }
  136. else
  137. {
  138. Type = AttribTypes[Attrib.Size] + (Unsigned ? 1 : 0);
  139. }
  140. int Size = AttribElements[Attrib.Size];
  141. int Offset = Attrib.Offset;
  142. GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Stride, Offset);
  143. }
  144. GL.BindVertexArray(0);
  145. }
  146. public void SetIndexArray(long Tag, int Size, GalIndexFormat Format)
  147. {
  148. IndexBuffer.Type = OGLEnumConverter.GetDrawElementsType(Format);
  149. IndexBuffer.Count = Size >> (int)Format;
  150. }
  151. public void DrawArrays(int First, int PrimCount, GalPrimitiveType PrimType)
  152. {
  153. if (PrimCount == 0)
  154. {
  155. return;
  156. }
  157. GL.BindVertexArray(VaoHandle);
  158. GL.DrawArrays(OGLEnumConverter.GetPrimitiveType(PrimType), First, PrimCount);
  159. }
  160. public void DrawElements(long IboTag, int First, GalPrimitiveType PrimType)
  161. {
  162. if (!IboCache.TryGetValue(IboTag, out int IboHandle))
  163. {
  164. return;
  165. }
  166. PrimitiveType Mode = OGLEnumConverter.GetPrimitiveType(PrimType);
  167. GL.BindVertexArray(VaoHandle);
  168. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IboHandle);
  169. GL.DrawElements(Mode, IndexBuffer.Count, IndexBuffer.Type, First);
  170. }
  171. }
  172. }