AttributeInfo.cs 12 KB

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