OGLRasterizer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 struct VbInfo
  45. {
  46. public int VaoHandle;
  47. public int VboHandle;
  48. public int PrimCount;
  49. }
  50. private struct IbInfo
  51. {
  52. public int IboHandle;
  53. public int Count;
  54. public DrawElementsType Type;
  55. }
  56. private VbInfo[] VertexBuffers;
  57. private IbInfo IndexBuffer;
  58. public OGLRasterizer()
  59. {
  60. VertexBuffers = new VbInfo[32];
  61. IndexBuffer = new IbInfo();
  62. }
  63. public void ClearBuffers(int RtIndex, GalClearBufferFlags Flags)
  64. {
  65. ClearBufferMask Mask = 0;
  66. //OpenGL doesn't support clearing just a single color channel,
  67. //so we can't just clear all channels...
  68. if (Flags.HasFlag(GalClearBufferFlags.ColorRed) &&
  69. Flags.HasFlag(GalClearBufferFlags.ColorGreen) &&
  70. Flags.HasFlag(GalClearBufferFlags.ColorBlue) &&
  71. Flags.HasFlag(GalClearBufferFlags.ColorAlpha))
  72. {
  73. Mask = ClearBufferMask.ColorBufferBit;
  74. }
  75. if (Flags.HasFlag(GalClearBufferFlags.Depth))
  76. {
  77. Mask |= ClearBufferMask.DepthBufferBit;
  78. }
  79. if (Flags.HasFlag(GalClearBufferFlags.Stencil))
  80. {
  81. Mask |= ClearBufferMask.StencilBufferBit;
  82. }
  83. GL.Clear(Mask);
  84. }
  85. public void SetVertexArray(int VbIndex, int Stride, byte[] Buffer, GalVertexAttrib[] Attribs)
  86. {
  87. EnsureVbInitialized(VbIndex);
  88. VertexBuffers[VbIndex].PrimCount = Buffer.Length / Stride;
  89. VbInfo Vb = VertexBuffers[VbIndex];
  90. IntPtr Length = new IntPtr(Buffer.Length);
  91. GL.BindBuffer(BufferTarget.ArrayBuffer, Vb.VboHandle);
  92. GL.BufferData(BufferTarget.ArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  93. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  94. GL.BindVertexArray(Vb.VaoHandle);
  95. for (int Attr = 0; Attr < 16; Attr++)
  96. {
  97. GL.DisableVertexAttribArray(Attr);
  98. }
  99. for (int Index = 0; Index < Attribs.Length; Index++)
  100. {
  101. GalVertexAttrib Attrib = Attribs[Index];
  102. GL.EnableVertexAttribArray(Index);
  103. GL.BindBuffer(BufferTarget.ArrayBuffer, Vb.VboHandle);
  104. bool Unsigned =
  105. Attrib.Type == GalVertexAttribType.Unorm ||
  106. Attrib.Type == GalVertexAttribType.Uint ||
  107. Attrib.Type == GalVertexAttribType.Uscaled;
  108. bool Normalize =
  109. Attrib.Type == GalVertexAttribType.Snorm ||
  110. Attrib.Type == GalVertexAttribType.Unorm;
  111. VertexAttribPointerType Type = 0;
  112. if (Attrib.Type == GalVertexAttribType.Float)
  113. {
  114. Type = VertexAttribPointerType.Float;
  115. }
  116. else
  117. {
  118. Type = AttribTypes[Attrib.Size] + (Unsigned ? 1 : 0);
  119. }
  120. int Size = AttribElements[Attrib.Size];
  121. int Offset = Attrib.Offset;
  122. GL.VertexAttribPointer(Index, Size, Type, Normalize, Stride, Offset);
  123. }
  124. GL.BindVertexArray(0);
  125. }
  126. public void SetIndexArray(byte[] Buffer, GalIndexFormat Format)
  127. {
  128. EnsureIbInitialized();
  129. IndexBuffer.Type = OGLEnumConverter.GetDrawElementsType(Format);
  130. IndexBuffer.Count = Buffer.Length >> (int)Format;
  131. IntPtr Length = new IntPtr(Buffer.Length);
  132. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IndexBuffer.IboHandle);
  133. GL.BufferData(BufferTarget.ElementArrayBuffer, Length, Buffer, BufferUsageHint.StreamDraw);
  134. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  135. }
  136. public void DrawArrays(int VbIndex, GalPrimitiveType PrimType)
  137. {
  138. VbInfo Vb = VertexBuffers[VbIndex];
  139. if (Vb.PrimCount == 0)
  140. {
  141. return;
  142. }
  143. GL.BindVertexArray(Vb.VaoHandle);
  144. GL.DrawArrays(OGLEnumConverter.GetPrimitiveType(PrimType), 0, Vb.PrimCount);
  145. }
  146. public void DrawElements(int VbIndex, int First, GalPrimitiveType PrimType)
  147. {
  148. VbInfo Vb = VertexBuffers[VbIndex];
  149. if (Vb.PrimCount == 0)
  150. {
  151. return;
  152. }
  153. PrimitiveType Mode = OGLEnumConverter.GetPrimitiveType(PrimType);
  154. GL.BindVertexArray(Vb.VaoHandle);
  155. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IndexBuffer.IboHandle);
  156. GL.DrawElements(Mode, IndexBuffer.Count, IndexBuffer.Type, First);
  157. }
  158. private void EnsureVbInitialized(int VbIndex)
  159. {
  160. VbInfo Vb = VertexBuffers[VbIndex];
  161. if (Vb.VaoHandle == 0)
  162. {
  163. Vb.VaoHandle = GL.GenVertexArray();
  164. }
  165. if (Vb.VboHandle == 0)
  166. {
  167. Vb.VboHandle = GL.GenBuffer();
  168. }
  169. VertexBuffers[VbIndex] = Vb;
  170. }
  171. private void EnsureIbInitialized()
  172. {
  173. if (IndexBuffer.IboHandle == 0)
  174. {
  175. IndexBuffer.IboHandle = GL.GenBuffer();
  176. }
  177. }
  178. }
  179. }