TextureManager.cs 17 KB

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