Ryujinx/src/Ryujinx.Graphics.Metal/Pipeline.cs

589 lines
20 KiB
C#
Raw Normal View History

2023-07-28 20:39:14 +00:00
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Shader;
using SharpMetal.Foundation;
using SharpMetal.Metal;
2023-08-02 23:56:59 +00:00
using SharpMetal.QuartzCore;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
2024-03-18 18:48:54 +00:00
enum EncoderType
{
Blit,
Compute,
2024-03-20 02:58:42 +00:00
Render,
None
2024-03-18 18:48:54 +00:00
}
[SupportedOSPlatform("macos")]
2023-08-03 18:50:49 +00:00
class Pipeline : IPipeline, IDisposable
{
2023-07-28 02:54:24 +00:00
private readonly MTLDevice _device;
2023-08-03 21:04:59 +00:00
private readonly MTLCommandQueue _commandQueue;
2024-05-18 22:54:55 +00:00
private readonly HelperShader _helperShader;
2023-07-28 02:54:24 +00:00
private MTLCommandBuffer _commandBuffer;
2024-05-18 22:54:55 +00:00
public MTLCommandBuffer CommandBuffer => _commandBuffer;
2024-03-18 18:32:59 +00:00
private MTLCommandEncoder? _currentEncoder;
2024-05-18 22:54:55 +00:00
public MTLCommandEncoder? CurrentEncoder => _currentEncoder;
2023-07-28 02:54:24 +00:00
2024-05-18 22:54:55 +00:00
private EncoderType _currentEncoderType = EncoderType.None;
public EncoderType CurrentEncoderType => _currentEncoderType;
2024-05-18 22:54:55 +00:00
private EncoderStateManager _encoderStateManager;
2023-08-03 12:48:41 +00:00
public Pipeline(MTLDevice device, MTLCommandQueue commandQueue)
{
2023-07-28 02:54:24 +00:00
_device = device;
2023-08-03 21:04:59 +00:00
_commandQueue = commandQueue;
2024-05-18 22:54:55 +00:00
_helperShader = new HelperShader(_device, this);
2023-08-03 21:04:59 +00:00
_commandBuffer = _commandQueue.CommandBuffer();
2024-05-18 22:54:55 +00:00
_encoderStateManager = new EncoderStateManager(_device, this);
2023-07-28 02:54:24 +00:00
}
public void SaveState()
{
_encoderStateManager.SaveState();
}
public void RestoreState()
{
_encoderStateManager.RestoreState();
}
public MTLRenderCommandEncoder GetOrCreateRenderEncoder()
{
2024-05-19 15:07:55 +00:00
MTLRenderCommandEncoder renderCommandEncoder;
if (_currentEncoder == null || _currentEncoderType != EncoderType.Render)
{
2024-05-19 15:07:55 +00:00
renderCommandEncoder = BeginRenderPass();
}
else
{
renderCommandEncoder = new MTLRenderCommandEncoder(_currentEncoder.Value);
}
_encoderStateManager.RebindState(renderCommandEncoder);
return renderCommandEncoder;
}
public MTLBlitCommandEncoder GetOrCreateBlitEncoder()
{
2024-03-18 18:48:54 +00:00
if (_currentEncoder != null)
{
2024-03-18 18:48:54 +00:00
if (_currentEncoderType == EncoderType.Blit)
{
return new MTLBlitCommandEncoder(_currentEncoder.Value);
}
}
return BeginBlitPass();
}
public MTLComputeCommandEncoder GetOrCreateComputeEncoder()
{
2024-03-18 18:48:54 +00:00
if (_currentEncoder != null)
{
2024-03-18 18:48:54 +00:00
if (_currentEncoderType == EncoderType.Compute)
{
return new MTLComputeCommandEncoder(_currentEncoder.Value);
}
}
return BeginComputePass();
}
2023-07-28 20:23:13 +00:00
public void EndCurrentPass()
2023-07-28 02:54:24 +00:00
{
2023-07-28 20:23:13 +00:00
if (_currentEncoder != null)
{
2024-03-18 18:48:54 +00:00
switch (_currentEncoderType)
{
case EncoderType.Blit:
new MTLBlitCommandEncoder(_currentEncoder.Value).EndEncoding();
_currentEncoder = null;
break;
case EncoderType.Compute:
new MTLComputeCommandEncoder(_currentEncoder.Value).EndEncoding();
_currentEncoder = null;
break;
case EncoderType.Render:
new MTLRenderCommandEncoder(_currentEncoder.Value).EndEncoding();
_currentEncoder = null;
break;
default:
throw new ArgumentOutOfRangeException();
}
2024-03-20 02:58:42 +00:00
_currentEncoderType = EncoderType.None;
2023-07-28 20:23:13 +00:00
}
}
public MTLRenderCommandEncoder BeginRenderPass()
{
EndCurrentPass();
2024-05-18 22:54:55 +00:00
var renderCommandEncoder = _encoderStateManager.CreateRenderCommandEncoder();
2023-10-10 23:02:38 +00:00
2023-07-28 20:23:13 +00:00
_currentEncoder = renderCommandEncoder;
2024-03-18 18:48:54 +00:00
_currentEncoderType = EncoderType.Render;
2024-05-18 22:54:55 +00:00
2023-07-28 20:23:13 +00:00
return renderCommandEncoder;
2023-07-28 02:54:24 +00:00
}
2023-07-28 20:23:13 +00:00
public MTLBlitCommandEncoder BeginBlitPass()
2023-07-28 02:54:24 +00:00
{
2023-07-28 20:23:13 +00:00
EndCurrentPass();
2023-08-02 02:36:07 +00:00
var descriptor = new MTLBlitPassDescriptor();
2023-07-28 20:23:13 +00:00
var blitCommandEncoder = _commandBuffer.BlitCommandEncoder(descriptor);
_currentEncoder = blitCommandEncoder;
2024-03-18 18:48:54 +00:00
_currentEncoderType = EncoderType.Blit;
2023-07-28 20:23:13 +00:00
return blitCommandEncoder;
}
2023-07-29 05:18:51 +00:00
public MTLComputeCommandEncoder BeginComputePass()
{
EndCurrentPass();
2023-08-02 02:36:07 +00:00
var descriptor = new MTLComputePassDescriptor();
2023-07-29 05:18:51 +00:00
var computeCommandEncoder = _commandBuffer.ComputeCommandEncoder(descriptor);
_currentEncoder = computeCommandEncoder;
2024-03-18 18:48:54 +00:00
_currentEncoderType = EncoderType.Compute;
2023-07-29 05:18:51 +00:00
return computeCommandEncoder;
}
2023-08-03 18:50:49 +00:00
public void Present(CAMetalDrawable drawable, ITexture texture)
2023-07-28 20:23:13 +00:00
{
2023-08-03 18:50:49 +00:00
if (texture is not Texture tex)
{
return;
}
2023-07-28 20:23:13 +00:00
EndCurrentPass();
2023-07-28 02:54:24 +00:00
SaveState();
2023-08-02 23:56:59 +00:00
2024-05-19 00:27:27 +00:00
// TODO: Clean this up
var textureInfo = new TextureCreateInfo((int)drawable.Texture.Width, (int)drawable.Texture.Height, (int)drawable.Texture.Depth, (int)drawable.Texture.MipmapLevelCount, (int)drawable.Texture.SampleCount, 0, 0, 0, Format.B8G8R8A8Unorm, 0, Target.Texture2D, SwizzleComponent.Red, SwizzleComponent.Green, SwizzleComponent.Blue, SwizzleComponent.Alpha);
var dest = new Texture(_device, this, textureInfo, drawable.Texture, 0, 0);
_helperShader.BlitColor(tex, dest);
2023-08-02 23:56:59 +00:00
EndCurrentPass();
2023-08-02 23:56:59 +00:00
_commandBuffer.PresentDrawable(drawable);
_commandBuffer.Commit();
2023-08-03 21:04:59 +00:00
_commandBuffer = _commandQueue.CommandBuffer();
RestoreState();
2024-05-25 12:38:39 +00:00
// Cleanup
dest.Dispose();
}
2024-05-18 22:54:55 +00:00
public void Barrier()
2024-03-20 20:37:08 +00:00
{
2024-05-24 16:37:31 +00:00
if (_currentEncoderType == EncoderType.Render)
{
var renderCommandEncoder = GetOrCreateRenderEncoder();
var scope = MTLBarrierScope.Buffers | MTLBarrierScope.Textures | MTLBarrierScope.RenderTargets;
MTLRenderStages stages = MTLRenderStages.RenderStageVertex | MTLRenderStages.RenderStageFragment;
renderCommandEncoder.MemoryBarrier(scope, stages, stages);
2024-05-25 16:12:58 +00:00
}
else if (_currentEncoderType == EncoderType.Compute)
2024-05-24 16:37:31 +00:00
{
var computeCommandEncoder = GetOrCreateComputeEncoder();
// TODO: Should there be a barrier on render targets?
var scope = MTLBarrierScope.Buffers | MTLBarrierScope.Textures;
computeCommandEncoder.MemoryBarrier(scope);
2024-05-25 16:12:58 +00:00
}
else
2024-05-24 16:41:36 +00:00
{
Logger.Warning?.Print(LogClass.Gpu, "Barrier called outside of a render or compute pass");
2024-05-24 16:37:31 +00:00
}
2024-03-20 20:37:08 +00:00
}
public void ClearBuffer(BufferHandle destination, int offset, int size, uint value)
{
var blitCommandEncoder = GetOrCreateBlitEncoder();
2023-07-28 20:51:07 +00:00
// Might need a closer look, range's count, lower, and upper bound
// must be a multiple of 4
MTLBuffer mtlBuffer = new(Unsafe.As<BufferHandle, IntPtr>(ref destination));
blitCommandEncoder.FillBuffer(mtlBuffer,
new NSRange
{
location = (ulong)offset,
length = (ulong)size
},
(byte)value);
}
public void ClearRenderTargetColor(int index, int layer, int layerCount, uint componentMask, ColorF color)
{
2024-05-24 12:41:51 +00:00
float[] colors = [color.Red, color.Green, color.Blue, color.Alpha];
2024-05-24 15:59:49 +00:00
_helperShader.ClearColor(index, colors);
}
2024-05-18 22:54:55 +00:00
public void ClearRenderTargetDepthStencil(int layer, int layerCount, float depthValue, bool depthMask, int stencilValue, int stencilMask)
{
2024-05-24 15:59:49 +00:00
_helperShader.ClearDepthStencil([depthValue], depthMask, stencilValue, stencilMask);
}
public void CommandBufferBarrier()
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void CopyBuffer(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
{
var blitCommandEncoder = GetOrCreateBlitEncoder();
2023-07-29 03:56:33 +00:00
MTLBuffer sourceBuffer = new(Unsafe.As<BufferHandle, IntPtr>(ref source));
MTLBuffer destinationBuffer = new(Unsafe.As<BufferHandle, IntPtr>(ref destination));
blitCommandEncoder.CopyFromBuffer(
sourceBuffer,
(ulong)srcOffset,
destinationBuffer,
(ulong)dstOffset,
(ulong)size);
}
public void DispatchCompute(int groupsX, int groupsY, int groupsZ)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
{
var renderCommandEncoder = GetOrCreateRenderEncoder();
2023-07-28 20:23:13 +00:00
// TODO: Support topology re-indexing to provide support for TriangleFans
2024-05-18 22:54:55 +00:00
var primitiveType = _encoderStateManager.Topology.Convert();
2024-05-18 22:54:55 +00:00
renderCommandEncoder.DrawPrimitives(
primitiveType,
(ulong)firstVertex,
(ulong)vertexCount,
(ulong)instanceCount,
(ulong)firstInstance);
}
public void DrawIndexed(int indexCount, int instanceCount, int firstIndex, int firstVertex, int firstInstance)
{
var renderCommandEncoder = GetOrCreateRenderEncoder();
2023-08-03 12:58:14 +00:00
// TODO: Support topology re-indexing to provide support for TriangleFans
2024-05-18 22:54:55 +00:00
var primitiveType = _encoderStateManager.Topology.Convert();
2024-05-18 22:54:55 +00:00
renderCommandEncoder.DrawIndexedPrimitives(
primitiveType,
(ulong)indexCount,
_encoderStateManager.IndexType,
_encoderStateManager.IndexBuffer,
_encoderStateManager.IndexBufferOffset,
(ulong)instanceCount,
firstVertex,
(ulong)firstInstance);
}
public void DrawIndexedIndirect(BufferRange indirectBuffer)
{
2024-03-19 20:23:43 +00:00
// var renderCommandEncoder = GetOrCreateRenderEncoder();
2024-03-19 18:05:09 +00:00
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void DrawIndexedIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
{
2024-03-19 20:23:43 +00:00
// var renderCommandEncoder = GetOrCreateRenderEncoder();
2024-03-19 18:05:09 +00:00
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void DrawIndirect(BufferRange indirectBuffer)
{
2024-03-19 20:23:43 +00:00
// var renderCommandEncoder = GetOrCreateRenderEncoder();
2024-03-19 18:05:09 +00:00
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void DrawIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
{
2024-03-19 20:23:43 +00:00
// var renderCommandEncoder = GetOrCreateRenderEncoder();
2024-03-19 18:05:09 +00:00
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void DrawTexture(ITexture texture, ISampler sampler, Extents2DF srcRegion, Extents2DF dstRegion)
{
2024-03-19 20:23:43 +00:00
// var renderCommandEncoder = GetOrCreateRenderEncoder();
2024-03-19 18:05:09 +00:00
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetAlphaTest(bool enable, float reference, CompareOp op)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetBlendState(AdvancedBlendDescriptor blend)
{
// Metal does not support advanced blend.
}
public void SetBlendState(int index, BlendDescriptor blend)
{
2024-05-19 01:20:15 +00:00
_encoderStateManager.UpdateBlendDescriptors(index, blend);
}
public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetDepthClamp(bool clamp)
{
2024-05-19 01:29:46 +00:00
_encoderStateManager.UpdateDepthClamp(clamp);
}
public void SetDepthMode(DepthMode mode)
{
2024-05-19 01:02:49 +00:00
// Metal does not support depth clip control.
}
public void SetDepthTest(DepthTestDescriptor depthTest)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdateDepthState(depthTest);
}
public void SetFaceCulling(bool enable, Face face)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdateCullMode(enable, face);
}
public void SetFrontFace(FrontFace frontFace)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdateFrontFace(frontFace);
}
public void SetIndexBuffer(BufferRange buffer, IndexType type)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdateIndexBuffer(buffer, type);
}
2024-03-18 18:32:59 +00:00
public void SetImage(ShaderStage stage, int binding, ITexture texture, Format imageFormat)
{
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
2024-04-22 21:44:55 +00:00
public void SetImageArray(ShaderStage stage, int binding, IImageArray array)
{
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetLineParameters(float width, bool smooth)
{
// Metal does not support wide-lines.
}
public void SetLogicOpState(bool enable, LogicalOp op)
{
// Metal does not support logic operations.
}
public void SetMultisampleState(MultisampleDescriptor multisample)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetPatchParameters(int vertices, ReadOnlySpan<float> defaultOuterLevel, ReadOnlySpan<float> defaultInnerLevel)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetPointParameters(float size, bool isProgramPointSize, bool enablePointSprite, Origin origin)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetPolygonMode(PolygonMode frontMode, PolygonMode backMode)
{
// Metal does not support polygon mode.
}
public void SetPrimitiveRestart(bool enable, int index)
{
// TODO: Supported for LineStrip and TriangleStrip
// https://github.com/gpuweb/gpuweb/issues/1220#issuecomment-732483263
// https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515520-drawindexedprimitives
// https://stackoverflow.com/questions/70813665/how-to-render-multiple-trianglestrips-using-metal
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetPrimitiveTopology(PrimitiveTopology topology)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdatePrimitiveTopology(topology);
}
public void SetProgram(IProgram program)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdateProgram(program);
}
public void SetRasterizerDiscard(bool discard)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMask)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdateRenderTargets(colors, depthStencil);
}
2024-05-18 22:54:55 +00:00
public void SetScissors(ReadOnlySpan<Rectangle<int>> regions)
{
2024-05-24 13:52:17 +00:00
_encoderStateManager.UpdateScissors(regions);
}
public void SetStencilTest(StencilTestDescriptor stencilTest)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdateStencilState(stencilTest);
}
2023-07-29 04:30:08 +00:00
2024-05-18 22:54:55 +00:00
public void SetUniformBuffers(ReadOnlySpan<BufferAssignment> buffers)
{
_encoderStateManager.UpdateUniformBuffers(buffers);
}
public void SetStorageBuffers(ReadOnlySpan<BufferAssignment> buffers)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdateStorageBuffers(buffers);
}
public void SetTextureAndSampler(ShaderStage stage, int binding, ITexture texture, ISampler sampler)
{
2024-03-19 19:07:35 +00:00
if (texture is Texture tex)
{
if (sampler is Sampler samp)
{
var mtlTexture = tex.MTLTexture;
var mtlSampler = samp.GetSampler();
var index = (ulong)binding;
switch (stage)
{
case ShaderStage.Vertex:
2024-05-18 22:54:55 +00:00
case ShaderStage.Fragment:
_encoderStateManager.UpdateTextureAndSampler(stage, index, mtlTexture, mtlSampler);
2024-03-19 19:07:35 +00:00
break;
case ShaderStage.Compute:
2024-05-18 22:54:55 +00:00
var computeCommandEncoder = GetOrCreateComputeEncoder();
2024-03-19 19:07:35 +00:00
computeCommandEncoder.SetTexture(mtlTexture, index);
computeCommandEncoder.SetSamplerState(mtlSampler, index);
break;
default:
throw new ArgumentOutOfRangeException(nameof(stage), stage, "Unsupported shader stage!");
}
}
}
}
2024-04-22 21:44:55 +00:00
public void SetTextureArray(ShaderStage stage, int binding, ITextureArray array)
{
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetUserClipDistance(int index, bool enableClip)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdateVertexAttribs(vertexAttribs);
}
public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
{
2024-05-18 22:54:55 +00:00
_encoderStateManager.UpdateVertexBuffers(vertexBuffers);
}
2024-05-18 22:54:55 +00:00
public void SetViewports(ReadOnlySpan<Viewport> viewports)
{
2024-05-24 13:52:17 +00:00
_encoderStateManager.UpdateViewports(viewports);
}
public void TextureBarrier()
{
2024-05-23 15:49:22 +00:00
var renderCommandEncoder = GetOrCreateRenderEncoder();
2024-03-19 18:05:09 +00:00
2024-05-23 15:49:22 +00:00
renderCommandEncoder.MemoryBarrier(MTLBarrierScope.Textures, MTLRenderStages.RenderStageFragment, MTLRenderStages.RenderStageFragment);
}
public void TextureBarrierTiled()
{
2024-05-23 16:23:01 +00:00
TextureBarrier();
}
public bool TryHostConditionalRendering(ICounterEvent value, ulong compare, bool isEqual)
{
// TODO: Implementable via indirect draw commands
return false;
}
public bool TryHostConditionalRendering(ICounterEvent value, ICounterEvent compare, bool isEqual)
{
// TODO: Implementable via indirect draw commands
return false;
}
public void EndHostConditionalRendering()
{
// TODO: Implementable via indirect draw commands
}
public void BeginTransformFeedback(PrimitiveTopology topology)
{
// Metal does not support transform feedback.
}
public void EndTransformFeedback()
{
// Metal does not support transform feedback.
}
public void SetTransformFeedbackBuffers(ReadOnlySpan<BufferRange> buffers)
{
// Metal does not support transform feedback.
}
public void Dispose()
{
2024-01-27 21:09:24 +00:00
EndCurrentPass();
2024-05-25 06:27:28 +00:00
_encoderStateManager.Dispose();
}
}
}