Compute.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Image;
  3. using Ryujinx.Graphics.Gpu.Shader;
  4. using Ryujinx.Graphics.Gpu.State;
  5. using Ryujinx.Graphics.Shader;
  6. using System;
  7. using System.Runtime.InteropServices;
  8. namespace Ryujinx.Graphics.Gpu.Engine
  9. {
  10. partial class Methods
  11. {
  12. /// <summary>
  13. /// Dispatches compute work.
  14. /// </summary>
  15. /// <param name="state">Current GPU state</param>
  16. /// <param name="argument">Method call argument</param>
  17. public void Dispatch(GpuState state, int argument)
  18. {
  19. uint qmdAddress = (uint)state.Get<int>(MethodOffset.DispatchParamsAddress);
  20. var qmd = _context.MemoryAccessor.Read<ComputeQmd>((ulong)qmdAddress << 8);
  21. GpuVa shaderBaseAddress = state.Get<GpuVa>(MethodOffset.ShaderBaseAddress);
  22. ulong shaderGpuVa = shaderBaseAddress.Pack() + (uint)qmd.ProgramOffset;
  23. int localMemorySize = qmd.ShaderLocalMemoryLowSize + qmd.ShaderLocalMemoryHighSize;
  24. int sharedMemorySize = Math.Min(qmd.SharedMemorySize, _context.Capabilities.MaximumComputeSharedMemorySize);
  25. ComputeShader cs = ShaderCache.GetComputeShader(
  26. shaderGpuVa,
  27. qmd.CtaThreadDimension0,
  28. qmd.CtaThreadDimension1,
  29. qmd.CtaThreadDimension2,
  30. localMemorySize,
  31. sharedMemorySize);
  32. _context.Renderer.Pipeline.SetProgram(cs.HostProgram);
  33. var samplerPool = state.Get<PoolState>(MethodOffset.SamplerPoolState);
  34. TextureManager.SetComputeSamplerPool(samplerPool.Address.Pack(), samplerPool.MaximumId, qmd.SamplerIndex);
  35. var texturePool = state.Get<PoolState>(MethodOffset.TexturePoolState);
  36. TextureManager.SetComputeTexturePool(texturePool.Address.Pack(), texturePool.MaximumId);
  37. TextureManager.SetComputeTextureBufferIndex(state.Get<int>(MethodOffset.TextureBufferIndex));
  38. ShaderProgramInfo info = cs.Shader.Program.Info;
  39. uint sbEnableMask = 0;
  40. uint ubEnableMask = 0;
  41. for (int index = 0; index < Constants.TotalCpUniformBuffers; index++)
  42. {
  43. if (!qmd.ConstantBufferValid(index))
  44. {
  45. continue;
  46. }
  47. ubEnableMask |= 1u << index;
  48. ulong gpuVa = (uint)qmd.ConstantBufferAddrLower(index) | (ulong)qmd.ConstantBufferAddrUpper(index) << 32;
  49. ulong size = (ulong)qmd.ConstantBufferSize(index);
  50. BufferManager.SetComputeUniformBuffer(index, gpuVa, size);
  51. }
  52. for (int index = 0; index < info.CBuffers.Count; index++)
  53. {
  54. BufferDescriptor cb = info.CBuffers[index];
  55. // NVN uses the "hardware" constant buffer for anything that is less than 8,
  56. // and those are already bound above.
  57. // Anything greater than or equal to 8 uses the emulated constant buffers.
  58. // They are emulated using global memory loads.
  59. if (cb.Slot < 8)
  60. {
  61. continue;
  62. }
  63. ubEnableMask |= 1u << cb.Slot;
  64. ulong cbDescAddress = BufferManager.GetComputeUniformBufferAddress(0);
  65. int cbDescOffset = 0x260 + cb.Slot * 0x10;
  66. cbDescAddress += (ulong)cbDescOffset;
  67. ReadOnlySpan<byte> cbDescriptorData = _context.PhysicalMemory.GetSpan(cbDescAddress, 0x10);
  68. SbDescriptor cbDescriptor = MemoryMarshal.Cast<byte, SbDescriptor>(cbDescriptorData)[0];
  69. BufferManager.SetComputeUniformBuffer(cb.Slot, cbDescriptor.PackAddress(), (uint)cbDescriptor.Size);
  70. }
  71. for (int index = 0; index < info.SBuffers.Count; index++)
  72. {
  73. BufferDescriptor sb = info.SBuffers[index];
  74. sbEnableMask |= 1u << sb.Slot;
  75. ulong sbDescAddress = BufferManager.GetComputeUniformBufferAddress(0);
  76. int sbDescOffset = 0x310 + sb.Slot * 0x10;
  77. sbDescAddress += (ulong)sbDescOffset;
  78. ReadOnlySpan<byte> sbDescriptorData = _context.PhysicalMemory.GetSpan(sbDescAddress, 0x10);
  79. SbDescriptor sbDescriptor = MemoryMarshal.Cast<byte, SbDescriptor>(sbDescriptorData)[0];
  80. BufferManager.SetComputeStorageBuffer(sb.Slot, sbDescriptor.PackAddress(), (uint)sbDescriptor.Size);
  81. }
  82. ubEnableMask = 0;
  83. for (int index = 0; index < info.CBuffers.Count; index++)
  84. {
  85. ubEnableMask |= 1u << info.CBuffers[index].Slot;
  86. }
  87. BufferManager.SetComputeStorageBufferEnableMask(sbEnableMask);
  88. BufferManager.SetComputeUniformBufferEnableMask(ubEnableMask);
  89. var textureBindings = new TextureBindingInfo[info.Textures.Count];
  90. for (int index = 0; index < info.Textures.Count; index++)
  91. {
  92. var descriptor = info.Textures[index];
  93. Target target = GetTarget(descriptor.Type);
  94. if (descriptor.IsBindless)
  95. {
  96. textureBindings[index] = new TextureBindingInfo(target, descriptor.CbufOffset, descriptor.CbufSlot);
  97. }
  98. else
  99. {
  100. textureBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
  101. }
  102. }
  103. TextureManager.SetComputeTextures(textureBindings);
  104. var imageBindings = new TextureBindingInfo[info.Images.Count];
  105. for (int index = 0; index < info.Images.Count; index++)
  106. {
  107. var descriptor = info.Images[index];
  108. Target target = GetTarget(descriptor.Type);
  109. imageBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
  110. }
  111. TextureManager.SetComputeImages(imageBindings);
  112. BufferManager.CommitComputeBindings();
  113. TextureManager.CommitComputeBindings();
  114. _context.Renderer.Pipeline.DispatchCompute(
  115. qmd.CtaRasterWidth,
  116. qmd.CtaRasterHeight,
  117. qmd.CtaRasterDepth);
  118. UpdateShaderState(state);
  119. }
  120. }
  121. }