Compute.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. namespace Ryujinx.Graphics.Gpu.Engine
  8. {
  9. partial class Methods
  10. {
  11. /// <summary>
  12. /// Dispatches compute work.
  13. /// </summary>
  14. /// <param name="state">Current GPU state</param>
  15. /// <param name="argument">Method call argument</param>
  16. public void Dispatch(GpuState state, int argument)
  17. {
  18. uint qmdAddress = (uint)state.Get<int>(MethodOffset.DispatchParamsAddress);
  19. var qmd = _context.MemoryManager.Read<ComputeQmd>((ulong)qmdAddress << 8);
  20. GpuVa shaderBaseAddress = state.Get<GpuVa>(MethodOffset.ShaderBaseAddress);
  21. ulong shaderGpuVa = shaderBaseAddress.Pack() + (uint)qmd.ProgramOffset;
  22. int localMemorySize = qmd.ShaderLocalMemoryLowSize + qmd.ShaderLocalMemoryHighSize;
  23. int sharedMemorySize = Math.Min(qmd.SharedMemorySize, _context.Capabilities.MaximumComputeSharedMemorySize);
  24. uint sbEnableMask = 0;
  25. uint ubEnableMask = 0;
  26. for (int index = 0; index < Constants.TotalCpUniformBuffers; index++)
  27. {
  28. if (!qmd.ConstantBufferValid(index))
  29. {
  30. continue;
  31. }
  32. ubEnableMask |= 1u << index;
  33. ulong gpuVa = (uint)qmd.ConstantBufferAddrLower(index) | (ulong)qmd.ConstantBufferAddrUpper(index) << 32;
  34. ulong size = (ulong)qmd.ConstantBufferSize(index);
  35. BufferManager.SetComputeUniformBuffer(index, gpuVa, size);
  36. }
  37. ShaderBundle cs = ShaderCache.GetComputeShader(
  38. state,
  39. shaderGpuVa,
  40. qmd.CtaThreadDimension0,
  41. qmd.CtaThreadDimension1,
  42. qmd.CtaThreadDimension2,
  43. localMemorySize,
  44. sharedMemorySize);
  45. _context.Renderer.Pipeline.SetProgram(cs.HostProgram);
  46. var samplerPool = state.Get<PoolState>(MethodOffset.SamplerPoolState);
  47. TextureManager.SetComputeSamplerPool(samplerPool.Address.Pack(), samplerPool.MaximumId, qmd.SamplerIndex);
  48. var texturePool = state.Get<PoolState>(MethodOffset.TexturePoolState);
  49. TextureManager.SetComputeTexturePool(texturePool.Address.Pack(), texturePool.MaximumId);
  50. TextureManager.SetComputeTextureBufferIndex(state.Get<int>(MethodOffset.TextureBufferIndex));
  51. ShaderProgramInfo info = cs.Shaders[0].Program.Info;
  52. for (int index = 0; index < info.CBuffers.Count; index++)
  53. {
  54. BufferDescriptor cb = info.CBuffers[index];
  55. // NVN uses the "hardware" constant buffer for anything that is less than 8,
  56. // and those are already bound above.
  57. // Anything greater than or equal to 8 uses the emulated constant buffers.
  58. // They are emulated using global memory loads.
  59. if (cb.Slot < 8)
  60. {
  61. continue;
  62. }
  63. ubEnableMask |= 1u << cb.Slot;
  64. ulong cbDescAddress = BufferManager.GetComputeUniformBufferAddress(0);
  65. int cbDescOffset = 0x260 + (cb.Slot - 8) * 0x10;
  66. cbDescAddress += (ulong)cbDescOffset;
  67. SbDescriptor cbDescriptor = _context.PhysicalMemory.Read<SbDescriptor>(cbDescAddress);
  68. BufferManager.SetComputeUniformBuffer(cb.Slot, cbDescriptor.PackAddress(), (uint)cbDescriptor.Size);
  69. }
  70. for (int index = 0; index < info.SBuffers.Count; index++)
  71. {
  72. BufferDescriptor sb = info.SBuffers[index];
  73. sbEnableMask |= 1u << sb.Slot;
  74. ulong sbDescAddress = BufferManager.GetComputeUniformBufferAddress(0);
  75. int sbDescOffset = 0x310 + sb.Slot * 0x10;
  76. sbDescAddress += (ulong)sbDescOffset;
  77. SbDescriptor sbDescriptor = _context.PhysicalMemory.Read<SbDescriptor>(sbDescAddress);
  78. BufferManager.SetComputeStorageBuffer(sb.Slot, sbDescriptor.PackAddress(), (uint)sbDescriptor.Size);
  79. }
  80. ubEnableMask = 0;
  81. for (int index = 0; index < info.CBuffers.Count; index++)
  82. {
  83. ubEnableMask |= 1u << info.CBuffers[index].Slot;
  84. }
  85. BufferManager.SetComputeStorageBufferEnableMask(sbEnableMask);
  86. BufferManager.SetComputeUniformBufferEnableMask(ubEnableMask);
  87. var textureBindings = new TextureBindingInfo[info.Textures.Count];
  88. for (int index = 0; index < info.Textures.Count; index++)
  89. {
  90. var descriptor = info.Textures[index];
  91. Target target = GetTarget(descriptor.Type);
  92. if (descriptor.IsBindless)
  93. {
  94. textureBindings[index] = new TextureBindingInfo(target, descriptor.CbufOffset, descriptor.CbufSlot, descriptor.Flags);
  95. }
  96. else
  97. {
  98. textureBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex, descriptor.Flags);
  99. }
  100. }
  101. TextureManager.SetComputeTextures(textureBindings);
  102. var imageBindings = new TextureBindingInfo[info.Images.Count];
  103. for (int index = 0; index < info.Images.Count; index++)
  104. {
  105. var descriptor = info.Images[index];
  106. Target target = GetTarget(descriptor.Type);
  107. imageBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex, descriptor.Flags);
  108. }
  109. TextureManager.SetComputeImages(imageBindings);
  110. BufferManager.CommitComputeBindings();
  111. TextureManager.CommitComputeBindings();
  112. _context.Renderer.Pipeline.DispatchCompute(
  113. qmd.CtaRasterWidth,
  114. qmd.CtaRasterHeight,
  115. qmd.CtaRasterDepth);
  116. _forceShaderUpdate = true;
  117. }
  118. }
  119. }