TextureBindingsManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.State;
  3. using Ryujinx.Graphics.Shader;
  4. namespace Ryujinx.Graphics.Gpu.Image
  5. {
  6. /// <summary>
  7. /// Texture bindings manager.
  8. /// </summary>
  9. class TextureBindingsManager
  10. {
  11. private const int HandleHigh = 16;
  12. private const int HandleMask = (1 << HandleHigh) - 1;
  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. bool changed = false;
  147. for (int index = 0; index < _textureBindings[stageIndex].Length; index++)
  148. {
  149. TextureBindingInfo binding = _textureBindings[stageIndex][index];
  150. int packedId;
  151. if (binding.IsBindless)
  152. {
  153. ulong address;
  154. var bufferManager = _context.Methods.BufferManager;
  155. if (_isCompute)
  156. {
  157. address = bufferManager.GetComputeUniformBufferAddress(binding.CbufSlot);
  158. }
  159. else
  160. {
  161. address = bufferManager.GetGraphicsUniformBufferAddress(stageIndex, binding.CbufSlot);
  162. }
  163. packedId = _context.PhysicalMemory.Read<int>(address + (ulong)binding.CbufOffset * 4);
  164. }
  165. else
  166. {
  167. packedId = ReadPackedId(stageIndex, binding.Handle, _textureBufferIndex);
  168. }
  169. int textureId = UnpackTextureId(packedId);
  170. int samplerId;
  171. if (_samplerIndex == SamplerIndex.ViaHeaderIndex)
  172. {
  173. samplerId = textureId;
  174. }
  175. else
  176. {
  177. samplerId = UnpackSamplerId(packedId);
  178. }
  179. Texture texture = pool.Get(textureId);
  180. if ((binding.Flags & TextureUsageFlags.ResScaleUnsupported) != 0)
  181. {
  182. texture?.BlacklistScale();
  183. }
  184. ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
  185. if (_textureState[stageIndex][index].Texture != hostTexture || _rebind)
  186. {
  187. _textureState[stageIndex][index].Texture = hostTexture;
  188. _context.Renderer.Pipeline.SetTexture(index, stage, hostTexture);
  189. changed = true;
  190. }
  191. if (hostTexture != null && texture.Info.Target == Target.TextureBuffer)
  192. {
  193. // Ensure that the buffer texture is using the correct buffer as storage.
  194. // Buffers are frequently re-created to accomodate larger data, so we need to re-bind
  195. // to ensure we're not using a old buffer that was already deleted.
  196. _context.Methods.BufferManager.SetBufferTextureStorage(hostTexture, texture.Address, texture.Size, _isCompute);
  197. }
  198. Sampler sampler = _samplerPool.Get(samplerId);
  199. ISampler hostSampler = sampler?.HostSampler;
  200. if (_textureState[stageIndex][index].Sampler != hostSampler || _rebind)
  201. {
  202. _textureState[stageIndex][index].Sampler = hostSampler;
  203. _context.Renderer.Pipeline.SetSampler(index, stage, hostSampler);
  204. }
  205. }
  206. if (changed)
  207. {
  208. _context.Renderer.Pipeline.UpdateRenderScale(stage, _textureBindings[stageIndex].Length);
  209. }
  210. }
  211. /// <summary>
  212. /// Ensures that the image bindings are visible to the host GPU.
  213. /// Note: this actually performs the binding using the host graphics API.
  214. /// </summary>
  215. /// <param name="pool">The current texture pool</param>
  216. /// <param name="stage">The shader stage using the textures to be bound</param>
  217. /// <param name="stageIndex">The stage number of the specified shader stage</param>
  218. private void CommitImageBindings(TexturePool pool, ShaderStage stage, int stageIndex)
  219. {
  220. if (_imageBindings[stageIndex] == null)
  221. {
  222. return;
  223. }
  224. for (int index = 0; index < _imageBindings[stageIndex].Length; index++)
  225. {
  226. TextureBindingInfo binding = _imageBindings[stageIndex][index];
  227. int packedId = ReadPackedId(stageIndex, binding.Handle, _textureBufferIndex);
  228. int textureId = UnpackTextureId(packedId);
  229. Texture texture = pool.Get(textureId);
  230. if ((binding.Flags & TextureUsageFlags.ResScaleUnsupported) != 0)
  231. {
  232. texture?.BlacklistScale();
  233. }
  234. ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
  235. if (_imageState[stageIndex][index].Texture != hostTexture || _rebind)
  236. {
  237. _imageState[stageIndex][index].Texture = hostTexture;
  238. _context.Renderer.Pipeline.SetImage(index, stage, hostTexture);
  239. }
  240. }
  241. }
  242. /// <summary>
  243. /// Gets the texture descriptor for a given texture handle.
  244. /// </summary>
  245. /// <param name="state">The current GPU state</param>
  246. /// <param name="stageIndex">The stage number where the texture is bound</param>
  247. /// <param name="handle">The texture handle</param>
  248. /// <returns>The texture descriptor for the specified texture</returns>
  249. public TextureDescriptor GetTextureDescriptor(GpuState state, int stageIndex, int handle)
  250. {
  251. int packedId = ReadPackedId(stageIndex, handle, state.Get<int>(MethodOffset.TextureBufferIndex));
  252. int textureId = UnpackTextureId(packedId);
  253. var poolState = state.Get<PoolState>(MethodOffset.TexturePoolState);
  254. ulong poolAddress = _context.MemoryManager.Translate(poolState.Address.Pack());
  255. TexturePool texturePool = _texturePoolCache.FindOrCreate(poolAddress, poolState.MaximumId);
  256. return texturePool.GetDescriptor(textureId);
  257. }
  258. /// <summary>
  259. /// Reads a packed texture and sampler ID (basically, the real texture handle)
  260. /// from the texture constant buffer.
  261. /// </summary>
  262. /// <param name="stageIndex">The number of the shader stage where the texture is bound</param>
  263. /// <param name="wordOffset">A word offset of the handle on the buffer (the "fake" shader handle)</param>
  264. /// <param name="textureBufferIndex">Index of the constant buffer holding the texture handles</param>
  265. /// <returns>The packed texture and sampler ID (the real texture handle)</returns>
  266. private int ReadPackedId(int stageIndex, int wordOffset, int textureBufferIndex)
  267. {
  268. ulong address;
  269. var bufferManager = _context.Methods.BufferManager;
  270. if (_isCompute)
  271. {
  272. address = bufferManager.GetComputeUniformBufferAddress(textureBufferIndex);
  273. }
  274. else
  275. {
  276. address = bufferManager.GetGraphicsUniformBufferAddress(stageIndex, textureBufferIndex);
  277. }
  278. int handle = _context.PhysicalMemory.Read<int>(address + (ulong)(wordOffset & HandleMask) * 4);
  279. // The "wordOffset" (which is really the immediate value used on texture instructions on the shader)
  280. // is a 13-bit value. However, in order to also support separate samplers and textures (which uses
  281. // bindless textures on the shader), we extend it with another value on the higher 16 bits with
  282. // another offset for the sampler.
  283. // The shader translator has code to detect separate texture and sampler uses with a bindless texture,
  284. // turn that into a regular texture access and produce those special handles with values on the higher 16 bits.
  285. if (wordOffset >> HandleHigh != 0)
  286. {
  287. handle |= _context.PhysicalMemory.Read<int>(address + (ulong)(wordOffset >> HandleHigh) * 4);
  288. }
  289. return handle;
  290. }
  291. /// <summary>
  292. /// Unpacks the texture ID from the real texture handle.
  293. /// </summary>
  294. /// <param name="packedId">The real texture handle</param>
  295. /// <returns>The texture ID</returns>
  296. private static int UnpackTextureId(int packedId)
  297. {
  298. return (packedId >> 0) & 0xfffff;
  299. }
  300. /// <summary>
  301. /// Unpacks the sampler ID from the real texture handle.
  302. /// </summary>
  303. /// <param name="packedId">The real texture handle</param>
  304. /// <returns>The sampler ID</returns>
  305. private static int UnpackSamplerId(int packedId)
  306. {
  307. return (packedId >> 20) & 0xfff;
  308. }
  309. /// <summary>
  310. /// Invalidates a range of memory on all GPU resource pools (both texture and sampler pools).
  311. /// </summary>
  312. /// <param name="address">Start address of the range to invalidate</param>
  313. /// <param name="size">Size of the range to invalidate</param>
  314. public void InvalidatePoolRange(ulong address, ulong size)
  315. {
  316. _samplerPool?.InvalidateRange(address, size);
  317. _texturePoolCache.InvalidateRange(address, size);
  318. }
  319. /// <summary>
  320. /// Force all bound textures and images to be rebound the next time CommitBindings is called.
  321. /// </summary>
  322. public void Rebind()
  323. {
  324. _rebind = true;
  325. }
  326. }
  327. }