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

837 lines
30 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;
2024-03-20 20:37:08 +00:00
using System.Collections.Generic;
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;
2023-08-03 12:48:41 +00:00
private readonly HelperShaders _helperShaders;
2023-07-28 02:54:24 +00:00
private MTLCommandBuffer _commandBuffer;
2024-03-18 18:32:59 +00:00
private MTLCommandEncoder? _currentEncoder;
2024-03-20 02:58:42 +00:00
private EncoderType _currentEncoderType = EncoderType.None;
2024-03-18 18:32:59 +00:00
private MTLTexture[] _renderTargets = [];
2024-05-17 00:29:37 +00:00
private MTLTexture _depthTarget;
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();
2024-03-20 20:37:08 +00:00
private List<BufferInfo> _vertexBuffers = [];
private List<BufferInfo> _uniformBuffers = [];
private List<BufferInfo> _storageBuffers = [];
private MTLBuffer _indexBuffer;
private MTLIndexType _indexType;
private ulong _indexBufferOffset;
2023-08-03 01:18:28 +00:00
private MTLClearColor _clearColor;
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;
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);
2023-08-03 21:04:59 +00:00
_commandBuffer = _commandQueue.CommandBuffer();
2023-07-28 02:54:24 +00:00
}
public MTLRenderCommandEncoder GetOrCreateRenderEncoder()
{
2024-03-18 18:48:54 +00:00
if (_currentEncoder != null)
{
2024-03-18 18:48:54 +00:00
if (_currentEncoderType == EncoderType.Render)
{
return new MTLRenderCommandEncoder(_currentEncoder.Value);
}
}
return BeginRenderPass();
}
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();
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];
2024-03-20 01:15:31 +00:00
attachment.LoadAction = MTLLoadAction.Load;
2023-10-12 00:19:28 +00:00
}
}
2024-05-17 00:29:37 +00:00
var depthAttachment = descriptor.DepthAttachment;
depthAttachment.Texture = _depthTarget;
depthAttachment.LoadAction = MTLLoadAction.Load;
2023-07-28 20:23:13 +00:00
var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor);
_renderEncoderState.SetEncoderState(renderCommandEncoder, descriptor, _vertexDescriptor);
2023-07-28 02:54:24 +00:00
2024-03-20 20:37:08 +00:00
RebindBuffers(renderCommandEncoder);
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;
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
2023-08-02 23:56:59 +00:00
var descriptor = new MTLRenderPassDescriptor();
2024-03-18 18:48:54 +00:00
var colorAttachment = descriptor.ColorAttachments.Object(0);
colorAttachment.Texture = drawable.Texture;
colorAttachment.LoadAction = MTLLoadAction.Clear;
colorAttachment.ClearColor = _clearColor;
descriptor.ColorAttachments.SetObject(colorAttachment, 0);
2023-08-02 23:56:59 +00:00
var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor);
2024-03-20 01:29:14 +00:00
_renderEncoderState = new RenderEncoderState(
_helperShaders.BlitShader.VertexFunction,
_helperShaders.BlitShader.FragmentFunction,
_device);
_renderEncoderState.SetEncoderState(renderCommandEncoder, descriptor, _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 21:04:59 +00:00
_commandBuffer = _commandQueue.CommandBuffer();
}
public void Barrier()
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
2024-03-20 20:37:08 +00:00
public void RebindBuffers(MTLRenderCommandEncoder renderCommandEncoder)
{
foreach (var vertexBuffer in _vertexBuffers)
{
renderCommandEncoder.SetVertexBuffer(new MTLBuffer(vertexBuffer.Handle), (ulong)vertexBuffer.Offset, (ulong)vertexBuffer.Index);
}
foreach (var uniformBuffer in _uniformBuffers)
{
renderCommandEncoder.SetVertexBuffer(new MTLBuffer(uniformBuffer.Handle), (ulong)uniformBuffer.Offset, (ulong)uniformBuffer.Index);
renderCommandEncoder.SetFragmentBuffer(new MTLBuffer(uniformBuffer.Handle), (ulong)uniformBuffer.Offset, (ulong)uniformBuffer.Index);
}
foreach (var storageBuffer in _storageBuffers)
{
renderCommandEncoder.SetVertexBuffer(new MTLBuffer(storageBuffer.Handle), (ulong)storageBuffer.Offset, (ulong)storageBuffer.Index);
renderCommandEncoder.SetFragmentBuffer(new MTLBuffer(storageBuffer.Handle), (ulong)storageBuffer.Offset, (ulong)storageBuffer.Index);
}
}
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)
{
2023-08-03 18:50:49 +00:00
_clearColor = new MTLClearColor { red = color.Red, green = color.Green, blue = color.Blue, alpha = color.Alpha };
}
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!");
}
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
2023-07-28 20:23:13 +00:00
var primitiveType = _renderEncoderState.Topology.Convert();
2023-07-28 20:23:13 +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
2023-07-28 20:23:13 +00:00
var primitiveType = _renderEncoderState.Topology.Convert();
2023-07-28 20:23:13 +00:00
renderCommandEncoder.DrawIndexedPrimitives(primitiveType, (ulong)indexCount, _indexType, _indexBuffer, _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)
{
2024-03-20 20:37:08 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Advanced blend is not supported in Metal!");
}
public void SetBlendState(int index, BlendDescriptor blend)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
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)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetDepthMode(DepthMode mode)
{
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void SetDepthTest(DepthTestDescriptor depthTest)
{
2023-07-29 04:46:13 +00:00
var depthStencilState = _renderEncoderState.UpdateDepthState(
depthTest.TestEnable ? depthTest.Func.Convert() : MTLCompareFunction.Always,
2023-07-29 04:46:13 +00:00
depthTest.WriteEnable);
2024-03-18 18:48:54 +00:00
if (_currentEncoderType == EncoderType.Render)
2023-07-29 04:46:13 +00:00
{
2024-03-18 18:48:54 +00:00
new MTLRenderCommandEncoder(_currentEncoder.Value).SetDepthStencilState(depthStencilState);
2023-07-29 04:46:13 +00:00
}
}
public void SetFaceCulling(bool enable, Face face)
{
2023-07-28 20:23:13 +00:00
var cullMode = enable ? face.Convert() : MTLCullMode.None;
2024-03-18 18:48:54 +00:00
if (_currentEncoderType == EncoderType.Render)
2023-07-28 20:23:13 +00:00
{
2024-03-18 18:48:54 +00:00
new MTLRenderCommandEncoder(_currentEncoder.Value).SetCullMode(cullMode);
2023-07-28 20:23:13 +00:00
}
_renderEncoderState.CullMode = cullMode;
}
public void SetFrontFace(FrontFace frontFace)
{
2023-07-28 20:23:13 +00:00
var winding = frontFace.Convert();
2024-03-18 18:48:54 +00:00
if (_currentEncoderType == EncoderType.Render)
2023-07-28 20:23:13 +00:00
{
2024-03-18 18:48:54 +00:00
new MTLRenderCommandEncoder(_currentEncoder.Value).SetFrontFacingWinding(winding);
2023-07-28 20:23:13 +00:00
}
_renderEncoderState.Winding = winding;
}
public void SetIndexBuffer(BufferRange buffer, IndexType type)
{
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-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)
{
// Not supported in Metal
2024-03-19 18:05:09 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Wide-line is not supported without private Metal API");
}
public void SetLogicOpState(bool enable, LogicalOp op)
{
// Not supported in Metal
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
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)
{
// Not supported in Metal
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
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)
{
2023-07-28 20:23:13 +00:00
_renderEncoderState.Topology = topology;
}
public void SetProgram(IProgram program)
{
2023-10-10 19:26:40 +00:00
Program prg = (Program)program;
2024-03-19 18:05:09 +00:00
if (prg.VertexFunction == IntPtr.Zero)
{
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);
}
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)
{
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;
}
}
2024-05-17 00:29:37 +00:00
if (depthStencil is Texture depthTexture)
{
_depthTarget = depthTexture.MTLTexture;
}
2023-10-12 00:19:28 +00:00
// Recreate Render Command Encoder
BeginRenderPass();
}
public unsafe void SetScissors(ReadOnlySpan<Rectangle<int>> regions)
{
2024-05-17 01:06:04 +00:00
int maxScissors = Math.Min(regions.Length, _renderEncoderState.Viewports.Length);
2024-04-22 21:51:31 +00:00
if (maxScissors == 0)
{
return;
}
var mtlScissorRects = new MTLScissorRect[maxScissors];
for (int i = 0; i < maxScissors; i++)
{
var region = regions[i];
mtlScissorRects[i] = new MTLScissorRect
{
2024-05-17 01:06:04 +00:00
height = Math.Clamp((ulong)region.Height, 0, (ulong)_renderEncoderState.Viewports[i].height),
width = Math.Clamp((ulong)region.Width, 0, (ulong)_renderEncoderState.Viewports[i].width),
x = (ulong)region.X,
y = (ulong)region.Y
};
}
_renderEncoderState.UpdateScissors(mtlScissorRects);
2024-04-22 21:51:31 +00:00
if (_currentEncoderType == EncoderType.Render)
{
fixed (MTLScissorRect* pMtlScissorRects = mtlScissorRects)
{
var renderCommandEncoder = GetOrCreateRenderEncoder();
renderCommandEncoder.SetScissorRects((IntPtr)pMtlScissorRects, (ulong)regions.Length);
}
}
}
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
2024-03-18 18:48:54 +00:00
if (_currentEncoderType == EncoderType.Render)
2023-07-29 04:30:08 +00:00
{
2024-03-18 18:48:54 +00:00
new MTLRenderCommandEncoder(_currentEncoder.Value).SetDepthStencilState(depthStencilState);
2023-07-29 04:30:08 +00:00
}
}
public void SetStorageBuffers(ReadOnlySpan<BufferAssignment> buffers)
{
2024-03-20 20:37:08 +00:00
_storageBuffers = [];
foreach (BufferAssignment buffer in buffers)
{
if (buffer.Range.Size != 0)
{
2024-05-16 14:06:35 +00:00
// Offset the binding by 15
2024-03-20 20:37:08 +00:00
_storageBuffers.Add(new BufferInfo
{
Handle = buffer.Range.Handle.ToIntPtr(),
Offset = buffer.Range.Offset,
2024-05-16 14:06:35 +00:00
Index = buffer.Binding + 15
2024-03-20 20:37:08 +00:00
});
}
}
if (_currentEncoderType == EncoderType.Render)
{
var renderCommandEncoder = GetOrCreateRenderEncoder();
RebindBuffers(renderCommandEncoder);
}
}
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)
{
MTLRenderCommandEncoder renderCommandEncoder;
MTLComputeCommandEncoder computeCommandEncoder;
var mtlTexture = tex.MTLTexture;
var mtlSampler = samp.GetSampler();
var index = (ulong)binding;
switch (stage)
{
case ShaderStage.Fragment:
renderCommandEncoder = GetOrCreateRenderEncoder();
renderCommandEncoder.SetFragmentTexture(mtlTexture, index);
renderCommandEncoder.SetFragmentSamplerState(mtlSampler, index);
break;
case ShaderStage.Vertex:
renderCommandEncoder = GetOrCreateRenderEncoder();
renderCommandEncoder.SetVertexTexture(mtlTexture, index);
renderCommandEncoder.SetVertexSamplerState(mtlSampler, index);
break;
case ShaderStage.Compute:
computeCommandEncoder = GetOrCreateComputeEncoder();
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 SetUniformBuffers(ReadOnlySpan<BufferAssignment> buffers)
{
2024-03-20 20:37:08 +00:00
_uniformBuffers = [];
foreach (BufferAssignment buffer in buffers)
{
if (buffer.Range.Size != 0)
{
_uniformBuffers.Add(new BufferInfo
{
Handle = buffer.Range.Handle.ToIntPtr(),
Offset = buffer.Range.Offset,
Index = buffer.Binding
});
}
}
if (_currentEncoderType == EncoderType.Render)
{
var renderCommandEncoder = GetOrCreateRenderEncoder();
RebindBuffers(renderCommandEncoder);
}
}
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)
{
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;
var layout = _vertexDescriptor.Layouts.Object((ulong)vertexAttribs[i].BufferIndex);
layout.Stride = 1;
2023-10-10 22:36:52 +00:00
}
}
}
public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
{
2024-03-20 20:37:08 +00:00
_vertexBuffers = [];
2023-10-10 23:02:38 +00:00
2023-10-10 22:36:52 +00:00
for (int i = 0; i < vertexBuffers.Length; i++)
{
if (vertexBuffers[i].Stride != 0)
{
var layout = _vertexDescriptor.Layouts.Object((ulong)i);
2024-03-18 18:48:54 +00:00
layout.Stride = (ulong)vertexBuffers[i].Stride;
2024-03-20 20:37:08 +00:00
_vertexBuffers.Add(new BufferInfo
2024-03-20 03:09:17 +00:00
{
Handle = vertexBuffers[i].Buffer.Handle.ToIntPtr(),
2024-03-20 20:37:08 +00:00
Offset = vertexBuffers[i].Buffer.Offset,
Index = i
});
2023-10-10 22:36:52 +00:00
}
}
2024-03-20 20:37:08 +00:00
if (_currentEncoderType == EncoderType.Render)
{
var renderCommandEncoder = GetOrCreateRenderEncoder();
RebindBuffers(renderCommandEncoder);
}
}
public unsafe void SetViewports(ReadOnlySpan<Viewport> viewports)
{
2024-05-16 19:59:56 +00:00
static float Clamp(float value)
{
return Math.Clamp(value, 0f, 1f);
}
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,
2024-05-16 19:59:56 +00:00
znear = Clamp(viewport.DepthNear),
zfar = Clamp(viewport.DepthFar)
};
}
_renderEncoderState.UpdateViewport(mtlViewports);
if (_currentEncoderType == EncoderType.Render)
{
fixed (MTLViewport* pMtlViewports = mtlViewports)
{
var renderCommandEncoder = GetOrCreateRenderEncoder();
renderCommandEncoder.SetViewports((IntPtr)pMtlViewports, (ulong)viewports.Length);
}
}
}
public void TextureBarrier()
{
2024-03-19 20:23:43 +00:00
// var renderCommandEncoder = GetOrCreateRenderEncoder();
2024-03-19 18:05:09 +00:00
// renderCommandEncoder.MemoryBarrier(MTLBarrierScope.Textures, );
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void TextureBarrierTiled()
{
2024-03-19 20:23:43 +00:00
// var renderCommandEncoder = GetOrCreateRenderEncoder();
2024-03-19 18:05:09 +00:00
// renderCommandEncoder.MemoryBarrier(MTLBarrierScope.Textures, );
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
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
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
public void EndTransformFeedback()
{
// Metal does not support Transform Feedback
2023-08-02 02:36:07 +00:00
Logger.Warning?.Print(LogClass.Gpu, "Not Implemented!");
}
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!");
}
public void Dispose()
{
2024-01-27 21:09:24 +00:00
EndCurrentPass();
}
}
}