Compute.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.SBuffers.Count; index++)
  53. {
  54. BufferDescriptor sb = info.SBuffers[index];
  55. sbEnableMask |= 1u << sb.Slot;
  56. ulong sbDescAddress = BufferManager.GetComputeUniformBufferAddress(0);
  57. int sbDescOffset = 0x310 + sb.Slot * 0x10;
  58. sbDescAddress += (ulong)sbDescOffset;
  59. ReadOnlySpan<byte> sbDescriptorData = _context.PhysicalMemory.GetSpan(sbDescAddress, 0x10);
  60. SbDescriptor sbDescriptor = MemoryMarshal.Cast<byte, SbDescriptor>(sbDescriptorData)[0];
  61. BufferManager.SetComputeStorageBuffer(sb.Slot, sbDescriptor.PackAddress(), (uint)sbDescriptor.Size);
  62. }
  63. ubEnableMask = 0;
  64. for (int index = 0; index < info.CBuffers.Count; index++)
  65. {
  66. ubEnableMask |= 1u << info.CBuffers[index].Slot;
  67. }
  68. BufferManager.SetComputeStorageBufferEnableMask(sbEnableMask);
  69. BufferManager.SetComputeUniformBufferEnableMask(ubEnableMask);
  70. var textureBindings = new TextureBindingInfo[info.Textures.Count];
  71. for (int index = 0; index < info.Textures.Count; index++)
  72. {
  73. var descriptor = info.Textures[index];
  74. Target target = GetTarget(descriptor.Type);
  75. if (descriptor.IsBindless)
  76. {
  77. textureBindings[index] = new TextureBindingInfo(target, descriptor.CbufOffset, descriptor.CbufSlot);
  78. }
  79. else
  80. {
  81. textureBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
  82. }
  83. }
  84. TextureManager.SetComputeTextures(textureBindings);
  85. var imageBindings = new TextureBindingInfo[info.Images.Count];
  86. for (int index = 0; index < info.Images.Count; index++)
  87. {
  88. var descriptor = info.Images[index];
  89. Target target = GetTarget(descriptor.Type);
  90. imageBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
  91. }
  92. TextureManager.SetComputeImages(imageBindings);
  93. BufferManager.CommitComputeBindings();
  94. TextureManager.CommitComputeBindings();
  95. _context.Renderer.Pipeline.DispatchCompute(
  96. qmd.CtaRasterWidth,
  97. qmd.CtaRasterHeight,
  98. qmd.CtaRasterDepth);
  99. UpdateShaderState(state);
  100. }
  101. }
  102. }