ShaderDefinitions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Numerics;
  5. namespace Ryujinx.Graphics.Shader.Translation
  6. {
  7. class ShaderDefinitions
  8. {
  9. private readonly GpuGraphicsState _graphicsState;
  10. public ShaderStage Stage { get; }
  11. public int ComputeLocalSizeX { get; }
  12. public int ComputeLocalSizeY { get; }
  13. public int ComputeLocalSizeZ { get; }
  14. public bool TessCw => _graphicsState.TessCw;
  15. public TessPatchType TessPatchType => _graphicsState.TessPatchType;
  16. public TessSpacing TessSpacing => _graphicsState.TessSpacing;
  17. public bool AlphaToCoverageDitherEnable => _graphicsState.AlphaToCoverageEnable && _graphicsState.AlphaToCoverageDitherEnable;
  18. public bool ViewportTransformDisable => _graphicsState.ViewportTransformDisable;
  19. public bool DepthMode => _graphicsState.DepthMode;
  20. public float PointSize => _graphicsState.PointSize;
  21. public AlphaTestOp AlphaTestCompare => _graphicsState.AlphaTestCompare;
  22. public float AlphaTestReference => _graphicsState.AlphaTestReference;
  23. public bool GpPassthrough { get; }
  24. public bool LastInVertexPipeline { get; set; }
  25. public int ThreadsPerInputPrimitive { get; private set; }
  26. public InputTopology InputTopology => _graphicsState.Topology;
  27. public OutputTopology OutputTopology { get; }
  28. public int MaxOutputVertices { get; }
  29. public bool DualSourceBlend => _graphicsState.DualSourceBlendEnable;
  30. public bool EarlyZForce => _graphicsState.EarlyZForce;
  31. public bool YNegateEnabled => _graphicsState.YNegateEnabled;
  32. public bool OriginUpperLeft => _graphicsState.OriginUpperLeft;
  33. public bool HalvePrimitiveId => _graphicsState.HalvePrimitiveId;
  34. public ImapPixelType[] ImapTypes { get; }
  35. public bool IaIndexing { get; private set; }
  36. public bool OaIndexing { get; private set; }
  37. public int OmapTargets { get; }
  38. public bool OmapSampleMask { get; }
  39. public bool OmapDepth { get; }
  40. public bool SupportsScaledVertexFormats { get; }
  41. public bool TransformFeedbackEnabled { get; }
  42. private readonly TransformFeedbackOutput[] _transformFeedbackOutputs;
  43. readonly struct TransformFeedbackVariable : IEquatable<TransformFeedbackVariable>
  44. {
  45. public IoVariable IoVariable { get; }
  46. public int Location { get; }
  47. public int Component { get; }
  48. public TransformFeedbackVariable(IoVariable ioVariable, int location = 0, int component = 0)
  49. {
  50. IoVariable = ioVariable;
  51. Location = location;
  52. Component = component;
  53. }
  54. public override bool Equals(object other)
  55. {
  56. return other is TransformFeedbackVariable tfbVar && Equals(tfbVar);
  57. }
  58. public bool Equals(TransformFeedbackVariable other)
  59. {
  60. return IoVariable == other.IoVariable &&
  61. Location == other.Location &&
  62. Component == other.Component;
  63. }
  64. public override int GetHashCode()
  65. {
  66. return (int)IoVariable | (Location << 8) | (Component << 16);
  67. }
  68. public override string ToString()
  69. {
  70. return $"{IoVariable}.{Location}.{Component}";
  71. }
  72. }
  73. private readonly Dictionary<TransformFeedbackVariable, TransformFeedbackOutput> _transformFeedbackDefinitions;
  74. public ShaderDefinitions(ShaderStage stage, ulong transformFeedbackVecMap, TransformFeedbackOutput[] transformFeedbackOutputs)
  75. {
  76. Stage = stage;
  77. TransformFeedbackEnabled = transformFeedbackOutputs != null;
  78. _transformFeedbackOutputs = transformFeedbackOutputs;
  79. _transformFeedbackDefinitions = new();
  80. PopulateTransformFeedbackDefinitions(transformFeedbackVecMap, transformFeedbackOutputs);
  81. }
  82. public ShaderDefinitions(
  83. ShaderStage stage,
  84. int computeLocalSizeX,
  85. int computeLocalSizeY,
  86. int computeLocalSizeZ)
  87. {
  88. Stage = stage;
  89. ComputeLocalSizeX = computeLocalSizeX;
  90. ComputeLocalSizeY = computeLocalSizeY;
  91. ComputeLocalSizeZ = computeLocalSizeZ;
  92. }
  93. public ShaderDefinitions(
  94. ShaderStage stage,
  95. GpuGraphicsState graphicsState,
  96. bool gpPassthrough,
  97. int threadsPerInputPrimitive,
  98. OutputTopology outputTopology,
  99. int maxOutputVertices)
  100. {
  101. Stage = stage;
  102. _graphicsState = graphicsState;
  103. GpPassthrough = gpPassthrough;
  104. ThreadsPerInputPrimitive = threadsPerInputPrimitive;
  105. OutputTopology = outputTopology;
  106. MaxOutputVertices = maxOutputVertices;
  107. }
  108. public ShaderDefinitions(
  109. ShaderStage stage,
  110. GpuGraphicsState graphicsState,
  111. bool gpPassthrough,
  112. int threadsPerInputPrimitive,
  113. OutputTopology outputTopology,
  114. int maxOutputVertices,
  115. ImapPixelType[] imapTypes,
  116. int omapTargets,
  117. bool omapSampleMask,
  118. bool omapDepth,
  119. bool supportsScaledVertexFormats,
  120. ulong transformFeedbackVecMap,
  121. TransformFeedbackOutput[] transformFeedbackOutputs)
  122. {
  123. Stage = stage;
  124. _graphicsState = graphicsState;
  125. GpPassthrough = gpPassthrough;
  126. ThreadsPerInputPrimitive = threadsPerInputPrimitive;
  127. OutputTopology = outputTopology;
  128. MaxOutputVertices = gpPassthrough ? graphicsState.Topology.ToInputVerticesNoAdjacency() : maxOutputVertices;
  129. ImapTypes = imapTypes;
  130. OmapTargets = omapTargets;
  131. OmapSampleMask = omapSampleMask;
  132. OmapDepth = omapDepth;
  133. LastInVertexPipeline = stage < ShaderStage.Fragment;
  134. SupportsScaledVertexFormats = supportsScaledVertexFormats;
  135. TransformFeedbackEnabled = transformFeedbackOutputs != null;
  136. _transformFeedbackOutputs = transformFeedbackOutputs;
  137. _transformFeedbackDefinitions = new();
  138. PopulateTransformFeedbackDefinitions(transformFeedbackVecMap, transformFeedbackOutputs);
  139. }
  140. private void PopulateTransformFeedbackDefinitions(ulong transformFeedbackVecMap, TransformFeedbackOutput[] transformFeedbackOutputs)
  141. {
  142. while (transformFeedbackVecMap != 0)
  143. {
  144. int vecIndex = BitOperations.TrailingZeroCount(transformFeedbackVecMap);
  145. for (int subIndex = 0; subIndex < 4; subIndex++)
  146. {
  147. int wordOffset = vecIndex * 4 + subIndex;
  148. int byteOffset = wordOffset * 4;
  149. if (transformFeedbackOutputs[wordOffset].Valid)
  150. {
  151. IoVariable ioVariable = Instructions.AttributeMap.GetIoVariable(this, byteOffset, out int location);
  152. int component = 0;
  153. if (HasPerLocationInputOrOutputComponent(ioVariable, location, subIndex, isOutput: true))
  154. {
  155. component = subIndex;
  156. }
  157. TransformFeedbackVariable transformFeedbackVariable = new(ioVariable, location, component);
  158. _transformFeedbackDefinitions.TryAdd(transformFeedbackVariable, transformFeedbackOutputs[wordOffset]);
  159. }
  160. }
  161. transformFeedbackVecMap &= ~(1UL << vecIndex);
  162. }
  163. }
  164. public void EnableInputIndexing()
  165. {
  166. IaIndexing = true;
  167. }
  168. public void EnableOutputIndexing()
  169. {
  170. OaIndexing = true;
  171. }
  172. public bool TryGetTransformFeedbackOutput(IoVariable ioVariable, int location, int component, out TransformFeedbackOutput transformFeedbackOutput)
  173. {
  174. if (!HasTransformFeedbackOutputs())
  175. {
  176. transformFeedbackOutput = default;
  177. return false;
  178. }
  179. TransformFeedbackVariable transformFeedbackVariable = new(ioVariable, location, component);
  180. return _transformFeedbackDefinitions.TryGetValue(transformFeedbackVariable, out transformFeedbackOutput);
  181. }
  182. private bool HasTransformFeedbackOutputs()
  183. {
  184. return TransformFeedbackEnabled && (LastInVertexPipeline || Stage == ShaderStage.Fragment);
  185. }
  186. public bool HasTransformFeedbackOutputs(bool isOutput)
  187. {
  188. return TransformFeedbackEnabled && ((isOutput && LastInVertexPipeline) || (!isOutput && Stage == ShaderStage.Fragment));
  189. }
  190. public bool HasPerLocationInputOrOutput(IoVariable ioVariable, bool isOutput)
  191. {
  192. if (ioVariable == IoVariable.UserDefined)
  193. {
  194. return (!isOutput && !IaIndexing) || (isOutput && !OaIndexing);
  195. }
  196. return ioVariable == IoVariable.FragmentOutputColor;
  197. }
  198. public bool HasPerLocationInputOrOutputComponent(IoVariable ioVariable, int location, int component, bool isOutput)
  199. {
  200. if (ioVariable != IoVariable.UserDefined || !HasTransformFeedbackOutputs(isOutput))
  201. {
  202. return false;
  203. }
  204. return GetTransformFeedbackOutputComponents(location, component) == 1;
  205. }
  206. public TransformFeedbackOutput GetTransformFeedbackOutput(int wordOffset)
  207. {
  208. return _transformFeedbackOutputs[wordOffset];
  209. }
  210. public TransformFeedbackOutput GetTransformFeedbackOutput(int location, int component)
  211. {
  212. return GetTransformFeedbackOutput((AttributeConsts.UserAttributeBase / 4) + location * 4 + component);
  213. }
  214. public int GetTransformFeedbackOutputComponents(int location, int component)
  215. {
  216. int baseIndex = (AttributeConsts.UserAttributeBase / 4) + location * 4;
  217. int index = baseIndex + component;
  218. int count = 1;
  219. for (; count < 4; count++)
  220. {
  221. ref TransformFeedbackOutput prev = ref _transformFeedbackOutputs[baseIndex + count - 1];
  222. ref TransformFeedbackOutput curr = ref _transformFeedbackOutputs[baseIndex + count];
  223. int prevOffset = prev.Offset;
  224. int currOffset = curr.Offset;
  225. if (!prev.Valid || !curr.Valid || prevOffset + 4 != currOffset)
  226. {
  227. break;
  228. }
  229. }
  230. if (baseIndex + count <= index)
  231. {
  232. return 1;
  233. }
  234. return count;
  235. }
  236. public AggregateType GetFragmentOutputColorType(int location)
  237. {
  238. return AggregateType.Vector4 | _graphicsState.FragmentOutputTypes[location].ToAggregateType();
  239. }
  240. public AggregateType GetUserDefinedType(int location, bool isOutput)
  241. {
  242. if ((!isOutput && IaIndexing) || (isOutput && OaIndexing))
  243. {
  244. return AggregateType.Array | AggregateType.Vector4 | AggregateType.FP32;
  245. }
  246. AggregateType type = AggregateType.Vector4;
  247. if (Stage == ShaderStage.Vertex && !isOutput)
  248. {
  249. type |= _graphicsState.AttributeTypes[location].ToAggregateType(SupportsScaledVertexFormats);
  250. }
  251. else
  252. {
  253. type |= AggregateType.FP32;
  254. }
  255. return type;
  256. }
  257. public AttributeType GetAttributeType(int location)
  258. {
  259. return _graphicsState.AttributeTypes[location];
  260. }
  261. public bool IsAttributeSint(int location)
  262. {
  263. return (_graphicsState.AttributeTypes[location] & ~AttributeType.AnyPacked) == AttributeType.Sint;
  264. }
  265. public bool IsAttributePacked(int location)
  266. {
  267. return _graphicsState.AttributeTypes[location].HasFlag(AttributeType.Packed);
  268. }
  269. public bool IsAttributePackedRgb10A2Signed(int location)
  270. {
  271. return _graphicsState.AttributeTypes[location].HasFlag(AttributeType.PackedRgb10A2Signed);
  272. }
  273. public int GetGeometryOutputIndexBufferStridePerInstance()
  274. {
  275. return MaxOutputVertices + OutputTopology switch
  276. {
  277. OutputTopology.LineStrip => MaxOutputVertices / 2,
  278. OutputTopology.TriangleStrip => MaxOutputVertices / 3,
  279. _ => MaxOutputVertices,
  280. };
  281. }
  282. public int GetGeometryOutputIndexBufferStride()
  283. {
  284. return GetGeometryOutputIndexBufferStridePerInstance() * ThreadsPerInputPrimitive;
  285. }
  286. }
  287. }