ShaderConfig.cs 25 KB

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