Compute.cs 5.0 KB

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