Compute.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Ryujinx.Graphics.GAL.Texture;
  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. public void Dispatch(GpuState state, int argument)
  13. {
  14. uint dispatchParamsAddress = (uint)state.Get<int>(MethodOffset.DispatchParamsAddress);
  15. var dispatchParams = _context.MemoryAccessor.Read<ComputeParams>((ulong)dispatchParamsAddress << 8);
  16. GpuVa shaderBaseAddress = state.Get<GpuVa>(MethodOffset.ShaderBaseAddress);
  17. ulong shaderGpuVa = shaderBaseAddress.Pack() + (uint)dispatchParams.ShaderOffset;
  18. ComputeShader cs = _shaderCache.GetComputeShader(
  19. shaderGpuVa,
  20. dispatchParams.UnpackBlockSizeX(),
  21. dispatchParams.UnpackBlockSizeY(),
  22. dispatchParams.UnpackBlockSizeZ());
  23. _context.Renderer.Pipeline.BindProgram(cs.HostProgram);
  24. var samplerPool = state.Get<PoolState>(MethodOffset.SamplerPoolState);
  25. _textureManager.SetComputeSamplerPool(samplerPool.Address.Pack(), samplerPool.MaximumId);
  26. var texturePool = state.Get<PoolState>(MethodOffset.TexturePoolState);
  27. _textureManager.SetComputeTexturePool(texturePool.Address.Pack(), texturePool.MaximumId);
  28. _textureManager.SetComputeTextureBufferIndex(state.Get<int>(MethodOffset.TextureBufferIndex));
  29. ShaderProgramInfo info = cs.Shader.Program.Info;
  30. var textureBindings = new TextureBindingInfo[info.Textures.Count];
  31. for (int index = 0; index < info.Textures.Count; index++)
  32. {
  33. var descriptor = info.Textures[index];
  34. Target target = GetTarget(descriptor.Type);
  35. textureBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
  36. }
  37. _textureManager.SetComputeTextures(textureBindings);
  38. var imageBindings = new TextureBindingInfo[info.Images.Count];
  39. for (int index = 0; index < info.Images.Count; index++)
  40. {
  41. var descriptor = info.Images[index];
  42. Target target = GetTarget(descriptor.Type);
  43. imageBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
  44. }
  45. _textureManager.SetComputeImages(imageBindings);
  46. uint sbEnableMask = 0;
  47. uint ubEnableMask = dispatchParams.UnpackUniformBuffersEnableMask();
  48. for (int index = 0; index < dispatchParams.UniformBuffers.Length; index++)
  49. {
  50. if ((ubEnableMask & (1 << index)) == 0)
  51. {
  52. continue;
  53. }
  54. ulong gpuVa = dispatchParams.UniformBuffers[index].PackAddress();
  55. ulong size = dispatchParams.UniformBuffers[index].UnpackSize();
  56. _bufferManager.SetComputeUniformBuffer(index, gpuVa, size);
  57. }
  58. for (int index = 0; index < info.SBuffers.Count; index++)
  59. {
  60. BufferDescriptor sb = info.SBuffers[index];
  61. sbEnableMask |= 1u << sb.Slot;
  62. ulong sbDescAddress = _bufferManager.GetComputeUniformBufferAddress(0);
  63. int sbDescOffset = 0x310 + sb.Slot * 0x10;
  64. sbDescAddress += (ulong)sbDescOffset;
  65. Span<byte> sbDescriptorData = _context.PhysicalMemory.Read(sbDescAddress, 0x10);
  66. SbDescriptor sbDescriptor = MemoryMarshal.Cast<byte, SbDescriptor>(sbDescriptorData)[0];
  67. _bufferManager.SetComputeStorageBuffer(sb.Slot, sbDescriptor.PackAddress(), (uint)sbDescriptor.Size);
  68. }
  69. ubEnableMask = 0;
  70. for (int index = 0; index < info.CBuffers.Count; index++)
  71. {
  72. ubEnableMask |= 1u << info.CBuffers[index].Slot;
  73. }
  74. _bufferManager.SetComputeStorageBufferEnableMask(sbEnableMask);
  75. _bufferManager.SetComputeUniformBufferEnableMask(ubEnableMask);
  76. _bufferManager.CommitComputeBindings();
  77. _textureManager.CommitComputeBindings();
  78. _context.Renderer.Pipeline.Dispatch(
  79. dispatchParams.UnpackGridSizeX(),
  80. dispatchParams.UnpackGridSizeY(),
  81. dispatchParams.UnpackGridSizeZ());
  82. UpdateShaderState(state);
  83. }
  84. }
  85. }