TextureBindingsManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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);
  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. Sampler sampler = _samplerPool.Get(samplerId);
  186. ISampler hostSampler = sampler?.HostSampler;
  187. if (_textureState[stageIndex][index].Sampler != hostSampler || _rebind)
  188. {
  189. _textureState[stageIndex][index].Sampler = hostSampler;
  190. _context.Renderer.Pipeline.SetSampler(index, stage, hostSampler);
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// Ensures that the image bindings are visible to the host GPU.
  196. /// Note: this actually performs the binding using the host graphics API.
  197. /// </summary>
  198. /// <param name="pool">The current texture pool</param>
  199. /// <param name="stage">The shader stage using the textures to be bound</param>
  200. /// <param name="stageIndex">The stage number of the specified shader stage</param>
  201. private void CommitImageBindings(TexturePool pool, ShaderStage stage, int stageIndex)
  202. {
  203. if (_imageBindings[stageIndex] == null)
  204. {
  205. return;
  206. }
  207. for (int index = 0; index < _imageBindings[stageIndex].Length; index++)
  208. {
  209. TextureBindingInfo binding = _imageBindings[stageIndex][index];
  210. int packedId = ReadPackedId(stageIndex, binding.Handle);
  211. int textureId = UnpackTextureId(packedId);
  212. Texture texture = pool.Get(textureId);
  213. ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
  214. if (_imageState[stageIndex][index].Texture != hostTexture || _rebind)
  215. {
  216. _imageState[stageIndex][index].Texture = hostTexture;
  217. _context.Renderer.Pipeline.SetImage(index, stage, hostTexture);
  218. }
  219. }
  220. }
  221. /// <summary>
  222. /// Gets the texture descriptor for a given texture handle.
  223. /// </summary>
  224. /// <param name="state">The current GPU state</param>
  225. /// <param name="stageIndex">The stage number where the texture is bound</param>
  226. /// <param name="handle">The texture handle</param>
  227. /// <returns>The texture descriptor for the specified texture</returns>
  228. public TextureDescriptor GetTextureDescriptor(GpuState state, int stageIndex, int handle)
  229. {
  230. int packedId = ReadPackedId(stageIndex, handle);
  231. int textureId = UnpackTextureId(packedId);
  232. var poolState = state.Get<PoolState>(MethodOffset.TexturePoolState);
  233. ulong poolAddress = _context.MemoryManager.Translate(poolState.Address.Pack());
  234. TexturePool texturePool = _texturePoolCache.FindOrCreate(poolAddress, poolState.MaximumId);
  235. return texturePool.GetDescriptor(textureId);
  236. }
  237. /// <summary>
  238. /// Reads a packed texture and sampler ID (basically, the real texture handle)
  239. /// from the texture constant buffer.
  240. /// </summary>
  241. /// <param name="stageIndex">The number of the shader stage where the texture is bound</param>
  242. /// <param name="wordOffset">A word offset of the handle on the buffer (the "fake" shader handle)</param>
  243. /// <returns>The packed texture and sampler ID (the real texture handle)</returns>
  244. private int ReadPackedId(int stageIndex, int wordOffset)
  245. {
  246. ulong address;
  247. var bufferManager = _context.Methods.BufferManager;
  248. if (_isCompute)
  249. {
  250. address = bufferManager.GetComputeUniformBufferAddress(_textureBufferIndex);
  251. }
  252. else
  253. {
  254. address = bufferManager.GetGraphicsUniformBufferAddress(stageIndex, _textureBufferIndex);
  255. }
  256. address += (uint)wordOffset * 4;
  257. return BitConverter.ToInt32(_context.PhysicalMemory.GetSpan(address, 4));
  258. }
  259. /// <summary>
  260. /// Unpacks the texture ID from the real texture handle.
  261. /// </summary>
  262. /// <param name="packedId">The real texture handle</param>
  263. /// <returns>The texture ID</returns>
  264. private static int UnpackTextureId(int packedId)
  265. {
  266. return (packedId >> 0) & 0xfffff;
  267. }
  268. /// <summary>
  269. /// Unpacks the sampler ID from the real texture handle.
  270. /// </summary>
  271. /// <param name="packedId">The real texture handle</param>
  272. /// <returns>The sampler ID</returns>
  273. private static int UnpackSamplerId(int packedId)
  274. {
  275. return (packedId >> 20) & 0xfff;
  276. }
  277. /// <summary>
  278. /// Invalidates a range of memory on all GPU resource pools (both texture and sampler pools).
  279. /// </summary>
  280. /// <param name="address">Start address of the range to invalidate</param>
  281. /// <param name="size">Size of the range to invalidate</param>
  282. public void InvalidatePoolRange(ulong address, ulong size)
  283. {
  284. _samplerPool?.InvalidateRange(address, size);
  285. _texturePoolCache.InvalidateRange(address, size);
  286. }
  287. /// <summary>
  288. /// Force all bound textures and images to be rebound the next time CommitBindings is called.
  289. /// </summary>
  290. public void Rebind()
  291. {
  292. _rebind = true;
  293. }
  294. }
  295. }