AttributeInfo.cs 13 KB

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