ShaderConfig.cs 25 KB

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