AttributeInfo.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Collections.Generic;
  2. namespace Ryujinx.Graphics.Shader.Translation
  3. {
  4. struct AttributeInfo
  5. {
  6. private static readonly Dictionary<int, AttributeInfo> BuiltInAttributes = new Dictionary<int, AttributeInfo>()
  7. {
  8. { AttributeConsts.TessLevelOuter0, new AttributeInfo(AttributeConsts.TessLevelOuter0, 0, 4, AggregateType.Array | AggregateType.FP32) },
  9. { AttributeConsts.TessLevelOuter1, new AttributeInfo(AttributeConsts.TessLevelOuter0, 1, 4, AggregateType.Array | AggregateType.FP32) },
  10. { AttributeConsts.TessLevelOuter2, new AttributeInfo(AttributeConsts.TessLevelOuter0, 2, 4, AggregateType.Array | AggregateType.FP32) },
  11. { AttributeConsts.TessLevelOuter3, new AttributeInfo(AttributeConsts.TessLevelOuter0, 3, 4, AggregateType.Array | AggregateType.FP32) },
  12. { AttributeConsts.TessLevelInner0, new AttributeInfo(AttributeConsts.TessLevelInner0, 0, 2, AggregateType.Array | AggregateType.FP32) },
  13. { AttributeConsts.TessLevelInner1, new AttributeInfo(AttributeConsts.TessLevelInner0, 1, 2, AggregateType.Array | AggregateType.FP32) },
  14. { AttributeConsts.Layer, new AttributeInfo(AttributeConsts.Layer, 0, 1, AggregateType.S32) },
  15. { AttributeConsts.ViewportIndex, new AttributeInfo(AttributeConsts.ViewportIndex, 0, 1, AggregateType.S32) },
  16. { AttributeConsts.PointSize, new AttributeInfo(AttributeConsts.PointSize, 0, 1, AggregateType.FP32) },
  17. { AttributeConsts.PositionX, new AttributeInfo(AttributeConsts.PositionX, 0, 4, AggregateType.Vector | AggregateType.FP32) },
  18. { AttributeConsts.PositionY, new AttributeInfo(AttributeConsts.PositionX, 1, 4, AggregateType.Vector | AggregateType.FP32) },
  19. { AttributeConsts.PositionZ, new AttributeInfo(AttributeConsts.PositionX, 2, 4, AggregateType.Vector | AggregateType.FP32) },
  20. { AttributeConsts.PositionW, new AttributeInfo(AttributeConsts.PositionX, 3, 4, AggregateType.Vector | AggregateType.FP32) },
  21. { AttributeConsts.ClipDistance0, new AttributeInfo(AttributeConsts.ClipDistance0, 0, 8, AggregateType.Array | AggregateType.FP32) },
  22. { AttributeConsts.ClipDistance1, new AttributeInfo(AttributeConsts.ClipDistance0, 1, 8, AggregateType.Array | AggregateType.FP32) },
  23. { AttributeConsts.ClipDistance2, new AttributeInfo(AttributeConsts.ClipDistance0, 2, 8, AggregateType.Array | AggregateType.FP32) },
  24. { AttributeConsts.ClipDistance3, new AttributeInfo(AttributeConsts.ClipDistance0, 3, 8, AggregateType.Array | AggregateType.FP32) },
  25. { AttributeConsts.ClipDistance4, new AttributeInfo(AttributeConsts.ClipDistance0, 4, 8, AggregateType.Array | AggregateType.FP32) },
  26. { AttributeConsts.ClipDistance5, new AttributeInfo(AttributeConsts.ClipDistance0, 5, 8, AggregateType.Array | AggregateType.FP32) },
  27. { AttributeConsts.ClipDistance6, new AttributeInfo(AttributeConsts.ClipDistance0, 6, 8, AggregateType.Array | AggregateType.FP32) },
  28. { AttributeConsts.ClipDistance7, new AttributeInfo(AttributeConsts.ClipDistance0, 7, 8, AggregateType.Array | AggregateType.FP32) },
  29. { AttributeConsts.PointCoordX, new AttributeInfo(AttributeConsts.PointCoordX, 0, 2, AggregateType.Vector | AggregateType.FP32) },
  30. { AttributeConsts.PointCoordY, new AttributeInfo(AttributeConsts.PointCoordX, 1, 2, AggregateType.Vector | AggregateType.FP32) },
  31. { AttributeConsts.TessCoordX, new AttributeInfo(AttributeConsts.TessCoordX, 0, 2, AggregateType.Vector | AggregateType.FP32) },
  32. { AttributeConsts.TessCoordY, new AttributeInfo(AttributeConsts.TessCoordX, 1, 2, AggregateType.Vector | AggregateType.FP32) },
  33. { AttributeConsts.InstanceId, new AttributeInfo(AttributeConsts.InstanceId, 0, 1, AggregateType.S32) },
  34. { AttributeConsts.VertexId, new AttributeInfo(AttributeConsts.VertexId, 0, 1, AggregateType.S32) },
  35. { AttributeConsts.FrontFacing, new AttributeInfo(AttributeConsts.FrontFacing, 0, 1, AggregateType.Bool) },
  36. // Special.
  37. { AttributeConsts.FragmentOutputDepth, new AttributeInfo(AttributeConsts.FragmentOutputDepth, 0, 1, AggregateType.FP32) },
  38. { AttributeConsts.ThreadKill, new AttributeInfo(AttributeConsts.ThreadKill, 0, 1, AggregateType.Bool) },
  39. { AttributeConsts.ThreadIdX, new AttributeInfo(AttributeConsts.ThreadIdX, 0, 3, AggregateType.Vector | AggregateType.U32) },
  40. { AttributeConsts.ThreadIdY, new AttributeInfo(AttributeConsts.ThreadIdX, 1, 3, AggregateType.Vector | AggregateType.U32) },
  41. { AttributeConsts.ThreadIdZ, new AttributeInfo(AttributeConsts.ThreadIdX, 2, 3, AggregateType.Vector | AggregateType.U32) },
  42. { AttributeConsts.CtaIdX, new AttributeInfo(AttributeConsts.CtaIdX, 0, 3, AggregateType.Vector | AggregateType.U32) },
  43. { AttributeConsts.CtaIdY, new AttributeInfo(AttributeConsts.CtaIdX, 1, 3, AggregateType.Vector | AggregateType.U32) },
  44. { AttributeConsts.CtaIdZ, new AttributeInfo(AttributeConsts.CtaIdX, 2, 3, AggregateType.Vector | AggregateType.U32) },
  45. { AttributeConsts.LaneId, new AttributeInfo(AttributeConsts.LaneId, 0, 1, AggregateType.U32) },
  46. { AttributeConsts.InvocationId, new AttributeInfo(AttributeConsts.InvocationId, 0, 1, AggregateType.S32) },
  47. { AttributeConsts.PrimitiveId, new AttributeInfo(AttributeConsts.PrimitiveId, 0, 1, AggregateType.S32) },
  48. { AttributeConsts.PatchVerticesIn, new AttributeInfo(AttributeConsts.PatchVerticesIn, 0, 1, AggregateType.S32) },
  49. { AttributeConsts.EqMask, new AttributeInfo(AttributeConsts.EqMask, 0, 4, AggregateType.Vector | AggregateType.U32) },
  50. { AttributeConsts.GeMask, new AttributeInfo(AttributeConsts.GeMask, 0, 4, AggregateType.Vector | AggregateType.U32) },
  51. { AttributeConsts.GtMask, new AttributeInfo(AttributeConsts.GtMask, 0, 4, AggregateType.Vector | AggregateType.U32) },
  52. { AttributeConsts.LeMask, new AttributeInfo(AttributeConsts.LeMask, 0, 4, AggregateType.Vector | AggregateType.U32) },
  53. { AttributeConsts.LtMask, new AttributeInfo(AttributeConsts.LtMask, 0, 4, AggregateType.Vector | AggregateType.U32) },
  54. };
  55. public int BaseValue { get; }
  56. public int Value { get; }
  57. public int Length { get; }
  58. public AggregateType Type { get; }
  59. public bool IsBuiltin { get; }
  60. public bool IsValid => Type != AggregateType.Invalid;
  61. public AttributeInfo(int baseValue, int index, int length, AggregateType type, bool isBuiltin = true)
  62. {
  63. BaseValue = baseValue;
  64. Value = baseValue + index * 4;
  65. Length = length;
  66. Type = type;
  67. IsBuiltin = isBuiltin;
  68. }
  69. public int GetInnermostIndex()
  70. {
  71. return (Value - BaseValue) / 4;
  72. }
  73. public static bool Validate(ShaderConfig config, int value, bool isOutAttr)
  74. {
  75. return From(config, value, isOutAttr).IsValid;
  76. }
  77. public static AttributeInfo From(ShaderConfig config, int value, bool isOutAttr)
  78. {
  79. value &= ~3;
  80. if (value >= AttributeConsts.UserAttributeBase && value < AttributeConsts.UserAttributeEnd)
  81. {
  82. int location = (value - AttributeConsts.UserAttributeBase) / 16;
  83. AggregateType elemType;
  84. if (config.Stage == ShaderStage.Vertex && !isOutAttr)
  85. {
  86. elemType = config.GpuAccessor.QueryAttributeType(location) switch
  87. {
  88. AttributeType.Sint => AggregateType.S32,
  89. AttributeType.Uint => AggregateType.U32,
  90. _ => AggregateType.FP32
  91. };
  92. }
  93. else
  94. {
  95. elemType = AggregateType.FP32;
  96. }
  97. return new AttributeInfo(value & ~0xf, (value >> 2) & 3, 4, AggregateType.Vector | elemType, false);
  98. }
  99. else if (value >= AttributeConsts.FragmentOutputColorBase && value < AttributeConsts.FragmentOutputColorEnd)
  100. {
  101. return new AttributeInfo(value & ~0xf, (value >> 2) & 3, 4, AggregateType.Vector | AggregateType.FP32, false);
  102. }
  103. else if (value == AttributeConsts.SupportBlockViewInverseX || value == AttributeConsts.SupportBlockViewInverseY)
  104. {
  105. return new AttributeInfo(value, 0, 1, AggregateType.FP32);
  106. }
  107. else if (BuiltInAttributes.TryGetValue(value, out AttributeInfo info))
  108. {
  109. return info;
  110. }
  111. return new AttributeInfo(value, 0, 0, AggregateType.Invalid);
  112. }
  113. public static bool IsArrayBuiltIn(int attr)
  114. {
  115. if (attr <= AttributeConsts.TessLevelInner1 ||
  116. attr == AttributeConsts.TessCoordX ||
  117. attr == AttributeConsts.TessCoordY)
  118. {
  119. return false;
  120. }
  121. return (attr & AttributeConsts.SpecialMask) == 0;
  122. }
  123. public static bool IsArrayAttributeGlsl(ShaderStage stage, bool isOutAttr)
  124. {
  125. if (isOutAttr)
  126. {
  127. return stage == ShaderStage.TessellationControl;
  128. }
  129. else
  130. {
  131. return stage == ShaderStage.TessellationControl ||
  132. stage == ShaderStage.TessellationEvaluation ||
  133. stage == ShaderStage.Geometry;
  134. }
  135. }
  136. public static bool IsArrayAttributeSpirv(ShaderStage stage, bool isOutAttr)
  137. {
  138. if (isOutAttr)
  139. {
  140. return false;
  141. }
  142. else
  143. {
  144. return stage == ShaderStage.TessellationControl ||
  145. stage == ShaderStage.TessellationEvaluation ||
  146. stage == ShaderStage.Geometry;
  147. }
  148. }
  149. }
  150. }