AttributeInfo.cs 12 KB

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