CommandHelper.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Ryujinx.Graphics.GAL.Multithreading.Commands;
  2. using Ryujinx.Graphics.GAL.Multithreading.Commands.Buffer;
  3. using Ryujinx.Graphics.GAL.Multithreading.Commands.CounterEvent;
  4. using Ryujinx.Graphics.GAL.Multithreading.Commands.Program;
  5. using Ryujinx.Graphics.GAL.Multithreading.Commands.Renderer;
  6. using Ryujinx.Graphics.GAL.Multithreading.Commands.Sampler;
  7. using Ryujinx.Graphics.GAL.Multithreading.Commands.Texture;
  8. using Ryujinx.Graphics.GAL.Multithreading.Commands.Window;
  9. using System;
  10. using System.Linq;
  11. using System.Runtime.CompilerServices;
  12. using System.Runtime.InteropServices;
  13. namespace Ryujinx.Graphics.GAL.Multithreading
  14. {
  15. static class CommandHelper
  16. {
  17. private delegate void CommandDelegate(Span<byte> memory, ThreadedRenderer threaded, IRenderer renderer);
  18. private static int _totalCommands = (int)Enum.GetValues<CommandType>().Max() + 1;
  19. private static CommandDelegate[] _lookup = new CommandDelegate[_totalCommands];
  20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  21. private static ref T GetCommand<T>(Span<byte> memory)
  22. {
  23. return ref Unsafe.As<byte, T>(ref MemoryMarshal.GetReference(memory));
  24. }
  25. public static int GetMaxCommandSize()
  26. {
  27. return InitLookup() + 1; // 1 byte reserved for command size.
  28. }
  29. private static int InitLookup()
  30. {
  31. int maxCommandSize = 0;
  32. void Register<T>(CommandType commandType) where T : unmanaged, IGALCommand, IGALCommand<T>
  33. {
  34. maxCommandSize = Math.Max(maxCommandSize, Unsafe.SizeOf<T>());
  35. _lookup[(int)commandType] = (memory, threaded, renderer) => T.Run(ref GetCommand<T>(memory), threaded, renderer);
  36. }
  37. Register<ActionCommand>(CommandType.Action);
  38. Register<CreateBufferCommand>(CommandType.CreateBuffer);
  39. Register<CreateProgramCommand>(CommandType.CreateProgram);
  40. Register<CreateSamplerCommand>(CommandType.CreateSampler);
  41. Register<CreateSyncCommand>(CommandType.CreateSync);
  42. Register<CreateTextureCommand>(CommandType.CreateTexture);
  43. Register<GetCapabilitiesCommand>(CommandType.GetCapabilities);
  44. Register<PreFrameCommand>(CommandType.PreFrame);
  45. Register<ReportCounterCommand>(CommandType.ReportCounter);
  46. Register<ResetCounterCommand>(CommandType.ResetCounter);
  47. Register<UpdateCountersCommand>(CommandType.UpdateCounters);
  48. Register<BufferDisposeCommand>(CommandType.BufferDispose);
  49. Register<BufferGetDataCommand>(CommandType.BufferGetData);
  50. Register<BufferSetDataCommand>(CommandType.BufferSetData);
  51. Register<CounterEventDisposeCommand>(CommandType.CounterEventDispose);
  52. Register<CounterEventFlushCommand>(CommandType.CounterEventFlush);
  53. Register<ProgramDisposeCommand>(CommandType.ProgramDispose);
  54. Register<ProgramGetBinaryCommand>(CommandType.ProgramGetBinary);
  55. Register<ProgramCheckLinkCommand>(CommandType.ProgramCheckLink);
  56. Register<SamplerDisposeCommand>(CommandType.SamplerDispose);
  57. Register<TextureCopyToCommand>(CommandType.TextureCopyTo);
  58. Register<TextureCopyToScaledCommand>(CommandType.TextureCopyToScaled);
  59. Register<TextureCopyToSliceCommand>(CommandType.TextureCopyToSlice);
  60. Register<TextureCreateViewCommand>(CommandType.TextureCreateView);
  61. Register<TextureGetDataCommand>(CommandType.TextureGetData);
  62. Register<TextureGetDataSliceCommand>(CommandType.TextureGetDataSlice);
  63. Register<TextureReleaseCommand>(CommandType.TextureRelease);
  64. Register<TextureSetDataCommand>(CommandType.TextureSetData);
  65. Register<TextureSetDataSliceCommand>(CommandType.TextureSetDataSlice);
  66. Register<TextureSetDataSliceRegionCommand>(CommandType.TextureSetDataSliceRegion);
  67. Register<TextureSetStorageCommand>(CommandType.TextureSetStorage);
  68. Register<WindowPresentCommand>(CommandType.WindowPresent);
  69. Register<BarrierCommand>(CommandType.Barrier);
  70. Register<BeginTransformFeedbackCommand>(CommandType.BeginTransformFeedback);
  71. Register<ClearBufferCommand>(CommandType.ClearBuffer);
  72. Register<ClearRenderTargetColorCommand>(CommandType.ClearRenderTargetColor);
  73. Register<ClearRenderTargetDepthStencilCommand>(CommandType.ClearRenderTargetDepthStencil);
  74. Register<CommandBufferBarrierCommand>(CommandType.CommandBufferBarrier);
  75. Register<CopyBufferCommand>(CommandType.CopyBuffer);
  76. Register<DispatchComputeCommand>(CommandType.DispatchCompute);
  77. Register<DrawCommand>(CommandType.Draw);
  78. Register<DrawIndexedCommand>(CommandType.DrawIndexed);
  79. Register<DrawIndexedIndirectCommand>(CommandType.DrawIndexedIndirect);
  80. Register<DrawIndexedIndirectCountCommand>(CommandType.DrawIndexedIndirectCount);
  81. Register<DrawIndirectCommand>(CommandType.DrawIndirect);
  82. Register<DrawIndirectCountCommand>(CommandType.DrawIndirectCount);
  83. Register<DrawTextureCommand>(CommandType.DrawTexture);
  84. Register<EndHostConditionalRenderingCommand>(CommandType.EndHostConditionalRendering);
  85. Register<EndTransformFeedbackCommand>(CommandType.EndTransformFeedback);
  86. Register<SetAlphaTestCommand>(CommandType.SetAlphaTest);
  87. Register<SetBlendStateAdvancedCommand>(CommandType.SetBlendStateAdvanced);
  88. Register<SetBlendStateCommand>(CommandType.SetBlendState);
  89. Register<SetDepthBiasCommand>(CommandType.SetDepthBias);
  90. Register<SetDepthClampCommand>(CommandType.SetDepthClamp);
  91. Register<SetDepthModeCommand>(CommandType.SetDepthMode);
  92. Register<SetDepthTestCommand>(CommandType.SetDepthTest);
  93. Register<SetFaceCullingCommand>(CommandType.SetFaceCulling);
  94. Register<SetFrontFaceCommand>(CommandType.SetFrontFace);
  95. Register<SetStorageBuffersCommand>(CommandType.SetStorageBuffers);
  96. Register<SetTransformFeedbackBuffersCommand>(CommandType.SetTransformFeedbackBuffers);
  97. Register<SetUniformBuffersCommand>(CommandType.SetUniformBuffers);
  98. Register<SetImageCommand>(CommandType.SetImage);
  99. Register<SetIndexBufferCommand>(CommandType.SetIndexBuffer);
  100. Register<SetLineParametersCommand>(CommandType.SetLineParameters);
  101. Register<SetLogicOpStateCommand>(CommandType.SetLogicOpState);
  102. Register<SetMultisampleStateCommand>(CommandType.SetMultisampleState);
  103. Register<SetPatchParametersCommand>(CommandType.SetPatchParameters);
  104. Register<SetPointParametersCommand>(CommandType.SetPointParameters);
  105. Register<SetPolygonModeCommand>(CommandType.SetPolygonMode);
  106. Register<SetPrimitiveRestartCommand>(CommandType.SetPrimitiveRestart);
  107. Register<SetPrimitiveTopologyCommand>(CommandType.SetPrimitiveTopology);
  108. Register<SetProgramCommand>(CommandType.SetProgram);
  109. Register<SetRasterizerDiscardCommand>(CommandType.SetRasterizerDiscard);
  110. Register<SetRenderTargetColorMasksCommand>(CommandType.SetRenderTargetColorMasks);
  111. Register<SetRenderTargetScaleCommand>(CommandType.SetRenderTargetScale);
  112. Register<SetRenderTargetsCommand>(CommandType.SetRenderTargets);
  113. Register<SetScissorsCommand>(CommandType.SetScissor);
  114. Register<SetStencilTestCommand>(CommandType.SetStencilTest);
  115. Register<SetTextureAndSamplerCommand>(CommandType.SetTextureAndSampler);
  116. Register<SetUserClipDistanceCommand>(CommandType.SetUserClipDistance);
  117. Register<SetVertexAttribsCommand>(CommandType.SetVertexAttribs);
  118. Register<SetVertexBuffersCommand>(CommandType.SetVertexBuffers);
  119. Register<SetViewportsCommand>(CommandType.SetViewports);
  120. Register<TextureBarrierCommand>(CommandType.TextureBarrier);
  121. Register<TextureBarrierTiledCommand>(CommandType.TextureBarrierTiled);
  122. Register<TryHostConditionalRenderingCommand>(CommandType.TryHostConditionalRendering);
  123. Register<TryHostConditionalRenderingFlushCommand>(CommandType.TryHostConditionalRenderingFlush);
  124. Register<UpdateRenderScaleCommand>(CommandType.UpdateRenderScale);
  125. return maxCommandSize;
  126. }
  127. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  128. public static void RunCommand(Span<byte> memory, ThreadedRenderer threaded, IRenderer renderer)
  129. {
  130. _lookup[memory[memory.Length - 1]](memory, threaded, renderer);
  131. }
  132. }
  133. }