ShaderConfig.cs 25 KB

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