ShaderConfig.cs 24 KB

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