ComputeClass.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using Ryujinx.Graphics.Device;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Engine.InlineToMemory;
  4. using Ryujinx.Graphics.Gpu.Engine.Threed;
  5. using Ryujinx.Graphics.Gpu.Engine.Types;
  6. using Ryujinx.Graphics.Gpu.Image;
  7. using Ryujinx.Graphics.Gpu.Shader;
  8. using Ryujinx.Graphics.Shader;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Runtime.CompilerServices;
  12. namespace Ryujinx.Graphics.Gpu.Engine.Compute
  13. {
  14. /// <summary>
  15. /// Represents a compute engine class.
  16. /// </summary>
  17. class ComputeClass : IDeviceState
  18. {
  19. private readonly GpuContext _context;
  20. private readonly GpuChannel _channel;
  21. private readonly ThreedClass _3dEngine;
  22. private readonly DeviceState<ComputeClassState> _state;
  23. private readonly InlineToMemoryClass _i2mClass;
  24. /// <summary>
  25. /// Creates a new instance of the compute engine class.
  26. /// </summary>
  27. /// <param name="context">GPU context</param>
  28. /// <param name="channel">GPU channel</param>
  29. /// <param name="threedEngine">3D engine</param>
  30. public ComputeClass(GpuContext context, GpuChannel channel, ThreedClass threedEngine)
  31. {
  32. _context = context;
  33. _channel = channel;
  34. _3dEngine = threedEngine;
  35. _state = new DeviceState<ComputeClassState>(new Dictionary<string, RwCallback>
  36. {
  37. { nameof(ComputeClassState.LaunchDma), new RwCallback(LaunchDma, null) },
  38. { nameof(ComputeClassState.LoadInlineData), new RwCallback(LoadInlineData, null) },
  39. { nameof(ComputeClassState.SendSignalingPcasB), new RwCallback(SendSignalingPcasB, null) }
  40. });
  41. _i2mClass = new InlineToMemoryClass(context, channel, initializeState: false);
  42. }
  43. /// <summary>
  44. /// Reads data from the class registers.
  45. /// </summary>
  46. /// <param name="offset">Register byte offset</param>
  47. /// <returns>Data at the specified offset</returns>
  48. public int Read(int offset) => _state.Read(offset);
  49. /// <summary>
  50. /// Writes data to the class registers.
  51. /// </summary>
  52. /// <param name="offset">Register byte offset</param>
  53. /// <param name="data">Data to be written</param>
  54. public void Write(int offset, int data) => _state.Write(offset, data);
  55. /// <summary>
  56. /// Launches the Inline-to-Memory DMA copy operation.
  57. /// </summary>
  58. /// <param name="argument">Method call argument</param>
  59. private void LaunchDma(int argument)
  60. {
  61. _i2mClass.LaunchDma(ref Unsafe.As<ComputeClassState, InlineToMemoryClassState>(ref _state.State), argument);
  62. }
  63. /// <summary>
  64. /// Pushes a block of data to the Inline-to-Memory engine.
  65. /// </summary>
  66. /// <param name="data">Data to push</param>
  67. public void LoadInlineData(ReadOnlySpan<int> data)
  68. {
  69. _i2mClass.LoadInlineData(data);
  70. }
  71. /// <summary>
  72. /// Pushes a word of data to the Inline-to-Memory engine.
  73. /// </summary>
  74. /// <param name="argument">Method call argument</param>
  75. private void LoadInlineData(int argument)
  76. {
  77. _i2mClass.LoadInlineData(argument);
  78. }
  79. /// <summary>
  80. /// Performs the compute dispatch operation.
  81. /// </summary>
  82. /// <param name="argument">Method call argument</param>
  83. private void SendSignalingPcasB(int argument)
  84. {
  85. var memoryManager = _channel.MemoryManager;
  86. _3dEngine.FlushUboDirty();
  87. uint qmdAddress = _state.State.SendPcasA;
  88. var qmd = _channel.MemoryManager.Read<ComputeQmd>((ulong)qmdAddress << 8);
  89. ulong shaderGpuVa = ((ulong)_state.State.SetProgramRegionAAddressUpper << 32) | _state.State.SetProgramRegionB;
  90. shaderGpuVa += (uint)qmd.ProgramOffset;
  91. int localMemorySize = qmd.ShaderLocalMemoryLowSize + qmd.ShaderLocalMemoryHighSize;
  92. int sharedMemorySize = Math.Min(qmd.SharedMemorySize, _context.Capabilities.MaximumComputeSharedMemorySize);
  93. for (int index = 0; index < Constants.TotalCpUniformBuffers; index++)
  94. {
  95. if (!qmd.ConstantBufferValid(index))
  96. {
  97. continue;
  98. }
  99. ulong gpuVa = (uint)qmd.ConstantBufferAddrLower(index) | (ulong)qmd.ConstantBufferAddrUpper(index) << 32;
  100. ulong size = (ulong)qmd.ConstantBufferSize(index);
  101. _channel.BufferManager.SetComputeUniformBuffer(index, gpuVa, size);
  102. }
  103. ulong samplerPoolGpuVa = ((ulong)_state.State.SetTexSamplerPoolAOffsetUpper << 32) | _state.State.SetTexSamplerPoolB;
  104. ulong texturePoolGpuVa = ((ulong)_state.State.SetTexHeaderPoolAOffsetUpper << 32) | _state.State.SetTexHeaderPoolB;
  105. GpuChannelPoolState poolState = new GpuChannelPoolState(
  106. texturePoolGpuVa,
  107. _state.State.SetTexHeaderPoolCMaximumIndex,
  108. _state.State.SetBindlessTextureConstantBufferSlotSelect);
  109. GpuChannelComputeState computeState = new GpuChannelComputeState(
  110. qmd.CtaThreadDimension0,
  111. qmd.CtaThreadDimension1,
  112. qmd.CtaThreadDimension2,
  113. localMemorySize,
  114. sharedMemorySize);
  115. CachedShaderProgram cs = memoryManager.Physical.ShaderCache.GetComputeShader(_channel, poolState, computeState, shaderGpuVa);
  116. _context.Renderer.Pipeline.SetProgram(cs.HostProgram);
  117. _channel.TextureManager.SetComputeSamplerPool(samplerPoolGpuVa, _state.State.SetTexSamplerPoolCMaximumIndex, qmd.SamplerIndex);
  118. _channel.TextureManager.SetComputeTexturePool(texturePoolGpuVa, _state.State.SetTexHeaderPoolCMaximumIndex);
  119. _channel.TextureManager.SetComputeTextureBufferIndex(_state.State.SetBindlessTextureConstantBufferSlotSelect);
  120. ShaderProgramInfo info = cs.Shaders[0].Info;
  121. for (int index = 0; index < info.CBuffers.Count; index++)
  122. {
  123. BufferDescriptor cb = info.CBuffers[index];
  124. // NVN uses the "hardware" constant buffer for anything that is less than 8,
  125. // and those are already bound above.
  126. // Anything greater than or equal to 8 uses the emulated constant buffers.
  127. // They are emulated using global memory loads.
  128. if (cb.Slot < 8)
  129. {
  130. continue;
  131. }
  132. ulong cbDescAddress = _channel.BufferManager.GetComputeUniformBufferAddress(0);
  133. int cbDescOffset = 0x260 + (cb.Slot - 8) * 0x10;
  134. cbDescAddress += (ulong)cbDescOffset;
  135. SbDescriptor cbDescriptor = _channel.MemoryManager.Physical.Read<SbDescriptor>(cbDescAddress);
  136. _channel.BufferManager.SetComputeUniformBuffer(cb.Slot, cbDescriptor.PackAddress(), (uint)cbDescriptor.Size);
  137. }
  138. for (int index = 0; index < info.SBuffers.Count; index++)
  139. {
  140. BufferDescriptor sb = info.SBuffers[index];
  141. ulong sbDescAddress = _channel.BufferManager.GetComputeUniformBufferAddress(0);
  142. int sbDescOffset = 0x310 + sb.Slot * 0x10;
  143. sbDescAddress += (ulong)sbDescOffset;
  144. SbDescriptor sbDescriptor = _channel.MemoryManager.Physical.Read<SbDescriptor>(sbDescAddress);
  145. _channel.BufferManager.SetComputeStorageBuffer(sb.Slot, sbDescriptor.PackAddress(), (uint)sbDescriptor.Size, sb.Flags);
  146. }
  147. _channel.BufferManager.SetComputeStorageBufferBindings(info.SBuffers);
  148. _channel.BufferManager.SetComputeUniformBufferBindings(info.CBuffers);
  149. int maxTextureBinding = -1;
  150. int maxImageBinding = -1;
  151. TextureBindingInfo[] textureBindings = _channel.TextureManager.RentComputeTextureBindings(info.Textures.Count);
  152. for (int index = 0; index < info.Textures.Count; index++)
  153. {
  154. var descriptor = info.Textures[index];
  155. Target target = ShaderTexture.GetTarget(descriptor.Type);
  156. textureBindings[index] = new TextureBindingInfo(
  157. target,
  158. descriptor.Binding,
  159. descriptor.CbufSlot,
  160. descriptor.HandleIndex,
  161. descriptor.Flags);
  162. if (descriptor.Binding > maxTextureBinding)
  163. {
  164. maxTextureBinding = descriptor.Binding;
  165. }
  166. }
  167. TextureBindingInfo[] imageBindings = _channel.TextureManager.RentComputeImageBindings(info.Images.Count);
  168. for (int index = 0; index < info.Images.Count; index++)
  169. {
  170. var descriptor = info.Images[index];
  171. Target target = ShaderTexture.GetTarget(descriptor.Type);
  172. Format format = ShaderTexture.GetFormat(descriptor.Format);
  173. imageBindings[index] = new TextureBindingInfo(
  174. target,
  175. format,
  176. descriptor.Binding,
  177. descriptor.CbufSlot,
  178. descriptor.HandleIndex,
  179. descriptor.Flags);
  180. if (descriptor.Binding > maxImageBinding)
  181. {
  182. maxImageBinding = descriptor.Binding;
  183. }
  184. }
  185. _channel.TextureManager.SetComputeMaxBindings(maxTextureBinding, maxImageBinding);
  186. // Should never return false for mismatching spec state, since the shader was fetched above.
  187. _channel.TextureManager.CommitComputeBindings(cs.SpecializationState);
  188. _channel.BufferManager.CommitComputeBindings();
  189. _context.Renderer.Pipeline.DispatchCompute(qmd.CtaRasterWidth, qmd.CtaRasterHeight, qmd.CtaRasterDepth);
  190. _3dEngine.ForceShaderUpdate();
  191. }
  192. }
  193. }