TextureBindingsManager.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Engine.Types;
  4. using Ryujinx.Graphics.Gpu.Memory;
  5. using Ryujinx.Graphics.Gpu.Shader;
  6. using Ryujinx.Graphics.Shader;
  7. using System;
  8. using System.Runtime.CompilerServices;
  9. using System.Runtime.InteropServices;
  10. namespace Ryujinx.Graphics.Gpu.Image
  11. {
  12. /// <summary>
  13. /// Texture bindings manager.
  14. /// </summary>
  15. class TextureBindingsManager : IDisposable
  16. {
  17. private const int InitialTextureStateSize = 32;
  18. private const int InitialImageStateSize = 8;
  19. private readonly GpuContext _context;
  20. private readonly bool _isCompute;
  21. private SamplerPool _samplerPool;
  22. private SamplerIndex _samplerIndex;
  23. private ulong _texturePoolAddress;
  24. private int _texturePoolMaximumId;
  25. private readonly GpuChannel _channel;
  26. private readonly TexturePoolCache _texturePoolCache;
  27. private readonly TextureBindingInfo[][] _textureBindings;
  28. private readonly TextureBindingInfo[][] _imageBindings;
  29. private struct TextureState
  30. {
  31. public ITexture Texture;
  32. public ISampler Sampler;
  33. public int TextureHandle;
  34. public int SamplerHandle;
  35. public int InvalidatedSequence;
  36. public Texture CachedTexture;
  37. public Sampler CachedSampler;
  38. public int ScaleIndex;
  39. public TextureUsageFlags UsageFlags;
  40. }
  41. private TextureState[] _textureState;
  42. private TextureState[] _imageState;
  43. private int[] _textureBindingsCount;
  44. private int[] _imageBindingsCount;
  45. private int _texturePoolSequence;
  46. private int _samplerPoolSequence;
  47. private int _textureBufferIndex;
  48. private readonly float[] _scales;
  49. private bool _scaleChanged;
  50. private int _lastFragmentTotal;
  51. /// <summary>
  52. /// Constructs a new instance of the texture bindings manager.
  53. /// </summary>
  54. /// <param name="context">The GPU context that the texture bindings manager belongs to</param>
  55. /// <param name="channel">The GPU channel that the texture bindings manager belongs to</param>
  56. /// <param name="poolCache">Texture pools cache used to get texture pools from</param>
  57. /// <param name="scales">Array where the scales for the currently bound textures are stored</param>
  58. /// <param name="isCompute">True if the bindings manager is used for the compute engine</param>
  59. public TextureBindingsManager(GpuContext context, GpuChannel channel, TexturePoolCache poolCache, float[] scales, bool isCompute)
  60. {
  61. _context = context;
  62. _channel = channel;
  63. _texturePoolCache = poolCache;
  64. _scales = scales;
  65. _isCompute = isCompute;
  66. int stages = isCompute ? 1 : Constants.ShaderStages;
  67. _textureBindings = new TextureBindingInfo[stages][];
  68. _imageBindings = new TextureBindingInfo[stages][];
  69. _textureState = new TextureState[InitialTextureStateSize];
  70. _imageState = new TextureState[InitialImageStateSize];
  71. _textureBindingsCount = new int[stages];
  72. _imageBindingsCount = new int[stages];
  73. for (int stage = 0; stage < stages; stage++)
  74. {
  75. _textureBindings[stage] = new TextureBindingInfo[InitialTextureStateSize];
  76. _imageBindings[stage] = new TextureBindingInfo[InitialImageStateSize];
  77. }
  78. }
  79. /// <summary>
  80. /// Rents the texture bindings array for a given stage, so that they can be modified.
  81. /// </summary>
  82. /// <param name="stage">Shader stage number, or 0 for compute shaders</param>
  83. /// <param name="count">The number of bindings needed</param>
  84. /// <returns>The texture bindings array</returns>
  85. public TextureBindingInfo[] RentTextureBindings(int stage, int count)
  86. {
  87. if (count > _textureBindings[stage].Length)
  88. {
  89. Array.Resize(ref _textureBindings[stage], count);
  90. }
  91. _textureBindingsCount[stage] = count;
  92. return _textureBindings[stage];
  93. }
  94. /// <summary>
  95. /// Rents the image bindings array for a given stage, so that they can be modified.
  96. /// </summary>
  97. /// <param name="stage">Shader stage number, or 0 for compute shaders</param>
  98. /// <param name="count">The number of bindings needed</param>
  99. /// <returns>The image bindings array</returns>
  100. public TextureBindingInfo[] RentImageBindings(int stage, int count)
  101. {
  102. if (count > _imageBindings[stage].Length)
  103. {
  104. Array.Resize(ref _imageBindings[stage], count);
  105. }
  106. _imageBindingsCount[stage] = count;
  107. return _imageBindings[stage];
  108. }
  109. /// <summary>
  110. /// Sets the max binding indexes for textures and images.
  111. /// </summary>
  112. /// <param name="maxTextureBinding">The maximum texture binding</param>
  113. /// <param name="maxImageBinding">The maximum image binding</param>
  114. public void SetMaxBindings(int maxTextureBinding, int maxImageBinding)
  115. {
  116. if (maxTextureBinding >= _textureState.Length)
  117. {
  118. Array.Resize(ref _textureState, maxTextureBinding + 1);
  119. }
  120. if (maxImageBinding >= _imageState.Length)
  121. {
  122. Array.Resize(ref _imageState, maxImageBinding + 1);
  123. }
  124. }
  125. /// <summary>
  126. /// Sets the textures constant buffer index.
  127. /// The constant buffer specified holds the texture handles.
  128. /// </summary>
  129. /// <param name="index">Constant buffer index</param>
  130. public void SetTextureBufferIndex(int index)
  131. {
  132. _textureBufferIndex = index;
  133. }
  134. /// <summary>
  135. /// Sets the current texture sampler pool to be used.
  136. /// </summary>
  137. /// <param name="gpuVa">Start GPU virtual address of the pool</param>
  138. /// <param name="maximumId">Maximum ID of the pool (total count minus one)</param>
  139. /// <param name="samplerIndex">Type of the sampler pool indexing used for bound samplers</param>
  140. public void SetSamplerPool(ulong gpuVa, int maximumId, SamplerIndex samplerIndex)
  141. {
  142. if (gpuVa != 0)
  143. {
  144. ulong address = _channel.MemoryManager.Translate(gpuVa);
  145. if (_samplerPool != null && _samplerPool.Address == address && _samplerPool.MaximumId >= maximumId)
  146. {
  147. return;
  148. }
  149. _samplerPool?.Dispose();
  150. _samplerPool = new SamplerPool(_context, _channel.MemoryManager.Physical, address, maximumId);
  151. }
  152. else
  153. {
  154. _samplerPool?.Dispose();
  155. _samplerPool = null;
  156. }
  157. _samplerIndex = samplerIndex;
  158. }
  159. /// <summary>
  160. /// Sets the current texture pool to be used.
  161. /// </summary>
  162. /// <param name="gpuVa">Start GPU virtual address of the pool</param>
  163. /// <param name="maximumId">Maximum ID of the pool (total count minus one)</param>
  164. public void SetTexturePool(ulong gpuVa, int maximumId)
  165. {
  166. if (gpuVa != 0)
  167. {
  168. ulong address = _channel.MemoryManager.Translate(gpuVa);
  169. _texturePoolAddress = address;
  170. _texturePoolMaximumId = maximumId;
  171. }
  172. else
  173. {
  174. _texturePoolAddress = 0;
  175. _texturePoolMaximumId = 0;
  176. }
  177. }
  178. /// <summary>
  179. /// Gets a texture and a sampler from their respective pools from a texture ID and a sampler ID.
  180. /// </summary>
  181. /// <param name="textureId">ID of the texture</param>
  182. /// <param name="samplerId">ID of the sampler</param>
  183. public (Texture, Sampler) GetTextureAndSampler(int textureId, int samplerId)
  184. {
  185. ulong texturePoolAddress = _texturePoolAddress;
  186. TexturePool texturePool = texturePoolAddress != 0
  187. ? _texturePoolCache.FindOrCreate(_channel, texturePoolAddress, _texturePoolMaximumId)
  188. : null;
  189. return (texturePool.Get(textureId), _samplerPool.Get(samplerId));
  190. }
  191. /// <summary>
  192. /// Updates the texture scale for a given texture or image.
  193. /// </summary>
  194. /// <param name="texture">Start GPU virtual address of the pool</param>
  195. /// <param name="usageFlags">The related texture usage flags</param>
  196. /// <param name="index">The texture/image binding index</param>
  197. /// <param name="stage">The active shader stage</param>
  198. /// <returns>True if the given texture has become blacklisted, indicating that its host texture may have changed.</returns>
  199. private bool UpdateScale(Texture texture, TextureUsageFlags usageFlags, int index, ShaderStage stage)
  200. {
  201. float result = 1f;
  202. bool changed = false;
  203. if ((usageFlags & TextureUsageFlags.NeedsScaleValue) != 0 && texture != null)
  204. {
  205. if ((usageFlags & TextureUsageFlags.ResScaleUnsupported) != 0)
  206. {
  207. changed = texture.ScaleMode != TextureScaleMode.Blacklisted;
  208. texture.BlacklistScale();
  209. }
  210. else
  211. {
  212. switch (stage)
  213. {
  214. case ShaderStage.Fragment:
  215. float scale = texture.ScaleFactor;
  216. if (scale != 1)
  217. {
  218. Texture activeTarget = _channel.TextureManager.GetAnyRenderTarget();
  219. if (activeTarget != null && (activeTarget.Info.Width / (float)texture.Info.Width) == (activeTarget.Info.Height / (float)texture.Info.Height))
  220. {
  221. // If the texture's size is a multiple of the sampler size, enable interpolation using gl_FragCoord. (helps "invent" new integer values between scaled pixels)
  222. result = -scale;
  223. break;
  224. }
  225. }
  226. result = scale;
  227. break;
  228. case ShaderStage.Vertex:
  229. int fragmentIndex = (int)ShaderStage.Fragment - 1;
  230. index += _textureBindingsCount[fragmentIndex] + _imageBindingsCount[fragmentIndex];
  231. result = texture.ScaleFactor;
  232. break;
  233. case ShaderStage.Compute:
  234. result = texture.ScaleFactor;
  235. break;
  236. }
  237. }
  238. }
  239. if (result != _scales[index])
  240. {
  241. _scaleChanged = true;
  242. _scales[index] = result;
  243. }
  244. return changed;
  245. }
  246. /// <summary>
  247. /// Uploads texture and image scales to the backend when they are used.
  248. /// </summary>
  249. private void CommitRenderScale()
  250. {
  251. // Stage 0 total: Compute or Vertex.
  252. int total = _textureBindingsCount[0] + _imageBindingsCount[0];
  253. int fragmentIndex = (int)ShaderStage.Fragment - 1;
  254. int fragmentTotal = _isCompute ? 0 : (_textureBindingsCount[fragmentIndex] + _imageBindingsCount[fragmentIndex]);
  255. if (total != 0 && fragmentTotal != _lastFragmentTotal)
  256. {
  257. // Must update scales in the support buffer if:
  258. // - Vertex stage has bindings.
  259. // - Fragment stage binding count has been updated since last render scale update.
  260. _scaleChanged = true;
  261. }
  262. if (_scaleChanged)
  263. {
  264. if (!_isCompute)
  265. {
  266. total += fragmentTotal; // Add the fragment bindings to the total.
  267. }
  268. _lastFragmentTotal = fragmentTotal;
  269. _context.Renderer.Pipeline.UpdateRenderScale(_scales, total, fragmentTotal);
  270. _scaleChanged = false;
  271. }
  272. }
  273. /// <summary>
  274. /// Ensures that the bindings are visible to the host GPU.
  275. /// Note: this actually performs the binding using the host graphics API.
  276. /// </summary>
  277. /// <param name="specState">Specialization state for the bound shader</param>
  278. /// <returns>True if all bound textures match the current shader specialiation state, false otherwise</returns>
  279. public bool CommitBindings(ShaderSpecializationState specState)
  280. {
  281. ulong texturePoolAddress = _texturePoolAddress;
  282. TexturePool texturePool = texturePoolAddress != 0
  283. ? _texturePoolCache.FindOrCreate(_channel, texturePoolAddress, _texturePoolMaximumId)
  284. : null;
  285. // Check if the texture pool has been modified since bindings were last committed.
  286. // If it wasn't, then it's possible to avoid looking up textures again when the handle remains the same.
  287. bool poolModified = false;
  288. if (texturePool != null)
  289. {
  290. int texturePoolSequence = texturePool.CheckModified();
  291. if (_texturePoolSequence != texturePoolSequence)
  292. {
  293. poolModified = true;
  294. _texturePoolSequence = texturePoolSequence;
  295. }
  296. }
  297. if (_samplerPool != null)
  298. {
  299. int samplerPoolSequence = _samplerPool.CheckModified();
  300. if (_samplerPoolSequence != samplerPoolSequence)
  301. {
  302. poolModified = true;
  303. _samplerPoolSequence = samplerPoolSequence;
  304. }
  305. }
  306. bool specStateMatches = true;
  307. if (_isCompute)
  308. {
  309. specStateMatches &= CommitTextureBindings(texturePool, ShaderStage.Compute, 0, poolModified, specState);
  310. specStateMatches &= CommitImageBindings(texturePool, ShaderStage.Compute, 0, poolModified, specState);
  311. }
  312. else
  313. {
  314. for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
  315. {
  316. int stageIndex = (int)stage - 1;
  317. specStateMatches &= CommitTextureBindings(texturePool, stage, stageIndex, poolModified, specState);
  318. specStateMatches &= CommitImageBindings(texturePool, stage, stageIndex, poolModified, specState);
  319. }
  320. }
  321. CommitRenderScale();
  322. return specStateMatches;
  323. }
  324. /// <summary>
  325. /// Fetch the constant buffers used for a texture to cache.
  326. /// </summary>
  327. /// <param name="stageIndex">Stage index of the constant buffer</param>
  328. /// <param name="cachedTextureBufferIndex">The currently cached texture buffer index</param>
  329. /// <param name="cachedSamplerBufferIndex">The currently cached sampler buffer index</param>
  330. /// <param name="cachedTextureBuffer">The currently cached texture buffer data</param>
  331. /// <param name="cachedSamplerBuffer">The currently cached sampler buffer data</param>
  332. /// <param name="textureBufferIndex">The new texture buffer index</param>
  333. /// <param name="samplerBufferIndex">The new sampler buffer index</param>
  334. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  335. private void UpdateCachedBuffer(
  336. int stageIndex,
  337. ref int cachedTextureBufferIndex,
  338. ref int cachedSamplerBufferIndex,
  339. ref ReadOnlySpan<int> cachedTextureBuffer,
  340. ref ReadOnlySpan<int> cachedSamplerBuffer,
  341. int textureBufferIndex,
  342. int samplerBufferIndex)
  343. {
  344. if (textureBufferIndex != cachedTextureBufferIndex)
  345. {
  346. ref BufferBounds bounds = ref _channel.BufferManager.GetUniformBufferBounds(_isCompute, stageIndex, textureBufferIndex);
  347. cachedTextureBuffer = MemoryMarshal.Cast<byte, int>(_channel.MemoryManager.Physical.GetSpan(bounds.Address, (int)bounds.Size));
  348. cachedTextureBufferIndex = textureBufferIndex;
  349. if (samplerBufferIndex == textureBufferIndex)
  350. {
  351. cachedSamplerBuffer = cachedTextureBuffer;
  352. cachedSamplerBufferIndex = samplerBufferIndex;
  353. }
  354. }
  355. if (samplerBufferIndex != cachedSamplerBufferIndex)
  356. {
  357. ref BufferBounds bounds = ref _channel.BufferManager.GetUniformBufferBounds(_isCompute, stageIndex, samplerBufferIndex);
  358. cachedSamplerBuffer = MemoryMarshal.Cast<byte, int>(_channel.MemoryManager.Physical.GetSpan(bounds.Address, (int)bounds.Size));
  359. cachedSamplerBufferIndex = samplerBufferIndex;
  360. }
  361. }
  362. /// <summary>
  363. /// Ensures that the texture bindings are visible to the host GPU.
  364. /// Note: this actually performs the binding using the host graphics API.
  365. /// </summary>
  366. /// <param name="pool">The current texture pool</param>
  367. /// <param name="stage">The shader stage using the textures to be bound</param>
  368. /// <param name="stageIndex">The stage number of the specified shader stage</param
  369. /// <param name="poolModified">True if either the texture or sampler pool was modified, false otherwise</param>
  370. /// <param name="specState">Specialization state for the bound shader</param>
  371. /// <returns>True if all bound textures match the current shader specialiation state, false otherwise</returns>
  372. private bool CommitTextureBindings(TexturePool pool, ShaderStage stage, int stageIndex, bool poolModified, ShaderSpecializationState specState)
  373. {
  374. int textureCount = _textureBindingsCount[stageIndex];
  375. if (textureCount == 0)
  376. {
  377. return true;
  378. }
  379. var samplerPool = _samplerPool;
  380. if (pool == null)
  381. {
  382. Logger.Error?.Print(LogClass.Gpu, $"Shader stage \"{stage}\" uses textures, but texture pool was not set.");
  383. return true;
  384. }
  385. bool specStateMatches = true;
  386. int cachedTextureBufferIndex = -1;
  387. int cachedSamplerBufferIndex = -1;
  388. ReadOnlySpan<int> cachedTextureBuffer = Span<int>.Empty;
  389. ReadOnlySpan<int> cachedSamplerBuffer = Span<int>.Empty;
  390. for (int index = 0; index < textureCount; index++)
  391. {
  392. TextureBindingInfo bindingInfo = _textureBindings[stageIndex][index];
  393. TextureUsageFlags usageFlags = bindingInfo.Flags;
  394. (int textureBufferIndex, int samplerBufferIndex) = TextureHandle.UnpackSlots(bindingInfo.CbufSlot, _textureBufferIndex);
  395. UpdateCachedBuffer(stageIndex, ref cachedTextureBufferIndex, ref cachedSamplerBufferIndex, ref cachedTextureBuffer, ref cachedSamplerBuffer, textureBufferIndex, samplerBufferIndex);
  396. int packedId = TextureHandle.ReadPackedId(bindingInfo.Handle, cachedTextureBuffer, cachedSamplerBuffer);
  397. int textureId = TextureHandle.UnpackTextureId(packedId);
  398. int samplerId;
  399. if (_samplerIndex == SamplerIndex.ViaHeaderIndex)
  400. {
  401. samplerId = textureId;
  402. }
  403. else
  404. {
  405. samplerId = TextureHandle.UnpackSamplerId(packedId);
  406. }
  407. ref TextureState state = ref _textureState[bindingInfo.Binding];
  408. if (!poolModified &&
  409. state.TextureHandle == textureId &&
  410. state.SamplerHandle == samplerId &&
  411. state.CachedTexture != null &&
  412. state.CachedTexture.InvalidatedSequence == state.InvalidatedSequence &&
  413. state.CachedSampler?.IsDisposed != true)
  414. {
  415. // The texture is already bound.
  416. state.CachedTexture.SynchronizeMemory();
  417. if ((state.ScaleIndex != index || state.UsageFlags != usageFlags) &&
  418. UpdateScale(state.CachedTexture, usageFlags, index, stage))
  419. {
  420. ITexture hostTextureRebind = state.CachedTexture.GetTargetTexture(bindingInfo.Target);
  421. state.Texture = hostTextureRebind;
  422. state.ScaleIndex = index;
  423. state.UsageFlags = usageFlags;
  424. _context.Renderer.Pipeline.SetTexture(bindingInfo.Binding, hostTextureRebind);
  425. }
  426. continue;
  427. }
  428. state.TextureHandle = textureId;
  429. state.SamplerHandle = samplerId;
  430. ref readonly TextureDescriptor descriptor = ref pool.GetForBinding(textureId, out Texture texture);
  431. specStateMatches &= specState.MatchesTexture(stage, index, descriptor);
  432. ITexture hostTexture = texture?.GetTargetTexture(bindingInfo.Target);
  433. if (hostTexture != null && texture.Target == Target.TextureBuffer)
  434. {
  435. // Ensure that the buffer texture is using the correct buffer as storage.
  436. // Buffers are frequently re-created to accomodate larger data, so we need to re-bind
  437. // to ensure we're not using a old buffer that was already deleted.
  438. _channel.BufferManager.SetBufferTextureStorage(hostTexture, texture.Range.GetSubRange(0).Address, texture.Size, bindingInfo, bindingInfo.Format, false);
  439. }
  440. else
  441. {
  442. if (state.Texture != hostTexture)
  443. {
  444. if (UpdateScale(texture, usageFlags, index, stage))
  445. {
  446. hostTexture = texture?.GetTargetTexture(bindingInfo.Target);
  447. }
  448. state.Texture = hostTexture;
  449. state.ScaleIndex = index;
  450. state.UsageFlags = usageFlags;
  451. _context.Renderer.Pipeline.SetTexture(bindingInfo.Binding, hostTexture);
  452. }
  453. Sampler sampler = samplerPool?.Get(samplerId);
  454. state.CachedSampler = sampler;
  455. ISampler hostSampler = sampler?.GetHostSampler(texture);
  456. if (state.Sampler != hostSampler)
  457. {
  458. state.Sampler = hostSampler;
  459. _context.Renderer.Pipeline.SetSampler(bindingInfo.Binding, hostSampler);
  460. }
  461. state.CachedTexture = texture;
  462. state.InvalidatedSequence = texture?.InvalidatedSequence ?? 0;
  463. }
  464. }
  465. return specStateMatches;
  466. }
  467. /// <summary>
  468. /// Ensures that the image bindings are visible to the host GPU.
  469. /// Note: this actually performs the binding using the host graphics API.
  470. /// </summary>
  471. /// <param name="pool">The current texture pool</param>
  472. /// <param name="stage">The shader stage using the textures to be bound</param>
  473. /// <param name="stageIndex">The stage number of the specified shader stage</param>
  474. /// <param name="poolModified">True if either the texture or sampler pool was modified, false otherwise</param>
  475. /// <param name="specState">Specialization state for the bound shader</param>
  476. /// <returns>True if all bound images match the current shader specialiation state, false otherwise</returns>
  477. private bool CommitImageBindings(TexturePool pool, ShaderStage stage, int stageIndex, bool poolModified, ShaderSpecializationState specState)
  478. {
  479. int imageCount = _imageBindingsCount[stageIndex];
  480. if (imageCount == 0)
  481. {
  482. return true;
  483. }
  484. if (pool == null)
  485. {
  486. Logger.Error?.Print(LogClass.Gpu, $"Shader stage \"{stage}\" uses images, but texture pool was not set.");
  487. return true;
  488. }
  489. // Scales for images appear after the texture ones.
  490. int baseScaleIndex = _textureBindingsCount[stageIndex];
  491. int cachedTextureBufferIndex = -1;
  492. int cachedSamplerBufferIndex = -1;
  493. ReadOnlySpan<int> cachedTextureBuffer = Span<int>.Empty;
  494. ReadOnlySpan<int> cachedSamplerBuffer = Span<int>.Empty;
  495. bool specStateMatches = true;
  496. for (int index = 0; index < imageCount; index++)
  497. {
  498. TextureBindingInfo bindingInfo = _imageBindings[stageIndex][index];
  499. TextureUsageFlags usageFlags = bindingInfo.Flags;
  500. int scaleIndex = baseScaleIndex + index;
  501. (int textureBufferIndex, int samplerBufferIndex) = TextureHandle.UnpackSlots(bindingInfo.CbufSlot, _textureBufferIndex);
  502. UpdateCachedBuffer(stageIndex, ref cachedTextureBufferIndex, ref cachedSamplerBufferIndex, ref cachedTextureBuffer, ref cachedSamplerBuffer, textureBufferIndex, samplerBufferIndex);
  503. int packedId = TextureHandle.ReadPackedId(bindingInfo.Handle, cachedTextureBuffer, cachedSamplerBuffer);
  504. int textureId = TextureHandle.UnpackTextureId(packedId);
  505. ref TextureState state = ref _imageState[bindingInfo.Binding];
  506. bool isStore = bindingInfo.Flags.HasFlag(TextureUsageFlags.ImageStore);
  507. if (!poolModified &&
  508. state.TextureHandle == textureId &&
  509. state.CachedTexture != null &&
  510. state.CachedTexture.InvalidatedSequence == state.InvalidatedSequence)
  511. {
  512. Texture cachedTexture = state.CachedTexture;
  513. // The texture is already bound.
  514. cachedTexture.SynchronizeMemory();
  515. if (isStore)
  516. {
  517. cachedTexture?.SignalModified();
  518. }
  519. if ((state.ScaleIndex != index || state.UsageFlags != usageFlags) &&
  520. UpdateScale(state.CachedTexture, usageFlags, scaleIndex, stage))
  521. {
  522. ITexture hostTextureRebind = state.CachedTexture.GetTargetTexture(bindingInfo.Target);
  523. Format format = bindingInfo.Format == 0 ? cachedTexture.Format : bindingInfo.Format;
  524. state.Texture = hostTextureRebind;
  525. state.ScaleIndex = scaleIndex;
  526. state.UsageFlags = usageFlags;
  527. _context.Renderer.Pipeline.SetImage(bindingInfo.Binding, hostTextureRebind, format);
  528. }
  529. continue;
  530. }
  531. state.TextureHandle = textureId;
  532. ref readonly TextureDescriptor descriptor = ref pool.GetForBinding(textureId, out Texture texture);
  533. specStateMatches &= specState.MatchesImage(stage, index, descriptor);
  534. ITexture hostTexture = texture?.GetTargetTexture(bindingInfo.Target);
  535. if (hostTexture != null && texture.Target == Target.TextureBuffer)
  536. {
  537. // Ensure that the buffer texture is using the correct buffer as storage.
  538. // Buffers are frequently re-created to accomodate larger data, so we need to re-bind
  539. // to ensure we're not using a old buffer that was already deleted.
  540. Format format = bindingInfo.Format;
  541. if (format == 0 && texture != null)
  542. {
  543. format = texture.Format;
  544. }
  545. _channel.BufferManager.SetBufferTextureStorage(hostTexture, texture.Range.GetSubRange(0).Address, texture.Size, bindingInfo, format, true);
  546. }
  547. else
  548. {
  549. if (isStore)
  550. {
  551. texture?.SignalModified();
  552. }
  553. if (state.Texture != hostTexture)
  554. {
  555. if (UpdateScale(texture, usageFlags, scaleIndex, stage))
  556. {
  557. hostTexture = texture?.GetTargetTexture(bindingInfo.Target);
  558. }
  559. state.Texture = hostTexture;
  560. state.ScaleIndex = scaleIndex;
  561. state.UsageFlags = usageFlags;
  562. Format format = bindingInfo.Format;
  563. if (format == 0 && texture != null)
  564. {
  565. format = texture.Format;
  566. }
  567. _context.Renderer.Pipeline.SetImage(bindingInfo.Binding, hostTexture, format);
  568. }
  569. state.CachedTexture = texture;
  570. state.InvalidatedSequence = texture?.InvalidatedSequence ?? 0;
  571. }
  572. }
  573. return specStateMatches;
  574. }
  575. /// <summary>
  576. /// Gets the texture descriptor for a given texture handle.
  577. /// </summary>
  578. /// <param name="poolGpuVa">GPU virtual address of the texture pool</param>
  579. /// <param name="bufferIndex">Index of the constant buffer with texture handles</param>
  580. /// <param name="maximumId">Maximum ID of the texture pool</param>
  581. /// <param name="stageIndex">The stage number where the texture is bound</param>
  582. /// <param name="handle">The texture handle</param>
  583. /// <param name="cbufSlot">The texture handle's constant buffer slot</param>
  584. /// <returns>The texture descriptor for the specified texture</returns>
  585. public TextureDescriptor GetTextureDescriptor(
  586. ulong poolGpuVa,
  587. int bufferIndex,
  588. int maximumId,
  589. int stageIndex,
  590. int handle,
  591. int cbufSlot)
  592. {
  593. (int textureBufferIndex, int samplerBufferIndex) = TextureHandle.UnpackSlots(cbufSlot, bufferIndex);
  594. int packedId = ReadPackedId(stageIndex, handle, textureBufferIndex, samplerBufferIndex);
  595. int textureId = TextureHandle.UnpackTextureId(packedId);
  596. ulong poolAddress = _channel.MemoryManager.Translate(poolGpuVa);
  597. TexturePool texturePool = _texturePoolCache.FindOrCreate(_channel, poolAddress, maximumId);
  598. return texturePool.GetDescriptor(textureId);
  599. }
  600. /// <summary>
  601. /// Reads a packed texture and sampler ID (basically, the real texture handle)
  602. /// from the texture constant buffer.
  603. /// </summary>
  604. /// <param name="stageIndex">The number of the shader stage where the texture is bound</param>
  605. /// <param name="wordOffset">A word offset of the handle on the buffer (the "fake" shader handle)</param>
  606. /// <param name="textureBufferIndex">Index of the constant buffer holding the texture handles</param>
  607. /// <param name="samplerBufferIndex">Index of the constant buffer holding the sampler handles</param>
  608. /// <returns>The packed texture and sampler ID (the real texture handle)</returns>
  609. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  610. private int ReadPackedId(int stageIndex, int wordOffset, int textureBufferIndex, int samplerBufferIndex)
  611. {
  612. (int textureWordOffset, int samplerWordOffset, TextureHandleType handleType) = TextureHandle.UnpackOffsets(wordOffset);
  613. ulong textureBufferAddress = _isCompute
  614. ? _channel.BufferManager.GetComputeUniformBufferAddress(textureBufferIndex)
  615. : _channel.BufferManager.GetGraphicsUniformBufferAddress(stageIndex, textureBufferIndex);
  616. int handle = _channel.MemoryManager.Physical.Read<int>(textureBufferAddress + (uint)textureWordOffset * 4);
  617. // The "wordOffset" (which is really the immediate value used on texture instructions on the shader)
  618. // is a 13-bit value. However, in order to also support separate samplers and textures (which uses
  619. // bindless textures on the shader), we extend it with another value on the higher 16 bits with
  620. // another offset for the sampler.
  621. // The shader translator has code to detect separate texture and sampler uses with a bindless texture,
  622. // turn that into a regular texture access and produce those special handles with values on the higher 16 bits.
  623. if (handleType != TextureHandleType.CombinedSampler)
  624. {
  625. ulong samplerBufferAddress = _isCompute
  626. ? _channel.BufferManager.GetComputeUniformBufferAddress(samplerBufferIndex)
  627. : _channel.BufferManager.GetGraphicsUniformBufferAddress(stageIndex, samplerBufferIndex);
  628. int samplerHandle = _channel.MemoryManager.Physical.Read<int>(samplerBufferAddress + (uint)samplerWordOffset * 4);
  629. if (handleType == TextureHandleType.SeparateSamplerId)
  630. {
  631. samplerHandle <<= 20;
  632. }
  633. handle |= samplerHandle;
  634. }
  635. return handle;
  636. }
  637. /// <summary>
  638. /// Force all bound textures and images to be rebound the next time CommitBindings is called.
  639. /// </summary>
  640. public void Rebind()
  641. {
  642. Array.Clear(_textureState);
  643. Array.Clear(_imageState);
  644. }
  645. /// <summary>
  646. /// Disposes all textures and samplers in the cache.
  647. /// </summary>
  648. public void Dispose()
  649. {
  650. _samplerPool?.Dispose();
  651. }
  652. }
  653. }