2023-07-28 20:39:14 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2024-08-31 20:42:56 +00:00
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using Ryujinx.Graphics.Shader;
|
2023-07-28 01:51:20 +00:00
|
|
|
using SharpMetal.Foundation;
|
|
|
|
using SharpMetal.Metal;
|
2024-08-31 20:42:56 +00:00
|
|
|
using System;
|
2023-07-28 01:51:20 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2024-08-31 20:42:56 +00:00
|
|
|
using System.Runtime.Versioning;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Metal
|
|
|
|
{
|
|
|
|
[SupportedOSPlatform("macos")]
|
|
|
|
public class Pipeline : IPipeline, IDisposable
|
|
|
|
{
|
2023-07-28 02:54:24 +00:00
|
|
|
private readonly MTLDevice _device;
|
|
|
|
private readonly MTLCommandQueue _mtlCommandQueue;
|
|
|
|
|
2024-08-31 20:42:56 +00:00
|
|
|
private MTLCommandBuffer _commandBuffer;
|
2023-07-28 20:23:13 +00:00
|
|
|
private MTLCommandEncoder _currentEncoder;
|
2023-07-28 02:54:24 +00:00
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
public MTLCommandEncoder CurrentEncoder;
|
2024-08-31 20:42:56 +00:00
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
private RenderEncoderState _renderEncoderState;
|
2023-07-28 01:51:20 +00:00
|
|
|
|
|
|
|
private MTLBuffer _indexBuffer;
|
|
|
|
private MTLIndexType _indexType;
|
|
|
|
private ulong _indexBufferOffset;
|
|
|
|
|
2023-07-28 02:54:24 +00:00
|
|
|
public Pipeline(MTLDevice device, MTLCommandQueue commandQueue)
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2023-07-28 02:54:24 +00:00
|
|
|
_device = device;
|
|
|
|
_mtlCommandQueue = commandQueue;
|
|
|
|
|
2024-08-31 20:42:56 +00:00
|
|
|
var renderPipelineDescriptor = new MTLRenderPipelineDescriptor();
|
2023-07-28 01:51:20 +00:00
|
|
|
var error = new NSError(IntPtr.Zero);
|
2023-07-28 20:23:13 +00:00
|
|
|
_renderEncoderState = new(_device.NewRenderPipelineState(renderPipelineDescriptor, ref error));
|
2023-07-28 01:51:20 +00:00
|
|
|
if (error != IntPtr.Zero)
|
|
|
|
{
|
2023-07-28 20:39:14 +00:00
|
|
|
Logger.Error?.PrintMsg(LogClass.Gpu, $"Failed to create Render Pipeline State: {StringHelper.String(error.LocalizedDescription)}");
|
2023-07-28 01:51:20 +00:00
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
_commandBuffer = _mtlCommandQueue.CommandBuffer();
|
2023-07-28 02:54:24 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_currentEncoder.EndEncoding();
|
|
|
|
_currentEncoder = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public MTLRenderCommandEncoder BeginRenderPass()
|
|
|
|
{
|
|
|
|
EndCurrentPass();
|
|
|
|
|
|
|
|
var descriptor = new MTLRenderPassDescriptor { };
|
|
|
|
var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor);
|
|
|
|
_renderEncoderState.SetEncoderState(renderCommandEncoder);
|
2023-07-28 02:54:24 +00:00
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
_currentEncoder = renderCommandEncoder;
|
|
|
|
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();
|
|
|
|
|
|
|
|
var descriptor = new MTLBlitPassDescriptor { };
|
|
|
|
var blitCommandEncoder = _commandBuffer.BlitCommandEncoder(descriptor);
|
|
|
|
|
|
|
|
_currentEncoder = blitCommandEncoder;
|
|
|
|
return blitCommandEncoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Present()
|
|
|
|
{
|
|
|
|
EndCurrentPass();
|
2023-07-28 02:54:24 +00:00
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
// TODO: Give command buffer a valid MTLDrawable
|
|
|
|
// _commandBuffer.PresentDrawable();
|
|
|
|
_commandBuffer.Commit();
|
2023-07-28 02:54:24 +00:00
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
_commandBuffer = _mtlCommandQueue.CommandBuffer();
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Barrier()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ClearBuffer(BufferHandle destination, int offset, int size, uint value)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ClearRenderTargetColor(int index, int layer, int layerCount, uint componentMask, ColorF color)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ClearRenderTargetDepthStencil(int layer, int layerCount, float depthValue, bool depthMask, int stencilValue,
|
|
|
|
int stencilMask)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CommandBufferBarrier()
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// TODO: Only required for MTLHeap or untracked resources
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyBuffer(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DispatchCompute(int groupsX, int groupsY, int groupsZ)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
|
|
|
|
{
|
2023-07-28 20:23:13 +00:00
|
|
|
MTLRenderCommandEncoder renderCommandEncoder;
|
|
|
|
|
|
|
|
if (_currentEncoder is MTLRenderCommandEncoder encoder)
|
|
|
|
{
|
|
|
|
renderCommandEncoder = encoder;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
renderCommandEncoder = BeginRenderPass();
|
|
|
|
}
|
|
|
|
|
2023-07-28 01:51:20 +00:00
|
|
|
// TODO: Support topology re-indexing to provide support for TriangleFans
|
2023-07-28 20:23:13 +00:00
|
|
|
var primitiveType = _renderEncoderState.Topology.Convert();
|
2023-07-28 01:51:20 +00:00
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
renderCommandEncoder.DrawPrimitives(primitiveType, (ulong)firstVertex, (ulong)vertexCount, (ulong)instanceCount, (ulong)firstInstance);
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawIndexed(int indexCount, int instanceCount, int firstIndex, int firstVertex, int firstInstance)
|
|
|
|
{
|
2023-07-28 20:23:13 +00:00
|
|
|
MTLRenderCommandEncoder renderCommandEncoder;
|
|
|
|
|
|
|
|
if (_currentEncoder is MTLRenderCommandEncoder encoder)
|
|
|
|
{
|
|
|
|
renderCommandEncoder = encoder;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
renderCommandEncoder = BeginRenderPass();
|
|
|
|
}
|
|
|
|
|
2023-07-28 01:51:20 +00:00
|
|
|
// TODO: Support topology re-indexing to provide support for TriangleFans
|
2023-07-28 20:23:13 +00:00
|
|
|
var primitiveType = _renderEncoderState.Topology.Convert();
|
2023-07-28 01:51:20 +00:00
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
renderCommandEncoder.DrawIndexedPrimitives(primitiveType, (ulong)indexCount, _indexType, _indexBuffer, _indexBufferOffset, (ulong)instanceCount, firstVertex, (ulong)firstInstance);
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawIndexedIndirect(BufferRange indirectBuffer)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawIndexedIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawIndirect(BufferRange indirectBuffer)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawTexture(ITexture texture, ISampler sampler, Extents2DF srcRegion, Extents2DF dstRegion)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetAlphaTest(bool enable, float reference, CompareOp op)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// Metal does not support alpha test.
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetBlendState(AdvancedBlendDescriptor blend)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetBlendState(int index, BlendDescriptor blend)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetDepthClamp(bool clamp)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetDepthMode(DepthMode mode)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetDepthTest(DepthTestDescriptor depthTest)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetFaceCulling(bool enable, Face face)
|
|
|
|
{
|
2023-07-28 20:23:13 +00:00
|
|
|
var cullMode = enable ? face.Convert() : MTLCullMode.None;
|
|
|
|
|
|
|
|
if (_currentEncoder is MTLRenderCommandEncoder renderCommandEncoder)
|
|
|
|
{
|
|
|
|
renderCommandEncoder.SetCullMode(cullMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
_renderEncoderState.CullMode = cullMode;
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetFrontFace(FrontFace frontFace)
|
|
|
|
{
|
2023-07-28 20:23:13 +00:00
|
|
|
var winding = frontFace.Convert();
|
|
|
|
|
|
|
|
if (_currentEncoder is MTLRenderCommandEncoder renderCommandEncoder)
|
|
|
|
{
|
|
|
|
renderCommandEncoder.SetFrontFacingWinding(winding);
|
|
|
|
}
|
|
|
|
|
|
|
|
_renderEncoderState.Winding = winding;
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetIndexBuffer(BufferRange buffer, IndexType type)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
if (buffer.Handle != BufferHandle.Null)
|
|
|
|
{
|
|
|
|
_indexType = type.Convert();
|
|
|
|
_indexBufferOffset = (ulong)buffer.Offset;
|
|
|
|
var handle = buffer.Handle;
|
|
|
|
_indexBuffer = new(Unsafe.As<BufferHandle, IntPtr>(ref handle));
|
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetImage(int binding, ITexture texture, Format imageFormat)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetLineParameters(float width, bool smooth)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// Not supported in Metal
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetLogicOpState(bool enable, LogicalOp op)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// Not supported in Metal
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetMultisampleState(MultisampleDescriptor multisample)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetPatchParameters(int vertices, ReadOnlySpan<float> defaultOuterLevel, ReadOnlySpan<float> defaultInnerLevel)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetPointParameters(float size, bool isProgramPointSize, bool enablePointSprite, Origin origin)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetPolygonMode(PolygonMode frontMode, PolygonMode backMode)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// Not supported in Metal
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetPrimitiveRestart(bool enable, int index)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// 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
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetPrimitiveTopology(PrimitiveTopology topology)
|
|
|
|
{
|
2023-07-28 20:23:13 +00:00
|
|
|
_renderEncoderState.Topology = topology;
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetProgram(IProgram program)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetRasterizerDiscard(bool discard)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMask)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2023-07-28 01:51:20 +00:00
|
|
|
public unsafe void SetScissors(ReadOnlySpan<Rectangle<int>> regions)
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// TODO: Test max allowed scissor rects on device
|
|
|
|
var mtlScissorRects = new MTLScissorRect[regions.Length];
|
|
|
|
|
|
|
|
for (int i = 0; i < regions.Length; i++)
|
|
|
|
{
|
|
|
|
var region = regions[i];
|
|
|
|
mtlScissorRects[i] = new MTLScissorRect
|
|
|
|
{
|
|
|
|
height = (ulong)region.Height,
|
|
|
|
width = (ulong)region.Width,
|
|
|
|
x = (ulong)region.X,
|
|
|
|
y = (ulong)region.Y
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fixed (MTLScissorRect* pMtlScissorRects = mtlScissorRects)
|
|
|
|
{
|
|
|
|
// TODO: Fix this function which currently wont accept pointer as intended
|
|
|
|
// _renderCommandEncoder.SetScissorRects(pMtlScissorRects, regions.Length);
|
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetStencilTest(StencilTestDescriptor stencilTest)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// TODO
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetStorageBuffers(ReadOnlySpan<BufferAssignment> buffers)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetTextureAndSampler(ShaderStage stage, int binding, ITexture texture, ISampler sampler)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetUniformBuffers(ReadOnlySpan<BufferAssignment> buffers)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetUserClipDistance(int index, bool enableClip)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2023-07-28 01:51:20 +00:00
|
|
|
public unsafe void SetViewports(ReadOnlySpan<Viewport> viewports)
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// TODO: Test max allowed viewports on device
|
|
|
|
var mtlViewports = new MTLViewport[viewports.Length];
|
|
|
|
|
|
|
|
for (int i = 0; i < viewports.Length; i++)
|
|
|
|
{
|
|
|
|
var viewport = viewports[i];
|
|
|
|
mtlViewports[i] = new MTLViewport
|
|
|
|
{
|
|
|
|
originX = viewport.Region.X,
|
|
|
|
originY = viewport.Region.Y,
|
|
|
|
width = viewport.Region.Width,
|
|
|
|
height = viewport.Region.Height,
|
|
|
|
znear = viewport.DepthNear,
|
|
|
|
zfar = viewport.DepthFar
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fixed (MTLViewport* pMtlViewports = mtlViewports)
|
|
|
|
{
|
|
|
|
// TODO: Fix this function which currently wont accept pointer as intended
|
|
|
|
// _renderCommandEncoder.SetViewports(pMtlViewports, viewports.Length);
|
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void TextureBarrier()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void TextureBarrierTiled()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool TryHostConditionalRendering(ICounterEvent value, ulong compare, bool isEqual)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// TODO: Implementable via indirect draw commands
|
|
|
|
return false;
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool TryHostConditionalRendering(ICounterEvent value, ICounterEvent compare, bool isEqual)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// TODO: Implementable via indirect draw commands
|
|
|
|
return false;
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void EndHostConditionalRendering()
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// TODO: Implementable via indirect draw commands
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2023-07-28 01:51:20 +00:00
|
|
|
}
|