| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using Ryujinx.Graphics.Metal;
- using SharpMetal.Metal;
- using System;
- using System.Runtime.CompilerServices;
- using System.Runtime.Versioning;
- interface IEncoderFactory
- {
- MTLRenderCommandEncoder CreateRenderCommandEncoder();
- MTLComputeCommandEncoder CreateComputeCommandEncoder();
- }
- /// <summary>
- /// Tracks active encoder object for a command buffer.
- /// </summary>
- [SupportedOSPlatform("macos")]
- class CommandBufferEncoder
- {
- public EncoderType CurrentEncoderType { get; private set; } = EncoderType.None;
- public MTLBlitCommandEncoder BlitEncoder => new(CurrentEncoder.Value);
- public MTLComputeCommandEncoder ComputeEncoder => new(CurrentEncoder.Value);
- public MTLRenderCommandEncoder RenderEncoder => new(CurrentEncoder.Value);
- internal MTLCommandEncoder? CurrentEncoder { get; private set; }
- private MTLCommandBuffer _commandBuffer;
- private IEncoderFactory _encoderFactory;
- public void Initialize(MTLCommandBuffer commandBuffer, IEncoderFactory encoderFactory)
- {
- _commandBuffer = commandBuffer;
- _encoderFactory = encoderFactory;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public MTLRenderCommandEncoder EnsureRenderEncoder()
- {
- if (CurrentEncoderType != EncoderType.Render)
- {
- return BeginRenderPass();
- }
- return RenderEncoder;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public MTLBlitCommandEncoder EnsureBlitEncoder()
- {
- if (CurrentEncoderType != EncoderType.Blit)
- {
- return BeginBlitPass();
- }
- return BlitEncoder;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public MTLComputeCommandEncoder EnsureComputeEncoder()
- {
- if (CurrentEncoderType != EncoderType.Compute)
- {
- return BeginComputePass();
- }
- return ComputeEncoder;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool TryGetRenderEncoder(out MTLRenderCommandEncoder encoder)
- {
- if (CurrentEncoderType != EncoderType.Render)
- {
- encoder = default;
- return false;
- }
- encoder = RenderEncoder;
- return true;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool TryGetBlitEncoder(out MTLBlitCommandEncoder encoder)
- {
- if (CurrentEncoderType != EncoderType.Blit)
- {
- encoder = default;
- return false;
- }
- encoder = BlitEncoder;
- return true;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool TryGetComputeEncoder(out MTLComputeCommandEncoder encoder)
- {
- if (CurrentEncoderType != EncoderType.Compute)
- {
- encoder = default;
- return false;
- }
- encoder = ComputeEncoder;
- return true;
- }
- public void EndCurrentPass()
- {
- if (CurrentEncoder != null)
- {
- switch (CurrentEncoderType)
- {
- case EncoderType.Blit:
- BlitEncoder.EndEncoding();
- CurrentEncoder = null;
- break;
- case EncoderType.Compute:
- ComputeEncoder.EndEncoding();
- CurrentEncoder = null;
- break;
- case EncoderType.Render:
- RenderEncoder.EndEncoding();
- CurrentEncoder = null;
- break;
- default:
- throw new InvalidOperationException();
- }
- CurrentEncoderType = EncoderType.None;
- }
- }
- private MTLRenderCommandEncoder BeginRenderPass()
- {
- EndCurrentPass();
- MTLRenderCommandEncoder renderCommandEncoder = _encoderFactory.CreateRenderCommandEncoder();
- CurrentEncoder = renderCommandEncoder;
- CurrentEncoderType = EncoderType.Render;
- return renderCommandEncoder;
- }
- private MTLBlitCommandEncoder BeginBlitPass()
- {
- EndCurrentPass();
- using MTLBlitPassDescriptor descriptor = new();
- MTLBlitCommandEncoder blitCommandEncoder = _commandBuffer.BlitCommandEncoder(descriptor);
- CurrentEncoder = blitCommandEncoder;
- CurrentEncoderType = EncoderType.Blit;
- return blitCommandEncoder;
- }
- private MTLComputeCommandEncoder BeginComputePass()
- {
- EndCurrentPass();
- MTLComputeCommandEncoder computeCommandEncoder = _encoderFactory.CreateComputeCommandEncoder();
- CurrentEncoder = computeCommandEncoder;
- CurrentEncoderType = EncoderType.Compute;
- return computeCommandEncoder;
- }
- }
|