Cleanup encoder getting + Fix capture overflow

This commit is contained in:
Isaac Marovitz 2023-08-03 16:47:10 -04:00 committed by Isaac Marovitz
parent 1cb2ec7ebc
commit 61910fe342
3 changed files with 51 additions and 134 deletions

View file

@ -118,7 +118,6 @@ namespace Ryujinx.Graphics.Metal
public ITexture CreateTexture(TextureCreateInfo info) public ITexture CreateTexture(TextureCreateInfo info)
{ {
var texture = new Texture(_device, _pipeline, info); var texture = new Texture(_device, _pipeline, info);
Logger.Warning?.Print(LogClass.Gpu, "Texture created!");
return texture; return texture;
} }

View file

@ -20,15 +20,14 @@ namespace Ryujinx.Graphics.Metal
private MTLCommandBuffer _commandBuffer; private MTLCommandBuffer _commandBuffer;
private MTLCommandEncoder _currentEncoder; private MTLCommandEncoder _currentEncoder;
public MTLCommandEncoder CurrentEncoder;
private RenderEncoderState _renderEncoderState; private RenderEncoderState _renderEncoderState;
private MTLBuffer _indexBuffer; private MTLBuffer _indexBuffer;
private MTLIndexType _indexType; private MTLIndexType _indexType;
private ulong _indexBufferOffset; private ulong _indexBufferOffset;
private MTLClearColor _clearColor; private MTLClearColor _clearColor;
private int frameCount = 0; private int _frameCount;
private bool _captureEnded = false;
public Pipeline(MTLDevice device, MTLCommandQueue commandQueue) public Pipeline(MTLDevice device, MTLCommandQueue commandQueue)
{ {
@ -41,6 +40,36 @@ namespace Ryujinx.Graphics.Metal
_commandBuffer = _mtlCommandQueue.CommandBuffer(); _commandBuffer = _mtlCommandQueue.CommandBuffer();
} }
public MTLRenderCommandEncoder GetOrCreateRenderEncoder()
{
if (_currentEncoder is MTLRenderCommandEncoder encoder)
{
return encoder;
}
return BeginRenderPass();
}
public MTLBlitCommandEncoder GetOrCreateBlitEncoder()
{
if (_currentEncoder is MTLBlitCommandEncoder encoder)
{
return encoder;
}
return BeginBlitPass();
}
public MTLComputeCommandEncoder GetOrCreateComputeEncoder()
{
if (_currentEncoder is MTLComputeCommandEncoder encoder)
{
return encoder;
}
return BeginComputePass();
}
public void EndCurrentPass() public void EndCurrentPass()
{ {
if (_currentEncoder != null) if (_currentEncoder != null)
@ -48,7 +77,6 @@ namespace Ryujinx.Graphics.Metal
_currentEncoder.EndEncoding(); _currentEncoder.EndEncoding();
_currentEncoder = null; _currentEncoder = null;
} }
Logger.Warning?.Print(LogClass.Gpu, "Current pass ended");
} }
public MTLRenderCommandEncoder BeginRenderPass() public MTLRenderCommandEncoder BeginRenderPass()
@ -58,7 +86,6 @@ namespace Ryujinx.Graphics.Metal
var descriptor = new MTLRenderPassDescriptor(); var descriptor = new MTLRenderPassDescriptor();
var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor); var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor);
_renderEncoderState.SetEncoderState(renderCommandEncoder); _renderEncoderState.SetEncoderState(renderCommandEncoder);
Logger.Warning?.Print(LogClass.Gpu, "Began render pass");
_currentEncoder = renderCommandEncoder; _currentEncoder = renderCommandEncoder;
return renderCommandEncoder; return renderCommandEncoder;
@ -70,7 +97,6 @@ namespace Ryujinx.Graphics.Metal
var descriptor = new MTLBlitPassDescriptor(); var descriptor = new MTLBlitPassDescriptor();
var blitCommandEncoder = _commandBuffer.BlitCommandEncoder(descriptor); var blitCommandEncoder = _commandBuffer.BlitCommandEncoder(descriptor);
Logger.Warning?.Print(LogClass.Gpu, "Began blit pass");
_currentEncoder = blitCommandEncoder; _currentEncoder = blitCommandEncoder;
return blitCommandEncoder; return blitCommandEncoder;
@ -82,7 +108,6 @@ namespace Ryujinx.Graphics.Metal
var descriptor = new MTLComputePassDescriptor(); var descriptor = new MTLComputePassDescriptor();
var computeCommandEncoder = _commandBuffer.ComputeCommandEncoder(descriptor); var computeCommandEncoder = _commandBuffer.ComputeCommandEncoder(descriptor);
Logger.Warning?.Print(LogClass.Gpu, "Began compute pass");
_currentEncoder = computeCommandEncoder; _currentEncoder = computeCommandEncoder;
return computeCommandEncoder; return computeCommandEncoder;
@ -103,7 +128,6 @@ namespace Ryujinx.Graphics.Metal
descriptor.ColorAttachments.Object(0).ClearColor = _clearColor; descriptor.ColorAttachments.Object(0).ClearColor = _clearColor;
var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor); var renderCommandEncoder = _commandBuffer.RenderCommandEncoder(descriptor);
Logger.Warning?.Print(LogClass.Gpu, "Began present");
_renderEncoderState.SetEncoderState(renderCommandEncoder); _renderEncoderState.SetEncoderState(renderCommandEncoder);
var sampler = _device.NewSamplerState(new MTLSamplerDescriptor var sampler = _device.NewSamplerState(new MTLSamplerDescriptor
@ -122,14 +146,16 @@ namespace Ryujinx.Graphics.Metal
_commandBuffer.PresentDrawable(drawable); _commandBuffer.PresentDrawable(drawable);
_commandBuffer.Commit(); _commandBuffer.Commit();
Logger.Warning?.Print(LogClass.Gpu, "Presented"); if (!_captureEnded)
frameCount++;
if (frameCount >= 5)
{ {
MTLCaptureManager.SharedCaptureManager().StopCapture(); _frameCount++;
Logger.Warning?.Print(LogClass.Gpu, "Trace ended!");
if (_frameCount >= 5)
{
_captureEnded = true;
MTLCaptureManager.SharedCaptureManager().StopCapture();
Logger.Warning?.Print(LogClass.Gpu, "Trace ended!");
}
} }
_commandBuffer = _mtlCommandQueue.CommandBuffer(); _commandBuffer = _mtlCommandQueue.CommandBuffer();
@ -142,16 +168,7 @@ namespace Ryujinx.Graphics.Metal
public void ClearBuffer(BufferHandle destination, int offset, int size, uint value) public void ClearBuffer(BufferHandle destination, int offset, int size, uint value)
{ {
MTLBlitCommandEncoder blitCommandEncoder; var blitCommandEncoder = GetOrCreateBlitEncoder();
if (_currentEncoder is MTLBlitCommandEncoder encoder)
{
blitCommandEncoder = encoder;
}
else
{
blitCommandEncoder = BeginBlitPass();
}
// Might need a closer look, range's count, lower, and upper bound // Might need a closer look, range's count, lower, and upper bound
// must be a multiple of 4 // must be a multiple of 4
@ -183,16 +200,7 @@ namespace Ryujinx.Graphics.Metal
public void CopyBuffer(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size) public void CopyBuffer(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
{ {
MTLBlitCommandEncoder blitCommandEncoder; var blitCommandEncoder = GetOrCreateBlitEncoder();
if (CurrentEncoder is MTLBlitCommandEncoder encoder)
{
blitCommandEncoder = encoder;
}
else
{
blitCommandEncoder = BeginBlitPass();
}
MTLBuffer sourceBuffer = new(Unsafe.As<BufferHandle, IntPtr>(ref source)); MTLBuffer sourceBuffer = new(Unsafe.As<BufferHandle, IntPtr>(ref source));
MTLBuffer destinationBuffer = new(Unsafe.As<BufferHandle, IntPtr>(ref destination)); MTLBuffer destinationBuffer = new(Unsafe.As<BufferHandle, IntPtr>(ref destination));
@ -212,18 +220,7 @@ namespace Ryujinx.Graphics.Metal
public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance) public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Draw"); var renderCommandEncoder = GetOrCreateRenderEncoder();
MTLRenderCommandEncoder renderCommandEncoder;
if (_currentEncoder is MTLRenderCommandEncoder encoder)
{
renderCommandEncoder = encoder;
}
else
{
renderCommandEncoder = BeginRenderPass();
}
// TODO: Support topology re-indexing to provide support for TriangleFans // TODO: Support topology re-indexing to provide support for TriangleFans
var primitiveType = _renderEncoderState.Topology.Convert(); var primitiveType = _renderEncoderState.Topology.Convert();
@ -233,18 +230,8 @@ namespace Ryujinx.Graphics.Metal
public void DrawIndexed(int indexCount, int instanceCount, int firstIndex, int firstVertex, int firstInstance) public void DrawIndexed(int indexCount, int instanceCount, int firstIndex, int firstVertex, int firstInstance)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Draw"); var renderCommandEncoder = GetOrCreateRenderEncoder();
MTLRenderCommandEncoder renderCommandEncoder;
if (_currentEncoder is MTLRenderCommandEncoder encoder)
{
renderCommandEncoder = encoder;
}
else
{
renderCommandEncoder = BeginRenderPass();
}
// TODO: Support topology re-indexing to provide support for TriangleFans // TODO: Support topology re-indexing to provide support for TriangleFans
var primitiveType = _renderEncoderState.Topology.Convert(); var primitiveType = _renderEncoderState.Topology.Convert();
@ -309,8 +296,6 @@ namespace Ryujinx.Graphics.Metal
public void SetDepthTest(DepthTestDescriptor depthTest) public void SetDepthTest(DepthTestDescriptor depthTest)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Set depth test");
var depthStencilState = _renderEncoderState.UpdateDepthState( var depthStencilState = _renderEncoderState.UpdateDepthState(
depthTest.TestEnable ? MTLCompareFunction.Always : depthTest.Func.Convert(), depthTest.TestEnable ? MTLCompareFunction.Always : depthTest.Func.Convert(),
depthTest.WriteEnable); depthTest.WriteEnable);
@ -323,8 +308,6 @@ namespace Ryujinx.Graphics.Metal
public void SetFaceCulling(bool enable, Face face) public void SetFaceCulling(bool enable, Face face)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Set face culling");
var cullMode = enable ? face.Convert() : MTLCullMode.None; var cullMode = enable ? face.Convert() : MTLCullMode.None;
if (_currentEncoder is MTLRenderCommandEncoder renderCommandEncoder) if (_currentEncoder is MTLRenderCommandEncoder renderCommandEncoder)
@ -337,8 +320,6 @@ namespace Ryujinx.Graphics.Metal
public void SetFrontFace(FrontFace frontFace) public void SetFrontFace(FrontFace frontFace)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Set front face");
var winding = frontFace.Convert(); var winding = frontFace.Convert();
if (_currentEncoder is MTLRenderCommandEncoder renderCommandEncoder) if (_currentEncoder is MTLRenderCommandEncoder renderCommandEncoder)
@ -351,8 +332,6 @@ namespace Ryujinx.Graphics.Metal
public void SetIndexBuffer(BufferRange buffer, IndexType type) public void SetIndexBuffer(BufferRange buffer, IndexType type)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Set index buffer");
if (buffer.Handle != BufferHandle.Null) if (buffer.Handle != BufferHandle.Null)
{ {
_indexType = type.Convert(); _indexType = type.Convert();

View file

@ -23,7 +23,6 @@ namespace Ryujinx.Graphics.Metal
public Texture(MTLDevice device, Pipeline pipeline, TextureCreateInfo info) public Texture(MTLDevice device, Pipeline pipeline, TextureCreateInfo info)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Texture init");
_device = device; _device = device;
_pipeline = pipeline; _pipeline = pipeline;
_info = info; _info = info;
@ -53,17 +52,7 @@ namespace Ryujinx.Graphics.Metal
public void CopyTo(ITexture destination, int firstLayer, int firstLevel) public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Copy to"); var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
MTLBlitCommandEncoder blitCommandEncoder;
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
{
blitCommandEncoder = encoder;
}
else
{
blitCommandEncoder = _pipeline.BeginBlitPass();
}
if (destination is Texture destinationTexture) if (destination is Texture destinationTexture)
{ {
@ -81,17 +70,7 @@ namespace Ryujinx.Graphics.Metal
public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel) public void CopyTo(ITexture destination, int srcLayer, int dstLayer, int srcLevel, int dstLevel)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Copy to"); var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
MTLBlitCommandEncoder blitCommandEncoder;
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
{
blitCommandEncoder = encoder;
}
else
{
blitCommandEncoder = _pipeline.BeginBlitPass();
}
if (destination is Texture destinationTexture) if (destination is Texture destinationTexture)
{ {
@ -114,17 +93,7 @@ namespace Ryujinx.Graphics.Metal
public void CopyTo(BufferRange range, int layer, int level, int stride) public void CopyTo(BufferRange range, int layer, int level, int stride)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Copy to"); var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
MTLBlitCommandEncoder blitCommandEncoder;
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
{
blitCommandEncoder = encoder;
}
else
{
blitCommandEncoder = _pipeline.BeginBlitPass();
}
ulong bytesPerRow = (ulong)Info.GetMipStride(level); ulong bytesPerRow = (ulong)Info.GetMipStride(level);
ulong bytesPerImage = 0; ulong bytesPerImage = 0;
@ -166,17 +135,7 @@ namespace Ryujinx.Graphics.Metal
// TODO: Handle array formats // TODO: Handle array formats
public unsafe void SetData(SpanOrArray<byte> data) public unsafe void SetData(SpanOrArray<byte> data)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Set data"); var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
MTLBlitCommandEncoder blitCommandEncoder;
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
{
blitCommandEncoder = encoder;
}
else
{
blitCommandEncoder = _pipeline.BeginBlitPass();
}
var dataSpan = data.Span; var dataSpan = data.Span;
var mtlBuffer = _device.NewBuffer((ulong)dataSpan.Length, MTLResourceOptions.ResourceStorageModeShared); var mtlBuffer = _device.NewBuffer((ulong)dataSpan.Length, MTLResourceOptions.ResourceStorageModeShared);
@ -223,17 +182,7 @@ namespace Ryujinx.Graphics.Metal
public void SetData(SpanOrArray<byte> data, int layer, int level) public void SetData(SpanOrArray<byte> data, int layer, int level)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Set data"); var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
MTLBlitCommandEncoder blitCommandEncoder;
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
{
blitCommandEncoder = encoder;
}
else
{
blitCommandEncoder = _pipeline.BeginBlitPass();
}
ulong bytesPerRow = (ulong)Info.GetMipStride(level); ulong bytesPerRow = (ulong)Info.GetMipStride(level);
ulong bytesPerImage = 0; ulong bytesPerImage = 0;
@ -265,17 +214,7 @@ namespace Ryujinx.Graphics.Metal
public void SetData(SpanOrArray<byte> data, int layer, int level, Rectangle<int> region) public void SetData(SpanOrArray<byte> data, int layer, int level, Rectangle<int> region)
{ {
Logger.Warning?.Print(LogClass.Gpu, "Set data"); var blitCommandEncoder = _pipeline.GetOrCreateBlitEncoder();
MTLBlitCommandEncoder blitCommandEncoder;
if (_pipeline.CurrentEncoder is MTLBlitCommandEncoder encoder)
{
blitCommandEncoder = encoder;
}
else
{
blitCommandEncoder = _pipeline.BeginBlitPass();
}
ulong bytesPerRow = (ulong)Info.GetMipStride(level); ulong bytesPerRow = (ulong)Info.GetMipStride(level);
ulong bytesPerImage = 0; ulong bytesPerImage = 0;