IoMap.cs 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using static Spv.Specification;
  4. namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
  5. {
  6. static class IoMap
  7. {
  8. // At least 16 attributes are guaranteed by the spec.
  9. private const int MaxAttributes = 16;
  10. public static (BuiltIn, AggregateType) GetSpirvBuiltIn(IoVariable ioVariable)
  11. {
  12. return ioVariable switch
  13. {
  14. IoVariable.BaseInstance => (BuiltIn.BaseInstance, AggregateType.S32),
  15. IoVariable.BaseVertex => (BuiltIn.BaseVertex, AggregateType.S32),
  16. IoVariable.ClipDistance => (BuiltIn.ClipDistance, AggregateType.Array | AggregateType.FP32),
  17. IoVariable.CtaId => (BuiltIn.WorkgroupId, AggregateType.Vector3 | AggregateType.U32),
  18. IoVariable.DrawIndex => (BuiltIn.DrawIndex, AggregateType.S32),
  19. IoVariable.FragmentCoord => (BuiltIn.FragCoord, AggregateType.Vector4 | AggregateType.FP32),
  20. IoVariable.FragmentOutputDepth => (BuiltIn.FragDepth, AggregateType.FP32),
  21. IoVariable.FrontFacing => (BuiltIn.FrontFacing, AggregateType.Bool),
  22. IoVariable.InstanceId => (BuiltIn.InstanceId, AggregateType.S32),
  23. IoVariable.InstanceIndex => (BuiltIn.InstanceIndex, AggregateType.S32),
  24. IoVariable.InvocationId => (BuiltIn.InvocationId, AggregateType.S32),
  25. IoVariable.Layer => (BuiltIn.Layer, AggregateType.S32),
  26. IoVariable.PatchVertices => (BuiltIn.PatchVertices, AggregateType.S32),
  27. IoVariable.PointCoord => (BuiltIn.PointCoord, AggregateType.Vector2 | AggregateType.FP32),
  28. IoVariable.PointSize => (BuiltIn.PointSize, AggregateType.FP32),
  29. IoVariable.Position => (BuiltIn.Position, AggregateType.Vector4 | AggregateType.FP32),
  30. IoVariable.PrimitiveId => (BuiltIn.PrimitiveId, AggregateType.S32),
  31. IoVariable.SubgroupEqMask => (BuiltIn.SubgroupEqMask, AggregateType.Vector4 | AggregateType.U32),
  32. IoVariable.SubgroupGeMask => (BuiltIn.SubgroupGeMask, AggregateType.Vector4 | AggregateType.U32),
  33. IoVariable.SubgroupGtMask => (BuiltIn.SubgroupGtMask, AggregateType.Vector4 | AggregateType.U32),
  34. IoVariable.SubgroupLaneId => (BuiltIn.SubgroupLocalInvocationId, AggregateType.U32),
  35. IoVariable.SubgroupLeMask => (BuiltIn.SubgroupLeMask, AggregateType.Vector4 | AggregateType.U32),
  36. IoVariable.SubgroupLtMask => (BuiltIn.SubgroupLtMask, AggregateType.Vector4 | AggregateType.U32),
  37. IoVariable.TessellationCoord => (BuiltIn.TessCoord, AggregateType.Vector3 | AggregateType.FP32),
  38. IoVariable.TessellationLevelInner => (BuiltIn.TessLevelInner, AggregateType.Array | AggregateType.FP32),
  39. IoVariable.TessellationLevelOuter => (BuiltIn.TessLevelOuter, AggregateType.Array | AggregateType.FP32),
  40. IoVariable.ThreadId => (BuiltIn.LocalInvocationId, AggregateType.Vector3 | AggregateType.U32),
  41. IoVariable.ThreadKill => (BuiltIn.HelperInvocation, AggregateType.Bool),
  42. IoVariable.VertexId => (BuiltIn.VertexId, AggregateType.S32),
  43. IoVariable.VertexIndex => (BuiltIn.VertexIndex, AggregateType.S32),
  44. IoVariable.ViewportIndex => (BuiltIn.ViewportIndex, AggregateType.S32),
  45. IoVariable.ViewportMask => (BuiltIn.ViewportMaskNV, AggregateType.Array | AggregateType.S32),
  46. _ => (default, AggregateType.Invalid)
  47. };
  48. }
  49. public static int GetSpirvBuiltInArrayLength(IoVariable ioVariable)
  50. {
  51. return ioVariable switch
  52. {
  53. IoVariable.ClipDistance => 8,
  54. IoVariable.TessellationLevelInner => 2,
  55. IoVariable.TessellationLevelOuter => 4,
  56. IoVariable.ViewportMask => 1,
  57. IoVariable.UserDefined => MaxAttributes,
  58. _ => 1
  59. };
  60. }
  61. public static bool IsPerVertex(IoVariable ioVariable, ShaderStage stage, bool isOutput)
  62. {
  63. switch (ioVariable)
  64. {
  65. case IoVariable.Layer:
  66. case IoVariable.ViewportIndex:
  67. case IoVariable.PointSize:
  68. case IoVariable.Position:
  69. case IoVariable.UserDefined:
  70. case IoVariable.ClipDistance:
  71. case IoVariable.PointCoord:
  72. case IoVariable.ViewportMask:
  73. return !isOutput &&
  74. (stage == ShaderStage.TessellationControl ||
  75. stage == ShaderStage.TessellationEvaluation ||
  76. stage == ShaderStage.Geometry);
  77. }
  78. return false;
  79. }
  80. }
  81. }