ComputeClass.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 word of data to the Inline-to-Memory engine.
  65. /// </summary>
  66. /// <param name="argument">Method call argument</param>
  67. private void LoadInlineData(int argument)
  68. {
  69. _i2mClass.LoadInlineData(argument);
  70. }
  71. /// <summary>
  72. /// Performs the compute dispatch operation.
  73. /// </summary>
  74. /// <param name="argument">Method call argument</param>
  75. private void SendSignalingPcasB(int argument)
  76. {
  77. var memoryManager = _channel.MemoryManager;
  78. _3dEngine.FlushUboDirty();
  79. uint qmdAddress = _state.State.SendPcasA;
  80. var qmd = _channel.MemoryManager.Read<ComputeQmd>((ulong)qmdAddress << 8);
  81. ulong shaderGpuVa = ((ulong)_state.State.SetProgramRegionAAddressUpper << 32) | _state.State.SetProgramRegionB;
  82. shaderGpuVa += (uint)qmd.ProgramOffset;
  83. int localMemorySize = qmd.ShaderLocalMemoryLowSize + qmd.ShaderLocalMemoryHighSize;
  84. int sharedMemorySize = Math.Min(qmd.SharedMemorySize, _context.Capabilities.MaximumComputeSharedMemorySize);
  85. for (int index = 0; index < Constants.TotalCpUniformBuffers; index++)
  86. {
  87. if (!qmd.ConstantBufferValid(index))
  88. {
  89. continue;
  90. }
  91. ulong gpuVa = (uint)qmd.ConstantBufferAddrLower(index) | (ulong)qmd.ConstantBufferAddrUpper(index) << 32;
  92. ulong size = (ulong)qmd.ConstantBufferSize(index);
  93. _channel.BufferManager.SetComputeUniformBuffer(index, gpuVa, size);
  94. }
  95. ulong samplerPoolGpuVa = ((ulong)_state.State.SetTexSamplerPoolAOffsetUpper << 32) | _state.State.SetTexSamplerPoolB;
  96. ulong texturePoolGpuVa = ((ulong)_state.State.SetTexHeaderPoolAOffsetUpper << 32) | _state.State.SetTexHeaderPoolB;
  97. GpuAccessorState gas = new GpuAccessorState(
  98. texturePoolGpuVa,
  99. _state.State.SetTexHeaderPoolCMaximumIndex,
  100. _state.State.SetBindlessTextureConstantBufferSlotSelect,
  101. false,
  102. PrimitiveTopology.Points);
  103. ShaderBundle cs = memoryManager.Physical.ShaderCache.GetComputeShader(
  104. _channel,
  105. gas,
  106. shaderGpuVa,
  107. qmd.CtaThreadDimension0,
  108. qmd.CtaThreadDimension1,
  109. qmd.CtaThreadDimension2,
  110. localMemorySize,
  111. sharedMemorySize);
  112. _context.Renderer.Pipeline.SetProgram(cs.HostProgram);
  113. _channel.TextureManager.SetComputeSamplerPool(samplerPoolGpuVa, _state.State.SetTexSamplerPoolCMaximumIndex, qmd.SamplerIndex);
  114. _channel.TextureManager.SetComputeTexturePool(texturePoolGpuVa, _state.State.SetTexHeaderPoolCMaximumIndex);
  115. _channel.TextureManager.SetComputeTextureBufferIndex(_state.State.SetBindlessTextureConstantBufferSlotSelect);
  116. ShaderProgramInfo info = cs.Shaders[0].Info;
  117. for (int index = 0; index < info.CBuffers.Count; index++)
  118. {
  119. BufferDescriptor cb = info.CBuffers[index];
  120. // NVN uses the "hardware" constant buffer for anything that is less than 8,
  121. // and those are already bound above.
  122. // Anything greater than or equal to 8 uses the emulated constant buffers.
  123. // They are emulated using global memory loads.
  124. if (cb.Slot < 8)
  125. {
  126. continue;
  127. }
  128. ulong cbDescAddress = _channel.BufferManager.GetComputeUniformBufferAddress(0);
  129. int cbDescOffset = 0x260 + (cb.Slot - 8) * 0x10;
  130. cbDescAddress += (ulong)cbDescOffset;
  131. SbDescriptor cbDescriptor = _channel.MemoryManager.Physical.Read<SbDescriptor>(cbDescAddress);
  132. _channel.BufferManager.SetComputeUniformBuffer(cb.Slot, cbDescriptor.PackAddress(), (uint)cbDescriptor.Size);
  133. }
  134. for (int index = 0; index < info.SBuffers.Count; index++)
  135. {
  136. BufferDescriptor sb = info.SBuffers[index];
  137. ulong sbDescAddress = _channel.BufferManager.GetComputeUniformBufferAddress(0);
  138. int sbDescOffset = 0x310 + sb.Slot * 0x10;
  139. sbDescAddress += (ulong)sbDescOffset;
  140. SbDescriptor sbDescriptor = _channel.MemoryManager.Physical.Read<SbDescriptor>(sbDescAddress);
  141. _channel.BufferManager.SetComputeStorageBuffer(sb.Slot, sbDescriptor.PackAddress(), (uint)sbDescriptor.Size, sb.Flags);
  142. }
  143. _channel.BufferManager.SetComputeStorageBufferBindings(info.SBuffers);
  144. _channel.BufferManager.SetComputeUniformBufferBindings(info.CBuffers);
  145. var textureBindings = new TextureBindingInfo[info.Textures.Count];
  146. for (int index = 0; index < info.Textures.Count; index++)
  147. {
  148. var descriptor = info.Textures[index];
  149. Target target = ShaderTexture.GetTarget(descriptor.Type);
  150. textureBindings[index] = new TextureBindingInfo(
  151. target,
  152. descriptor.Binding,
  153. descriptor.CbufSlot,
  154. descriptor.HandleIndex,
  155. descriptor.Flags);
  156. }
  157. _channel.TextureManager.SetComputeTextures(textureBindings);
  158. var imageBindings = new TextureBindingInfo[info.Images.Count];
  159. for (int index = 0; index < info.Images.Count; index++)
  160. {
  161. var descriptor = info.Images[index];
  162. Target target = ShaderTexture.GetTarget(descriptor.Type);
  163. Format format = ShaderTexture.GetFormat(descriptor.Format);
  164. imageBindings[index] = new TextureBindingInfo(
  165. target,
  166. format,
  167. descriptor.Binding,
  168. descriptor.CbufSlot,
  169. descriptor.HandleIndex,
  170. descriptor.Flags);
  171. }
  172. _channel.TextureManager.SetComputeImages(imageBindings);
  173. _channel.TextureManager.CommitComputeBindings();
  174. _channel.BufferManager.CommitComputeBindings();
  175. _context.Renderer.Pipeline.DispatchCompute(qmd.CtaRasterWidth, qmd.CtaRasterHeight, qmd.CtaRasterDepth);
  176. _3dEngine.ForceShaderUpdate();
  177. }
  178. }
  179. }