ComputeClass.cs 8.5 KB

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