EnumConversion.cs 17 KB

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