TextureBindingsManager.cs 36 KB

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