mark state as dirty

This commit is contained in:
Samuliak 2024-05-19 09:10:14 +02:00 committed by Isaac Marovitz
parent 348a37a355
commit ccce85e1bb
2 changed files with 84 additions and 36 deletions

View file

@ -16,6 +16,15 @@ namespace Ryujinx.Graphics.Metal
public bool Scissor = false; public bool Scissor = false;
public DirtyFlags() { } public DirtyFlags() { }
public void MarkAll() {
Pipeline = true;
DepthStencil = true;
CullMode = true;
Winding = true;
Viewport = true;
Scissor = true;
}
} }
[SupportedOSPlatform("macos")] [SupportedOSPlatform("macos")]

View file

@ -98,7 +98,7 @@ namespace Ryujinx.Graphics.Metal
var renderCommandEncoder = _pipeline.CommandBuffer.RenderCommandEncoder(renderPassDescriptor); var renderCommandEncoder = _pipeline.CommandBuffer.RenderCommandEncoder(renderPassDescriptor);
// TODO: set dirty flags all to true _currentState.Dirty.MarkAll();
return renderCommandEncoder; return renderCommandEncoder;
} }
@ -107,15 +107,15 @@ namespace Ryujinx.Graphics.Metal
{ {
// TODO: only rebind the dirty state // TODO: only rebind the dirty state
SetPipelineState(renderCommandEncoder); SetPipelineState(renderCommandEncoder);
SetDepthStencilState(renderCommandEncoder, _currentState.DepthStencilState); SetDepthStencilState(renderCommandEncoder);
SetDepthClamp(renderCommandEncoder, _currentState.DepthClipMode); SetDepthClamp(renderCommandEncoder);
SetScissors(renderCommandEncoder, _currentState.Scissors); SetCullMode(renderCommandEncoder);
SetViewports(renderCommandEncoder, _currentState.Viewports); SetFrontFace(renderCommandEncoder);
SetViewports(renderCommandEncoder);
SetScissors(renderCommandEncoder);
SetVertexBuffers(renderCommandEncoder, _currentState.VertexBuffers); SetVertexBuffers(renderCommandEncoder, _currentState.VertexBuffers);
SetBuffers(renderCommandEncoder, _currentState.UniformBuffers, true); SetBuffers(renderCommandEncoder, _currentState.UniformBuffers, true);
SetBuffers(renderCommandEncoder, _currentState.StorageBuffers, true); SetBuffers(renderCommandEncoder, _currentState.StorageBuffers, true);
SetCullMode(renderCommandEncoder, _currentState.CullMode);
SetFrontFace(renderCommandEncoder, _currentState.Winding);
SetTextureAndSampler(renderCommandEncoder, ShaderStage.Vertex, _currentState.VertexTextures, _currentState.VertexSamplers); SetTextureAndSampler(renderCommandEncoder, ShaderStage.Vertex, _currentState.VertexTextures, _currentState.VertexSamplers);
SetTextureAndSampler(renderCommandEncoder, ShaderStage.Fragment, _currentState.FragmentTextures, _currentState.FragmentSamplers); SetTextureAndSampler(renderCommandEncoder, ShaderStage.Fragment, _currentState.FragmentTextures, _currentState.FragmentSamplers);
@ -236,6 +236,9 @@ namespace Ryujinx.Graphics.Metal
_currentState.VertexFunction = prg.VertexFunction; _currentState.VertexFunction = prg.VertexFunction;
_currentState.FragmentFunction = prg.FragmentFunction; _currentState.FragmentFunction = prg.FragmentFunction;
// Mark dirty
_currentState.Dirty.Pipeline = true;
} }
public void UpdateRenderTargets(ITexture[] colors, ITexture depthStencil) public void UpdateRenderTargets(ITexture[] colors, ITexture depthStencil)
@ -267,6 +270,9 @@ namespace Ryujinx.Graphics.Metal
public void UpdateVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs) public void UpdateVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
{ {
_currentState.VertexAttribs = vertexAttribs.ToArray(); _currentState.VertexAttribs = vertexAttribs.ToArray();
// Mark dirty
_currentState.Dirty.Pipeline = true;
} }
public void UpdateBlendDescriptors(int index, BlendDescriptor blend) public void UpdateBlendDescriptors(int index, BlendDescriptor blend)
@ -313,6 +319,9 @@ namespace Ryujinx.Graphics.Metal
} }
_currentState.DepthStencilState = _device.NewDepthStencilState(descriptor); _currentState.DepthStencilState = _device.NewDepthStencilState(descriptor);
// Mark dirty
_currentState.Dirty.DepthStencil = true;
} }
// Inlineable // Inlineable
@ -334,6 +343,9 @@ namespace Ryujinx.Graphics.Metal
} }
_currentState.DepthStencilState = _device.NewDepthStencilState(descriptor); _currentState.DepthStencilState = _device.NewDepthStencilState(descriptor);
// Mark dirty
_currentState.Dirty.DepthStencil = true;
} }
// Inlineable // Inlineable
@ -366,6 +378,9 @@ namespace Ryujinx.Graphics.Metal
y = (ulong)region.Y y = (ulong)region.Y
}; };
} }
// Mark dirty
_currentState.Dirty.Scissor = true;
} }
// Inlineable // Inlineable
@ -391,6 +406,9 @@ namespace Ryujinx.Graphics.Metal
zfar = Clamp(viewport.DepthFar) zfar = Clamp(viewport.DepthFar)
}; };
} }
// Mark dirty
_currentState.Dirty.Viewport = true;
} }
public void UpdateVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers) public void UpdateVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
@ -441,12 +459,18 @@ namespace Ryujinx.Graphics.Metal
public void UpdateCullMode(bool enable, Face face) public void UpdateCullMode(bool enable, Face face)
{ {
_currentState.CullMode = enable ? face.Convert() : MTLCullMode.None; _currentState.CullMode = enable ? face.Convert() : MTLCullMode.None;
// Mark dirty
_currentState.Dirty.CullMode = true;
} }
// Inlineable // Inlineable
public void UpdateFrontFace(FrontFace frontFace) public void UpdateFrontFace(FrontFace frontFace)
{ {
_currentState.Winding = frontFace.Convert(); _currentState.Winding = frontFace.Convert();
// Mark dirty
_currentState.Dirty.Winding = true;
} }
// Inlineable // Inlineable
@ -465,42 +489,51 @@ namespace Ryujinx.Graphics.Metal
} }
} }
private static void SetDepthStencilState(MTLRenderCommandEncoder renderCommandEncoder, MTLDepthStencilState? depthStencilState) private void SetDepthStencilState(MTLRenderCommandEncoder renderCommandEncoder)
{ {
if (depthStencilState != null) if (_currentState.Dirty.DepthStencil)
{ {
renderCommandEncoder.SetDepthStencilState(depthStencilState.Value); if (_currentState.DepthStencilState != null)
}
}
private static void SetDepthClamp(MTLRenderCommandEncoder renderCommandEncoder, MTLDepthClipMode depthClipMode)
{ {
renderCommandEncoder.SetDepthClipMode(depthClipMode); renderCommandEncoder.SetDepthStencilState(_currentState.DepthStencilState.Value);
}
private unsafe static void SetScissors(MTLRenderCommandEncoder renderCommandEncoder, MTLScissorRect[] scissors)
{
if (scissors.Length > 0)
{
fixed (MTLScissorRect* pMtlScissors = scissors)
{
renderCommandEncoder.SetScissorRects((IntPtr)pMtlScissors, (ulong)scissors.Length);
} }
} }
} }
private unsafe static void SetViewports(MTLRenderCommandEncoder renderCommandEncoder, MTLViewport[] viewports) private void SetDepthClamp(MTLRenderCommandEncoder renderCommandEncoder)
{ {
if (viewports.Length > 0) renderCommandEncoder.SetDepthClipMode(_currentState.DepthClipMode);
}
private unsafe void SetScissors(MTLRenderCommandEncoder renderCommandEncoder)
{ {
fixed (MTLViewport* pMtlViewports = viewports) if (_currentState.Dirty.Scissor)
{ {
renderCommandEncoder.SetViewports((IntPtr)pMtlViewports, (ulong)viewports.Length); if (_currentState.Scissors.Length > 0)
{
fixed (MTLScissorRect* pMtlScissors = _currentState.Scissors)
{
renderCommandEncoder.SetScissorRects((IntPtr)pMtlScissors, (ulong)_currentState.Scissors.Length);
}
} }
} }
} }
private static MTLVertexDescriptor BuildVertexDescriptor(VertexBufferDescriptor[] bufferDescriptors, VertexAttribDescriptor[] attribDescriptors) private unsafe void SetViewports(MTLRenderCommandEncoder renderCommandEncoder)
{
if (_currentState.Dirty.Viewport)
{
if (_currentState.Viewports.Length > 0)
{
fixed (MTLViewport* pMtlViewports = _currentState.Viewports)
{
renderCommandEncoder.SetViewports((IntPtr)pMtlViewports, (ulong)_currentState.Viewports.Length);
}
}
}
}
private MTLVertexDescriptor BuildVertexDescriptor(VertexBufferDescriptor[] bufferDescriptors, VertexAttribDescriptor[] attribDescriptors)
{ {
var vertexDescriptor = new MTLVertexDescriptor(); var vertexDescriptor = new MTLVertexDescriptor();
uint indexMask = 0; uint indexMask = 0;
@ -525,7 +558,7 @@ namespace Ryujinx.Graphics.Metal
return vertexDescriptor; return vertexDescriptor;
} }
private static void SetVertexBuffers(MTLRenderCommandEncoder renderCommandEncoder, VertexBufferDescriptor[] bufferDescriptors) private void SetVertexBuffers(MTLRenderCommandEncoder renderCommandEncoder, VertexBufferDescriptor[] bufferDescriptors)
{ {
var buffers = new List<BufferInfo>(); var buffers = new List<BufferInfo>();
@ -543,7 +576,7 @@ namespace Ryujinx.Graphics.Metal
SetBuffers(renderCommandEncoder, buffers); SetBuffers(renderCommandEncoder, buffers);
} }
private static void SetBuffers(MTLRenderCommandEncoder renderCommandEncoder, List<BufferInfo> buffers, bool fragment = false) private void SetBuffers(MTLRenderCommandEncoder renderCommandEncoder, List<BufferInfo> buffers, bool fragment = false)
{ {
foreach (var buffer in buffers) foreach (var buffer in buffers)
{ {
@ -556,17 +589,23 @@ namespace Ryujinx.Graphics.Metal
} }
} }
private static void SetCullMode(MTLRenderCommandEncoder renderCommandEncoder, MTLCullMode cullMode) private void SetCullMode(MTLRenderCommandEncoder renderCommandEncoder)
{ {
renderCommandEncoder.SetCullMode(cullMode); if (_currentState.Dirty.CullMode)
{
renderCommandEncoder.SetCullMode(_currentState.CullMode);
}
} }
private static void SetFrontFace(MTLRenderCommandEncoder renderCommandEncoder, MTLWinding winding) private void SetFrontFace(MTLRenderCommandEncoder renderCommandEncoder)
{ {
renderCommandEncoder.SetFrontFacingWinding(winding); if (_currentState.Dirty.Winding)
{
renderCommandEncoder.SetFrontFacingWinding(_currentState.Winding);
}
} }
private static void SetTextureAndSampler(MTLRenderCommandEncoder renderCommandEncoder, ShaderStage stage, Dictionary<ulong, MTLTexture> textures, Dictionary<ulong, MTLSamplerState> samplers) private void SetTextureAndSampler(MTLRenderCommandEncoder renderCommandEncoder, ShaderStage stage, Dictionary<ulong, MTLTexture> textures, Dictionary<ulong, MTLSamplerState> samplers)
{ {
foreach (var texture in textures) foreach (var texture in textures)
{ {