Start vertex descriptor work

This commit is contained in:
Isaac Marovitz
2023-10-10 15:26:40 -04:00
committed by Isaac Marovitz
parent fb5402ce81
commit 5c9d1bd0da
4 changed files with 76 additions and 34 deletions

View File

@@ -1,4 +1,6 @@
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.GAL;
using SharpMetal.Foundation;
using SharpMetal.Metal;
using System;
using System.Runtime.Versioning;
@@ -9,8 +11,8 @@ namespace Ryujinx.Graphics.Metal
struct RenderEncoderState
{
private readonly MTLDevice _device;
// TODO: Work with more than one pipeline state
private readonly MTLRenderPipelineState _copyPipeline;
private readonly MTLFunction _vertexFunction = null;
private readonly MTLFunction _fragmentFunction = null;
private MTLDepthStencilState _depthStencilState = null;
private MTLCompareFunction _depthCompareFunction = MTLCompareFunction.Always;
@@ -19,19 +21,51 @@ namespace Ryujinx.Graphics.Metal
private MTLStencilDescriptor _backFaceStencil = null;
private MTLStencilDescriptor _frontFaceStencil = null;
private MTLVertexDescriptor _vertexDescriptor = new();
public PrimitiveTopology Topology = PrimitiveTopology.Triangles;
public MTLCullMode CullMode = MTLCullMode.None;
public MTLWinding Winding = MTLWinding.Clockwise;
public RenderEncoderState(MTLRenderPipelineState copyPipeline, MTLDevice device)
public RenderEncoderState(MTLFunction vertexFunction, MTLFunction fragmentFunction, MTLDevice device)
{
_vertexFunction = vertexFunction;
_fragmentFunction = fragmentFunction;
_device = device;
_copyPipeline = copyPipeline;
}
public readonly void SetEncoderState(MTLRenderCommandEncoder renderCommandEncoder)
{
renderCommandEncoder.SetRenderPipelineState(_copyPipeline);
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);
renderCommandEncoder.SetCullMode(CullMode);
renderCommandEncoder.SetFrontFacingWinding(Winding);
@@ -68,5 +102,21 @@ namespace Ryujinx.Graphics.Metal
FrontFaceStencil = _frontFaceStencil
});
}
public void UpdateVertexDescriptor(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
{
_vertexDescriptor = new();
for (int i = 0; i < vertexBuffers.Length; i++)
{
if (vertexBuffers[i].Stride != 0)
{
// TODO: Format should not be hardcoded
_vertexDescriptor.Attributes.Object((ulong)i).Format = MTLVertexFormat.Float4;
_vertexDescriptor.Attributes.Object((ulong)i).BufferIndex = (ulong)i;
_vertexDescriptor.Layouts.Object((ulong)i).Stride = (ulong)vertexBuffers[i].Stride;
}
}
}
}
}