TextureBindingsManager.cs 35 KB

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