TextureBindingsManager.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. class TextureBindingsManager
  9. {
  10. private GpuContext _context;
  11. private bool _isCompute;
  12. private SamplerPool _samplerPool;
  13. private SamplerIndex _samplerIndex;
  14. private ulong _texturePoolAddress;
  15. private int _texturePoolMaximumId;
  16. private TexturePoolCache _texturePoolCache;
  17. private TextureBindingInfo[][] _textureBindings;
  18. private TextureBindingInfo[][] _imageBindings;
  19. private struct TextureStatePerStage
  20. {
  21. public ITexture Texture;
  22. public ISampler Sampler;
  23. }
  24. private TextureStatePerStage[][] _textureState;
  25. private TextureStatePerStage[][] _imageState;
  26. private int _textureBufferIndex;
  27. private bool _rebind;
  28. public TextureBindingsManager(GpuContext context, TexturePoolCache texturePoolCache, bool isCompute)
  29. {
  30. _context = context;
  31. _texturePoolCache = texturePoolCache;
  32. _isCompute = isCompute;
  33. int stages = isCompute ? 1 : Constants.TotalShaderStages;
  34. _textureBindings = new TextureBindingInfo[stages][];
  35. _imageBindings = new TextureBindingInfo[stages][];
  36. _textureState = new TextureStatePerStage[stages][];
  37. _imageState = new TextureStatePerStage[stages][];
  38. }
  39. public void SetTextures(int stage, TextureBindingInfo[] bindings)
  40. {
  41. _textureBindings[stage] = bindings;
  42. _textureState[stage] = new TextureStatePerStage[bindings.Length];
  43. }
  44. public void SetImages(int stage, TextureBindingInfo[] bindings)
  45. {
  46. _imageBindings[stage] = bindings;
  47. _imageState[stage] = new TextureStatePerStage[bindings.Length];
  48. }
  49. public void SetTextureBufferIndex(int index)
  50. {
  51. _textureBufferIndex = index;
  52. }
  53. public void SetSamplerPool(ulong gpuVa, int maximumId, SamplerIndex samplerIndex)
  54. {
  55. ulong address = _context.MemoryManager.Translate(gpuVa);
  56. if (_samplerPool != null)
  57. {
  58. if (_samplerPool.Address == address && _samplerPool.MaximumId >= maximumId)
  59. {
  60. return;
  61. }
  62. _samplerPool.Dispose();
  63. }
  64. _samplerPool = new SamplerPool(_context, address, maximumId);
  65. _samplerIndex = samplerIndex;
  66. }
  67. public void SetTexturePool(ulong gpuVa, int maximumId)
  68. {
  69. ulong address = _context.MemoryManager.Translate(gpuVa);
  70. _texturePoolAddress = address;
  71. _texturePoolMaximumId = maximumId;
  72. }
  73. public void CommitBindings()
  74. {
  75. TexturePool texturePool = _texturePoolCache.FindOrCreate(
  76. _texturePoolAddress,
  77. _texturePoolMaximumId);
  78. if (_isCompute)
  79. {
  80. CommitTextureBindings(texturePool, ShaderStage.Compute, 0);
  81. CommitImageBindings (texturePool, ShaderStage.Compute, 0);
  82. }
  83. else
  84. {
  85. for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
  86. {
  87. int stageIndex = (int)stage - 1;
  88. CommitTextureBindings(texturePool, stage, stageIndex);
  89. CommitImageBindings (texturePool, stage, stageIndex);
  90. }
  91. }
  92. _rebind = false;
  93. }
  94. private void CommitTextureBindings(TexturePool pool, ShaderStage stage, int stageIndex)
  95. {
  96. if (_textureBindings[stageIndex] == null)
  97. {
  98. return;
  99. }
  100. for (int index = 0; index < _textureBindings[stageIndex].Length; index++)
  101. {
  102. TextureBindingInfo binding = _textureBindings[stageIndex][index];
  103. int packedId;
  104. if (binding.IsBindless)
  105. {
  106. ulong address;
  107. var bufferManager = _context.Methods.BufferManager;
  108. if (_isCompute)
  109. {
  110. address = bufferManager.GetComputeUniformBufferAddress(binding.CbufSlot);
  111. }
  112. else
  113. {
  114. address = bufferManager.GetGraphicsUniformBufferAddress(stageIndex, binding.CbufSlot);
  115. }
  116. packedId = MemoryMarshal.Cast<byte, int>(_context.PhysicalMemory.Read(address + (ulong)binding.CbufOffset * 4, 4))[0];
  117. }
  118. else
  119. {
  120. packedId = ReadPackedId(stageIndex, binding.Handle);
  121. }
  122. int textureId = UnpackTextureId(packedId);
  123. int samplerId;
  124. if (_samplerIndex == SamplerIndex.ViaHeaderIndex)
  125. {
  126. samplerId = textureId;
  127. }
  128. else
  129. {
  130. samplerId = UnpackSamplerId(packedId);
  131. }
  132. Texture texture = pool.Get(textureId);
  133. ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
  134. if (_textureState[stageIndex][index].Texture != hostTexture || _rebind)
  135. {
  136. _textureState[stageIndex][index].Texture = hostTexture;
  137. _context.Renderer.Pipeline.BindTexture(index, stage, hostTexture);
  138. }
  139. Sampler sampler = _samplerPool.Get(samplerId);
  140. ISampler hostSampler = sampler?.HostSampler;
  141. if (_textureState[stageIndex][index].Sampler != hostSampler || _rebind)
  142. {
  143. _textureState[stageIndex][index].Sampler = hostSampler;
  144. _context.Renderer.Pipeline.BindSampler(index, stage, hostSampler);
  145. }
  146. }
  147. }
  148. private void CommitImageBindings(TexturePool pool, ShaderStage stage, int stageIndex)
  149. {
  150. if (_imageBindings[stageIndex] == null)
  151. {
  152. return;
  153. }
  154. for (int index = 0; index < _imageBindings[stageIndex].Length; index++)
  155. {
  156. TextureBindingInfo binding = _imageBindings[stageIndex][index];
  157. int packedId = ReadPackedId(stageIndex, binding.Handle);
  158. int textureId = UnpackTextureId(packedId);
  159. Texture texture = pool.Get(textureId);
  160. ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
  161. if (_imageState[stageIndex][index].Texture != hostTexture || _rebind)
  162. {
  163. _imageState[stageIndex][index].Texture = hostTexture;
  164. _context.Renderer.Pipeline.BindImage(index, stage, hostTexture);
  165. }
  166. }
  167. }
  168. public TextureDescriptor GetTextureDescriptor(GpuState state, int stageIndex, int handle)
  169. {
  170. int packedId = ReadPackedId(stageIndex, handle);
  171. int textureId = UnpackTextureId(packedId);
  172. var poolState = state.Get<PoolState>(MethodOffset.TexturePoolState);
  173. ulong poolAddress = _context.MemoryManager.Translate(poolState.Address.Pack());
  174. TexturePool texturePool = _texturePoolCache.FindOrCreate(poolAddress, poolState.MaximumId);
  175. return texturePool.GetDescriptor(textureId);
  176. }
  177. private int ReadPackedId(int stage, int wordOffset)
  178. {
  179. ulong address;
  180. var bufferManager = _context.Methods.BufferManager;
  181. if (_isCompute)
  182. {
  183. address = bufferManager.GetComputeUniformBufferAddress(_textureBufferIndex);
  184. }
  185. else
  186. {
  187. address = bufferManager.GetGraphicsUniformBufferAddress(stage, _textureBufferIndex);
  188. }
  189. address += (uint)wordOffset * 4;
  190. return BitConverter.ToInt32(_context.PhysicalMemory.Read(address, 4));
  191. }
  192. private static int UnpackTextureId(int packedId)
  193. {
  194. return (packedId >> 0) & 0xfffff;
  195. }
  196. private static int UnpackSamplerId(int packedId)
  197. {
  198. return (packedId >> 20) & 0xfff;
  199. }
  200. public void InvalidatePoolRange(ulong address, ulong size)
  201. {
  202. _samplerPool?.InvalidateRange(address, size);
  203. _texturePoolCache.InvalidateRange(address, size);
  204. }
  205. public void Rebind()
  206. {
  207. _rebind = true;
  208. }
  209. }
  210. }