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;
|
2023-08-02 23:56:59 +00:00
|
|
|
using SharpMetal.QuartzCore;
|
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")]
|
2023-08-03 18:50:49 +00:00
|
|
|
class Pipeline : IPipeline, IDisposable
|
2024-08-31 20:42:56 +00:00
|
|
|
{
|
2023-08-03 21:04:59 +00:00
|
|
|
// 0 Frames = No capture
|
|
|
|
// Some games like Undertale trigger a stack overflow on capture end
|
2023-08-03 23:01:34 +00:00
|
|
|
private const int MaxFramesPerCapture = 0;
|
2023-08-03 21:04:59 +00:00
|
|
|
private const string CaptureLocation = "/Users/isaacmarovitz/Desktop/Captures/Trace-";
|
2023-08-03 20:53:53 +00:00
|
|
|
|
2023-07-28 02:54:24 +00:00
|
|
|
private readonly MTLDevice _device;
|
2023-08-03 21:04:59 +00:00
|
|
|
private readonly MTLCommandQueue _commandQueue;
|
2023-08-03 12:48:41 +00:00
|
|
|
private readonly HelperShaders _helperShaders;
|
2023-07-28 02:54:24 +00:00
|
|
|
|
2024-08-31 20:42:56 +00:00
|
|
|
private MTLCommandBuffer _commandBuffer;
|
2023-07-28 20:23:13 +00:00
|
|
|
private MTLCommandEncoder _currentEncoder;
|
2023-10-12 00:19:28 +00:00
|
|
|
private MTLTexture[] _renderTargets = Array.Empty<MTLTexture>();
|
2023-07-28 02:54:24 +00:00
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
private RenderEncoderState _renderEncoderState;
|
2023-10-11 04:39:18 +00:00
|
|
|
private readonly MTLVertexDescriptor _vertexDescriptor = new();
|
2023-10-12 00:19:28 +00:00
|
|
|
private MTLBuffer[] _vertexBuffers = Array.Empty<MTLBuffer>();
|
2023-07-28 01:51:20 +00:00
|
|
|
|
|
|
|
private MTLBuffer _indexBuffer;
|
|
|
|
private MTLIndexType _indexType;
|
|
|
|
private ulong _indexBufferOffset;
|
2023-08-03 01:18:28 +00:00
|
|
|
private MTLClearColor _clearColor;
|
2023-08-03 20:47:10 +00:00
|
|
|
private int _frameCount;
|
2023-08-03 21:04:59 +00:00
|
|
|
private bool _captureEnded = true;
|
2023-07-28 01:51:20 +00:00
|
|
|
|
2023-08-03 12:48:41 +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;
|
2023-08-03 21:04:59 +00:00
|
|
|
_commandQueue = commandQueue;
|
2023-08-03 12:48:41 +00:00
|
|
|
_helperShaders = new HelperShaders(_device);
|
2023-07-28 02:54:24 +00:00
|
|
|
|
2023-10-10 19:26:40 +00:00
|
|
|
_renderEncoderState = new RenderEncoderState(
|
|
|
|
_helperShaders.BlitShader.VertexFunction,
|
|
|
|
_helperShaders.BlitShader.FragmentFunction,
|
|
|
|
_device);
|
2024-08-31 20:42:56 +00:00
|
|
|
|
2023-08-03 21:04:59 +00:00
|
|
|
_commandBuffer = _commandQueue.CommandBuffer();
|
|
|
|
|
|
|
|
if (MaxFramesPerCapture > 0)
|
|
|
|
{
|
|
|
|
StartCapture();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void StartCapture()
|
|
|
|
{
|
|
|
|
var captureDescriptor = new MTLCaptureDescriptor
|
|
|
|
{
|
|
|
|
CaptureObject = _commandQueue,
|
|
|
|
Destination = MTLCaptureDestination.GPUTraceDocument,
|
|
|
|
OutputURL = NSURL.FileURLWithPath(StringHelper.NSString(CaptureLocation + DateTimeOffset.UtcNow.ToUnixTimeSeconds() + ".gputrace"))
|
|
|
|
};
|
|
|
|
var captureError = new NSError(IntPtr.Zero);
|
|
|
|
MTLCaptureManager.SharedCaptureManager().StartCapture(captureDescriptor, ref captureError);
|
|
|
|
if (captureError != IntPtr.Zero)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Failed to start capture! {StringHelper.String(captureError.LocalizedDescription)}");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_captureEnded = false;
|
2023-07-28 02:54:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 20:47:10 +00:00
|
|
|
public MTLRenderCommandEncoder GetOrCreateRenderEncoder()
|
|
|
|
{
|
|
|
|
if (_currentEncoder is MTLRenderCommandEncoder encoder)
|
|
|
|
{
|
|
|
|
return encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BeginRenderPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
public MTLBlitCommandEncoder GetOrCreateBlitEncoder()
|
|
|
|
{
|
|
|
|
if (_currentEncoder is MTLBlitCommandEncoder encoder)
|
|
|
|
{
|
|
|
|
return encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BeginBlitPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
public MTLComputeCommandEncoder GetOrCreateComputeEncoder()
|
|
|
|
{
|
|
|
|
if (_currentEncoder is MTLComputeCommandEncoder encoder)
|
|
|
|
{
|
|
|
|
return encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_currentEncoder.EndEncoding();
|
|
|
|
_currentEncoder = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public MTLRenderCommandEncoder BeginRenderPass()
|
|
|
|
{
|
|
|
|
EndCurrentPass();
|
|
|
|
|
2023-08-02 02:36:07 +00:00
|
|
|
var descriptor = new MTLRenderPassDescriptor();
|
2023-10-12 00:19:28 +00:00
|
|
|
for (int i = 0; i < _renderTargets.Length; i++)
|
|
|
|
{
|
|
|
|
if (_renderTargets[i] != null)
|
|
|
|
{
|
|
|
|
var attachment = descriptor.ColorAttachments.Object((ulong)i);
|
|
|
|
attachment.Texture = _renderTargets[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor);
|
2023-10-10 22:36:52 +00:00
|
|
|
_renderEncoderState.SetEncoderState(renderCommandEncoder, _vertexDescriptor);
|
2023-07-28 02:54:24 +00:00
|
|
|
|
2023-10-10 23:02:38 +00:00
|
|
|
for (int i = 0; i < _vertexBuffers.Length; i++)
|
|
|
|
{
|
|
|
|
if (_vertexBuffers[i] != null)
|
|
|
|
{
|
|
|
|
renderCommandEncoder.SetVertexBuffer(_vertexBuffers[i], 0, (ulong)i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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
|
|
|
|
2023-08-02 23:56:59 +00:00
|
|
|
var descriptor = new MTLRenderPassDescriptor();
|
|
|
|
descriptor.ColorAttachments.Object(0).Texture = drawable.Texture;
|
|
|
|
descriptor.ColorAttachments.Object(0).LoadAction = MTLLoadAction.Clear;
|
|
|
|
descriptor.ColorAttachments.Object(0).ClearColor = _clearColor;
|
|
|
|
|
|
|
|
var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor);
|
2023-10-10 22:36:52 +00:00
|
|
|
_renderEncoderState.SetEncoderState(renderCommandEncoder, _vertexDescriptor);
|
2023-08-02 23:56:59 +00:00
|
|
|
|
2023-08-03 12:58:14 +00:00
|
|
|
var sampler = _device.NewSamplerState(new MTLSamplerDescriptor
|
|
|
|
{
|
|
|
|
MinFilter = MTLSamplerMinMagFilter.Nearest,
|
|
|
|
MagFilter = MTLSamplerMinMagFilter.Nearest,
|
|
|
|
MipFilter = MTLSamplerMipFilter.NotMipmapped
|
|
|
|
});
|
|
|
|
|
2023-08-03 18:50:49 +00:00
|
|
|
renderCommandEncoder.SetFragmentTexture(tex.MTLTexture, 0);
|
2023-08-03 12:58:14 +00:00
|
|
|
renderCommandEncoder.SetFragmentSamplerState(sampler, 0);
|
2023-08-02 23:56:59 +00:00
|
|
|
|
|
|
|
renderCommandEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, 6);
|
|
|
|
renderCommandEncoder.EndEncoding();
|
|
|
|
|
|
|
|
_commandBuffer.PresentDrawable(drawable);
|
|
|
|
_commandBuffer.Commit();
|
|
|
|
|
2023-08-03 20:47:10 +00:00
|
|
|
if (!_captureEnded)
|
2023-08-02 23:56:59 +00:00
|
|
|
{
|
2023-08-03 20:47:10 +00:00
|
|
|
_frameCount++;
|
|
|
|
|
2023-08-03 20:53:53 +00:00
|
|
|
if (_frameCount >= MaxFramesPerCapture)
|
2023-08-03 20:47:10 +00:00
|
|
|
{
|
|
|
|
_captureEnded = true;
|
|
|
|
MTLCaptureManager.SharedCaptureManager().StopCapture();
|
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Trace ended!");
|
|
|
|
}
|
2023-08-02 23:56:59 +00:00
|
|
|
}
|
2023-07-28 02:54:24 +00:00
|
|
|
|
2023-08-03 21:04:59 +00:00
|
|
|
_commandBuffer = _commandQueue.CommandBuffer();
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Barrier()
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ClearBuffer(BufferHandle destination, int offset, int size, uint value)
|
|
|
|
{
|
2023-08-03 20:47:10 +00:00
|
|
|
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);
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ClearRenderTargetColor(int index, int layer, int layerCount, uint componentMask, ColorF color)
|
|
|
|
{
|
2023-08-03 18:50:49 +00:00
|
|
|
_clearColor = new MTLClearColor { red = color.Red, green = color.Green, blue = color.Blue, alpha = color.Alpha };
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ClearRenderTargetDepthStencil(int layer, int layerCount, float depthValue, bool depthMask, int stencilValue,
|
|
|
|
int stencilMask)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CommandBufferBarrier()
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyBuffer(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
|
|
|
|
{
|
2023-08-03 20:47:10 +00:00
|
|
|
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);
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DispatchCompute(int groupsX, int groupsY, int groupsZ)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
|
|
|
|
{
|
2023-08-03 20:47:10 +00:00
|
|
|
var renderCommandEncoder = GetOrCreateRenderEncoder();
|
2023-07-28 20:23:13 +00:00
|
|
|
|
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-08-03 20:47:10 +00:00
|
|
|
var renderCommandEncoder = GetOrCreateRenderEncoder();
|
2023-08-03 12:58:14 +00:00
|
|
|
|
2023-07-28 20:23:13 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawIndexedIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawIndirect(BufferRange indirectBuffer)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawTexture(ITexture texture, ISampler sampler, Extents2DF srcRegion, Extents2DF dstRegion)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetAlphaTest(bool enable, float reference, CompareOp op)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetBlendState(AdvancedBlendDescriptor blend)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetBlendState(int index, BlendDescriptor blend)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetDepthClamp(bool clamp)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetDepthMode(DepthMode mode)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetDepthTest(DepthTestDescriptor depthTest)
|
|
|
|
{
|
2023-07-29 04:46:13 +00:00
|
|
|
var depthStencilState = _renderEncoderState.UpdateDepthState(
|
2023-08-07 11:42:41 +00:00
|
|
|
depthTest.TestEnable ? depthTest.Func.Convert() : MTLCompareFunction.Always,
|
2023-07-29 04:46:13 +00:00
|
|
|
depthTest.WriteEnable);
|
|
|
|
|
|
|
|
if (_currentEncoder is MTLRenderCommandEncoder renderCommandEncoder)
|
|
|
|
{
|
|
|
|
renderCommandEncoder.SetDepthStencilState(depthStencilState);
|
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetLineParameters(float width, bool smooth)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// Not supported in Metal
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
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
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetMultisampleState(MultisampleDescriptor multisample)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetPolygonMode(PolygonMode frontMode, PolygonMode backMode)
|
|
|
|
{
|
2023-07-28 01:51:20 +00:00
|
|
|
// Not supported in Metal
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
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
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
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)
|
|
|
|
{
|
2023-10-10 19:26:40 +00:00
|
|
|
Program prg = (Program)program;
|
|
|
|
|
2024-01-27 21:09:16 +00:00
|
|
|
if (prg.VertexFunction == null)
|
|
|
|
{
|
|
|
|
Logger.Error?.PrintMsg(LogClass.Gpu, "Invalid Vertex Function!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-10 19:26:40 +00:00
|
|
|
_renderEncoderState = new RenderEncoderState(
|
|
|
|
prg.VertexFunction,
|
|
|
|
prg.FragmentFunction,
|
|
|
|
_device);
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetRasterizerDiscard(bool discard)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMask)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
|
|
|
|
{
|
2023-10-12 00:19:28 +00:00
|
|
|
_renderTargets = new MTLTexture[colors.Length];
|
|
|
|
|
|
|
|
for (int i = 0; i < colors.Length; i++)
|
|
|
|
{
|
|
|
|
if (colors[i] is not Texture tex)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tex.MTLTexture != null)
|
|
|
|
{
|
|
|
|
_renderTargets[i] = tex.MTLTexture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recreate Render Command Encoder
|
|
|
|
BeginRenderPass();
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
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-29 04:46:13 +00:00
|
|
|
var backFace = new MTLStencilDescriptor
|
2023-07-29 04:30:08 +00:00
|
|
|
{
|
2023-07-29 04:46:13 +00:00
|
|
|
StencilFailureOperation = stencilTest.BackSFail.Convert(),
|
|
|
|
DepthFailureOperation = stencilTest.BackDpFail.Convert(),
|
|
|
|
DepthStencilPassOperation = stencilTest.BackDpPass.Convert(),
|
|
|
|
StencilCompareFunction = stencilTest.BackFunc.Convert(),
|
|
|
|
ReadMask = (uint)stencilTest.BackFuncMask,
|
|
|
|
WriteMask = (uint)stencilTest.BackMask
|
|
|
|
};
|
|
|
|
|
|
|
|
var frontFace = new MTLStencilDescriptor
|
|
|
|
{
|
|
|
|
StencilFailureOperation = stencilTest.FrontSFail.Convert(),
|
|
|
|
DepthFailureOperation = stencilTest.FrontDpFail.Convert(),
|
|
|
|
DepthStencilPassOperation = stencilTest.FrontDpPass.Convert(),
|
|
|
|
StencilCompareFunction = stencilTest.FrontFunc.Convert(),
|
|
|
|
ReadMask = (uint)stencilTest.FrontFuncMask,
|
|
|
|
WriteMask = (uint)stencilTest.FrontMask
|
2023-07-29 04:30:08 +00:00
|
|
|
};
|
|
|
|
|
2023-07-29 04:46:13 +00:00
|
|
|
var depthStencilState = _renderEncoderState.UpdateStencilState(backFace, frontFace);
|
2023-07-29 04:30:08 +00:00
|
|
|
|
|
|
|
if (_currentEncoder is MTLRenderCommandEncoder renderCommandEncoder)
|
|
|
|
{
|
|
|
|
renderCommandEncoder.SetDepthStencilState(depthStencilState);
|
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetStorageBuffers(ReadOnlySpan<BufferAssignment> buffers)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetTextureAndSampler(ShaderStage stage, int binding, ITexture texture, ISampler sampler)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetUniformBuffers(ReadOnlySpan<BufferAssignment> buffers)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetUserClipDistance(int index, bool enableClip)
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
|
|
|
|
{
|
2023-10-10 22:36:52 +00:00
|
|
|
for (int i = 0; i < vertexAttribs.Length; i++)
|
|
|
|
{
|
|
|
|
if (!vertexAttribs[i].IsZero)
|
|
|
|
{
|
|
|
|
// TODO: Format should not be hardcoded
|
|
|
|
var attrib = _vertexDescriptor.Attributes.Object((ulong)i);
|
|
|
|
attrib.Format = MTLVertexFormat.Float4;
|
|
|
|
attrib.BufferIndex = (ulong)vertexAttribs[i].BufferIndex;
|
|
|
|
attrib.Offset = (ulong)vertexAttribs[i].Offset;
|
|
|
|
}
|
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
|
|
|
|
{
|
2023-10-10 23:02:38 +00:00
|
|
|
_vertexBuffers = new MTLBuffer[vertexBuffers.Length];
|
|
|
|
|
2023-10-10 22:36:52 +00:00
|
|
|
for (int i = 0; i < vertexBuffers.Length; i++)
|
|
|
|
{
|
|
|
|
if (vertexBuffers[i].Stride != 0)
|
|
|
|
{
|
|
|
|
_vertexDescriptor.Layouts.Object((ulong)i).Stride = (ulong)vertexBuffers[i].Stride;
|
2023-10-10 23:02:38 +00:00
|
|
|
_vertexBuffers[i] = _device.NewBuffer(
|
|
|
|
vertexBuffers[i].Buffer.Handle.ToIntPtr(),
|
|
|
|
(ulong)vertexBuffers[i].Buffer.Size,
|
|
|
|
MTLResourceOptions.ResourceStorageModeManaged);
|
2023-10-10 22:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void TextureBarrierTiled()
|
|
|
|
{
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void EndTransformFeedback()
|
|
|
|
{
|
|
|
|
// Metal does not support Transform Feedback
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetTransformFeedbackBuffers(ReadOnlySpan<BufferRange> buffers)
|
|
|
|
{
|
|
|
|
// Metal does not support Transform Feedback
|
2023-08-02 02:36:07 +00:00
|
|
|
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2024-01-27 21:09:24 +00:00
|
|
|
EndCurrentPass();
|
2024-08-31 20:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-28 01:51:20 +00:00
|
|
|
}
|