TextureManager.cs 17 KB

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