Compute.cs 6.0 KB

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