TextureManager.cs 16 KB

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