TextureBindingsManager.cs 36 KB

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