OGLRasterizer.cs 7.0 KB

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