TextureBindingsManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.State;
  3. using Ryujinx.Graphics.Shader;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Graphics.Gpu.Image
  7. {
  8. /// <summary>
  9. /// Texture bindings manager.
  10. /// </summary>
  11. class TextureBindingsManager
  12. {
  13. private GpuContext _context;
  14. private bool _isCompute;
  15. private SamplerPool _samplerPool;
  16. private SamplerIndex _samplerIndex;
  17. private ulong _texturePoolAddress;
  18. private int _texturePoolMaximumId;
  19. private TexturePoolCache _texturePoolCache;
  20. private TextureBindingInfo[][] _textureBindings;
  21. private TextureBindingInfo[][] _imageBindings;
  22. private struct TextureStatePerStage
  23. {
  24. public ITexture Texture;
  25. public ISampler Sampler;
  26. }
  27. private TextureStatePerStage[][] _textureState;
  28. private TextureStatePerStage[][] _imageState;
  29. private int _textureBufferIndex;
  30. private bool _rebind;
  31. /// <summary>
  32. /// Constructs a new instance of the texture bindings manager.
  33. /// </summary>
  34. /// <param name="context">The GPU context that the texture bindings manager belongs to</param>
  35. /// <param name="texturePoolCache">Texture pools cache used to get texture pools from</param>
  36. /// <param name="isCompute">True if the bindings manager is used for the compute engine</param>
  37. public TextureBindingsManager(GpuContext context, TexturePoolCache texturePoolCache, bool isCompute)
  38. {
  39. _context = context;
  40. _texturePoolCache = texturePoolCache;
  41. _isCompute = isCompute;
  42. int stages = isCompute ? 1 : Constants.ShaderStages;
  43. _textureBindings = new TextureBindingInfo[stages][];
  44. _imageBindings = new TextureBindingInfo[stages][];
  45. _textureState = new TextureStatePerStage[stages][];
  46. _imageState = new TextureStatePerStage[stages][];
  47. }
  48. /// <summary>
  49. /// Binds textures for a given shader stage.
  50. /// </summary>
  51. /// <param name="stage">Shader stage number, or 0 for compute shaders</param>
  52. /// <param name="bindings">Texture bindings</param>
  53. public void SetTextures(int stage, TextureBindingInfo[] bindings)
  54. {
  55. _textureBindings[stage] = bindings;
  56. _textureState[stage] = new TextureStatePerStage[bindings.Length];
  57. }
  58. /// <summary>
  59. /// Binds images for a given shader stage.
  60. /// </summary>
  61. /// <param name="stage">Shader stage number, or 0 for compute shaders</param>
  62. /// <param name="bindings">Image bindings</param>
  63. public void SetImages(int stage, TextureBindingInfo[] bindings)
  64. {
  65. _imageBindings[stage] = bindings;
  66. _imageState[stage] = new TextureStatePerStage[bindings.Length];
  67. }
  68. /// <summary>
  69. /// Sets the textures constant buffer index.
  70. /// The constant buffer specified holds the texture handles.
  71. /// </summary>
  72. /// <param name="index">Constant buffer index</param>
  73. public void SetTextureBufferIndex(int index)
  74. {
  75. _textureBufferIndex = index;
  76. }
  77. /// <summary>
  78. /// Sets the current texture sampler pool to be used.
  79. /// </summary>
  80. /// <param name="gpuVa">Start GPU virtual address of the pool</param>
  81. /// <param name="maximumId">Maximum ID of the pool (total count minus one)</param>
  82. /// <param name="samplerIndex">Type of the sampler pool indexing used for bound samplers</param>
  83. public void SetSamplerPool(ulong gpuVa, int maximumId, SamplerIndex samplerIndex)
  84. {
  85. ulong address = _context.MemoryManager.Translate(gpuVa);
  86. if (_samplerPool != null)
  87. {
  88. if (_samplerPool.Address == address && _samplerPool.MaximumId >= maximumId)
  89. {
  90. return;
  91. }
  92. _samplerPool.Dispose();
  93. }
  94. _samplerPool = new SamplerPool(_context, address, maximumId);
  95. _samplerIndex = samplerIndex;
  96. }
  97. /// <summary>
  98. /// Sets the current texture pool to be used.
  99. /// </summary>
  100. /// <param name="gpuVa">Start GPU virtual address of the pool</param>
  101. /// <param name="maximumId">Maximum ID of the pool (total count minus one)</param>
  102. public void SetTexturePool(ulong gpuVa, int maximumId)
  103. {
  104. ulong address = _context.MemoryManager.Translate(gpuVa);
  105. _texturePoolAddress = address;
  106. _texturePoolMaximumId = maximumId;
  107. }
  108. /// <summary>
  109. /// Ensures that the bindings are visible to the host GPU.
  110. /// Note: this actually performs the binding using the host graphics API.
  111. /// </summary>
  112. public void CommitBindings()
  113. {
  114. TexturePool texturePool = _texturePoolCache.FindOrCreate(
  115. _texturePoolAddress,
  116. _texturePoolMaximumId);
  117. if (_isCompute)
  118. {
  119. CommitTextureBindings(texturePool, ShaderStage.Compute, 0);
  120. CommitImageBindings (texturePool, ShaderStage.Compute, 0);
  121. }
  122. else
  123. {
  124. for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
  125. {
  126. int stageIndex = (int)stage - 1;
  127. CommitTextureBindings(texturePool, stage, stageIndex);
  128. CommitImageBindings (texturePool, stage, stageIndex);
  129. }
  130. }
  131. _rebind = false;
  132. }
  133. /// <summary>
  134. /// Ensures that the texture bindings are visible to the host GPU.
  135. /// Note: this actually performs the binding using the host graphics API.
  136. /// </summary>
  137. /// <param name="pool">The current texture pool</param>
  138. /// <param name="stage">The shader stage using the textures to be bound</param>
  139. /// <param name="stageIndex">The stage number of the specified shader stage</param>
  140. private void CommitTextureBindings(TexturePool pool, ShaderStage stage, int stageIndex)
  141. {
  142. if (_textureBindings[stageIndex] == null)
  143. {
  144. return;
  145. }
  146. for (int index = 0; index < _textureBindings[stageIndex].Length; index++)
  147. {
  148. TextureBindingInfo binding = _textureBindings[stageIndex][index];
  149. int packedId;
  150. if (binding.IsBindless)
  151. {
  152. ulong address;
  153. var bufferManager = _context.Methods.BufferManager;
  154. if (_isCompute)
  155. {
  156. address = bufferManager.GetComputeUniformBufferAddress(binding.CbufSlot);
  157. }
  158. else
  159. {
  160. address = bufferManager.GetGraphicsUniformBufferAddress(stageIndex, binding.CbufSlot);
  161. }
  162. packedId = MemoryMarshal.Cast<byte, int>(_context.PhysicalMemory.GetSpan(address + (ulong)binding.CbufOffset * 4, 4))[0];
  163. }
  164. else
  165. {
  166. packedId = ReadPackedId(stageIndex, binding.Handle, _textureBufferIndex);
  167. }
  168. int textureId = UnpackTextureId(packedId);
  169. int samplerId;
  170. if (_samplerIndex == SamplerIndex.ViaHeaderIndex)
  171. {
  172. samplerId = textureId;
  173. }
  174. else
  175. {
  176. samplerId = UnpackSamplerId(packedId);
  177. }
  178. Texture texture = pool.Get(textureId);
  179. ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
  180. if (_textureState[stageIndex][index].Texture != hostTexture || _rebind)
  181. {
  182. _textureState[stageIndex][index].Texture = hostTexture;
  183. _context.Renderer.Pipeline.SetTexture(index, stage, hostTexture);
  184. }
  185. if (hostTexture != null && texture.Info.Target == Target.TextureBuffer)
  186. {
  187. // Ensure that the buffer texture is using the correct buffer as storage.
  188. // Buffers are frequently re-created to accomodate larger data, so we need to re-bind
  189. // to ensure we're not using a old buffer that was already deleted.
  190. _context.Methods.BufferManager.SetBufferTextureStorage(hostTexture, texture.Address, texture.Size, _isCompute);
  191. }
  192. Sampler sampler = _samplerPool.Get(samplerId);
  193. ISampler hostSampler = sampler?.HostSampler;
  194. if (_textureState[stageIndex][index].Sampler != hostSampler || _rebind)
  195. {
  196. _textureState[stageIndex][index].Sampler = hostSampler;
  197. _context.Renderer.Pipeline.SetSampler(index, stage, hostSampler);
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// Ensures that the image bindings are visible to the host GPU.
  203. /// Note: this actually performs the binding using the host graphics API.
  204. /// </summary>
  205. /// <param name="pool">The current texture pool</param>
  206. /// <param name="stage">The shader stage using the textures to be bound</param>
  207. /// <param name="stageIndex">The stage number of the specified shader stage</param>
  208. private void CommitImageBindings(TexturePool pool, ShaderStage stage, int stageIndex)
  209. {
  210. if (_imageBindings[stageIndex] == null)
  211. {
  212. return;
  213. }
  214. for (int index = 0; index < _imageBindings[stageIndex].Length; index++)
  215. {
  216. TextureBindingInfo binding = _imageBindings[stageIndex][index];
  217. int packedId = ReadPackedId(stageIndex, binding.Handle, _textureBufferIndex);
  218. int textureId = UnpackTextureId(packedId);
  219. Texture texture = pool.Get(textureId);
  220. ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
  221. if (_imageState[stageIndex][index].Texture != hostTexture || _rebind)
  222. {
  223. _imageState[stageIndex][index].Texture = hostTexture;
  224. _context.Renderer.Pipeline.SetImage(index, stage, hostTexture);
  225. }
  226. }
  227. }
  228. /// <summary>
  229. /// Gets the texture descriptor for a given texture handle.
  230. /// </summary>
  231. /// <param name="state">The current GPU state</param>
  232. /// <param name="stageIndex">The stage number where the texture is bound</param>
  233. /// <param name="handle">The texture handle</param>
  234. /// <returns>The texture descriptor for the specified texture</returns>
  235. public TextureDescriptor GetTextureDescriptor(GpuState state, int stageIndex, int handle)
  236. {
  237. int packedId = ReadPackedId(stageIndex, handle, state.Get<int>(MethodOffset.TextureBufferIndex));
  238. int textureId = UnpackTextureId(packedId);
  239. var poolState = state.Get<PoolState>(MethodOffset.TexturePoolState);
  240. ulong poolAddress = _context.MemoryManager.Translate(poolState.Address.Pack());
  241. TexturePool texturePool = _texturePoolCache.FindOrCreate(poolAddress, poolState.MaximumId);
  242. return texturePool.GetDescriptor(textureId);
  243. }
  244. /// <summary>
  245. /// Reads a packed texture and sampler ID (basically, the real texture handle)
  246. /// from the texture constant buffer.
  247. /// </summary>
  248. /// <param name="stageIndex">The number of the shader stage where the texture is bound</param>
  249. /// <param name="wordOffset">A word offset of the handle on the buffer (the "fake" shader handle)</param>
  250. /// <param name="textureBufferIndex">Index of the constant buffer holding the texture handles</param>
  251. /// <returns>The packed texture and sampler ID (the real texture handle)</returns>
  252. private int ReadPackedId(int stageIndex, int wordOffset, int textureBufferIndex)
  253. {
  254. ulong address;
  255. var bufferManager = _context.Methods.BufferManager;
  256. if (_isCompute)
  257. {
  258. address = bufferManager.GetComputeUniformBufferAddress(textureBufferIndex);
  259. }
  260. else
  261. {
  262. address = bufferManager.GetGraphicsUniformBufferAddress(stageIndex, textureBufferIndex);
  263. }
  264. address += (uint)wordOffset * 4;
  265. return BitConverter.ToInt32(_context.PhysicalMemory.GetSpan(address, 4));
  266. }
  267. /// <summary>
  268. /// Unpacks the texture ID from the real texture handle.
  269. /// </summary>
  270. /// <param name="packedId">The real texture handle</param>
  271. /// <returns>The texture ID</returns>
  272. private static int UnpackTextureId(int packedId)
  273. {
  274. return (packedId >> 0) & 0xfffff;
  275. }
  276. /// <summary>
  277. /// Unpacks the sampler ID from the real texture handle.
  278. /// </summary>
  279. /// <param name="packedId">The real texture handle</param>
  280. /// <returns>The sampler ID</returns>
  281. private static int UnpackSamplerId(int packedId)
  282. {
  283. return (packedId >> 20) & 0xfff;
  284. }
  285. /// <summary>
  286. /// Invalidates a range of memory on all GPU resource pools (both texture and sampler pools).
  287. /// </summary>
  288. /// <param name="address">Start address of the range to invalidate</param>
  289. /// <param name="size">Size of the range to invalidate</param>
  290. public void InvalidatePoolRange(ulong address, ulong size)
  291. {
  292. _samplerPool?.InvalidateRange(address, size);
  293. _texturePoolCache.InvalidateRange(address, size);
  294. }
  295. /// <summary>
  296. /// Force all bound textures and images to be rebound the next time CommitBindings is called.
  297. /// </summary>
  298. public void Rebind()
  299. {
  300. _rebind = true;
  301. }
  302. }
  303. }