ShaderConfig.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 bool HasLayerInputAttribute { get; private set; }
  18. public int GpLayerInputAttribute { get; private set; }
  19. public int ThreadsPerInputPrimitive { get; }
  20. public OutputTopology OutputTopology { get; }
  21. public int MaxOutputVertices { get; }
  22. public int LocalMemorySize { get; }
  23. public ImapPixelType[] ImapTypes { get; }
  24. public int OmapTargets { get; }
  25. public bool OmapSampleMask { get; }
  26. public bool OmapDepth { get; }
  27. public IGpuAccessor GpuAccessor { get; }
  28. public TranslationOptions Options { get; }
  29. public ShaderProperties Properties => ResourceManager.Properties;
  30. public ResourceManager ResourceManager { get; set; }
  31. public bool TransformFeedbackEnabled { get; }
  32. private TransformFeedbackOutput[] _transformFeedbackOutputs;
  33. readonly struct TransformFeedbackVariable : IEquatable<TransformFeedbackVariable>
  34. {
  35. public IoVariable IoVariable { get; }
  36. public int Location { get; }
  37. public int Component { get; }
  38. public TransformFeedbackVariable(IoVariable ioVariable, int location = 0, int component = 0)
  39. {
  40. IoVariable = ioVariable;
  41. Location = location;
  42. Component = component;
  43. }
  44. public override bool Equals(object other)
  45. {
  46. return other is TransformFeedbackVariable tfbVar && Equals(tfbVar);
  47. }
  48. public bool Equals(TransformFeedbackVariable other)
  49. {
  50. return IoVariable == other.IoVariable &&
  51. Location == other.Location &&
  52. Component == other.Component;
  53. }
  54. public override int GetHashCode()
  55. {
  56. return (int)IoVariable | (Location << 8) | (Component << 16);
  57. }
  58. public override string ToString()
  59. {
  60. return $"{IoVariable}.{Location}.{Component}";
  61. }
  62. }
  63. private readonly Dictionary<TransformFeedbackVariable, TransformFeedbackOutput> _transformFeedbackDefinitions;
  64. public int Size { get; private set; }
  65. public byte ClipDistancesWritten { get; private set; }
  66. public FeatureFlags UsedFeatures { get; private set; }
  67. public int Cb1DataSize { get; private set; }
  68. public bool LayerOutputWritten { get; private set; }
  69. public int LayerOutputAttribute { get; private set; }
  70. public bool NextUsesFixedFuncAttributes { get; private set; }
  71. public int UsedInputAttributes { get; private set; }
  72. public int UsedOutputAttributes { get; private set; }
  73. public HashSet<int> UsedInputAttributesPerPatch { get; }
  74. public HashSet<int> UsedOutputAttributesPerPatch { get; }
  75. public HashSet<int> NextUsedInputAttributesPerPatch { get; private set; }
  76. public int PassthroughAttributes { get; private set; }
  77. private int _nextUsedInputAttributes;
  78. private int _thisUsedInputAttributes;
  79. private Dictionary<int, int> _perPatchAttributeLocations;
  80. public UInt128 NextInputAttributesComponents { get; private set; }
  81. public UInt128 ThisInputAttributesComponents { get; private set; }
  82. private readonly record struct TextureInfo(int CbufSlot, int Handle, bool Indexed, TextureFormat Format);
  83. private struct TextureMeta
  84. {
  85. public bool AccurateType;
  86. public SamplerType Type;
  87. public TextureUsageFlags UsageFlags;
  88. }
  89. private readonly Dictionary<TextureInfo, TextureMeta> _usedTextures;
  90. private readonly Dictionary<TextureInfo, TextureMeta> _usedImages;
  91. private TextureDescriptor[] _cachedTextureDescriptors;
  92. private TextureDescriptor[] _cachedImageDescriptors;
  93. public ShaderConfig(ShaderStage stage, IGpuAccessor gpuAccessor, TranslationOptions options, int localMemorySize)
  94. {
  95. Stage = stage;
  96. GpuAccessor = gpuAccessor;
  97. Options = options;
  98. LocalMemorySize = localMemorySize;
  99. _transformFeedbackDefinitions = new Dictionary<TransformFeedbackVariable, TransformFeedbackOutput>();
  100. TransformFeedbackEnabled =
  101. stage != ShaderStage.Compute &&
  102. gpuAccessor.QueryTransformFeedbackEnabled() &&
  103. gpuAccessor.QueryHostSupportsTransformFeedback();
  104. UsedInputAttributesPerPatch = new HashSet<int>();
  105. UsedOutputAttributesPerPatch = new HashSet<int>();
  106. _usedTextures = new Dictionary<TextureInfo, TextureMeta>();
  107. _usedImages = new Dictionary<TextureInfo, TextureMeta>();
  108. ResourceManager = new ResourceManager(stage, gpuAccessor, new ShaderProperties());
  109. if (!gpuAccessor.QueryHostSupportsTransformFeedback() && gpuAccessor.QueryTransformFeedbackEnabled())
  110. {
  111. StructureType tfeInfoStruct = new StructureType(new StructureField[]
  112. {
  113. new StructureField(AggregateType.Array | AggregateType.U32, "base_offset", 4),
  114. new StructureField(AggregateType.U32, "vertex_count")
  115. });
  116. BufferDefinition tfeInfoBuffer = new BufferDefinition(BufferLayout.Std430, 1, Constants.TfeInfoBinding, "tfe_info", tfeInfoStruct);
  117. Properties.AddStorageBuffer(Constants.TfeInfoBinding, tfeInfoBuffer);
  118. StructureType tfeDataStruct = new StructureType(new StructureField[]
  119. {
  120. new StructureField(AggregateType.Array | AggregateType.U32, "data", 0)
  121. });
  122. for (int i = 0; i < Constants.TfeBuffersCount; i++)
  123. {
  124. int binding = Constants.TfeBufferBaseBinding + i;
  125. BufferDefinition tfeDataBuffer = new BufferDefinition(BufferLayout.Std430, 1, binding, $"tfe_data{i}", tfeDataStruct);
  126. Properties.AddStorageBuffer(binding, tfeDataBuffer);
  127. }
  128. }
  129. }
  130. public ShaderConfig(
  131. ShaderStage stage,
  132. OutputTopology outputTopology,
  133. int maxOutputVertices,
  134. IGpuAccessor gpuAccessor,
  135. TranslationOptions options) : this(stage, gpuAccessor, options, 0)
  136. {
  137. ThreadsPerInputPrimitive = 1;
  138. OutputTopology = outputTopology;
  139. MaxOutputVertices = maxOutputVertices;
  140. }
  141. public ShaderConfig(
  142. ShaderHeader header,
  143. IGpuAccessor gpuAccessor,
  144. TranslationOptions options) : this(header.Stage, gpuAccessor, options, GetLocalMemorySize(header))
  145. {
  146. GpPassthrough = header.Stage == ShaderStage.Geometry && header.GpPassthrough;
  147. ThreadsPerInputPrimitive = header.ThreadsPerInputPrimitive;
  148. OutputTopology = header.OutputTopology;
  149. MaxOutputVertices = header.MaxOutputVertexCount;
  150. ImapTypes = header.ImapTypes;
  151. OmapTargets = header.OmapTargets;
  152. OmapSampleMask = header.OmapSampleMask;
  153. OmapDepth = header.OmapDepth;
  154. LastInVertexPipeline = header.Stage < ShaderStage.Fragment;
  155. }
  156. private static int GetLocalMemorySize(ShaderHeader header)
  157. {
  158. return header.ShaderLocalMemoryLowSize + header.ShaderLocalMemoryHighSize + (header.ShaderLocalMemoryCrsSize / ThreadsPerWarp);
  159. }
  160. private void EnsureTransformFeedbackInitialized()
  161. {
  162. if (HasTransformFeedbackOutputs() && _transformFeedbackOutputs == null)
  163. {
  164. TransformFeedbackOutput[] transformFeedbackOutputs = new TransformFeedbackOutput[0xc0];
  165. ulong vecMap = 0UL;
  166. for (int tfbIndex = 0; tfbIndex < 4; tfbIndex++)
  167. {
  168. var locations = GpuAccessor.QueryTransformFeedbackVaryingLocations(tfbIndex);
  169. var stride = GpuAccessor.QueryTransformFeedbackStride(tfbIndex);
  170. for (int i = 0; i < locations.Length; i++)
  171. {
  172. byte wordOffset = locations[i];
  173. if (wordOffset < 0xc0)
  174. {
  175. transformFeedbackOutputs[wordOffset] = new TransformFeedbackOutput(tfbIndex, i * 4, stride);
  176. vecMap |= 1UL << (wordOffset / 4);
  177. }
  178. }
  179. }
  180. _transformFeedbackOutputs = transformFeedbackOutputs;
  181. while (vecMap != 0)
  182. {
  183. int vecIndex = BitOperations.TrailingZeroCount(vecMap);
  184. for (int subIndex = 0; subIndex < 4; subIndex++)
  185. {
  186. int wordOffset = vecIndex * 4 + subIndex;
  187. int byteOffset = wordOffset * 4;
  188. if (transformFeedbackOutputs[wordOffset].Valid)
  189. {
  190. IoVariable ioVariable = Instructions.AttributeMap.GetIoVariable(this, byteOffset, out int location);
  191. int component = 0;
  192. if (HasPerLocationInputOrOutputComponent(ioVariable, location, subIndex, isOutput: true))
  193. {
  194. component = subIndex;
  195. }
  196. var transformFeedbackVariable = new TransformFeedbackVariable(ioVariable, location, component);
  197. _transformFeedbackDefinitions.TryAdd(transformFeedbackVariable, transformFeedbackOutputs[wordOffset]);
  198. }
  199. }
  200. vecMap &= ~(1UL << vecIndex);
  201. }
  202. }
  203. }
  204. public TransformFeedbackOutput[] GetTransformFeedbackOutputs()
  205. {
  206. EnsureTransformFeedbackInitialized();
  207. return _transformFeedbackOutputs;
  208. }
  209. public bool TryGetTransformFeedbackOutput(IoVariable ioVariable, int location, int component, out TransformFeedbackOutput transformFeedbackOutput)
  210. {
  211. EnsureTransformFeedbackInitialized();
  212. var transformFeedbackVariable = new TransformFeedbackVariable(ioVariable, location, component);
  213. return _transformFeedbackDefinitions.TryGetValue(transformFeedbackVariable, out transformFeedbackOutput);
  214. }
  215. private bool HasTransformFeedbackOutputs()
  216. {
  217. return TransformFeedbackEnabled && (LastInVertexPipeline || Stage == ShaderStage.Fragment);
  218. }
  219. public bool HasTransformFeedbackOutputs(bool isOutput)
  220. {
  221. return TransformFeedbackEnabled && ((isOutput && LastInVertexPipeline) || (!isOutput && Stage == ShaderStage.Fragment));
  222. }
  223. public bool HasPerLocationInputOrOutput(IoVariable ioVariable, bool isOutput)
  224. {
  225. if (ioVariable == IoVariable.UserDefined)
  226. {
  227. return (!isOutput && !UsedFeatures.HasFlag(FeatureFlags.IaIndexing)) ||
  228. (isOutput && !UsedFeatures.HasFlag(FeatureFlags.OaIndexing));
  229. }
  230. return ioVariable == IoVariable.FragmentOutputColor;
  231. }
  232. public bool HasPerLocationInputOrOutputComponent(IoVariable ioVariable, int location, int component, bool isOutput)
  233. {
  234. if (ioVariable != IoVariable.UserDefined || !HasTransformFeedbackOutputs(isOutput))
  235. {
  236. return false;
  237. }
  238. return GetTransformFeedbackOutputComponents(location, component) == 1;
  239. }
  240. public TransformFeedbackOutput GetTransformFeedbackOutput(int wordOffset)
  241. {
  242. EnsureTransformFeedbackInitialized();
  243. return _transformFeedbackOutputs[wordOffset];
  244. }
  245. public TransformFeedbackOutput GetTransformFeedbackOutput(int location, int component)
  246. {
  247. return GetTransformFeedbackOutput((AttributeConsts.UserAttributeBase / 4) + location * 4 + component);
  248. }
  249. public int GetTransformFeedbackOutputComponents(int location, int component)
  250. {
  251. EnsureTransformFeedbackInitialized();
  252. int baseIndex = (AttributeConsts.UserAttributeBase / 4) + location * 4;
  253. int index = baseIndex + component;
  254. int count = 1;
  255. for (; count < 4; count++)
  256. {
  257. ref var prev = ref _transformFeedbackOutputs[baseIndex + count - 1];
  258. ref var curr = ref _transformFeedbackOutputs[baseIndex + count];
  259. int prevOffset = prev.Offset;
  260. int currOffset = curr.Offset;
  261. if (!prev.Valid || !curr.Valid || prevOffset + 4 != currOffset)
  262. {
  263. break;
  264. }
  265. }
  266. if (baseIndex + count <= index)
  267. {
  268. return 1;
  269. }
  270. return count;
  271. }
  272. public AggregateType GetFragmentOutputColorType(int location)
  273. {
  274. return AggregateType.Vector4 | GpuAccessor.QueryFragmentOutputType(location).ToAggregateType();
  275. }
  276. public AggregateType GetUserDefinedType(int location, bool isOutput)
  277. {
  278. if ((!isOutput && UsedFeatures.HasFlag(FeatureFlags.IaIndexing)) ||
  279. (isOutput && UsedFeatures.HasFlag(FeatureFlags.OaIndexing)))
  280. {
  281. return AggregateType.Array | AggregateType.Vector4 | AggregateType.FP32;
  282. }
  283. AggregateType type = AggregateType.Vector4;
  284. if (Stage == ShaderStage.Vertex && !isOutput)
  285. {
  286. type |= GpuAccessor.QueryAttributeType(location).ToAggregateType();
  287. }
  288. else
  289. {
  290. type |= AggregateType.FP32;
  291. }
  292. return type;
  293. }
  294. public int GetDepthRegister()
  295. {
  296. // The depth register is always two registers after the last color output.
  297. return BitOperations.PopCount((uint)OmapTargets) + 1;
  298. }
  299. public uint ConstantBuffer1Read(int offset)
  300. {
  301. if (Cb1DataSize < offset + 4)
  302. {
  303. Cb1DataSize = offset + 4;
  304. }
  305. return GpuAccessor.ConstantBuffer1Read(offset);
  306. }
  307. public TextureFormat GetTextureFormat(int handle, int cbufSlot = -1)
  308. {
  309. // When the formatted load extension is supported, we don't need to
  310. // specify a format, we can just declare it without a format and the GPU will handle it.
  311. if (GpuAccessor.QueryHostSupportsImageLoadFormatted())
  312. {
  313. return TextureFormat.Unknown;
  314. }
  315. var format = GpuAccessor.QueryTextureFormat(handle, cbufSlot);
  316. if (format == TextureFormat.Unknown)
  317. {
  318. GpuAccessor.Log($"Unknown format for texture {handle}.");
  319. format = TextureFormat.R8G8B8A8Unorm;
  320. }
  321. return format;
  322. }
  323. private static bool FormatSupportsAtomic(TextureFormat format)
  324. {
  325. return format == TextureFormat.R32Sint || format == TextureFormat.R32Uint;
  326. }
  327. public TextureFormat GetTextureFormatAtomic(int handle, int cbufSlot = -1)
  328. {
  329. // Atomic image instructions do not support GL_EXT_shader_image_load_formatted,
  330. // and must have a type specified. Default to R32Sint if not available.
  331. var format = GpuAccessor.QueryTextureFormat(handle, cbufSlot);
  332. if (!FormatSupportsAtomic(format))
  333. {
  334. GpuAccessor.Log($"Unsupported format for texture {handle}: {format}.");
  335. format = TextureFormat.R32Sint;
  336. }
  337. return format;
  338. }
  339. public void SizeAdd(int size)
  340. {
  341. Size += size;
  342. }
  343. public void InheritFrom(ShaderConfig other)
  344. {
  345. ClipDistancesWritten |= other.ClipDistancesWritten;
  346. UsedFeatures |= other.UsedFeatures;
  347. UsedInputAttributes |= other.UsedInputAttributes;
  348. UsedOutputAttributes |= other.UsedOutputAttributes;
  349. foreach (var kv in other._usedTextures)
  350. {
  351. if (!_usedTextures.TryAdd(kv.Key, kv.Value))
  352. {
  353. _usedTextures[kv.Key] = MergeTextureMeta(kv.Value, _usedTextures[kv.Key]);
  354. }
  355. }
  356. foreach (var kv in other._usedImages)
  357. {
  358. if (!_usedImages.TryAdd(kv.Key, kv.Value))
  359. {
  360. _usedImages[kv.Key] = MergeTextureMeta(kv.Value, _usedImages[kv.Key]);
  361. }
  362. }
  363. }
  364. public void SetLayerOutputAttribute(int attr)
  365. {
  366. LayerOutputWritten = true;
  367. LayerOutputAttribute = attr;
  368. }
  369. public void SetGeometryShaderLayerInputAttribute(int attr)
  370. {
  371. HasLayerInputAttribute = true;
  372. GpLayerInputAttribute = attr;
  373. }
  374. public void SetLastInVertexPipeline()
  375. {
  376. LastInVertexPipeline = true;
  377. }
  378. public void SetInputUserAttributeFixedFunc(int index)
  379. {
  380. UsedInputAttributes |= 1 << index;
  381. }
  382. public void SetOutputUserAttributeFixedFunc(int index)
  383. {
  384. UsedOutputAttributes |= 1 << index;
  385. }
  386. public void SetInputUserAttribute(int index, int component)
  387. {
  388. int mask = 1 << index;
  389. UsedInputAttributes |= mask;
  390. _thisUsedInputAttributes |= mask;
  391. ThisInputAttributesComponents |= UInt128.One << (index * 4 + component);
  392. }
  393. public void SetInputUserAttributePerPatch(int index)
  394. {
  395. UsedInputAttributesPerPatch.Add(index);
  396. }
  397. public void SetOutputUserAttribute(int index)
  398. {
  399. UsedOutputAttributes |= 1 << index;
  400. }
  401. public void SetOutputUserAttributePerPatch(int index)
  402. {
  403. UsedOutputAttributesPerPatch.Add(index);
  404. }
  405. public void MergeFromtNextStage(ShaderConfig config)
  406. {
  407. NextInputAttributesComponents = config.ThisInputAttributesComponents;
  408. NextUsedInputAttributesPerPatch = config.UsedInputAttributesPerPatch;
  409. NextUsesFixedFuncAttributes = config.UsedFeatures.HasFlag(FeatureFlags.FixedFuncAttr);
  410. MergeOutputUserAttributes(config.UsedInputAttributes, config.UsedInputAttributesPerPatch);
  411. if (UsedOutputAttributesPerPatch.Count != 0)
  412. {
  413. // Regular and per-patch input/output locations can't overlap,
  414. // so we must assign on our location using unused regular input/output locations.
  415. Dictionary<int, int> locationsMap = new Dictionary<int, int>();
  416. int freeMask = ~UsedOutputAttributes;
  417. foreach (int attr in UsedOutputAttributesPerPatch)
  418. {
  419. int location = BitOperations.TrailingZeroCount(freeMask);
  420. if (location == 32)
  421. {
  422. config.GpuAccessor.Log($"No enough free locations for patch input/output 0x{attr:X}.");
  423. break;
  424. }
  425. locationsMap.Add(attr, location);
  426. freeMask &= ~(1 << location);
  427. }
  428. // Both stages must agree on the locations, so use the same "map" for both.
  429. _perPatchAttributeLocations = locationsMap;
  430. config._perPatchAttributeLocations = locationsMap;
  431. }
  432. // We don't consider geometry shaders using the geometry shader passthrough feature
  433. // as being the last because when this feature is used, it can't actually modify any of the outputs,
  434. // so the stage that comes before it is the last one that can do modifications.
  435. if (config.Stage != ShaderStage.Fragment && (config.Stage != ShaderStage.Geometry || !config.GpPassthrough))
  436. {
  437. LastInVertexPipeline = false;
  438. }
  439. }
  440. public void MergeOutputUserAttributes(int mask, IEnumerable<int> perPatch)
  441. {
  442. _nextUsedInputAttributes = mask;
  443. if (GpPassthrough)
  444. {
  445. PassthroughAttributes = mask & ~UsedOutputAttributes;
  446. }
  447. else
  448. {
  449. UsedOutputAttributes |= mask;
  450. UsedOutputAttributesPerPatch.UnionWith(perPatch);
  451. }
  452. }
  453. public int GetPerPatchAttributeLocation(int index)
  454. {
  455. if (_perPatchAttributeLocations == null || !_perPatchAttributeLocations.TryGetValue(index, out int location))
  456. {
  457. return index;
  458. }
  459. return location;
  460. }
  461. public bool IsUsedOutputAttribute(int attr)
  462. {
  463. // The check for fixed function attributes on the next stage is conservative,
  464. // returning false if the output is just not used by the next stage is also valid.
  465. if (NextUsesFixedFuncAttributes &&
  466. attr >= AttributeConsts.UserAttributeBase &&
  467. attr < AttributeConsts.UserAttributeEnd)
  468. {
  469. int index = (attr - AttributeConsts.UserAttributeBase) >> 4;
  470. return (_nextUsedInputAttributes & (1 << index)) != 0;
  471. }
  472. return true;
  473. }
  474. public int GetFreeUserAttribute(bool isOutput, int index)
  475. {
  476. int useMask = isOutput ? _nextUsedInputAttributes : _thisUsedInputAttributes;
  477. int bit = -1;
  478. while (useMask != -1)
  479. {
  480. bit = BitOperations.TrailingZeroCount(~useMask);
  481. if (bit == 32)
  482. {
  483. bit = -1;
  484. break;
  485. }
  486. else if (index < 1)
  487. {
  488. break;
  489. }
  490. useMask |= 1 << bit;
  491. index--;
  492. }
  493. return bit;
  494. }
  495. public void SetAllInputUserAttributes()
  496. {
  497. UsedInputAttributes |= Constants.AllAttributesMask;
  498. ThisInputAttributesComponents |= ~UInt128.Zero >> (128 - Constants.MaxAttributes * 4);
  499. }
  500. public void SetAllOutputUserAttributes()
  501. {
  502. UsedOutputAttributes |= Constants.AllAttributesMask;
  503. }
  504. public void SetClipDistanceWritten(int index)
  505. {
  506. ClipDistancesWritten |= (byte)(1 << index);
  507. }
  508. public void SetUsedFeature(FeatureFlags flags)
  509. {
  510. UsedFeatures |= flags;
  511. }
  512. public void SetUsedTexture(
  513. Instruction inst,
  514. SamplerType type,
  515. TextureFormat format,
  516. TextureFlags flags,
  517. int cbufSlot,
  518. int handle)
  519. {
  520. inst &= Instruction.Mask;
  521. bool isImage = inst == Instruction.ImageLoad || inst == Instruction.ImageStore || inst == Instruction.ImageAtomic;
  522. bool isWrite = inst == Instruction.ImageStore || inst == Instruction.ImageAtomic;
  523. bool accurateType = inst != Instruction.Lod && inst != Instruction.TextureSize;
  524. bool coherent = flags.HasFlag(TextureFlags.Coherent);
  525. if (isImage)
  526. {
  527. SetUsedTextureOrImage(_usedImages, cbufSlot, handle, type, format, true, isWrite, false, coherent);
  528. }
  529. else
  530. {
  531. bool intCoords = flags.HasFlag(TextureFlags.IntCoords) || inst == Instruction.TextureSize;
  532. SetUsedTextureOrImage(_usedTextures, cbufSlot, handle, type, TextureFormat.Unknown, intCoords, false, accurateType, coherent);
  533. }
  534. GpuAccessor.RegisterTexture(handle, cbufSlot);
  535. }
  536. private void SetUsedTextureOrImage(
  537. Dictionary<TextureInfo, TextureMeta> dict,
  538. int cbufSlot,
  539. int handle,
  540. SamplerType type,
  541. TextureFormat format,
  542. bool intCoords,
  543. bool write,
  544. bool accurateType,
  545. bool coherent)
  546. {
  547. var dimensions = type.GetDimensions();
  548. var isIndexed = type.HasFlag(SamplerType.Indexed);
  549. var usageFlags = TextureUsageFlags.None;
  550. if (intCoords)
  551. {
  552. usageFlags |= TextureUsageFlags.NeedsScaleValue;
  553. var canScale = Stage.SupportsRenderScale() && !isIndexed && !write && dimensions == 2;
  554. if (!canScale)
  555. {
  556. // Resolution scaling cannot be applied to this texture right now.
  557. // Flag so that we know to blacklist scaling on related textures when binding them.
  558. usageFlags |= TextureUsageFlags.ResScaleUnsupported;
  559. }
  560. }
  561. if (write)
  562. {
  563. usageFlags |= TextureUsageFlags.ImageStore;
  564. }
  565. if (coherent)
  566. {
  567. usageFlags |= TextureUsageFlags.ImageCoherent;
  568. }
  569. int arraySize = isIndexed ? SamplerArraySize : 1;
  570. for (int layer = 0; layer < arraySize; layer++)
  571. {
  572. var info = new TextureInfo(cbufSlot, handle + layer * 2, isIndexed, format);
  573. var meta = new TextureMeta()
  574. {
  575. AccurateType = accurateType,
  576. Type = type,
  577. UsageFlags = usageFlags
  578. };
  579. if (dict.TryGetValue(info, out var existingMeta))
  580. {
  581. dict[info] = MergeTextureMeta(meta, existingMeta);
  582. }
  583. else
  584. {
  585. dict.Add(info, meta);
  586. }
  587. }
  588. }
  589. private static TextureMeta MergeTextureMeta(TextureMeta meta, TextureMeta existingMeta)
  590. {
  591. meta.UsageFlags |= existingMeta.UsageFlags;
  592. // If the texture we have has inaccurate type information, then
  593. // we prefer the most accurate one.
  594. if (existingMeta.AccurateType)
  595. {
  596. meta.AccurateType = true;
  597. meta.Type = existingMeta.Type;
  598. }
  599. return meta;
  600. }
  601. public TextureDescriptor[] GetTextureDescriptors()
  602. {
  603. return _cachedTextureDescriptors ??= GetTextureOrImageDescriptors(_usedTextures, GpuAccessor.QueryBindingTexture);
  604. }
  605. public TextureDescriptor[] GetImageDescriptors()
  606. {
  607. return _cachedImageDescriptors ??= GetTextureOrImageDescriptors(_usedImages, GpuAccessor.QueryBindingImage);
  608. }
  609. private static TextureDescriptor[] GetTextureOrImageDescriptors(Dictionary<TextureInfo, TextureMeta> dict, Func<int, bool, int> getBindingCallback)
  610. {
  611. var descriptors = new TextureDescriptor[dict.Count];
  612. int i = 0;
  613. foreach (var kv in dict.OrderBy(x => x.Key.Indexed).ThenBy(x => x.Key.Handle))
  614. {
  615. var info = kv.Key;
  616. var meta = kv.Value;
  617. bool isBuffer = (meta.Type & SamplerType.Mask) == SamplerType.TextureBuffer;
  618. int binding = getBindingCallback(i, isBuffer);
  619. descriptors[i] = new TextureDescriptor(binding, meta.Type, info.Format, info.CbufSlot, info.Handle);
  620. descriptors[i].SetFlag(meta.UsageFlags);
  621. i++;
  622. }
  623. return descriptors;
  624. }
  625. public TextureDescriptor FindTextureDescriptor(AstTextureOperation texOp)
  626. {
  627. TextureDescriptor[] descriptors = GetTextureDescriptors();
  628. for (int i = 0; i < descriptors.Length; i++)
  629. {
  630. var descriptor = descriptors[i];
  631. if (descriptor.CbufSlot == texOp.CbufSlot &&
  632. descriptor.HandleIndex == texOp.Handle &&
  633. descriptor.Format == texOp.Format)
  634. {
  635. return descriptor;
  636. }
  637. }
  638. return default;
  639. }
  640. private static int FindDescriptorIndex(TextureDescriptor[] array, AstTextureOperation texOp)
  641. {
  642. for (int i = 0; i < array.Length; i++)
  643. {
  644. var descriptor = array[i];
  645. if (descriptor.Type == texOp.Type &&
  646. descriptor.CbufSlot == texOp.CbufSlot &&
  647. descriptor.HandleIndex == texOp.Handle &&
  648. descriptor.Format == texOp.Format)
  649. {
  650. return i;
  651. }
  652. }
  653. return -1;
  654. }
  655. private static int FindDescriptorIndex(TextureDescriptor[] array, TextureOperation texOp, bool ignoreType = false)
  656. {
  657. for (int i = 0; i < array.Length; i++)
  658. {
  659. var descriptor = array[i];
  660. if ((descriptor.Type == texOp.Type || ignoreType) &&
  661. descriptor.CbufSlot == texOp.CbufSlot &&
  662. descriptor.HandleIndex == texOp.Handle &&
  663. descriptor.Format == texOp.Format)
  664. {
  665. return i;
  666. }
  667. }
  668. return -1;
  669. }
  670. public int FindTextureDescriptorIndex(TextureOperation texOp, bool ignoreType = false)
  671. {
  672. return FindDescriptorIndex(GetTextureDescriptors(), texOp, ignoreType);
  673. }
  674. public int FindImageDescriptorIndex(TextureOperation texOp)
  675. {
  676. return FindDescriptorIndex(GetImageDescriptors(), texOp);
  677. }
  678. public ShaderProgramInfo CreateProgramInfo(ShaderIdentification identification = ShaderIdentification.None)
  679. {
  680. return new ShaderProgramInfo(
  681. ResourceManager.GetConstantBufferDescriptors(),
  682. ResourceManager.GetStorageBufferDescriptors(),
  683. GetTextureDescriptors(),
  684. GetImageDescriptors(),
  685. identification,
  686. GpLayerInputAttribute,
  687. Stage,
  688. UsedFeatures.HasFlag(FeatureFlags.InstanceId),
  689. UsedFeatures.HasFlag(FeatureFlags.DrawParameters),
  690. UsedFeatures.HasFlag(FeatureFlags.RtLayer),
  691. ClipDistancesWritten,
  692. OmapTargets);
  693. }
  694. }
  695. }