OGLEnumConverter.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using OpenTK.Graphics.OpenGL;
  2. using System;
  3. namespace Ryujinx.Graphics.Gal.OpenGL
  4. {
  5. static class OGLEnumConverter
  6. {
  7. public static FrontFaceDirection GetFrontFace(GalFrontFace FrontFace)
  8. {
  9. switch (FrontFace)
  10. {
  11. case GalFrontFace.CW: return FrontFaceDirection.Cw;
  12. case GalFrontFace.CCW: return FrontFaceDirection.Ccw;
  13. }
  14. throw new ArgumentException(nameof(FrontFace));
  15. }
  16. public static CullFaceMode GetCullFace(GalCullFace CullFace)
  17. {
  18. switch (CullFace)
  19. {
  20. case GalCullFace.Front: return CullFaceMode.Front;
  21. case GalCullFace.Back: return CullFaceMode.Back;
  22. case GalCullFace.FrontAndBack: return CullFaceMode.FrontAndBack;
  23. }
  24. throw new ArgumentException(nameof(CullFace));
  25. }
  26. public static StencilOp GetStencilOp(GalStencilOp Op)
  27. {
  28. switch (Op)
  29. {
  30. case GalStencilOp.Keep: return StencilOp.Keep;
  31. case GalStencilOp.Zero: return StencilOp.Zero;
  32. case GalStencilOp.Replace: return StencilOp.Replace;
  33. case GalStencilOp.Incr: return StencilOp.Incr;
  34. case GalStencilOp.Decr: return StencilOp.Decr;
  35. case GalStencilOp.Invert: return StencilOp.Invert;
  36. case GalStencilOp.IncrWrap: return StencilOp.IncrWrap;
  37. case GalStencilOp.DecrWrap: return StencilOp.DecrWrap;
  38. }
  39. throw new ArgumentException(nameof(Op));
  40. }
  41. public static DepthFunction GetDepthFunc(GalComparisonOp Func)
  42. {
  43. //Looks like the GPU can take it's own values (described in GalComparisonOp) and OpenGL values alike
  44. if ((int)Func >= (int)DepthFunction.Never &&
  45. (int)Func <= (int)DepthFunction.Always)
  46. {
  47. return (DepthFunction)Func;
  48. }
  49. switch (Func)
  50. {
  51. case GalComparisonOp.Never: return DepthFunction.Never;
  52. case GalComparisonOp.Less: return DepthFunction.Less;
  53. case GalComparisonOp.Equal: return DepthFunction.Equal;
  54. case GalComparisonOp.Lequal: return DepthFunction.Lequal;
  55. case GalComparisonOp.Greater: return DepthFunction.Greater;
  56. case GalComparisonOp.NotEqual: return DepthFunction.Notequal;
  57. case GalComparisonOp.Gequal: return DepthFunction.Gequal;
  58. case GalComparisonOp.Always: return DepthFunction.Always;
  59. }
  60. throw new ArgumentException(nameof(Func));
  61. }
  62. public static StencilFunction GetStencilFunc(GalComparisonOp Func)
  63. {
  64. //OGL comparison values match, it's just an enum cast
  65. return (StencilFunction)GetDepthFunc(Func);
  66. }
  67. public static DrawElementsType GetDrawElementsType(GalIndexFormat Format)
  68. {
  69. switch (Format)
  70. {
  71. case GalIndexFormat.Byte: return DrawElementsType.UnsignedByte;
  72. case GalIndexFormat.Int16: return DrawElementsType.UnsignedShort;
  73. case GalIndexFormat.Int32: return DrawElementsType.UnsignedInt;
  74. }
  75. throw new ArgumentException(nameof(Format));
  76. }
  77. public static PrimitiveType GetPrimitiveType(GalPrimitiveType Type)
  78. {
  79. switch (Type)
  80. {
  81. case GalPrimitiveType.Points: return PrimitiveType.Points;
  82. case GalPrimitiveType.Lines: return PrimitiveType.Lines;
  83. case GalPrimitiveType.LineLoop: return PrimitiveType.LineLoop;
  84. case GalPrimitiveType.LineStrip: return PrimitiveType.LineStrip;
  85. case GalPrimitiveType.Triangles: return PrimitiveType.Triangles;
  86. case GalPrimitiveType.TriangleStrip: return PrimitiveType.TriangleStrip;
  87. case GalPrimitiveType.TriangleFan: return PrimitiveType.TriangleFan;
  88. case GalPrimitiveType.Quads: return PrimitiveType.Quads;
  89. case GalPrimitiveType.QuadStrip: return PrimitiveType.QuadStrip;
  90. case GalPrimitiveType.Polygon: return PrimitiveType.Polygon;
  91. case GalPrimitiveType.LinesAdjacency: return PrimitiveType.LinesAdjacency;
  92. case GalPrimitiveType.LineStripAdjacency: return PrimitiveType.LineStripAdjacency;
  93. case GalPrimitiveType.TrianglesAdjacency: return PrimitiveType.TrianglesAdjacency;
  94. case GalPrimitiveType.TriangleStripAdjacency: return PrimitiveType.TriangleStripAdjacency;
  95. case GalPrimitiveType.Patches: return PrimitiveType.Patches;
  96. }
  97. throw new ArgumentException(nameof(Type));
  98. }
  99. public static ShaderType GetShaderType(GalShaderType Type)
  100. {
  101. switch (Type)
  102. {
  103. case GalShaderType.Vertex: return ShaderType.VertexShader;
  104. case GalShaderType.TessControl: return ShaderType.TessControlShader;
  105. case GalShaderType.TessEvaluation: return ShaderType.TessEvaluationShader;
  106. case GalShaderType.Geometry: return ShaderType.GeometryShader;
  107. case GalShaderType.Fragment: return ShaderType.FragmentShader;
  108. }
  109. throw new ArgumentException(nameof(Type));
  110. }
  111. public static (PixelFormat, PixelType) GetTextureFormat(GalTextureFormat Format)
  112. {
  113. switch (Format)
  114. {
  115. case GalTextureFormat.R32G32B32A32: return (PixelFormat.Rgba, PixelType.Float);
  116. case GalTextureFormat.R16G16B16A16: return (PixelFormat.Rgba, PixelType.HalfFloat);
  117. case GalTextureFormat.A8B8G8R8: return (PixelFormat.Rgba, PixelType.UnsignedByte);
  118. case GalTextureFormat.R32: return (PixelFormat.Red, PixelType.Float);
  119. case GalTextureFormat.A1B5G5R5: return (PixelFormat.Rgba, PixelType.UnsignedShort5551);
  120. case GalTextureFormat.B5G6R5: return (PixelFormat.Rgb, PixelType.UnsignedShort565);
  121. case GalTextureFormat.G8R8: return (PixelFormat.Rg, PixelType.UnsignedByte);
  122. case GalTextureFormat.R16: return (PixelFormat.Red, PixelType.HalfFloat);
  123. case GalTextureFormat.R8: return (PixelFormat.Red, PixelType.UnsignedByte);
  124. case GalTextureFormat.ZF32: return (PixelFormat.DepthComponent, PixelType.Float);
  125. case GalTextureFormat.BF10GF11RF11: return (PixelFormat.Rgb, PixelType.UnsignedInt10F11F11FRev);
  126. case GalTextureFormat.Z24S8: return (PixelFormat.DepthStencil, PixelType.UnsignedInt248);
  127. }
  128. throw new NotImplementedException(Format.ToString());
  129. }
  130. public static InternalFormat GetCompressedTextureFormat(GalTextureFormat Format)
  131. {
  132. switch (Format)
  133. {
  134. case GalTextureFormat.BC6H_UF16: return InternalFormat.CompressedRgbBptcUnsignedFloat;
  135. case GalTextureFormat.BC6H_SF16: return InternalFormat.CompressedRgbBptcSignedFloat;
  136. case GalTextureFormat.BC7U: return InternalFormat.CompressedRgbaBptcUnorm;
  137. case GalTextureFormat.BC1: return InternalFormat.CompressedRgbaS3tcDxt1Ext;
  138. case GalTextureFormat.BC2: return InternalFormat.CompressedRgbaS3tcDxt3Ext;
  139. case GalTextureFormat.BC3: return InternalFormat.CompressedRgbaS3tcDxt5Ext;
  140. case GalTextureFormat.BC4: return InternalFormat.CompressedRedRgtc1;
  141. case GalTextureFormat.BC5: return InternalFormat.CompressedRgRgtc2;
  142. }
  143. throw new NotImplementedException(Format.ToString());
  144. }
  145. public static All GetTextureSwizzle(GalTextureSource Source)
  146. {
  147. switch (Source)
  148. {
  149. case GalTextureSource.Zero: return All.Zero;
  150. case GalTextureSource.Red: return All.Red;
  151. case GalTextureSource.Green: return All.Green;
  152. case GalTextureSource.Blue: return All.Blue;
  153. case GalTextureSource.Alpha: return All.Alpha;
  154. case GalTextureSource.OneInt: return All.One;
  155. case GalTextureSource.OneFloat: return All.One;
  156. }
  157. throw new ArgumentException(nameof(Source));
  158. }
  159. public static TextureWrapMode GetTextureWrapMode(GalTextureWrap Wrap)
  160. {
  161. switch (Wrap)
  162. {
  163. case GalTextureWrap.Repeat: return TextureWrapMode.Repeat;
  164. case GalTextureWrap.MirroredRepeat: return TextureWrapMode.MirroredRepeat;
  165. case GalTextureWrap.ClampToEdge: return TextureWrapMode.ClampToEdge;
  166. case GalTextureWrap.ClampToBorder: return TextureWrapMode.ClampToBorder;
  167. case GalTextureWrap.Clamp: return TextureWrapMode.Clamp;
  168. //TODO: Those needs extensions (and are currently wrong).
  169. case GalTextureWrap.MirrorClampToEdge: return TextureWrapMode.ClampToEdge;
  170. case GalTextureWrap.MirrorClampToBorder: return TextureWrapMode.ClampToBorder;
  171. case GalTextureWrap.MirrorClamp: return TextureWrapMode.Clamp;
  172. }
  173. throw new ArgumentException(nameof(Wrap));
  174. }
  175. public static TextureMinFilter GetTextureMinFilter(
  176. GalTextureFilter MinFilter,
  177. GalTextureMipFilter MipFilter)
  178. {
  179. //TODO: Mip (needs mipmap support first).
  180. switch (MinFilter)
  181. {
  182. case GalTextureFilter.Nearest: return TextureMinFilter.Nearest;
  183. case GalTextureFilter.Linear: return TextureMinFilter.Linear;
  184. }
  185. throw new ArgumentException(nameof(MinFilter));
  186. }
  187. public static TextureMagFilter GetTextureMagFilter(GalTextureFilter Filter)
  188. {
  189. switch (Filter)
  190. {
  191. case GalTextureFilter.Nearest: return TextureMagFilter.Nearest;
  192. case GalTextureFilter.Linear: return TextureMagFilter.Linear;
  193. }
  194. throw new ArgumentException(nameof(Filter));
  195. }
  196. public static BlendEquationMode GetBlendEquation(GalBlendEquation BlendEquation)
  197. {
  198. switch (BlendEquation)
  199. {
  200. case GalBlendEquation.FuncAdd: return BlendEquationMode.FuncAdd;
  201. case GalBlendEquation.FuncSubtract: return BlendEquationMode.FuncSubtract;
  202. case GalBlendEquation.FuncReverseSubtract: return BlendEquationMode.FuncReverseSubtract;
  203. case GalBlendEquation.Min: return BlendEquationMode.Min;
  204. case GalBlendEquation.Max: return BlendEquationMode.Max;
  205. }
  206. throw new ArgumentException(nameof(BlendEquation));
  207. }
  208. public static BlendingFactor GetBlendFactor(GalBlendFactor BlendFactor)
  209. {
  210. switch (BlendFactor)
  211. {
  212. case GalBlendFactor.Zero: return BlendingFactor.Zero;
  213. case GalBlendFactor.One: return BlendingFactor.One;
  214. case GalBlendFactor.SrcColor: return BlendingFactor.SrcColor;
  215. case GalBlendFactor.OneMinusSrcColor: return BlendingFactor.OneMinusSrcColor;
  216. case GalBlendFactor.DstColor: return BlendingFactor.DstColor;
  217. case GalBlendFactor.OneMinusDstColor: return BlendingFactor.OneMinusDstColor;
  218. case GalBlendFactor.SrcAlpha: return BlendingFactor.SrcAlpha;
  219. case GalBlendFactor.OneMinusSrcAlpha: return BlendingFactor.OneMinusSrcAlpha;
  220. case GalBlendFactor.DstAlpha: return BlendingFactor.DstAlpha;
  221. case GalBlendFactor.OneMinusDstAlpha: return BlendingFactor.OneMinusDstAlpha;
  222. case GalBlendFactor.OneMinusConstantColor: return BlendingFactor.OneMinusConstantColor;
  223. case GalBlendFactor.ConstantAlpha: return BlendingFactor.ConstantAlpha;
  224. case GalBlendFactor.OneMinusConstantAlpha: return BlendingFactor.OneMinusConstantAlpha;
  225. case GalBlendFactor.SrcAlphaSaturate: return BlendingFactor.SrcAlphaSaturate;
  226. case GalBlendFactor.Src1Color: return BlendingFactor.Src1Color;
  227. case GalBlendFactor.OneMinusSrc1Color: return (BlendingFactor)BlendingFactorSrc.OneMinusSrc1Color;
  228. case GalBlendFactor.Src1Alpha: return BlendingFactor.Src1Alpha;
  229. case GalBlendFactor.OneMinusSrc1Alpha: return (BlendingFactor)BlendingFactorSrc.OneMinusSrc1Alpha;
  230. case GalBlendFactor.ConstantColor:
  231. case GalBlendFactor.ConstantColorG80:
  232. return BlendingFactor.ConstantColor;
  233. }
  234. throw new ArgumentException(nameof(BlendFactor));
  235. }
  236. }
  237. }