ShaderConfig.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Numerics;
  6. namespace Ryujinx.Graphics.Shader.Translation
  7. {
  8. class ShaderConfig
  9. {
  10. // TODO: Non-hardcoded array size.
  11. public const int SamplerArraySize = 4;
  12. public ShaderStage Stage { get; }
  13. public bool GpPassthrough { get; }
  14. public OutputTopology OutputTopology { get; }
  15. public int MaxOutputVertices { get; }
  16. public int LocalMemorySize { get; }
  17. public ImapPixelType[] ImapTypes { get; }
  18. public OmapTarget[] OmapTargets { get; }
  19. public bool OmapSampleMask { get; }
  20. public bool OmapDepth { get; }
  21. public IGpuAccessor GpuAccessor { get; }
  22. public TranslationFlags Flags { get; }
  23. public int Size { get; private set; }
  24. public byte ClipDistancesWritten { get; private set; }
  25. public FeatureFlags UsedFeatures { get; private set; }
  26. public HashSet<int> TextureHandlesForCache { get; }
  27. private readonly TranslationCounts _counts;
  28. private int _usedConstantBuffers;
  29. private int _usedStorageBuffers;
  30. private int _usedStorageBuffersWrite;
  31. private struct TextureInfo : IEquatable<TextureInfo>
  32. {
  33. public int CbufSlot { get; }
  34. public int Handle { get; }
  35. public bool Indexed { get; }
  36. public TextureFormat Format { get; }
  37. public TextureInfo(int cbufSlot, int handle, bool indexed, TextureFormat format)
  38. {
  39. CbufSlot = cbufSlot;
  40. Handle = handle;
  41. Indexed = indexed;
  42. Format = format;
  43. }
  44. public override bool Equals(object obj)
  45. {
  46. return obj is TextureInfo other && Equals(other);
  47. }
  48. public bool Equals(TextureInfo other)
  49. {
  50. return CbufSlot == other.CbufSlot && Handle == other.Handle && Indexed == other.Indexed && Format == other.Format;
  51. }
  52. public override int GetHashCode()
  53. {
  54. return HashCode.Combine(CbufSlot, Handle, Indexed, Format);
  55. }
  56. }
  57. private struct TextureMeta
  58. {
  59. public bool AccurateType;
  60. public SamplerType Type;
  61. public TextureUsageFlags UsageFlags;
  62. }
  63. private readonly Dictionary<TextureInfo, TextureMeta> _usedTextures;
  64. private readonly Dictionary<TextureInfo, TextureMeta> _usedImages;
  65. private BufferDescriptor[] _cachedConstantBufferDescriptors;
  66. private BufferDescriptor[] _cachedStorageBufferDescriptors;
  67. private TextureDescriptor[] _cachedTextureDescriptors;
  68. private TextureDescriptor[] _cachedImageDescriptors;
  69. public int FirstConstantBufferBinding { get; private set; }
  70. public int FirstStorageBufferBinding { get; private set; }
  71. public ShaderConfig(IGpuAccessor gpuAccessor, TranslationFlags flags, TranslationCounts counts)
  72. {
  73. Stage = ShaderStage.Compute;
  74. GpuAccessor = gpuAccessor;
  75. Flags = flags;
  76. _counts = counts;
  77. TextureHandlesForCache = new HashSet<int>();
  78. _usedTextures = new Dictionary<TextureInfo, TextureMeta>();
  79. _usedImages = new Dictionary<TextureInfo, TextureMeta>();
  80. }
  81. public ShaderConfig(ShaderHeader header, IGpuAccessor gpuAccessor, TranslationFlags flags, TranslationCounts counts) : this(gpuAccessor, flags, counts)
  82. {
  83. Stage = header.Stage;
  84. GpPassthrough = header.Stage == ShaderStage.Geometry && header.GpPassthrough;
  85. OutputTopology = header.OutputTopology;
  86. MaxOutputVertices = header.MaxOutputVertexCount;
  87. LocalMemorySize = header.ShaderLocalMemoryLowSize + header.ShaderLocalMemoryHighSize;
  88. ImapTypes = header.ImapTypes;
  89. OmapTargets = header.OmapTargets;
  90. OmapSampleMask = header.OmapSampleMask;
  91. OmapDepth = header.OmapDepth;
  92. }
  93. public int GetDepthRegister()
  94. {
  95. int count = 0;
  96. for (int index = 0; index < OmapTargets.Length; index++)
  97. {
  98. for (int component = 0; component < 4; component++)
  99. {
  100. if (OmapTargets[index].ComponentEnabled(component))
  101. {
  102. count++;
  103. }
  104. }
  105. }
  106. // The depth register is always two registers after the last color output.
  107. return count + 1;
  108. }
  109. public TextureFormat GetTextureFormat(int handle, int cbufSlot = -1)
  110. {
  111. // When the formatted load extension is supported, we don't need to
  112. // specify a format, we can just declare it without a format and the GPU will handle it.
  113. if (GpuAccessor.QuerySupportsImageLoadFormatted())
  114. {
  115. return TextureFormat.Unknown;
  116. }
  117. var format = GpuAccessor.QueryTextureFormat(handle, cbufSlot);
  118. if (format == TextureFormat.Unknown)
  119. {
  120. GpuAccessor.Log($"Unknown format for texture {handle}.");
  121. format = TextureFormat.R8G8B8A8Unorm;
  122. }
  123. return format;
  124. }
  125. public void SizeAdd(int size)
  126. {
  127. Size += size;
  128. }
  129. public void SetClipDistanceWritten(int index)
  130. {
  131. ClipDistancesWritten |= (byte)(1 << index);
  132. }
  133. public void SetUsedFeature(FeatureFlags flags)
  134. {
  135. UsedFeatures |= flags;
  136. }
  137. public Operand CreateCbuf(int slot, int offset)
  138. {
  139. SetUsedConstantBuffer(slot);
  140. return OperandHelper.Cbuf(slot, offset);
  141. }
  142. public void SetUsedConstantBuffer(int slot)
  143. {
  144. _usedConstantBuffers |= 1 << slot;
  145. }
  146. public void SetUsedStorageBuffer(int slot, bool write)
  147. {
  148. int mask = 1 << slot;
  149. _usedStorageBuffers |= mask;
  150. if (write)
  151. {
  152. _usedStorageBuffersWrite |= mask;
  153. }
  154. }
  155. public void SetUsedTexture(
  156. Instruction inst,
  157. SamplerType type,
  158. TextureFormat format,
  159. TextureFlags flags,
  160. int cbufSlot,
  161. int handle)
  162. {
  163. inst &= Instruction.Mask;
  164. bool isImage = inst == Instruction.ImageLoad || inst == Instruction.ImageStore;
  165. bool isWrite = inst == Instruction.ImageStore;
  166. bool accurateType = inst != Instruction.TextureSize && inst != Instruction.Lod;
  167. if (isImage)
  168. {
  169. SetUsedTextureOrImage(_usedImages, cbufSlot, handle, type, format, true, isWrite, false);
  170. }
  171. else
  172. {
  173. SetUsedTextureOrImage(_usedTextures, cbufSlot, handle, type, TextureFormat.Unknown, flags.HasFlag(TextureFlags.IntCoords), false, accurateType);
  174. }
  175. }
  176. private void SetUsedTextureOrImage(
  177. Dictionary<TextureInfo, TextureMeta> dict,
  178. int cbufSlot,
  179. int handle,
  180. SamplerType type,
  181. TextureFormat format,
  182. bool intCoords,
  183. bool write,
  184. bool accurateType)
  185. {
  186. var dimensions = type.GetDimensions();
  187. var isArray = type.HasFlag(SamplerType.Array);
  188. var isIndexed = type.HasFlag(SamplerType.Indexed);
  189. var usageFlags = TextureUsageFlags.None;
  190. if (intCoords)
  191. {
  192. usageFlags |= TextureUsageFlags.NeedsScaleValue;
  193. var canScale = (Stage == ShaderStage.Fragment || Stage == ShaderStage.Compute) && !isIndexed && !write &&
  194. ((dimensions == 2 && !isArray) ||
  195. (dimensions == 3 && isArray));
  196. if (!canScale)
  197. {
  198. // Resolution scaling cannot be applied to this texture right now.
  199. // Flag so that we know to blacklist scaling on related textures when binding them.
  200. usageFlags |= TextureUsageFlags.ResScaleUnsupported;
  201. }
  202. }
  203. if (write)
  204. {
  205. usageFlags |= TextureUsageFlags.ImageStore;
  206. }
  207. int arraySize = isIndexed ? SamplerArraySize : 1;
  208. for (int layer = 0; layer < arraySize; layer++)
  209. {
  210. var info = new TextureInfo(cbufSlot, handle + layer * 2, isIndexed, format);
  211. var meta = new TextureMeta()
  212. {
  213. AccurateType = accurateType,
  214. Type = type,
  215. UsageFlags = usageFlags
  216. };
  217. if (dict.TryGetValue(info, out var existingMeta))
  218. {
  219. meta.UsageFlags |= existingMeta.UsageFlags;
  220. // If the texture we have has inaccurate type information, then
  221. // we prefer the most accurate one.
  222. if (existingMeta.AccurateType)
  223. {
  224. meta.AccurateType = true;
  225. meta.Type = existingMeta.Type;
  226. }
  227. dict[info] = meta;
  228. }
  229. else
  230. {
  231. dict.Add(info, meta);
  232. }
  233. }
  234. }
  235. public BufferDescriptor[] GetConstantBufferDescriptors()
  236. {
  237. if (_cachedConstantBufferDescriptors != null)
  238. {
  239. return _cachedConstantBufferDescriptors;
  240. }
  241. int usedMask = _usedConstantBuffers;
  242. if (UsedFeatures.HasFlag(FeatureFlags.CbIndexing))
  243. {
  244. usedMask |= (int)GpuAccessor.QueryConstantBufferUse();
  245. }
  246. FirstConstantBufferBinding = _counts.UniformBuffersCount;
  247. return _cachedConstantBufferDescriptors = GetBufferDescriptors(
  248. usedMask,
  249. 0,
  250. UsedFeatures.HasFlag(FeatureFlags.CbIndexing),
  251. _counts.IncrementUniformBuffersCount);
  252. }
  253. public BufferDescriptor[] GetStorageBufferDescriptors()
  254. {
  255. if (_cachedStorageBufferDescriptors != null)
  256. {
  257. return _cachedStorageBufferDescriptors;
  258. }
  259. FirstStorageBufferBinding = _counts.StorageBuffersCount;
  260. return _cachedStorageBufferDescriptors = GetBufferDescriptors(
  261. _usedStorageBuffers,
  262. _usedStorageBuffersWrite,
  263. true,
  264. _counts.IncrementStorageBuffersCount);
  265. }
  266. private static BufferDescriptor[] GetBufferDescriptors(
  267. int usedMask,
  268. int writtenMask,
  269. bool isArray,
  270. Func<int> getBindingCallback)
  271. {
  272. var descriptors = new BufferDescriptor[BitOperations.PopCount((uint)usedMask)];
  273. int lastSlot = -1;
  274. for (int i = 0; i < descriptors.Length; i++)
  275. {
  276. int slot = BitOperations.TrailingZeroCount(usedMask);
  277. if (isArray)
  278. {
  279. // The next array entries also consumes bindings, even if they are unused.
  280. for (int j = lastSlot + 1; j < slot; j++)
  281. {
  282. getBindingCallback();
  283. }
  284. }
  285. lastSlot = slot;
  286. descriptors[i] = new BufferDescriptor(getBindingCallback(), slot);
  287. if ((writtenMask & (1 << slot)) != 0)
  288. {
  289. descriptors[i].SetFlag(BufferUsageFlags.Write);
  290. }
  291. usedMask &= ~(1 << slot);
  292. }
  293. return descriptors;
  294. }
  295. public TextureDescriptor[] GetTextureDescriptors()
  296. {
  297. return _cachedTextureDescriptors ??= GetTextureOrImageDescriptors(_usedTextures, _counts.IncrementTexturesCount);
  298. }
  299. public TextureDescriptor[] GetImageDescriptors()
  300. {
  301. return _cachedImageDescriptors ??= GetTextureOrImageDescriptors(_usedImages, _counts.IncrementImagesCount);
  302. }
  303. private static TextureDescriptor[] GetTextureOrImageDescriptors(Dictionary<TextureInfo, TextureMeta> dict, Func<int> getBindingCallback)
  304. {
  305. var descriptors = new TextureDescriptor[dict.Count];
  306. int i = 0;
  307. foreach (var kv in dict.OrderBy(x => x.Key.Indexed).OrderBy(x => x.Key.Handle))
  308. {
  309. var info = kv.Key;
  310. var meta = kv.Value;
  311. int binding = getBindingCallback();
  312. descriptors[i] = new TextureDescriptor(binding, meta.Type, info.Format, info.CbufSlot, info.Handle);
  313. descriptors[i].SetFlag(meta.UsageFlags);
  314. i++;
  315. }
  316. return descriptors;
  317. }
  318. }
  319. }