Compute.cs 4.5 KB

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