OGLEnumConverter.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 (PixelInternalFormat, PixelFormat, PixelType) GetImageFormat(GalImageFormat Format)
  112. {
  113. switch (Format)
  114. {
  115. case GalImageFormat.R32G32B32A32 | GalImageFormat.Sfloat: return (PixelInternalFormat.Rgba32f, PixelFormat.Rgba, PixelType.Float);
  116. case GalImageFormat.R32G32B32A32 | GalImageFormat.Sint: return (PixelInternalFormat.Rgba32i, PixelFormat.RgbaInteger, PixelType.Int);
  117. case GalImageFormat.R32G32B32A32 | GalImageFormat.Uint: return (PixelInternalFormat.Rgba32ui, PixelFormat.RgbaInteger, PixelType.UnsignedInt);
  118. case GalImageFormat.R16G16B16A16 | GalImageFormat.Sfloat: return (PixelInternalFormat.Rgba16f, PixelFormat.Rgba, PixelType.HalfFloat);
  119. case GalImageFormat.R16G16B16A16 | GalImageFormat.Sint: return (PixelInternalFormat.Rgba16i, PixelFormat.RgbaInteger, PixelType.Short);
  120. case GalImageFormat.R16G16B16A16 | GalImageFormat.Uint: return (PixelInternalFormat.Rgba16ui, PixelFormat.RgbaInteger, PixelType.UnsignedShort);
  121. case GalImageFormat.R32G32 | GalImageFormat.Sfloat: return (PixelInternalFormat.Rg32f, PixelFormat.Rg, PixelType.Float);
  122. case GalImageFormat.R32G32 | GalImageFormat.Sint: return (PixelInternalFormat.Rg32i, PixelFormat.RgInteger, PixelType.Int);
  123. case GalImageFormat.R32G32 | GalImageFormat.Uint: return (PixelInternalFormat.Rg32ui, PixelFormat.RgInteger, PixelType.UnsignedInt);
  124. case GalImageFormat.A8B8G8R8 | GalImageFormat.Snorm: return (PixelInternalFormat.Rgba8Snorm, PixelFormat.Rgba, PixelType.Byte);
  125. case GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm: return (PixelInternalFormat.Rgba8, PixelFormat.Rgba, PixelType.UnsignedByte);
  126. case GalImageFormat.A8B8G8R8 | GalImageFormat.Sint: return (PixelInternalFormat.Rgba8i, PixelFormat.RgbaInteger, PixelType.Byte);
  127. case GalImageFormat.A8B8G8R8 | GalImageFormat.Uint: return (PixelInternalFormat.Rgba8ui, PixelFormat.RgbaInteger, PixelType.UnsignedByte);
  128. case GalImageFormat.A8B8G8R8_SRGB: return (PixelInternalFormat.Srgb8Alpha8, PixelFormat.Rgba, PixelType.UnsignedByte);
  129. case GalImageFormat.A4B4G4R4 | GalImageFormat.Unorm: return (PixelInternalFormat.Rgba4, PixelFormat.Rgba, PixelType.UnsignedShort4444Reversed);
  130. case GalImageFormat.A2B10G10R10 | GalImageFormat.Uint: return (PixelInternalFormat.Rgb10A2ui, PixelFormat.RgbaInteger, PixelType.UnsignedInt2101010Reversed);
  131. case GalImageFormat.A2B10G10R10 | GalImageFormat.Unorm: return (PixelInternalFormat.Rgb10A2, PixelFormat.Rgba, PixelType.UnsignedInt2101010Reversed);
  132. case GalImageFormat.R32 | GalImageFormat.Sfloat: return (PixelInternalFormat.R32f, PixelFormat.Red, PixelType.Float);
  133. case GalImageFormat.R32 | GalImageFormat.Sint: return (PixelInternalFormat.R32i, PixelFormat.Red, PixelType.Int);
  134. case GalImageFormat.R32 | GalImageFormat.Uint: return (PixelInternalFormat.R32ui, PixelFormat.Red, PixelType.UnsignedInt);
  135. case GalImageFormat.A1R5G5B5 | GalImageFormat.Unorm: return (PixelInternalFormat.Rgb5A1, PixelFormat.Rgba, PixelType.UnsignedShort1555Reversed);
  136. case GalImageFormat.B5G6R5 | GalImageFormat.Unorm: return (PixelInternalFormat.Rgba, PixelFormat.Rgb, PixelType.UnsignedShort565Reversed);
  137. case GalImageFormat.R16G16 | GalImageFormat.Sfloat: return (PixelInternalFormat.Rg16f, PixelFormat.Rg, PixelType.HalfFloat);
  138. case GalImageFormat.R16G16 | GalImageFormat.Sint: return (PixelInternalFormat.Rg16i, PixelFormat.RgInteger, PixelType.Short);
  139. case GalImageFormat.R16G16 | GalImageFormat.Snorm: return (PixelInternalFormat.Rg16Snorm, PixelFormat.Rg, PixelType.Byte);
  140. case GalImageFormat.R16G16 | GalImageFormat.Unorm: return (PixelInternalFormat.Rg16, PixelFormat.Rg, PixelType.UnsignedShort);
  141. case GalImageFormat.R8G8 | GalImageFormat.Sint: return (PixelInternalFormat.Rg8i, PixelFormat.RgInteger, PixelType.Byte);
  142. case GalImageFormat.R8G8 | GalImageFormat.Snorm: return (PixelInternalFormat.Rg8Snorm, PixelFormat.Rg, PixelType.Byte);
  143. case GalImageFormat.R8G8 | GalImageFormat.Uint: return (PixelInternalFormat.Rg8ui, PixelFormat.RgInteger, PixelType.UnsignedByte);
  144. case GalImageFormat.R8G8 | GalImageFormat.Unorm: return (PixelInternalFormat.Rg8, PixelFormat.Rg, PixelType.UnsignedByte);
  145. case GalImageFormat.R16 | GalImageFormat.Sfloat: return (PixelInternalFormat.R16f, PixelFormat.Red, PixelType.HalfFloat);
  146. case GalImageFormat.R16 | GalImageFormat.Sint: return (PixelInternalFormat.R16i, PixelFormat.RedInteger, PixelType.Short);
  147. case GalImageFormat.R16 | GalImageFormat.Snorm: return (PixelInternalFormat.R16Snorm, PixelFormat.Red, PixelType.Byte);
  148. case GalImageFormat.R16 | GalImageFormat.Uint: return (PixelInternalFormat.R16ui, PixelFormat.RedInteger, PixelType.UnsignedShort);
  149. case GalImageFormat.R16 | GalImageFormat.Unorm: return (PixelInternalFormat.R16, PixelFormat.Red, PixelType.UnsignedShort);
  150. case GalImageFormat.R8 | GalImageFormat.Sint: return (PixelInternalFormat.R8i, PixelFormat.RedInteger, PixelType.Byte);
  151. case GalImageFormat.R8 | GalImageFormat.Snorm: return (PixelInternalFormat.R8Snorm, PixelFormat.Red, PixelType.Byte);
  152. case GalImageFormat.R8 | GalImageFormat.Uint: return (PixelInternalFormat.R8ui, PixelFormat.RedInteger, PixelType.UnsignedByte);
  153. case GalImageFormat.R8 | GalImageFormat.Unorm: return (PixelInternalFormat.R8, PixelFormat.Red, PixelType.UnsignedByte);
  154. case GalImageFormat.B10G11R11 | GalImageFormat.Sfloat: return (PixelInternalFormat.R11fG11fB10f, PixelFormat.Rgb, PixelType.UnsignedInt10F11F11FRev);
  155. case GalImageFormat.D24_S8 | GalImageFormat.Unorm: return (PixelInternalFormat.Depth24Stencil8, PixelFormat.DepthStencil, PixelType.UnsignedInt248);
  156. case GalImageFormat.D32 | GalImageFormat.Sfloat: return (PixelInternalFormat.DepthComponent32f, PixelFormat.DepthComponent, PixelType.Float);
  157. case GalImageFormat.D16 | GalImageFormat.Unorm: return (PixelInternalFormat.DepthComponent16, PixelFormat.DepthComponent, PixelType.UnsignedShort);
  158. case GalImageFormat.D32_S8 | GalImageFormat.Sfloat: return (PixelInternalFormat.Depth32fStencil8, PixelFormat.DepthStencil, PixelType.Float32UnsignedInt248Rev);
  159. }
  160. throw new NotImplementedException($"{Format & GalImageFormat.FormatMask} {Format & GalImageFormat.TypeMask}");
  161. }
  162. public static InternalFormat GetCompressedImageFormat(GalImageFormat Format)
  163. {
  164. switch (Format)
  165. {
  166. case GalImageFormat.BC6H_UF16 | GalImageFormat.Sfloat: return InternalFormat.CompressedRgbBptcUnsignedFloat;
  167. case GalImageFormat.BC6H_SF16 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbBptcSignedFloat;
  168. case GalImageFormat.BC7 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaBptcUnorm;
  169. case GalImageFormat.BC1_RGBA | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaS3tcDxt1Ext;
  170. case GalImageFormat.BC2 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaS3tcDxt3Ext;
  171. case GalImageFormat.BC3 | GalImageFormat.Unorm: return InternalFormat.CompressedRgbaS3tcDxt5Ext;
  172. case GalImageFormat.BC4 | GalImageFormat.Snorm: return InternalFormat.CompressedSignedRedRgtc1;
  173. case GalImageFormat.BC4 | GalImageFormat.Unorm: return InternalFormat.CompressedRedRgtc1;
  174. case GalImageFormat.BC5 | GalImageFormat.Snorm: return InternalFormat.CompressedSignedRgRgtc2;
  175. case GalImageFormat.BC5 | GalImageFormat.Unorm: return InternalFormat.CompressedRgRgtc2;
  176. }
  177. throw new NotImplementedException($"{Format & GalImageFormat.FormatMask} {Format & GalImageFormat.TypeMask}");
  178. }
  179. public static All GetTextureSwizzle(GalTextureSource Source)
  180. {
  181. switch (Source)
  182. {
  183. case GalTextureSource.Zero: return All.Zero;
  184. case GalTextureSource.Red: return All.Red;
  185. case GalTextureSource.Green: return All.Green;
  186. case GalTextureSource.Blue: return All.Blue;
  187. case GalTextureSource.Alpha: return All.Alpha;
  188. case GalTextureSource.OneInt: return All.One;
  189. case GalTextureSource.OneFloat: return All.One;
  190. }
  191. throw new ArgumentException(nameof(Source));
  192. }
  193. public static TextureWrapMode GetTextureWrapMode(GalTextureWrap Wrap)
  194. {
  195. switch (Wrap)
  196. {
  197. case GalTextureWrap.Repeat: return TextureWrapMode.Repeat;
  198. case GalTextureWrap.MirroredRepeat: return TextureWrapMode.MirroredRepeat;
  199. case GalTextureWrap.ClampToEdge: return TextureWrapMode.ClampToEdge;
  200. case GalTextureWrap.ClampToBorder: return TextureWrapMode.ClampToBorder;
  201. case GalTextureWrap.Clamp: return TextureWrapMode.Clamp;
  202. }
  203. if (OGLExtension.HasTextureMirrorClamp())
  204. {
  205. switch (Wrap)
  206. {
  207. case GalTextureWrap.MirrorClampToEdge: return (TextureWrapMode)ExtTextureMirrorClamp.MirrorClampToEdgeExt;
  208. case GalTextureWrap.MirrorClampToBorder: return (TextureWrapMode)ExtTextureMirrorClamp.MirrorClampToBorderExt;
  209. case GalTextureWrap.MirrorClamp: return (TextureWrapMode)ExtTextureMirrorClamp.MirrorClampExt;
  210. }
  211. }
  212. else
  213. {
  214. //Fallback to non-mirrored clamps
  215. switch (Wrap)
  216. {
  217. case GalTextureWrap.MirrorClampToEdge: return TextureWrapMode.ClampToEdge;
  218. case GalTextureWrap.MirrorClampToBorder: return TextureWrapMode.ClampToBorder;
  219. case GalTextureWrap.MirrorClamp: return TextureWrapMode.Clamp;
  220. }
  221. }
  222. throw new ArgumentException(nameof(Wrap));
  223. }
  224. public static TextureMinFilter GetTextureMinFilter(
  225. GalTextureFilter MinFilter,
  226. GalTextureMipFilter MipFilter)
  227. {
  228. //TODO: Mip (needs mipmap support first).
  229. switch (MinFilter)
  230. {
  231. case GalTextureFilter.Nearest: return TextureMinFilter.Nearest;
  232. case GalTextureFilter.Linear: return TextureMinFilter.Linear;
  233. }
  234. throw new ArgumentException(nameof(MinFilter));
  235. }
  236. public static TextureMagFilter GetTextureMagFilter(GalTextureFilter Filter)
  237. {
  238. switch (Filter)
  239. {
  240. case GalTextureFilter.Nearest: return TextureMagFilter.Nearest;
  241. case GalTextureFilter.Linear: return TextureMagFilter.Linear;
  242. }
  243. throw new ArgumentException(nameof(Filter));
  244. }
  245. public static BlendEquationMode GetBlendEquation(GalBlendEquation BlendEquation)
  246. {
  247. switch (BlendEquation)
  248. {
  249. case GalBlendEquation.FuncAdd: return BlendEquationMode.FuncAdd;
  250. case GalBlendEquation.FuncSubtract: return BlendEquationMode.FuncSubtract;
  251. case GalBlendEquation.FuncReverseSubtract: return BlendEquationMode.FuncReverseSubtract;
  252. case GalBlendEquation.Min: return BlendEquationMode.Min;
  253. case GalBlendEquation.Max: return BlendEquationMode.Max;
  254. }
  255. throw new ArgumentException(nameof(BlendEquation));
  256. }
  257. public static BlendingFactor GetBlendFactor(GalBlendFactor BlendFactor)
  258. {
  259. switch (BlendFactor)
  260. {
  261. case GalBlendFactor.Zero: return BlendingFactor.Zero;
  262. case GalBlendFactor.One: return BlendingFactor.One;
  263. case GalBlendFactor.SrcColor: return BlendingFactor.SrcColor;
  264. case GalBlendFactor.OneMinusSrcColor: return BlendingFactor.OneMinusSrcColor;
  265. case GalBlendFactor.DstColor: return BlendingFactor.DstColor;
  266. case GalBlendFactor.OneMinusDstColor: return BlendingFactor.OneMinusDstColor;
  267. case GalBlendFactor.SrcAlpha: return BlendingFactor.SrcAlpha;
  268. case GalBlendFactor.OneMinusSrcAlpha: return BlendingFactor.OneMinusSrcAlpha;
  269. case GalBlendFactor.DstAlpha: return BlendingFactor.DstAlpha;
  270. case GalBlendFactor.OneMinusDstAlpha: return BlendingFactor.OneMinusDstAlpha;
  271. case GalBlendFactor.OneMinusConstantColor: return BlendingFactor.OneMinusConstantColor;
  272. case GalBlendFactor.ConstantAlpha: return BlendingFactor.ConstantAlpha;
  273. case GalBlendFactor.OneMinusConstantAlpha: return BlendingFactor.OneMinusConstantAlpha;
  274. case GalBlendFactor.SrcAlphaSaturate: return BlendingFactor.SrcAlphaSaturate;
  275. case GalBlendFactor.Src1Color: return BlendingFactor.Src1Color;
  276. case GalBlendFactor.OneMinusSrc1Color: return (BlendingFactor)BlendingFactorSrc.OneMinusSrc1Color;
  277. case GalBlendFactor.Src1Alpha: return BlendingFactor.Src1Alpha;
  278. case GalBlendFactor.OneMinusSrc1Alpha: return (BlendingFactor)BlendingFactorSrc.OneMinusSrc1Alpha;
  279. case GalBlendFactor.ConstantColor:
  280. case GalBlendFactor.ConstantColorG80:
  281. return BlendingFactor.ConstantColor;
  282. }
  283. throw new ArgumentException(nameof(BlendFactor));
  284. }
  285. }
  286. }