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, dispatchParams.SamplerIndex);
  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. uint sbEnableMask = 0;
  31. uint ubEnableMask = dispatchParams.UnpackUniformBuffersEnableMask();
  32. for (int index = 0; index < dispatchParams.UniformBuffers.Length; index++)
  33. {
  34. if ((ubEnableMask & (1 << index)) == 0)
  35. {
  36. continue;
  37. }
  38. ulong gpuVa = dispatchParams.UniformBuffers[index].PackAddress();
  39. ulong size = dispatchParams.UniformBuffers[index].UnpackSize();
  40. _bufferManager.SetComputeUniformBuffer(index, gpuVa, size);
  41. }
  42. for (int index = 0; index < info.SBuffers.Count; index++)
  43. {
  44. BufferDescriptor sb = info.SBuffers[index];
  45. sbEnableMask |= 1u << sb.Slot;
  46. ulong sbDescAddress = _bufferManager.GetComputeUniformBufferAddress(0);
  47. int sbDescOffset = 0x310 + sb.Slot * 0x10;
  48. sbDescAddress += (ulong)sbDescOffset;
  49. Span<byte> sbDescriptorData = _context.PhysicalMemory.Read(sbDescAddress, 0x10);
  50. SbDescriptor sbDescriptor = MemoryMarshal.Cast<byte, SbDescriptor>(sbDescriptorData)[0];
  51. _bufferManager.SetComputeStorageBuffer(sb.Slot, sbDescriptor.PackAddress(), (uint)sbDescriptor.Size);
  52. }
  53. ubEnableMask = 0;
  54. for (int index = 0; index < info.CBuffers.Count; index++)
  55. {
  56. ubEnableMask |= 1u << info.CBuffers[index].Slot;
  57. }
  58. _bufferManager.SetComputeStorageBufferEnableMask(sbEnableMask);
  59. _bufferManager.SetComputeUniformBufferEnableMask(ubEnableMask);
  60. var textureBindings = new TextureBindingInfo[info.Textures.Count];
  61. for (int index = 0; index < info.Textures.Count; index++)
  62. {
  63. var descriptor = info.Textures[index];
  64. Target target = GetTarget(descriptor.Type);
  65. textureBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
  66. }
  67. _textureManager.SetComputeTextures(textureBindings);
  68. var imageBindings = new TextureBindingInfo[info.Images.Count];
  69. for (int index = 0; index < info.Images.Count; index++)
  70. {
  71. var descriptor = info.Images[index];
  72. Target target = GetTarget(descriptor.Type);
  73. imageBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
  74. }
  75. _textureManager.SetComputeImages(imageBindings);
  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. }