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

132 lines
5.3 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;
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-10-10 15:26:40 -04:00
private MTLVertexDescriptor _vertexDescriptor = new();
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-08-03 14:50:49 -04:00
public readonly void SetEncoderState(MTLRenderCommandEncoder renderCommandEncoder)
2023-07-28 16:23:13 -04:00
{
2023-10-10 15:26:40 -04:00
var renderPipelineDescriptor = new MTLRenderPipelineDescriptor
{
VertexDescriptor = _vertexDescriptor
};
if (_vertexFunction != null)
{
renderPipelineDescriptor.VertexFunction = _vertexFunction;
}
if (_fragmentFunction != null)
{
renderPipelineDescriptor.VertexFunction = _fragmentFunction;
}
renderPipelineDescriptor.ColorAttachments.Object(0).SetBlendingEnabled(true);
renderPipelineDescriptor.ColorAttachments.Object(0).PixelFormat = MTLPixelFormat.BGRA8Unorm;
renderPipelineDescriptor.ColorAttachments.Object(0).SourceAlphaBlendFactor = MTLBlendFactor.SourceAlpha;
renderPipelineDescriptor.ColorAttachments.Object(0).DestinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlpha;
renderPipelineDescriptor.ColorAttachments.Object(0).SourceRGBBlendFactor = MTLBlendFactor.SourceAlpha;
renderPipelineDescriptor.ColorAttachments.Object(0).DestinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlpha;
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
}
2023-10-10 15:26:40 -04:00
2023-10-10 17:53:51 -04:00
public void UpdateVertexAttributes(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
2023-10-10 15:26:40 -04:00
{
2023-10-10 17:53:51 -04:00
for (int i = 0; i < vertexAttribs.Length; i++)
{
2023-10-10 18:04:26 -04:00
if (!vertexAttribs[i].IsZero)
{
// TODO: Format should not be hardcoded
_vertexDescriptor.Attributes.Object((ulong)i).Format = MTLVertexFormat.Float4;
_vertexDescriptor.Attributes.Object((ulong)i).BufferIndex = (ulong)vertexAttribs[i].BufferIndex;
_vertexDescriptor.Attributes.Object((ulong)i).Offset = (ulong)vertexAttribs[i].Offset;
}
2023-10-10 17:53:51 -04:00
}
}
public void UpdateVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
{
2023-10-10 15:26:40 -04: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-07-28 16:23:13 -04:00
}
}