Patch some leaks and only perform copies on valid textures (#37)

This commit is contained in:
riperiperi 2024-08-05 22:58:37 +01:00 committed by Isaac Marovitz
parent 9e05ee9a4d
commit 03c475d4b5
No known key found for this signature in database
GPG Key ID: 97250B2B09A132E1
6 changed files with 29 additions and 13 deletions

View File

@ -149,7 +149,7 @@ class CommandBufferEncoder
{
EndCurrentPass();
var descriptor = new MTLBlitPassDescriptor();
using var descriptor = new MTLBlitPassDescriptor();
var blitCommandEncoder = _commandBuffer.BlitCommandEncoder(descriptor);
CurrentEncoder = blitCommandEncoder;

View File

@ -176,15 +176,12 @@ namespace Ryujinx.Graphics.Metal
public readonly MTLComputeCommandEncoder CreateComputeCommandEncoder()
{
var descriptor = new MTLComputePassDescriptor();
using var descriptor = new MTLComputePassDescriptor();
var computeCommandEncoder = _pipeline.CommandBuffer.ComputeCommandEncoder(descriptor);
// Mark all state as dirty to ensure it is set on the encoder
SignalDirty(DirtyFlags.ComputeAll);
// Cleanup
descriptor.Dispose();
return computeCommandEncoder;
}

View File

@ -46,7 +46,7 @@ namespace Ryujinx.Graphics.Metal
{
ShaderSource shader = _shaders[i];
var compileOptions = new MTLCompileOptions
using var compileOptions = new MTLCompileOptions
{
PreserveInvariance = true,
LanguageVersion = MTLLanguageVersion.Version31,

View File

@ -16,7 +16,7 @@ namespace Ryujinx.Graphics.Metal
MTLSamplerBorderColor borderColor = GetConstrainedBorderColor(info.BorderColor, out _);
var samplerState = device.NewSamplerState(new MTLSamplerDescriptor
using var descriptor = new MTLSamplerDescriptor
{
BorderColor = borderColor,
MinFilter = minFilter,
@ -31,7 +31,9 @@ namespace Ryujinx.Graphics.Metal
TAddressMode = info.AddressV.Convert(),
RAddressMode = info.AddressP.Convert(),
SupportArgumentBuffers = true
});
};
var samplerState = device.NewSamplerState(descriptor);
_mtlSamplerState = samplerState;
}

View File

@ -151,6 +151,11 @@ namespace Ryujinx.Graphics.Metal
TextureBase src = this;
TextureBase dst = (TextureBase)destination;
if (!Valid || !dst.Valid)
{
return;
}
var srcImage = GetHandle();
var dstImage = dst.GetHandle();
@ -203,6 +208,11 @@ namespace Ryujinx.Graphics.Metal
TextureBase src = this;
TextureBase dst = (TextureBase)destination;
if (!Valid || !dst.Valid)
{
return;
}
var srcImage = GetHandle();
var dstImage = dst.GetHandle();

View File

@ -2,13 +2,16 @@ using Ryujinx.Graphics.GAL;
using SharpMetal.Metal;
using System;
using System.Runtime.Versioning;
using System.Threading;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
abstract class TextureBase : IDisposable
{
private bool _disposed;
private int _isValid = 1;
public bool Valid => Volatile.Read(ref _isValid) != 0;
protected readonly Pipeline Pipeline;
protected readonly MTLDevice Device;
@ -35,7 +38,7 @@ namespace Ryujinx.Graphics.Metal
public MTLTexture GetHandle()
{
if (_disposed)
if (_isValid == 0)
{
return new MTLTexture(IntPtr.Zero);
}
@ -50,11 +53,15 @@ namespace Ryujinx.Graphics.Metal
public void Dispose()
{
if (MtlTexture != IntPtr.Zero)
bool wasValid = Interlocked.Exchange(ref _isValid, 0) != 0;
if (wasValid)
{
MtlTexture.Dispose();
if (MtlTexture != IntPtr.Zero)
{
MtlTexture.Dispose();
}
}
_disposed = true;
}
}
}