TextureBindingsManager.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Shader;
  3. using System;
  4. namespace Ryujinx.Graphics.Gpu.Image
  5. {
  6. class TextureBindingsManager
  7. {
  8. private GpuContext _context;
  9. private bool _isCompute;
  10. private SamplerPool _samplerPool;
  11. private ulong _texturePoolAddress;
  12. private int _texturePoolMaximumId;
  13. private TexturePoolCache _texturePoolCache;
  14. private TextureBindingInfo[][] _textureBindings;
  15. private TextureBindingInfo[][] _imageBindings;
  16. private struct TextureStatePerStage
  17. {
  18. public ITexture Texture;
  19. public ISampler Sampler;
  20. }
  21. private TextureStatePerStage[][] _textureState;
  22. private TextureStatePerStage[][] _imageState;
  23. private int _textureBufferIndex;
  24. private bool _rebind;
  25. public TextureBindingsManager(GpuContext context, bool isCompute)
  26. {
  27. _context = context;
  28. _isCompute = isCompute;
  29. _texturePoolCache = new TexturePoolCache(context);
  30. int stages = isCompute ? 1 : Constants.TotalShaderStages;
  31. _textureBindings = new TextureBindingInfo[stages][];
  32. _imageBindings = new TextureBindingInfo[stages][];
  33. _textureState = new TextureStatePerStage[stages][];
  34. _imageState = new TextureStatePerStage[stages][];
  35. }
  36. public void SetTextures(int stage, TextureBindingInfo[] bindings)
  37. {
  38. _textureBindings[stage] = bindings;
  39. _textureState[stage] = new TextureStatePerStage[bindings.Length];
  40. }
  41. public void SetImages(int stage, TextureBindingInfo[] bindings)
  42. {
  43. _imageBindings[stage] = bindings;
  44. _imageState[stage] = new TextureStatePerStage[bindings.Length];
  45. }
  46. public void SetTextureBufferIndex(int index)
  47. {
  48. _textureBufferIndex = index;
  49. }
  50. public void SetSamplerPool(ulong gpuVa, int maximumId)
  51. {
  52. ulong address = _context.MemoryManager.Translate(gpuVa);
  53. if (_samplerPool != null)
  54. {
  55. if (_samplerPool.Address == address)
  56. {
  57. return;
  58. }
  59. _samplerPool.Dispose();
  60. }
  61. _samplerPool = new SamplerPool(_context, address, maximumId);
  62. }
  63. public void SetTexturePool(ulong gpuVa, int maximumId)
  64. {
  65. ulong address = _context.MemoryManager.Translate(gpuVa);
  66. _texturePoolAddress = address;
  67. _texturePoolMaximumId = maximumId;
  68. }
  69. public void CommitBindings()
  70. {
  71. TexturePool texturePool = _texturePoolCache.FindOrCreate(
  72. _texturePoolAddress,
  73. _texturePoolMaximumId);
  74. if (_isCompute)
  75. {
  76. CommitTextureBindings(texturePool, ShaderStage.Compute, 0);
  77. CommitImageBindings (texturePool, ShaderStage.Compute, 0);
  78. }
  79. else
  80. {
  81. for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
  82. {
  83. int stageIndex = (int)stage - 1;
  84. CommitTextureBindings(texturePool, stage, stageIndex);
  85. CommitImageBindings (texturePool, stage, stageIndex);
  86. }
  87. }
  88. _rebind = false;
  89. }
  90. private void CommitTextureBindings(TexturePool pool, ShaderStage stage, int stageIndex)
  91. {
  92. if (_textureBindings[stageIndex] == null)
  93. {
  94. return;
  95. }
  96. for (int index = 0; index < _textureBindings[stageIndex].Length; index++)
  97. {
  98. TextureBindingInfo binding = _textureBindings[stageIndex][index];
  99. int packedId = ReadPackedId(stageIndex, binding.Handle);
  100. int textureId = UnpackTextureId(packedId);
  101. int samplerId = UnpackSamplerId(packedId);
  102. Texture texture = pool.Get(textureId);
  103. ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
  104. if (_textureState[stageIndex][index].Texture != hostTexture || _rebind)
  105. {
  106. _textureState[stageIndex][index].Texture = hostTexture;
  107. _context.Renderer.Pipeline.BindTexture(index, stage, hostTexture);
  108. }
  109. Sampler sampler = _samplerPool.Get(samplerId);
  110. ISampler hostSampler = sampler?.HostSampler;
  111. if (_textureState[stageIndex][index].Sampler != hostSampler || _rebind)
  112. {
  113. _textureState[stageIndex][index].Sampler = hostSampler;
  114. _context.Renderer.Pipeline.BindSampler(index, stage, hostSampler);
  115. }
  116. }
  117. }
  118. private void CommitImageBindings(TexturePool pool, ShaderStage stage, int stageIndex)
  119. {
  120. if (_imageBindings[stageIndex] == null)
  121. {
  122. return;
  123. }
  124. for (int index = 0; index < _imageBindings[stageIndex].Length; index++)
  125. {
  126. TextureBindingInfo binding = _imageBindings[stageIndex][index];
  127. int packedId = ReadPackedId(stageIndex, binding.Handle);
  128. int textureId = UnpackTextureId(packedId);
  129. Texture texture = pool.Get(textureId);
  130. ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
  131. if (_imageState[stageIndex][index].Texture != hostTexture || _rebind)
  132. {
  133. _imageState[stageIndex][index].Texture = hostTexture;
  134. _context.Renderer.Pipeline.BindImage(index, stage, hostTexture);
  135. }
  136. }
  137. }
  138. private int ReadPackedId(int stage, int wordOffset)
  139. {
  140. ulong address;
  141. var bufferManager = _context.Methods.BufferManager;
  142. if (_isCompute)
  143. {
  144. address = bufferManager.GetComputeUniformBufferAddress(_textureBufferIndex);
  145. }
  146. else
  147. {
  148. address = bufferManager.GetGraphicsUniformBufferAddress(stage, _textureBufferIndex);
  149. }
  150. address += (uint)wordOffset * 4;
  151. return BitConverter.ToInt32(_context.PhysicalMemory.Read(address, 4));
  152. }
  153. private static int UnpackTextureId(int packedId)
  154. {
  155. return (packedId >> 0) & 0xfffff;
  156. }
  157. private static int UnpackSamplerId(int packedId)
  158. {
  159. return (packedId >> 20) & 0xfff;
  160. }
  161. public void InvalidatePoolRange(ulong address, ulong size)
  162. {
  163. _samplerPool?.InvalidateRange(address, size);
  164. _texturePoolCache.InvalidateRange(address, size);
  165. }
  166. public void Rebind()
  167. {
  168. _rebind = true;
  169. }
  170. }
  171. }