EnumConversion.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Shader;
  4. using Silk.NET.Vulkan;
  5. using System;
  6. namespace Ryujinx.Graphics.Vulkan
  7. {
  8. static class EnumConversion
  9. {
  10. public static ShaderStageFlags Convert(this ShaderStage stage)
  11. {
  12. return stage switch
  13. {
  14. ShaderStage.Vertex => ShaderStageFlags.ShaderStageVertexBit,
  15. ShaderStage.Geometry => ShaderStageFlags.ShaderStageGeometryBit,
  16. ShaderStage.TessellationControl => ShaderStageFlags.ShaderStageTessellationControlBit,
  17. ShaderStage.TessellationEvaluation => ShaderStageFlags.ShaderStageTessellationEvaluationBit,
  18. ShaderStage.Fragment => ShaderStageFlags.ShaderStageFragmentBit,
  19. ShaderStage.Compute => ShaderStageFlags.ShaderStageComputeBit,
  20. _ => LogInvalidAndReturn(stage, nameof(ShaderStage), (ShaderStageFlags)0)
  21. };
  22. }
  23. public static PipelineStageFlags ConvertToPipelineStageFlags(this ShaderStage stage)
  24. {
  25. return stage switch
  26. {
  27. ShaderStage.Vertex => PipelineStageFlags.PipelineStageVertexShaderBit,
  28. ShaderStage.Geometry => PipelineStageFlags.PipelineStageGeometryShaderBit,
  29. ShaderStage.TessellationControl => PipelineStageFlags.PipelineStageTessellationControlShaderBit,
  30. ShaderStage.TessellationEvaluation => PipelineStageFlags.PipelineStageTessellationEvaluationShaderBit,
  31. ShaderStage.Fragment => PipelineStageFlags.PipelineStageFragmentShaderBit,
  32. ShaderStage.Compute => PipelineStageFlags.PipelineStageComputeShaderBit,
  33. _ => LogInvalidAndReturn(stage, nameof(ShaderStage), (PipelineStageFlags)0)
  34. };
  35. }
  36. public static SamplerAddressMode Convert(this AddressMode mode)
  37. {
  38. return mode switch
  39. {
  40. AddressMode.Clamp => SamplerAddressMode.ClampToEdge, // TODO: Should be clamp.
  41. AddressMode.Repeat => SamplerAddressMode.Repeat,
  42. AddressMode.MirrorClamp => SamplerAddressMode.ClampToEdge, // TODO: Should be mirror clamp.
  43. AddressMode.MirrorClampToEdge => SamplerAddressMode.MirrorClampToEdgeKhr,
  44. AddressMode.MirrorClampToBorder => SamplerAddressMode.ClampToBorder, // TODO: Should be mirror clamp to border.
  45. AddressMode.ClampToBorder => SamplerAddressMode.ClampToBorder,
  46. AddressMode.MirroredRepeat => SamplerAddressMode.MirroredRepeat,
  47. AddressMode.ClampToEdge => SamplerAddressMode.ClampToEdge,
  48. _ => LogInvalidAndReturn(mode, nameof(AddressMode), SamplerAddressMode.ClampToEdge) // TODO: Should be clamp.
  49. };
  50. }
  51. public static Silk.NET.Vulkan.BlendFactor Convert(this GAL.BlendFactor factor)
  52. {
  53. return factor switch
  54. {
  55. GAL.BlendFactor.Zero or GAL.BlendFactor.ZeroGl => Silk.NET.Vulkan.BlendFactor.Zero,
  56. GAL.BlendFactor.One or GAL.BlendFactor.OneGl => Silk.NET.Vulkan.BlendFactor.One,
  57. GAL.BlendFactor.SrcColor or GAL.BlendFactor.SrcColorGl => Silk.NET.Vulkan.BlendFactor.SrcColor,
  58. GAL.BlendFactor.OneMinusSrcColor or GAL.BlendFactor.OneMinusSrcColorGl => Silk.NET.Vulkan.BlendFactor.OneMinusSrcColor,
  59. GAL.BlendFactor.SrcAlpha or GAL.BlendFactor.SrcAlphaGl => Silk.NET.Vulkan.BlendFactor.SrcAlpha,
  60. GAL.BlendFactor.OneMinusSrcAlpha or GAL.BlendFactor.OneMinusSrcAlphaGl => Silk.NET.Vulkan.BlendFactor.OneMinusSrcAlpha,
  61. GAL.BlendFactor.DstAlpha or GAL.BlendFactor.DstAlphaGl => Silk.NET.Vulkan.BlendFactor.DstAlpha,
  62. GAL.BlendFactor.OneMinusDstAlpha or GAL.BlendFactor.OneMinusDstAlphaGl => Silk.NET.Vulkan.BlendFactor.OneMinusDstAlpha,
  63. GAL.BlendFactor.DstColor or GAL.BlendFactor.DstColorGl => Silk.NET.Vulkan.BlendFactor.DstColor,
  64. GAL.BlendFactor.OneMinusDstColor or GAL.BlendFactor.OneMinusDstColorGl => Silk.NET.Vulkan.BlendFactor.OneMinusDstColor,
  65. GAL.BlendFactor.SrcAlphaSaturate or GAL.BlendFactor.SrcAlphaSaturateGl => Silk.NET.Vulkan.BlendFactor.SrcAlphaSaturate,
  66. GAL.BlendFactor.Src1Color or GAL.BlendFactor.Src1ColorGl => Silk.NET.Vulkan.BlendFactor.Src1Color,
  67. GAL.BlendFactor.OneMinusSrc1Color or GAL.BlendFactor.OneMinusSrc1ColorGl => Silk.NET.Vulkan.BlendFactor.OneMinusSrc1Color,
  68. GAL.BlendFactor.Src1Alpha or GAL.BlendFactor.Src1AlphaGl => Silk.NET.Vulkan.BlendFactor.Src1Alpha,
  69. GAL.BlendFactor.OneMinusSrc1Alpha or GAL.BlendFactor.OneMinusSrc1AlphaGl => Silk.NET.Vulkan.BlendFactor.OneMinusSrc1Alpha,
  70. GAL.BlendFactor.ConstantColor => Silk.NET.Vulkan.BlendFactor.ConstantColor,
  71. GAL.BlendFactor.OneMinusConstantColor => Silk.NET.Vulkan.BlendFactor.OneMinusConstantColor,
  72. GAL.BlendFactor.ConstantAlpha => Silk.NET.Vulkan.BlendFactor.ConstantAlpha,
  73. GAL.BlendFactor.OneMinusConstantAlpha => Silk.NET.Vulkan.BlendFactor.OneMinusConstantAlpha,
  74. _ => LogInvalidAndReturn(factor, nameof(GAL.BlendFactor), Silk.NET.Vulkan.BlendFactor.Zero)
  75. };
  76. }
  77. public static Silk.NET.Vulkan.BlendOp Convert(this GAL.BlendOp op)
  78. {
  79. return op switch
  80. {
  81. GAL.BlendOp.Add or GAL.BlendOp.AddGl => Silk.NET.Vulkan.BlendOp.Add,
  82. GAL.BlendOp.Subtract or GAL.BlendOp.SubtractGl => Silk.NET.Vulkan.BlendOp.Subtract,
  83. GAL.BlendOp.ReverseSubtract or GAL.BlendOp.ReverseSubtractGl => Silk.NET.Vulkan.BlendOp.ReverseSubtract,
  84. GAL.BlendOp.Minimum or GAL.BlendOp.MinimumGl => Silk.NET.Vulkan.BlendOp.Min,
  85. GAL.BlendOp.Maximum or GAL.BlendOp.MaximumGl => Silk.NET.Vulkan.BlendOp.Max,
  86. _ => LogInvalidAndReturn(op, nameof(GAL.BlendOp), Silk.NET.Vulkan.BlendOp.Add)
  87. };
  88. }
  89. public static Silk.NET.Vulkan.CompareOp Convert(this GAL.CompareOp op)
  90. {
  91. return op switch
  92. {
  93. GAL.CompareOp.Never or GAL.CompareOp.NeverGl => Silk.NET.Vulkan.CompareOp.Never,
  94. GAL.CompareOp.Less or GAL.CompareOp.LessGl => Silk.NET.Vulkan.CompareOp.Less,
  95. GAL.CompareOp.Equal or GAL.CompareOp.EqualGl => Silk.NET.Vulkan.CompareOp.Equal,
  96. GAL.CompareOp.LessOrEqual or GAL.CompareOp.LessOrEqualGl => Silk.NET.Vulkan.CompareOp.LessOrEqual,
  97. GAL.CompareOp.Greater or GAL.CompareOp.GreaterGl => Silk.NET.Vulkan.CompareOp.Greater,
  98. GAL.CompareOp.NotEqual or GAL.CompareOp.NotEqualGl => Silk.NET.Vulkan.CompareOp.NotEqual,
  99. GAL.CompareOp.GreaterOrEqual or GAL.CompareOp.GreaterOrEqualGl => Silk.NET.Vulkan.CompareOp.GreaterOrEqual,
  100. GAL.CompareOp.Always or GAL.CompareOp.AlwaysGl => Silk.NET.Vulkan.CompareOp.Always,
  101. _ => LogInvalidAndReturn(op, nameof(GAL.CompareOp), Silk.NET.Vulkan.CompareOp.Never)
  102. };
  103. }
  104. public static CullModeFlags Convert(this Face face)
  105. {
  106. return face switch
  107. {
  108. Face.Back => CullModeFlags.CullModeBackBit,
  109. Face.Front => CullModeFlags.CullModeFrontBit,
  110. Face.FrontAndBack => CullModeFlags.CullModeFrontAndBack,
  111. _ => LogInvalidAndReturn(face, nameof(Face), CullModeFlags.CullModeBackBit)
  112. };
  113. }
  114. public static Silk.NET.Vulkan.FrontFace Convert(this GAL.FrontFace frontFace)
  115. {
  116. // Flipped to account for origin differences.
  117. return frontFace switch
  118. {
  119. GAL.FrontFace.Clockwise => Silk.NET.Vulkan.FrontFace.CounterClockwise,
  120. GAL.FrontFace.CounterClockwise => Silk.NET.Vulkan.FrontFace.Clockwise,
  121. _ => LogInvalidAndReturn(frontFace, nameof(GAL.FrontFace), Silk.NET.Vulkan.FrontFace.Clockwise)
  122. };
  123. }
  124. public static Silk.NET.Vulkan.IndexType Convert(this GAL.IndexType type)
  125. {
  126. return type switch
  127. {
  128. GAL.IndexType.UByte => Silk.NET.Vulkan.IndexType.Uint8Ext,
  129. GAL.IndexType.UShort => Silk.NET.Vulkan.IndexType.Uint16,
  130. GAL.IndexType.UInt => Silk.NET.Vulkan.IndexType.Uint32,
  131. _ => LogInvalidAndReturn(type, nameof(GAL.IndexType), Silk.NET.Vulkan.IndexType.Uint16)
  132. };
  133. }
  134. public static Filter Convert(this MagFilter filter)
  135. {
  136. return filter switch
  137. {
  138. MagFilter.Nearest => Filter.Nearest,
  139. MagFilter.Linear => Filter.Linear,
  140. _ => LogInvalidAndReturn(filter, nameof(MagFilter), Filter.Nearest)
  141. };
  142. }
  143. public static (Filter, SamplerMipmapMode) Convert(this MinFilter filter)
  144. {
  145. return filter switch
  146. {
  147. MinFilter.Nearest => (Filter.Nearest, SamplerMipmapMode.Nearest),
  148. MinFilter.Linear => (Filter.Linear, SamplerMipmapMode.Nearest),
  149. MinFilter.NearestMipmapNearest => (Filter.Nearest, SamplerMipmapMode.Nearest),
  150. MinFilter.LinearMipmapNearest => (Filter.Linear, SamplerMipmapMode.Nearest),
  151. MinFilter.NearestMipmapLinear => (Filter.Nearest, SamplerMipmapMode.Linear),
  152. MinFilter.LinearMipmapLinear => (Filter.Linear, SamplerMipmapMode.Linear),
  153. _ => LogInvalidAndReturn(filter, nameof(MinFilter), (Filter.Nearest, SamplerMipmapMode.Nearest))
  154. };
  155. }
  156. public static Silk.NET.Vulkan.PrimitiveTopology Convert(this GAL.PrimitiveTopology topology)
  157. {
  158. return topology switch
  159. {
  160. GAL.PrimitiveTopology.Points => Silk.NET.Vulkan.PrimitiveTopology.PointList,
  161. GAL.PrimitiveTopology.Lines => Silk.NET.Vulkan.PrimitiveTopology.LineList,
  162. GAL.PrimitiveTopology.LineStrip => Silk.NET.Vulkan.PrimitiveTopology.LineStrip,
  163. GAL.PrimitiveTopology.Triangles => Silk.NET.Vulkan.PrimitiveTopology.TriangleList,
  164. GAL.PrimitiveTopology.TriangleStrip => Silk.NET.Vulkan.PrimitiveTopology.TriangleStrip,
  165. GAL.PrimitiveTopology.TriangleFan => Silk.NET.Vulkan.PrimitiveTopology.TriangleFan,
  166. GAL.PrimitiveTopology.LinesAdjacency => Silk.NET.Vulkan.PrimitiveTopology.LineListWithAdjacency,
  167. GAL.PrimitiveTopology.LineStripAdjacency => Silk.NET.Vulkan.PrimitiveTopology.LineStripWithAdjacency,
  168. GAL.PrimitiveTopology.TrianglesAdjacency => Silk.NET.Vulkan.PrimitiveTopology.TriangleListWithAdjacency,
  169. GAL.PrimitiveTopology.TriangleStripAdjacency => Silk.NET.Vulkan.PrimitiveTopology.TriangleStripWithAdjacency,
  170. GAL.PrimitiveTopology.Patches => Silk.NET.Vulkan.PrimitiveTopology.PatchList,
  171. GAL.PrimitiveTopology.Quads => throw new NotSupportedException("Quad topology is not available in Vulkan."),
  172. GAL.PrimitiveTopology.QuadStrip => throw new NotSupportedException("QuadStrip topology is not available in Vulkan."),
  173. _ => LogInvalidAndReturn(topology, nameof(GAL.PrimitiveTopology), Silk.NET.Vulkan.PrimitiveTopology.TriangleList)
  174. };
  175. }
  176. public static Silk.NET.Vulkan.StencilOp Convert(this GAL.StencilOp op)
  177. {
  178. return op switch
  179. {
  180. GAL.StencilOp.Keep or GAL.StencilOp.KeepGl => Silk.NET.Vulkan.StencilOp.Keep,
  181. GAL.StencilOp.Zero or GAL.StencilOp.ZeroGl => Silk.NET.Vulkan.StencilOp.Zero,
  182. GAL.StencilOp.Replace or GAL.StencilOp.ReplaceGl => Silk.NET.Vulkan.StencilOp.Replace,
  183. GAL.StencilOp.IncrementAndClamp or GAL.StencilOp.IncrementAndClampGl => Silk.NET.Vulkan.StencilOp.IncrementAndClamp,
  184. GAL.StencilOp.DecrementAndClamp or GAL.StencilOp.DecrementAndClampGl => Silk.NET.Vulkan.StencilOp.DecrementAndClamp,
  185. GAL.StencilOp.Invert or GAL.StencilOp.InvertGl => Silk.NET.Vulkan.StencilOp.Invert,
  186. GAL.StencilOp.IncrementAndWrap or GAL.StencilOp.IncrementAndWrapGl => Silk.NET.Vulkan.StencilOp.IncrementAndWrap,
  187. GAL.StencilOp.DecrementAndWrap or GAL.StencilOp.DecrementAndWrapGl => Silk.NET.Vulkan.StencilOp.DecrementAndWrap,
  188. _ => LogInvalidAndReturn(op, nameof(GAL.StencilOp), Silk.NET.Vulkan.StencilOp.Keep)
  189. };
  190. }
  191. public static ComponentSwizzle Convert(this SwizzleComponent swizzleComponent)
  192. {
  193. return swizzleComponent switch
  194. {
  195. SwizzleComponent.Zero => ComponentSwizzle.Zero,
  196. SwizzleComponent.One => ComponentSwizzle.One,
  197. SwizzleComponent.Red => ComponentSwizzle.R,
  198. SwizzleComponent.Green => ComponentSwizzle.G,
  199. SwizzleComponent.Blue => ComponentSwizzle.B,
  200. SwizzleComponent.Alpha => ComponentSwizzle.A,
  201. _ => LogInvalidAndReturn(swizzleComponent, nameof(SwizzleComponent), ComponentSwizzle.Zero)
  202. };
  203. }
  204. public static ImageType Convert(this Target target)
  205. {
  206. return target switch
  207. {
  208. Target.Texture1D or
  209. Target.Texture1DArray or
  210. Target.TextureBuffer => ImageType.ImageType1D,
  211. Target.Texture2D or
  212. Target.Texture2DArray or
  213. Target.Texture2DMultisample or
  214. Target.Cubemap or
  215. Target.CubemapArray => ImageType.ImageType2D,
  216. Target.Texture3D => ImageType.ImageType3D,
  217. _ => LogInvalidAndReturn(target, nameof(Target), ImageType.ImageType2D)
  218. };
  219. }
  220. public static ImageViewType ConvertView(this Target target)
  221. {
  222. return target switch
  223. {
  224. Target.Texture1D => ImageViewType.ImageViewType1D,
  225. Target.Texture2D or Target.Texture2DMultisample => ImageViewType.ImageViewType2D,
  226. Target.Texture3D => ImageViewType.ImageViewType3D,
  227. Target.Texture1DArray => ImageViewType.ImageViewType1DArray,
  228. Target.Texture2DArray => ImageViewType.ImageViewType2DArray,
  229. Target.Cubemap => ImageViewType.Cube,
  230. Target.CubemapArray => ImageViewType.CubeArray,
  231. _ => LogInvalidAndReturn(target, nameof(Target), ImageViewType.ImageViewType2D)
  232. };
  233. }
  234. public static ImageAspectFlags ConvertAspectFlags(this GAL.Format format)
  235. {
  236. return format switch
  237. {
  238. GAL.Format.D16Unorm or GAL.Format.D32Float => ImageAspectFlags.ImageAspectDepthBit,
  239. GAL.Format.S8Uint => ImageAspectFlags.ImageAspectStencilBit,
  240. GAL.Format.D24UnormS8Uint or
  241. GAL.Format.D32FloatS8Uint or
  242. GAL.Format.S8UintD24Unorm => ImageAspectFlags.ImageAspectDepthBit | ImageAspectFlags.ImageAspectStencilBit,
  243. _ => ImageAspectFlags.ImageAspectColorBit
  244. };
  245. }
  246. public static ImageAspectFlags ConvertAspectFlags(this GAL.Format format, DepthStencilMode depthStencilMode)
  247. {
  248. return format switch
  249. {
  250. GAL.Format.D16Unorm or GAL.Format.D32Float => ImageAspectFlags.ImageAspectDepthBit,
  251. GAL.Format.S8Uint => ImageAspectFlags.ImageAspectStencilBit,
  252. GAL.Format.D24UnormS8Uint or
  253. GAL.Format.D32FloatS8Uint or
  254. GAL.Format.S8UintD24Unorm => depthStencilMode == DepthStencilMode.Stencil ? ImageAspectFlags.ImageAspectStencilBit : ImageAspectFlags.ImageAspectDepthBit,
  255. _ => ImageAspectFlags.ImageAspectColorBit
  256. };
  257. }
  258. public static LogicOp Convert(this LogicalOp op)
  259. {
  260. return op switch
  261. {
  262. LogicalOp.Clear => LogicOp.Clear,
  263. LogicalOp.And => LogicOp.And,
  264. LogicalOp.AndReverse => LogicOp.AndReverse,
  265. LogicalOp.Copy => LogicOp.Copy,
  266. LogicalOp.AndInverted => LogicOp.AndInverted,
  267. LogicalOp.Noop => LogicOp.NoOp,
  268. LogicalOp.Xor => LogicOp.Xor,
  269. LogicalOp.Or => LogicOp.Or,
  270. LogicalOp.Nor => LogicOp.Nor,
  271. LogicalOp.Equiv => LogicOp.Equivalent,
  272. LogicalOp.Invert => LogicOp.Invert,
  273. LogicalOp.OrReverse => LogicOp.OrReverse,
  274. LogicalOp.CopyInverted => LogicOp.CopyInverted,
  275. LogicalOp.OrInverted => LogicOp.OrInverted,
  276. LogicalOp.Nand => LogicOp.Nand,
  277. LogicalOp.Set => LogicOp.Set,
  278. _ => LogInvalidAndReturn(op, nameof(LogicalOp), LogicOp.Copy)
  279. };
  280. }
  281. private static T2 LogInvalidAndReturn<T1, T2>(T1 value, string name, T2 defaultValue = default)
  282. {
  283. Logger.Debug?.Print(LogClass.Gpu, $"Invalid {name} enum value: {value}.");
  284. return defaultValue;
  285. }
  286. }
  287. }