ShaderConfig.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Numerics;
  7. namespace Ryujinx.Graphics.Shader.Translation
  8. {
  9. class ShaderConfig
  10. {
  11. // TODO: Non-hardcoded array size.
  12. public const int SamplerArraySize = 4;
  13. private const int ThreadsPerWarp = 32;
  14. public ShaderStage Stage { get; }
  15. public bool GpPassthrough { get; }
  16. public bool LastInVertexPipeline { get; private set; }
  17. public int ThreadsPerInputPrimitive { get; }
  18. public OutputTopology OutputTopology { get; }
  19. public int MaxOutputVertices { get; }
  20. public int LocalMemorySize { get; }
  21. public ImapPixelType[] ImapTypes { get; }
  22. public int OmapTargets { get; }
  23. public bool OmapSampleMask { get; }
  24. public bool OmapDepth { get; }
  25. public IGpuAccessor GpuAccessor { get; }
  26. public TranslationOptions Options { get; }
  27. public bool TransformFeedbackEnabled { get; }
  28. public int Size { get; private set; }
  29. public byte ClipDistancesWritten { get; private set; }
  30. public FeatureFlags UsedFeatures { get; private set; }
  31. public int Cb1DataSize { get; private set; }
  32. public bool NextUsesFixedFuncAttributes { get; private set; }
  33. public int UsedInputAttributes { get; private set; }
  34. public int UsedOutputAttributes { get; private set; }
  35. public int UsedInputAttributesPerPatch { get; private set; }
  36. public int UsedOutputAttributesPerPatch { get; private set; }
  37. public int PassthroughAttributes { get; private set; }
  38. private int _nextUsedInputAttributes;
  39. private int _thisUsedInputAttributes;
  40. public UInt128 NextInputAttributesComponents { get; private set; }
  41. public UInt128 ThisInputAttributesComponents { get; private set; }
  42. public UInt128 NextInputAttributesPerPatchComponents { get; private set; }
  43. public UInt128 ThisInputAttributesPerPatchComponents { get; private set; }
  44. private int _usedConstantBuffers;
  45. private int _usedStorageBuffers;
  46. private int _usedStorageBuffersWrite;
  47. private struct TextureInfo : IEquatable<TextureInfo>
  48. {
  49. public int CbufSlot { get; }
  50. public int Handle { get; }
  51. public bool Indexed { get; }
  52. public TextureFormat Format { get; }
  53. public TextureInfo(int cbufSlot, int handle, bool indexed, TextureFormat format)
  54. {
  55. CbufSlot = cbufSlot;
  56. Handle = handle;
  57. Indexed = indexed;
  58. Format = format;
  59. }
  60. public override bool Equals(object obj)
  61. {
  62. return obj is TextureInfo other && Equals(other);
  63. }
  64. public bool Equals(TextureInfo other)
  65. {
  66. return CbufSlot == other.CbufSlot && Handle == other.Handle && Indexed == other.Indexed && Format == other.Format;
  67. }
  68. public override int GetHashCode()
  69. {
  70. return HashCode.Combine(CbufSlot, Handle, Indexed, Format);
  71. }
  72. }
  73. private struct TextureMeta
  74. {
  75. public bool AccurateType;
  76. public SamplerType Type;
  77. public TextureUsageFlags UsageFlags;
  78. }
  79. private readonly Dictionary<TextureInfo, TextureMeta> _usedTextures;
  80. private readonly Dictionary<TextureInfo, TextureMeta> _usedImages;
  81. private BufferDescriptor[] _cachedConstantBufferDescriptors;
  82. private BufferDescriptor[] _cachedStorageBufferDescriptors;
  83. private TextureDescriptor[] _cachedTextureDescriptors;
  84. private TextureDescriptor[] _cachedImageDescriptors;
  85. private int _firstConstantBufferBinding;
  86. private int _firstStorageBufferBinding;
  87. public int FirstConstantBufferBinding => _firstConstantBufferBinding;
  88. public int FirstStorageBufferBinding => _firstStorageBufferBinding;
  89. public ShaderConfig(IGpuAccessor gpuAccessor, TranslationOptions options)
  90. {
  91. Stage = ShaderStage.Compute;
  92. GpuAccessor = gpuAccessor;
  93. Options = options;
  94. _usedTextures = new Dictionary<TextureInfo, TextureMeta>();
  95. _usedImages = new Dictionary<TextureInfo, TextureMeta>();
  96. }
  97. public ShaderConfig(ShaderHeader header, IGpuAccessor gpuAccessor, TranslationOptions options) : this(gpuAccessor, options)
  98. {
  99. Stage = header.Stage;
  100. GpPassthrough = header.Stage == ShaderStage.Geometry && header.GpPassthrough;
  101. ThreadsPerInputPrimitive = header.ThreadsPerInputPrimitive;
  102. OutputTopology = header.OutputTopology;
  103. MaxOutputVertices = header.MaxOutputVertexCount;
  104. LocalMemorySize = header.ShaderLocalMemoryLowSize + header.ShaderLocalMemoryHighSize + (header.ShaderLocalMemoryCrsSize / ThreadsPerWarp);
  105. ImapTypes = header.ImapTypes;
  106. OmapTargets = header.OmapTargets;
  107. OmapSampleMask = header.OmapSampleMask;
  108. OmapDepth = header.OmapDepth;
  109. TransformFeedbackEnabled = gpuAccessor.QueryTransformFeedbackEnabled();
  110. LastInVertexPipeline = header.Stage < ShaderStage.Fragment;
  111. }
  112. public int GetDepthRegister()
  113. {
  114. // The depth register is always two registers after the last color output.
  115. return BitOperations.PopCount((uint)OmapTargets) + 1;
  116. }
  117. public uint ConstantBuffer1Read(int offset)
  118. {
  119. if (Cb1DataSize < offset + 4)
  120. {
  121. Cb1DataSize = offset + 4;
  122. }
  123. return GpuAccessor.ConstantBuffer1Read(offset);
  124. }
  125. public TextureFormat GetTextureFormat(int handle, int cbufSlot = -1)
  126. {
  127. // When the formatted load extension is supported, we don't need to
  128. // specify a format, we can just declare it without a format and the GPU will handle it.
  129. if (GpuAccessor.QueryHostSupportsImageLoadFormatted())
  130. {
  131. return TextureFormat.Unknown;
  132. }
  133. var format = GpuAccessor.QueryTextureFormat(handle, cbufSlot);
  134. if (format == TextureFormat.Unknown)
  135. {
  136. GpuAccessor.Log($"Unknown format for texture {handle}.");
  137. format = TextureFormat.R8G8B8A8Unorm;
  138. }
  139. return format;
  140. }
  141. private bool FormatSupportsAtomic(TextureFormat format)
  142. {
  143. return format == TextureFormat.R32Sint || format == TextureFormat.R32Uint;
  144. }
  145. public TextureFormat GetTextureFormatAtomic(int handle, int cbufSlot = -1)
  146. {
  147. // Atomic image instructions do not support GL_EXT_shader_image_load_formatted,
  148. // and must have a type specified. Default to R32Sint if not available.
  149. var format = GpuAccessor.QueryTextureFormat(handle, cbufSlot);
  150. if (!FormatSupportsAtomic(format))
  151. {
  152. GpuAccessor.Log($"Unsupported format for texture {handle}: {format}.");
  153. format = TextureFormat.R32Sint;
  154. }
  155. return format;
  156. }
  157. public void SizeAdd(int size)
  158. {
  159. Size += size;
  160. }
  161. public void InheritFrom(ShaderConfig other)
  162. {
  163. ClipDistancesWritten |= other.ClipDistancesWritten;
  164. UsedFeatures |= other.UsedFeatures;
  165. UsedInputAttributes |= other.UsedInputAttributes;
  166. UsedOutputAttributes |= other.UsedOutputAttributes;
  167. _usedConstantBuffers |= other._usedConstantBuffers;
  168. _usedStorageBuffers |= other._usedStorageBuffers;
  169. _usedStorageBuffersWrite |= other._usedStorageBuffersWrite;
  170. foreach (var kv in other._usedTextures)
  171. {
  172. if (!_usedTextures.TryAdd(kv.Key, kv.Value))
  173. {
  174. _usedTextures[kv.Key] = MergeTextureMeta(kv.Value, _usedTextures[kv.Key]);
  175. }
  176. }
  177. foreach (var kv in other._usedImages)
  178. {
  179. if (!_usedImages.TryAdd(kv.Key, kv.Value))
  180. {
  181. _usedImages[kv.Key] = MergeTextureMeta(kv.Value, _usedImages[kv.Key]);
  182. }
  183. }
  184. }
  185. public void SetInputUserAttributeFixedFunc(int index)
  186. {
  187. UsedInputAttributes |= 1 << index;
  188. }
  189. public void SetOutputUserAttributeFixedFunc(int index)
  190. {
  191. UsedOutputAttributes |= 1 << index;
  192. }
  193. public void SetInputUserAttribute(int index, int component, bool perPatch)
  194. {
  195. if (perPatch)
  196. {
  197. UsedInputAttributesPerPatch |= 1 << index;
  198. ThisInputAttributesPerPatchComponents |= UInt128.Pow2(index * 4 + component);
  199. }
  200. else
  201. {
  202. int mask = 1 << index;
  203. UsedInputAttributes |= mask;
  204. _thisUsedInputAttributes |= mask;
  205. ThisInputAttributesComponents |= UInt128.Pow2(index * 4 + component);
  206. }
  207. }
  208. public void SetOutputUserAttribute(int index, bool perPatch)
  209. {
  210. if (perPatch)
  211. {
  212. UsedOutputAttributesPerPatch |= 1 << index;
  213. }
  214. else
  215. {
  216. UsedOutputAttributes |= 1 << index;
  217. }
  218. }
  219. public void MergeFromtNextStage(ShaderConfig config)
  220. {
  221. NextInputAttributesComponents = config.ThisInputAttributesComponents;
  222. NextInputAttributesPerPatchComponents = config.ThisInputAttributesPerPatchComponents;
  223. NextUsesFixedFuncAttributes = config.UsedFeatures.HasFlag(FeatureFlags.FixedFuncAttr);
  224. MergeOutputUserAttributes(config.UsedInputAttributes, config.UsedInputAttributesPerPatch);
  225. if (config.Stage != ShaderStage.Fragment)
  226. {
  227. LastInVertexPipeline = false;
  228. }
  229. }
  230. public void MergeOutputUserAttributes(int mask, int maskPerPatch)
  231. {
  232. _nextUsedInputAttributes = mask;
  233. if (GpPassthrough)
  234. {
  235. PassthroughAttributes = mask & ~UsedOutputAttributes;
  236. }
  237. else
  238. {
  239. UsedOutputAttributes |= mask;
  240. UsedOutputAttributesPerPatch |= maskPerPatch;
  241. }
  242. }
  243. public bool IsUsedOutputAttribute(int attr)
  244. {
  245. // The check for fixed function attributes on the next stage is conservative,
  246. // returning false if the output is just not used by the next stage is also valid.
  247. if (NextUsesFixedFuncAttributes &&
  248. attr >= AttributeConsts.UserAttributeBase &&
  249. attr < AttributeConsts.UserAttributeEnd)
  250. {
  251. int index = (attr - AttributeConsts.UserAttributeBase) >> 4;
  252. return (_nextUsedInputAttributes & (1 << index)) != 0;
  253. }
  254. return true;
  255. }
  256. public int GetFreeUserAttribute(bool isOutput, int index)
  257. {
  258. int useMask = isOutput ? _nextUsedInputAttributes : _thisUsedInputAttributes;
  259. int bit = -1;
  260. while (useMask != -1)
  261. {
  262. bit = BitOperations.TrailingZeroCount(~useMask);
  263. if (bit == 32)
  264. {
  265. bit = -1;
  266. break;
  267. }
  268. else if (index < 1)
  269. {
  270. break;
  271. }
  272. useMask |= 1 << bit;
  273. index--;
  274. }
  275. return bit;
  276. }
  277. public void SetAllInputUserAttributes()
  278. {
  279. UsedInputAttributes |= Constants.AllAttributesMask;
  280. ThisInputAttributesComponents |= ~UInt128.Zero >> (128 - Constants.MaxAttributes * 4);
  281. }
  282. public void SetAllOutputUserAttributes()
  283. {
  284. UsedOutputAttributes |= Constants.AllAttributesMask;
  285. }
  286. public void SetClipDistanceWritten(int index)
  287. {
  288. ClipDistancesWritten |= (byte)(1 << index);
  289. }
  290. public void SetUsedFeature(FeatureFlags flags)
  291. {
  292. UsedFeatures |= flags;
  293. }
  294. public void SetUsedConstantBuffer(int slot)
  295. {
  296. _usedConstantBuffers |= 1 << slot;
  297. }
  298. public void SetUsedStorageBuffer(int slot, bool write)
  299. {
  300. int mask = 1 << slot;
  301. _usedStorageBuffers |= mask;
  302. if (write)
  303. {
  304. _usedStorageBuffersWrite |= mask;
  305. }
  306. }
  307. public void SetUsedTexture(
  308. Instruction inst,
  309. SamplerType type,
  310. TextureFormat format,
  311. TextureFlags flags,
  312. int cbufSlot,
  313. int handle)
  314. {
  315. inst &= Instruction.Mask;
  316. bool isImage = inst == Instruction.ImageLoad || inst == Instruction.ImageStore || inst == Instruction.ImageAtomic;
  317. bool isWrite = inst == Instruction.ImageStore || inst == Instruction.ImageAtomic;
  318. bool accurateType = inst != Instruction.Lod && inst != Instruction.TextureSize;
  319. bool coherent = flags.HasFlag(TextureFlags.Coherent);
  320. if (isImage)
  321. {
  322. SetUsedTextureOrImage(_usedImages, cbufSlot, handle, type, format, true, isWrite, false, coherent);
  323. }
  324. else
  325. {
  326. bool intCoords = flags.HasFlag(TextureFlags.IntCoords) || inst == Instruction.TextureSize;
  327. SetUsedTextureOrImage(_usedTextures, cbufSlot, handle, type, TextureFormat.Unknown, intCoords, false, accurateType, coherent);
  328. }
  329. GpuAccessor.RegisterTexture(handle, cbufSlot);
  330. }
  331. private void SetUsedTextureOrImage(
  332. Dictionary<TextureInfo, TextureMeta> dict,
  333. int cbufSlot,
  334. int handle,
  335. SamplerType type,
  336. TextureFormat format,
  337. bool intCoords,
  338. bool write,
  339. bool accurateType,
  340. bool coherent)
  341. {
  342. var dimensions = type.GetDimensions();
  343. var isIndexed = type.HasFlag(SamplerType.Indexed);
  344. var usageFlags = TextureUsageFlags.None;
  345. if (intCoords)
  346. {
  347. usageFlags |= TextureUsageFlags.NeedsScaleValue;
  348. var canScale = Stage.SupportsRenderScale() && !isIndexed && !write && dimensions == 2;
  349. if (!canScale)
  350. {
  351. // Resolution scaling cannot be applied to this texture right now.
  352. // Flag so that we know to blacklist scaling on related textures when binding them.
  353. usageFlags |= TextureUsageFlags.ResScaleUnsupported;
  354. }
  355. }
  356. if (write)
  357. {
  358. usageFlags |= TextureUsageFlags.ImageStore;
  359. }
  360. if (coherent)
  361. {
  362. usageFlags |= TextureUsageFlags.ImageCoherent;
  363. }
  364. int arraySize = isIndexed ? SamplerArraySize : 1;
  365. for (int layer = 0; layer < arraySize; layer++)
  366. {
  367. var info = new TextureInfo(cbufSlot, handle + layer * 2, isIndexed, format);
  368. var meta = new TextureMeta()
  369. {
  370. AccurateType = accurateType,
  371. Type = type,
  372. UsageFlags = usageFlags
  373. };
  374. if (dict.TryGetValue(info, out var existingMeta))
  375. {
  376. dict[info] = MergeTextureMeta(meta, existingMeta);
  377. }
  378. else
  379. {
  380. dict.Add(info, meta);
  381. }
  382. }
  383. }
  384. private static TextureMeta MergeTextureMeta(TextureMeta meta, TextureMeta existingMeta)
  385. {
  386. meta.UsageFlags |= existingMeta.UsageFlags;
  387. // If the texture we have has inaccurate type information, then
  388. // we prefer the most accurate one.
  389. if (existingMeta.AccurateType)
  390. {
  391. meta.AccurateType = true;
  392. meta.Type = existingMeta.Type;
  393. }
  394. return meta;
  395. }
  396. public BufferDescriptor[] GetConstantBufferDescriptors()
  397. {
  398. if (_cachedConstantBufferDescriptors != null)
  399. {
  400. return _cachedConstantBufferDescriptors;
  401. }
  402. int usedMask = _usedConstantBuffers;
  403. if (UsedFeatures.HasFlag(FeatureFlags.CbIndexing))
  404. {
  405. usedMask |= (int)GpuAccessor.QueryConstantBufferUse();
  406. }
  407. return _cachedConstantBufferDescriptors = GetBufferDescriptors(
  408. usedMask,
  409. 0,
  410. UsedFeatures.HasFlag(FeatureFlags.CbIndexing),
  411. out _firstConstantBufferBinding,
  412. GpuAccessor.QueryBindingConstantBuffer);
  413. }
  414. public BufferDescriptor[] GetStorageBufferDescriptors()
  415. {
  416. if (_cachedStorageBufferDescriptors != null)
  417. {
  418. return _cachedStorageBufferDescriptors;
  419. }
  420. return _cachedStorageBufferDescriptors = GetBufferDescriptors(
  421. _usedStorageBuffers,
  422. _usedStorageBuffersWrite,
  423. true,
  424. out _firstStorageBufferBinding,
  425. GpuAccessor.QueryBindingStorageBuffer);
  426. }
  427. private static BufferDescriptor[] GetBufferDescriptors(
  428. int usedMask,
  429. int writtenMask,
  430. bool isArray,
  431. out int firstBinding,
  432. Func<int, int> getBindingCallback)
  433. {
  434. firstBinding = 0;
  435. bool hasFirstBinding = false;
  436. var descriptors = new BufferDescriptor[BitOperations.PopCount((uint)usedMask)];
  437. int lastSlot = -1;
  438. for (int i = 0; i < descriptors.Length; i++)
  439. {
  440. int slot = BitOperations.TrailingZeroCount(usedMask);
  441. if (isArray)
  442. {
  443. // The next array entries also consumes bindings, even if they are unused.
  444. for (int j = lastSlot + 1; j < slot; j++)
  445. {
  446. int binding = getBindingCallback(j);
  447. if (!hasFirstBinding)
  448. {
  449. firstBinding = binding;
  450. hasFirstBinding = true;
  451. }
  452. }
  453. }
  454. lastSlot = slot;
  455. descriptors[i] = new BufferDescriptor(getBindingCallback(slot), slot);
  456. if (!hasFirstBinding)
  457. {
  458. firstBinding = descriptors[i].Binding;
  459. hasFirstBinding = true;
  460. }
  461. if ((writtenMask & (1 << slot)) != 0)
  462. {
  463. descriptors[i].SetFlag(BufferUsageFlags.Write);
  464. }
  465. usedMask &= ~(1 << slot);
  466. }
  467. return descriptors;
  468. }
  469. public TextureDescriptor[] GetTextureDescriptors()
  470. {
  471. return _cachedTextureDescriptors ??= GetTextureOrImageDescriptors(_usedTextures, GpuAccessor.QueryBindingTexture);
  472. }
  473. public TextureDescriptor[] GetImageDescriptors()
  474. {
  475. return _cachedImageDescriptors ??= GetTextureOrImageDescriptors(_usedImages, GpuAccessor.QueryBindingImage);
  476. }
  477. private static TextureDescriptor[] GetTextureOrImageDescriptors(Dictionary<TextureInfo, TextureMeta> dict, Func<int, bool, int> getBindingCallback)
  478. {
  479. var descriptors = new TextureDescriptor[dict.Count];
  480. int i = 0;
  481. foreach (var kv in dict.OrderBy(x => x.Key.Indexed).OrderBy(x => x.Key.Handle))
  482. {
  483. var info = kv.Key;
  484. var meta = kv.Value;
  485. bool isBuffer = (meta.Type & SamplerType.Mask) == SamplerType.TextureBuffer;
  486. int binding = getBindingCallback(i, isBuffer);
  487. descriptors[i] = new TextureDescriptor(binding, meta.Type, info.Format, info.CbufSlot, info.Handle);
  488. descriptors[i].SetFlag(meta.UsageFlags);
  489. i++;
  490. }
  491. return descriptors;
  492. }
  493. public (TextureDescriptor, int) FindTextureDescriptor(AstTextureOperation texOp)
  494. {
  495. TextureDescriptor[] descriptors = GetTextureDescriptors();
  496. for (int i = 0; i < descriptors.Length; i++)
  497. {
  498. var descriptor = descriptors[i];
  499. if (descriptor.CbufSlot == texOp.CbufSlot &&
  500. descriptor.HandleIndex == texOp.Handle &&
  501. descriptor.Format == texOp.Format)
  502. {
  503. return (descriptor, i);
  504. }
  505. }
  506. return (default, -1);
  507. }
  508. private static int FindDescriptorIndex(TextureDescriptor[] array, AstTextureOperation texOp)
  509. {
  510. for (int i = 0; i < array.Length; i++)
  511. {
  512. var descriptor = array[i];
  513. if (descriptor.Type == texOp.Type &&
  514. descriptor.CbufSlot == texOp.CbufSlot &&
  515. descriptor.HandleIndex == texOp.Handle &&
  516. descriptor.Format == texOp.Format)
  517. {
  518. return i;
  519. }
  520. }
  521. return -1;
  522. }
  523. public int FindTextureDescriptorIndex(AstTextureOperation texOp)
  524. {
  525. return FindDescriptorIndex(GetTextureDescriptors(), texOp);
  526. }
  527. public int FindImageDescriptorIndex(AstTextureOperation texOp)
  528. {
  529. return FindDescriptorIndex(GetImageDescriptors(), texOp);
  530. }
  531. }
  532. }