2023-10-10 15:26:40 -04:00
|
|
|
using Ryujinx.Common.Logging;
|
2023-07-28 16:23:13 -04:00
|
|
|
using Ryujinx.Graphics.GAL;
|
2023-10-10 15:26:40 -04:00
|
|
|
using SharpMetal.Foundation;
|
2023-07-28 16:23:13 -04:00
|
|
|
using SharpMetal.Metal;
|
2023-08-03 14:50:49 -04:00
|
|
|
using System;
|
2023-07-28 16:23:13 -04:00
|
|
|
using System.Runtime.Versioning;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Metal
|
|
|
|
{
|
|
|
|
[SupportedOSPlatform("macos")]
|
|
|
|
struct RenderEncoderState
|
|
|
|
{
|
2023-08-03 14:50:49 -04:00
|
|
|
private readonly MTLDevice _device;
|
2023-10-10 15:26:40 -04:00
|
|
|
private readonly MTLFunction _vertexFunction = null;
|
|
|
|
private readonly MTLFunction _fragmentFunction = null;
|
2023-07-29 00:46:13 -04:00
|
|
|
private MTLDepthStencilState _depthStencilState = null;
|
|
|
|
|
|
|
|
private MTLCompareFunction _depthCompareFunction = MTLCompareFunction.Always;
|
|
|
|
private bool _depthWriteEnabled = false;
|
|
|
|
|
|
|
|
private MTLStencilDescriptor _backFaceStencil = null;
|
|
|
|
private MTLStencilDescriptor _frontFaceStencil = null;
|
|
|
|
|
2023-07-28 16:23:13 -04:00
|
|
|
public PrimitiveTopology Topology = PrimitiveTopology.Triangles;
|
|
|
|
public MTLCullMode CullMode = MTLCullMode.None;
|
|
|
|
public MTLWinding Winding = MTLWinding.Clockwise;
|
|
|
|
|
2023-10-10 15:26:40 -04:00
|
|
|
public RenderEncoderState(MTLFunction vertexFunction, MTLFunction fragmentFunction, MTLDevice device)
|
2023-07-28 16:23:13 -04:00
|
|
|
{
|
2023-10-10 15:26:40 -04:00
|
|
|
_vertexFunction = vertexFunction;
|
|
|
|
_fragmentFunction = fragmentFunction;
|
2023-07-29 00:46:13 -04:00
|
|
|
_device = device;
|
2023-07-28 16:23:13 -04:00
|
|
|
}
|
|
|
|
|
2023-10-10 18:36:52 -04:00
|
|
|
public readonly void SetEncoderState(MTLRenderCommandEncoder renderCommandEncoder, MTLVertexDescriptor vertexDescriptor)
|
2023-07-28 16:23:13 -04:00
|
|
|
{
|
2023-10-10 15:26:40 -04:00
|
|
|
var renderPipelineDescriptor = new MTLRenderPipelineDescriptor
|
|
|
|
{
|
2023-10-10 18:36:52 -04:00
|
|
|
VertexDescriptor = vertexDescriptor
|
2023-10-10 15:26:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (_vertexFunction != null)
|
|
|
|
{
|
|
|
|
renderPipelineDescriptor.VertexFunction = _vertexFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_fragmentFunction != null)
|
|
|
|
{
|
2023-10-10 22:10:28 -04:00
|
|
|
renderPipelineDescriptor.FragmentFunction = _fragmentFunction;
|
2023-10-10 15:26:40 -04:00
|
|
|
}
|
|
|
|
|
2023-10-10 18:36:52 -04:00
|
|
|
var attachment = renderPipelineDescriptor.ColorAttachments.Object(0);
|
|
|
|
attachment.SetBlendingEnabled(true);
|
|
|
|
attachment.PixelFormat = MTLPixelFormat.BGRA8Unorm;
|
|
|
|
attachment.SourceAlphaBlendFactor = MTLBlendFactor.SourceAlpha;
|
|
|
|
attachment.DestinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlpha;
|
|
|
|
attachment.SourceRGBBlendFactor = MTLBlendFactor.SourceAlpha;
|
|
|
|
attachment.DestinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlpha;
|
2023-10-10 15:26:40 -04:00
|
|
|
|
|
|
|
var error = new NSError(IntPtr.Zero);
|
|
|
|
var pipelineState = _device.NewRenderPipelineState(renderPipelineDescriptor, ref error);
|
|
|
|
if (error != IntPtr.Zero)
|
|
|
|
{
|
|
|
|
Logger.Error?.PrintMsg(LogClass.Gpu, $"Failed to create Render Pipeline State: {StringHelper.String(error.LocalizedDescription)}");
|
|
|
|
}
|
|
|
|
|
|
|
|
renderCommandEncoder.SetRenderPipelineState(pipelineState);
|
2023-07-28 16:23:13 -04:00
|
|
|
renderCommandEncoder.SetCullMode(CullMode);
|
|
|
|
renderCommandEncoder.SetFrontFacingWinding(Winding);
|
2023-08-03 14:50:49 -04:00
|
|
|
|
|
|
|
if (_depthStencilState != null)
|
|
|
|
{
|
|
|
|
renderCommandEncoder.SetDepthStencilState(_depthStencilState);
|
|
|
|
}
|
2023-07-29 00:46:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public MTLDepthStencilState UpdateStencilState(MTLStencilDescriptor backFace, MTLStencilDescriptor frontFace)
|
|
|
|
{
|
|
|
|
_backFaceStencil = backFace;
|
|
|
|
_frontFaceStencil = frontFace;
|
|
|
|
|
|
|
|
return _depthStencilState = _device.NewDepthStencilState(new MTLDepthStencilDescriptor
|
|
|
|
{
|
|
|
|
DepthCompareFunction = _depthCompareFunction,
|
|
|
|
DepthWriteEnabled = _depthWriteEnabled,
|
|
|
|
BackFaceStencil = _backFaceStencil,
|
|
|
|
FrontFaceStencil = _frontFaceStencil
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public MTLDepthStencilState UpdateDepthState(MTLCompareFunction depthCompareFunction, bool depthWriteEnabled)
|
|
|
|
{
|
|
|
|
_depthCompareFunction = depthCompareFunction;
|
|
|
|
_depthWriteEnabled = depthWriteEnabled;
|
|
|
|
|
|
|
|
return _depthStencilState = _device.NewDepthStencilState(new MTLDepthStencilDescriptor
|
|
|
|
{
|
|
|
|
DepthCompareFunction = _depthCompareFunction,
|
|
|
|
DepthWriteEnabled = _depthWriteEnabled,
|
|
|
|
BackFaceStencil = _backFaceStencil,
|
|
|
|
FrontFaceStencil = _frontFaceStencil
|
|
|
|
});
|
2023-07-28 16:23:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|