CommandBufferEncoder.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Ryujinx.Graphics.Metal;
  2. using SharpMetal.Metal;
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.Versioning;
  6. interface IEncoderFactory
  7. {
  8. MTLRenderCommandEncoder CreateRenderCommandEncoder();
  9. MTLComputeCommandEncoder CreateComputeCommandEncoder();
  10. }
  11. /// <summary>
  12. /// Tracks active encoder object for a command buffer.
  13. /// </summary>
  14. [SupportedOSPlatform("macos")]
  15. class CommandBufferEncoder
  16. {
  17. public EncoderType CurrentEncoderType { get; private set; } = EncoderType.None;
  18. public MTLBlitCommandEncoder BlitEncoder => new(CurrentEncoder.Value);
  19. public MTLComputeCommandEncoder ComputeEncoder => new(CurrentEncoder.Value);
  20. public MTLRenderCommandEncoder RenderEncoder => new(CurrentEncoder.Value);
  21. internal MTLCommandEncoder? CurrentEncoder { get; private set; }
  22. private MTLCommandBuffer _commandBuffer;
  23. private IEncoderFactory _encoderFactory;
  24. public void Initialize(MTLCommandBuffer commandBuffer, IEncoderFactory encoderFactory)
  25. {
  26. _commandBuffer = commandBuffer;
  27. _encoderFactory = encoderFactory;
  28. }
  29. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  30. public MTLRenderCommandEncoder EnsureRenderEncoder()
  31. {
  32. if (CurrentEncoderType != EncoderType.Render)
  33. {
  34. return BeginRenderPass();
  35. }
  36. return RenderEncoder;
  37. }
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public MTLBlitCommandEncoder EnsureBlitEncoder()
  40. {
  41. if (CurrentEncoderType != EncoderType.Blit)
  42. {
  43. return BeginBlitPass();
  44. }
  45. return BlitEncoder;
  46. }
  47. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  48. public MTLComputeCommandEncoder EnsureComputeEncoder()
  49. {
  50. if (CurrentEncoderType != EncoderType.Compute)
  51. {
  52. return BeginComputePass();
  53. }
  54. return ComputeEncoder;
  55. }
  56. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  57. public bool TryGetRenderEncoder(out MTLRenderCommandEncoder encoder)
  58. {
  59. if (CurrentEncoderType != EncoderType.Render)
  60. {
  61. encoder = default;
  62. return false;
  63. }
  64. encoder = RenderEncoder;
  65. return true;
  66. }
  67. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  68. public bool TryGetBlitEncoder(out MTLBlitCommandEncoder encoder)
  69. {
  70. if (CurrentEncoderType != EncoderType.Blit)
  71. {
  72. encoder = default;
  73. return false;
  74. }
  75. encoder = BlitEncoder;
  76. return true;
  77. }
  78. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. public bool TryGetComputeEncoder(out MTLComputeCommandEncoder encoder)
  80. {
  81. if (CurrentEncoderType != EncoderType.Compute)
  82. {
  83. encoder = default;
  84. return false;
  85. }
  86. encoder = ComputeEncoder;
  87. return true;
  88. }
  89. public void EndCurrentPass()
  90. {
  91. if (CurrentEncoder != null)
  92. {
  93. switch (CurrentEncoderType)
  94. {
  95. case EncoderType.Blit:
  96. BlitEncoder.EndEncoding();
  97. CurrentEncoder = null;
  98. break;
  99. case EncoderType.Compute:
  100. ComputeEncoder.EndEncoding();
  101. CurrentEncoder = null;
  102. break;
  103. case EncoderType.Render:
  104. RenderEncoder.EndEncoding();
  105. CurrentEncoder = null;
  106. break;
  107. default:
  108. throw new InvalidOperationException();
  109. }
  110. CurrentEncoderType = EncoderType.None;
  111. }
  112. }
  113. private MTLRenderCommandEncoder BeginRenderPass()
  114. {
  115. EndCurrentPass();
  116. MTLRenderCommandEncoder renderCommandEncoder = _encoderFactory.CreateRenderCommandEncoder();
  117. CurrentEncoder = renderCommandEncoder;
  118. CurrentEncoderType = EncoderType.Render;
  119. return renderCommandEncoder;
  120. }
  121. private MTLBlitCommandEncoder BeginBlitPass()
  122. {
  123. EndCurrentPass();
  124. using MTLBlitPassDescriptor descriptor = new();
  125. MTLBlitCommandEncoder blitCommandEncoder = _commandBuffer.BlitCommandEncoder(descriptor);
  126. CurrentEncoder = blitCommandEncoder;
  127. CurrentEncoderType = EncoderType.Blit;
  128. return blitCommandEncoder;
  129. }
  130. private MTLComputeCommandEncoder BeginComputePass()
  131. {
  132. EndCurrentPass();
  133. MTLComputeCommandEncoder computeCommandEncoder = _encoderFactory.CreateComputeCommandEncoder();
  134. CurrentEncoder = computeCommandEncoder;
  135. CurrentEncoderType = EncoderType.Compute;
  136. return computeCommandEncoder;
  137. }
  138. }