1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2025-02-15 17:45:34 +00:00
Ryujinx/src/Ryujinx.Graphics.Metal/RenderEncoderState.cs

150 lines
5.6 KiB
C#
Raw Normal View History

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;
2024-03-18 14:32:59 -04:00
private readonly MTLFunction? _vertexFunction = null;
private readonly MTLFunction? _fragmentFunction = null;
private MTLDepthStencilState? _depthStencilState = null;
2023-07-29 00:46:13 -04:00
private MTLCompareFunction _depthCompareFunction = MTLCompareFunction.Always;
private bool _depthWriteEnabled = false;
2024-03-19 22:58:42 -04:00
private MTLStencilDescriptor _backFaceStencil = new MTLStencilDescriptor();
private MTLStencilDescriptor _frontFaceStencil = new MTLStencilDescriptor();
2023-07-29 00:46:13 -04:00
2023-07-28 16:23:13 -04:00
public PrimitiveTopology Topology = PrimitiveTopology.Triangles;
public MTLCullMode CullMode = MTLCullMode.None;
public MTLWinding Winding = MTLWinding.Clockwise;
private MTLViewport[] _viewports = [];
private MTLScissorRect[] _scissors = [];
public int ViewportCount => _viewports.Length;
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
}
public unsafe readonly void SetEncoderState(MTLRenderCommandEncoder renderCommandEncoder, MTLRenderPassDescriptor descriptor, 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)
{
2024-03-18 14:32:59 -04:00
renderPipelineDescriptor.VertexFunction = _vertexFunction.Value;
2023-10-10 15:26:40 -04:00
}
if (_fragmentFunction != null)
{
2024-03-18 14:32:59 -04:00
renderPipelineDescriptor.FragmentFunction = _fragmentFunction.Value;
2023-10-10 15:26:40 -04:00
}
const int maxColorAttachments = 8;
for (int i = 0; i < maxColorAttachments; i++)
{
var renderAttachment = descriptor.ColorAttachments.Object((ulong)i);
if (renderAttachment.Texture != null)
{
var attachment = renderPipelineDescriptor.ColorAttachments.Object((ulong)i);
attachment.SetBlendingEnabled(true);
attachment.PixelFormat = renderAttachment.Texture.PixelFormat;
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)
{
2024-03-18 14:32:59 -04:00
renderCommandEncoder.SetDepthStencilState(_depthStencilState.Value);
2023-08-03 14:50:49 -04:00
}
if (_viewports.Length > 0)
{
fixed (MTLViewport* pMtlViewports = _viewports)
{
renderCommandEncoder.SetViewports((IntPtr)pMtlViewports, (ulong)_viewports.Length);
}
}
if (_scissors.Length > 0)
{
fixed (MTLScissorRect* pMtlScissors = _scissors)
{
renderCommandEncoder.SetScissorRects((IntPtr)pMtlScissors, (ulong)_scissors.Length);
}
}
2023-07-29 00:46:13 -04:00
}
public MTLDepthStencilState UpdateStencilState(MTLStencilDescriptor backFace, MTLStencilDescriptor frontFace)
{
_backFaceStencil = backFace;
_frontFaceStencil = frontFace;
2024-03-18 14:32:59 -04:00
_depthStencilState = _device.NewDepthStencilState(new MTLDepthStencilDescriptor
2023-07-29 00:46:13 -04:00
{
DepthCompareFunction = _depthCompareFunction,
DepthWriteEnabled = _depthWriteEnabled,
2024-03-19 22:58:42 -04:00
BackFaceStencil = _backFaceStencil,
FrontFaceStencil = _frontFaceStencil
2023-07-29 00:46:13 -04:00
});
2024-03-18 14:32:59 -04:00
return _depthStencilState.Value;
2023-07-29 00:46:13 -04:00
}
public MTLDepthStencilState UpdateDepthState(MTLCompareFunction depthCompareFunction, bool depthWriteEnabled)
{
_depthCompareFunction = depthCompareFunction;
_depthWriteEnabled = depthWriteEnabled;
2024-03-19 22:58:42 -04:00
var state = _device.NewDepthStencilState(new MTLDepthStencilDescriptor
2023-07-29 00:46:13 -04:00
{
DepthCompareFunction = _depthCompareFunction,
DepthWriteEnabled = _depthWriteEnabled,
2024-03-19 22:58:42 -04:00
BackFaceStencil = _backFaceStencil,
FrontFaceStencil = _frontFaceStencil
2023-07-29 00:46:13 -04:00
});
2024-03-18 14:32:59 -04:00
2024-03-19 22:58:42 -04:00
_depthStencilState = state;
return state;
2023-07-28 16:23:13 -04:00
}
public void UpdateScissors(MTLScissorRect[] scissors)
{
_scissors = scissors;
}
public void UpdateViewport(MTLViewport[] viewports)
{
_viewports = viewports;
}
2023-07-28 16:23:13 -04:00
}
}