TextureManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Engine.Types;
  3. using System;
  4. namespace Ryujinx.Graphics.Gpu.Image
  5. {
  6. /// <summary>
  7. /// Texture manager.
  8. /// </summary>
  9. class TextureManager : IDisposable
  10. {
  11. private readonly GpuContext _context;
  12. private readonly TextureBindingsManager _cpBindingsManager;
  13. private readonly TextureBindingsManager _gpBindingsManager;
  14. private readonly Texture[] _rtColors;
  15. private readonly ITexture[] _rtHostColors;
  16. private Texture _rtDepthStencil;
  17. private ITexture _rtHostDs;
  18. public int ClipRegionWidth { get; private set; }
  19. public int ClipRegionHeight { get; private set; }
  20. /// <summary>
  21. /// The scaling factor applied to all currently bound render targets.
  22. /// </summary>
  23. public float RenderTargetScale { get; private set; } = 1f;
  24. /// <summary>
  25. /// Creates a new instance of the texture manager.
  26. /// </summary>
  27. /// <param name="context">GPU context that the texture manager belongs to</param>
  28. /// <param name="channel">GPU channel that the texture manager belongs to</param>
  29. public TextureManager(GpuContext context, GpuChannel channel)
  30. {
  31. _context = context;
  32. TexturePoolCache texturePoolCache = new TexturePoolCache(context);
  33. float[] scales = new float[64];
  34. new Span<float>(scales).Fill(1f);
  35. _cpBindingsManager = new TextureBindingsManager(context, channel, texturePoolCache, scales, isCompute: true);
  36. _gpBindingsManager = new TextureBindingsManager(context, channel, texturePoolCache, scales, isCompute: false);
  37. _rtColors = new Texture[Constants.TotalRenderTargets];
  38. _rtHostColors = new ITexture[Constants.TotalRenderTargets];
  39. }
  40. /// <summary>
  41. /// Rents the texture bindings array of the compute pipeline.
  42. /// </summary>
  43. /// <param name="count">The number of bindings needed</param>
  44. /// <returns>The texture bindings array</returns>
  45. public TextureBindingInfo[] RentComputeTextureBindings(int count)
  46. {
  47. return _cpBindingsManager.RentTextureBindings(0, count);
  48. }
  49. /// <summary>
  50. /// Rents the texture bindings array for a given stage on the graphics pipeline.
  51. /// </summary>
  52. /// <param name="stage">The index of the shader stage to bind the textures</param>
  53. /// <param name="count">The number of bindings needed</param>
  54. /// <returns>The texture bindings array</returns>
  55. public TextureBindingInfo[] RentGraphicsTextureBindings(int stage, int count)
  56. {
  57. return _gpBindingsManager.RentTextureBindings(stage, count);
  58. }
  59. /// <summary>
  60. /// Rents the image bindings array of the compute pipeline.
  61. /// </summary>
  62. /// <param name="count">The number of bindings needed</param>
  63. /// <returns>The image bindings array</returns>
  64. public TextureBindingInfo[] RentComputeImageBindings(int count)
  65. {
  66. return _cpBindingsManager.RentImageBindings(0, count);
  67. }
  68. /// <summary>
  69. /// Rents the image bindings array for a given stage on the graphics pipeline.
  70. /// </summary>
  71. /// <param name="stage">The index of the shader stage to bind the images</param>
  72. /// <param name="count">The number of bindings needed</param>
  73. /// <returns>The image bindings array</returns>
  74. public TextureBindingInfo[] RentGraphicsImageBindings(int stage, int count)
  75. {
  76. return _gpBindingsManager.RentImageBindings(stage, count);
  77. }
  78. /// <summary>
  79. /// Sets the texture constant buffer index on the compute pipeline.
  80. /// </summary>
  81. /// <param name="index">The texture constant buffer index</param>
  82. public void SetComputeTextureBufferIndex(int index)
  83. {
  84. _cpBindingsManager.SetTextureBufferIndex(index);
  85. }
  86. /// <summary>
  87. /// Sets the texture constant buffer index on the graphics pipeline.
  88. /// </summary>
  89. /// <param name="index">The texture constant buffer index</param>
  90. public void SetGraphicsTextureBufferIndex(int index)
  91. {
  92. _gpBindingsManager.SetTextureBufferIndex(index);
  93. }
  94. /// <summary>
  95. /// Sets the current sampler pool on the compute pipeline.
  96. /// </summary>
  97. /// <param name="gpuVa">The start GPU virtual address of the sampler pool</param>
  98. /// <param name="maximumId">The maximum ID of the sampler pool</param>
  99. /// <param name="samplerIndex">The indexing type of the sampler pool</param>
  100. public void SetComputeSamplerPool(ulong gpuVa, int maximumId, SamplerIndex samplerIndex)
  101. {
  102. _cpBindingsManager.SetSamplerPool(gpuVa, maximumId, samplerIndex);
  103. }
  104. /// <summary>
  105. /// Sets the current sampler pool on the graphics pipeline.
  106. /// </summary>
  107. /// <param name="gpuVa">The start GPU virtual address of the sampler pool</param>
  108. /// <param name="maximumId">The maximum ID of the sampler pool</param>
  109. /// <param name="samplerIndex">The indexing type of the sampler pool</param>
  110. public void SetGraphicsSamplerPool(ulong gpuVa, int maximumId, SamplerIndex samplerIndex)
  111. {
  112. _gpBindingsManager.SetSamplerPool(gpuVa, maximumId, samplerIndex);
  113. }
  114. /// <summary>
  115. /// Sets the current texture pool on the compute pipeline.
  116. /// </summary>
  117. /// <param name="gpuVa">The start GPU virtual address of the texture pool</param>
  118. /// <param name="maximumId">The maximum ID of the texture pool</param>
  119. public void SetComputeTexturePool(ulong gpuVa, int maximumId)
  120. {
  121. _cpBindingsManager.SetTexturePool(gpuVa, maximumId);
  122. }
  123. /// <summary>
  124. /// Sets the current texture pool on the graphics pipeline.
  125. /// </summary>
  126. /// <param name="gpuVa">The start GPU virtual address of the texture pool</param>
  127. /// <param name="maximumId">The maximum ID of the texture pool</param>
  128. public void SetGraphicsTexturePool(ulong gpuVa, int maximumId)
  129. {
  130. _gpBindingsManager.SetTexturePool(gpuVa, maximumId);
  131. }
  132. /// <summary>
  133. /// Check if a texture's scale must be updated to match the configured resolution scale.
  134. /// </summary>
  135. /// <param name="texture">The texture to check</param>
  136. /// <returns>True if the scale needs updating, false if the scale is up to date</returns>
  137. private bool ScaleNeedsUpdated(Texture texture)
  138. {
  139. return texture != null && !(texture.ScaleMode == TextureScaleMode.Blacklisted || texture.ScaleMode == TextureScaleMode.Undesired) && texture.ScaleFactor != GraphicsConfig.ResScale;
  140. }
  141. /// <summary>
  142. /// Sets the render target color buffer.
  143. /// </summary>
  144. /// <param name="index">The index of the color buffer to set (up to 8)</param>
  145. /// <param name="color">The color buffer texture</param>
  146. /// <returns>True if render target scale must be updated.</returns>
  147. public bool SetRenderTargetColor(int index, Texture color)
  148. {
  149. bool hasValue = color != null;
  150. bool changesScale = (hasValue != (_rtColors[index] != null)) || (hasValue && RenderTargetScale != color.ScaleFactor);
  151. if (_rtColors[index] != color)
  152. {
  153. _rtColors[index]?.SignalModifying(false);
  154. if (color != null)
  155. {
  156. color.SynchronizeMemory();
  157. color.SignalModifying(true);
  158. }
  159. _rtColors[index] = color;
  160. }
  161. return changesScale || ScaleNeedsUpdated(color);
  162. }
  163. /// <summary>
  164. /// Sets the render target depth-stencil buffer.
  165. /// </summary>
  166. /// <param name="depthStencil">The depth-stencil buffer texture</param>
  167. /// <returns>True if render target scale must be updated.</returns>
  168. public bool SetRenderTargetDepthStencil(Texture depthStencil)
  169. {
  170. bool hasValue = depthStencil != null;
  171. bool changesScale = (hasValue != (_rtDepthStencil != null)) || (hasValue && RenderTargetScale != depthStencil.ScaleFactor);
  172. if (_rtDepthStencil != depthStencil)
  173. {
  174. _rtDepthStencil?.SignalModifying(false);
  175. if (depthStencil != null)
  176. {
  177. depthStencil.SynchronizeMemory();
  178. depthStencil.SignalModifying(true);
  179. }
  180. _rtDepthStencil = depthStencil;
  181. }
  182. return changesScale || ScaleNeedsUpdated(depthStencil);
  183. }
  184. /// <summary>
  185. /// Sets the host clip region, which should be the intersection of all render target texture sizes.
  186. /// </summary>
  187. /// <param name="width">Width of the clip region, defined as the minimum width across all bound textures</param>
  188. /// <param name="height">Height of the clip region, defined as the minimum height across all bound textures</param>
  189. public void SetClipRegion(int width, int height)
  190. {
  191. ClipRegionWidth = width;
  192. ClipRegionHeight = height;
  193. }
  194. /// <summary>
  195. /// Gets the first available bound colour target, or the depth stencil target if not present.
  196. /// </summary>
  197. /// <returns>The first bound colour target, otherwise the depth stencil target</returns>
  198. public Texture GetAnyRenderTarget()
  199. {
  200. return _rtColors[0] ?? _rtDepthStencil;
  201. }
  202. /// <summary>
  203. /// Updates the Render Target scale, given the currently bound render targets.
  204. /// This will update scale to match the configured scale, scale textures that are eligible but not scaled,
  205. /// and propagate blacklisted status from one texture to the ones bound with it.
  206. /// </summary>
  207. /// <param name="singleUse">If this is not -1, it indicates that only the given indexed target will be used.</param>
  208. public void UpdateRenderTargetScale(int singleUse)
  209. {
  210. // Make sure all scales for render targets are at the highest they should be. Blacklisted targets should propagate their scale to the other targets.
  211. bool mismatch = false;
  212. bool blacklisted = false;
  213. bool hasUpscaled = false;
  214. bool hasUndesired = false;
  215. float targetScale = GraphicsConfig.ResScale;
  216. void ConsiderTarget(Texture target)
  217. {
  218. if (target == null) return;
  219. float scale = target.ScaleFactor;
  220. switch (target.ScaleMode)
  221. {
  222. case TextureScaleMode.Blacklisted:
  223. mismatch |= scale != 1f;
  224. blacklisted = true;
  225. break;
  226. case TextureScaleMode.Eligible:
  227. mismatch = true; // We must make a decision.
  228. break;
  229. case TextureScaleMode.Undesired:
  230. hasUndesired = true;
  231. mismatch |= scale != 1f || hasUpscaled; // If another target is upscaled, scale this one up too.
  232. break;
  233. case TextureScaleMode.Scaled:
  234. hasUpscaled = true;
  235. mismatch |= hasUndesired || scale != targetScale; // If the target scale has changed, reset the scale for all targets.
  236. break;
  237. }
  238. }
  239. if (singleUse != -1)
  240. {
  241. // If only one target is in use (by a clear, for example) the others do not need to be checked for mismatching scale.
  242. ConsiderTarget(_rtColors[singleUse]);
  243. }
  244. else
  245. {
  246. foreach (Texture color in _rtColors)
  247. {
  248. ConsiderTarget(color);
  249. }
  250. }
  251. ConsiderTarget(_rtDepthStencil);
  252. mismatch |= blacklisted && hasUpscaled;
  253. if (blacklisted || (hasUndesired && !hasUpscaled))
  254. {
  255. targetScale = 1f;
  256. }
  257. if (mismatch)
  258. {
  259. if (blacklisted)
  260. {
  261. // Propagate the blacklisted state to the other textures.
  262. foreach (Texture color in _rtColors)
  263. {
  264. color?.BlacklistScale();
  265. }
  266. _rtDepthStencil?.BlacklistScale();
  267. }
  268. else
  269. {
  270. // Set the scale of the other textures.
  271. foreach (Texture color in _rtColors)
  272. {
  273. color?.SetScale(targetScale);
  274. }
  275. _rtDepthStencil?.SetScale(targetScale);
  276. }
  277. }
  278. RenderTargetScale = targetScale;
  279. }
  280. /// <summary>
  281. /// Gets a texture and a sampler from their respective pools from a texture ID and a sampler ID.
  282. /// </summary>
  283. /// <param name="textureId">ID of the texture</param>
  284. /// <param name="samplerId">ID of the sampler</param>
  285. public (Texture, Sampler) GetGraphicsTextureAndSampler(int textureId, int samplerId)
  286. {
  287. return _gpBindingsManager.GetTextureAndSampler(textureId, samplerId);
  288. }
  289. /// <summary>
  290. /// Commits bindings on the compute pipeline.
  291. /// </summary>
  292. public void CommitComputeBindings()
  293. {
  294. // Every time we switch between graphics and compute work,
  295. // we must rebind everything.
  296. // Since compute work happens less often, we always do that
  297. // before and after the compute dispatch.
  298. _cpBindingsManager.Rebind();
  299. _cpBindingsManager.CommitBindings();
  300. _gpBindingsManager.Rebind();
  301. }
  302. /// <summary>
  303. /// Commits bindings on the graphics pipeline.
  304. /// </summary>
  305. public void CommitGraphicsBindings()
  306. {
  307. _gpBindingsManager.CommitBindings();
  308. UpdateRenderTargets();
  309. }
  310. /// <summary>
  311. /// Gets a texture descriptor used on the compute pipeline.
  312. /// </summary>
  313. /// <param name="poolGpuVa">GPU virtual address of the texture pool</param>
  314. /// <param name="bufferIndex">Index of the constant buffer with texture handles</param>
  315. /// <param name="maximumId">Maximum ID of the texture pool</param>
  316. /// <param name="handle">Shader "fake" handle of the texture</param>
  317. /// <param name="cbufSlot">Shader constant buffer slot of the texture</param>
  318. /// <returns>The texture descriptor</returns>
  319. public TextureDescriptor GetComputeTextureDescriptor(ulong poolGpuVa, int bufferIndex, int maximumId, int handle, int cbufSlot)
  320. {
  321. return _cpBindingsManager.GetTextureDescriptor(poolGpuVa, bufferIndex, maximumId, 0, handle, cbufSlot);
  322. }
  323. /// <summary>
  324. /// Gets a texture descriptor used on the graphics pipeline.
  325. /// </summary>
  326. /// <param name="poolGpuVa">GPU virtual address of the texture pool</param>
  327. /// <param name="bufferIndex">Index of the constant buffer with texture handles</param>
  328. /// <param name="maximumId">Maximum ID of the texture pool</param>
  329. /// <param name="stageIndex">Index of the shader stage where the texture is bound</param>
  330. /// <param name="handle">Shader "fake" handle of the texture</param>
  331. /// <param name="cbufSlot">Shader constant buffer slot of the texture</param>
  332. /// <returns>The texture descriptor</returns>
  333. public TextureDescriptor GetGraphicsTextureDescriptor(
  334. ulong poolGpuVa,
  335. int bufferIndex,
  336. int maximumId,
  337. int stageIndex,
  338. int handle,
  339. int cbufSlot)
  340. {
  341. return _gpBindingsManager.GetTextureDescriptor(poolGpuVa, bufferIndex, maximumId, stageIndex, handle, cbufSlot);
  342. }
  343. /// <summary>
  344. /// Update host framebuffer attachments based on currently bound render target buffers.
  345. /// </summary>
  346. public void UpdateRenderTargets()
  347. {
  348. bool anyChanged = false;
  349. if (_rtHostDs != _rtDepthStencil?.HostTexture)
  350. {
  351. _rtHostDs = _rtDepthStencil?.HostTexture;
  352. anyChanged = true;
  353. }
  354. for (int index = 0; index < _rtColors.Length; index++)
  355. {
  356. ITexture hostTexture = _rtColors[index]?.HostTexture;
  357. if (_rtHostColors[index] != hostTexture)
  358. {
  359. _rtHostColors[index] = hostTexture;
  360. anyChanged = true;
  361. }
  362. }
  363. if (anyChanged)
  364. {
  365. _context.Renderer.Pipeline.SetRenderTargets(_rtHostColors, _rtHostDs);
  366. }
  367. }
  368. /// <summary>
  369. /// Update host framebuffer attachments based on currently bound render target buffers.
  370. /// </summary>
  371. /// <remarks>
  372. /// All attachments other than <paramref name="index"/> will be unbound.
  373. /// </remarks>
  374. /// <param name="index">Index of the render target color to be updated</param>
  375. public void UpdateRenderTarget(int index)
  376. {
  377. new Span<ITexture>(_rtHostColors).Fill(null);
  378. _rtHostColors[index] = _rtColors[index]?.HostTexture;
  379. _rtHostDs = null;
  380. _context.Renderer.Pipeline.SetRenderTargets(_rtHostColors, null);
  381. }
  382. /// <summary>
  383. /// Update host framebuffer attachments based on currently bound render target buffers.
  384. /// </summary>
  385. /// <remarks>
  386. /// All color attachments will be unbound.
  387. /// </remarks>
  388. public void UpdateRenderTargetDepthStencil()
  389. {
  390. new Span<ITexture>(_rtHostColors).Fill(null);
  391. _rtHostDs = _rtDepthStencil?.HostTexture;
  392. _context.Renderer.Pipeline.SetRenderTargets(_rtHostColors, _rtHostDs);
  393. }
  394. /// <summary>
  395. /// Forces all textures, samplers, images and render targets to be rebound the next time
  396. /// CommitGraphicsBindings is called.
  397. /// </summary>
  398. public void Rebind()
  399. {
  400. _gpBindingsManager.Rebind();
  401. for (int index = 0; index < _rtHostColors.Length; index++)
  402. {
  403. _rtHostColors[index] = null;
  404. }
  405. _rtHostDs = null;
  406. }
  407. /// <summary>
  408. /// Disposes the texture manager.
  409. /// It's an error to use the texture manager after disposal.
  410. /// </summary>
  411. public void Dispose()
  412. {
  413. _cpBindingsManager.Dispose();
  414. _gpBindingsManager.Dispose();
  415. for (int i = 0; i < _rtColors.Length; i++)
  416. {
  417. _rtColors[i]?.DecrementReferenceCount();
  418. _rtColors[i] = null;
  419. }
  420. _rtDepthStencil?.DecrementReferenceCount();
  421. _rtDepthStencil = null;
  422. }
  423. }
  424. }