Auto-backed samplers

This commit is contained in:
Isaac Marovitz 2024-09-07 12:24:40 +02:00 committed by Isaac Marovitz
parent 2f0235fc37
commit 7245193f08
8 changed files with 66 additions and 31 deletions

View file

@ -0,0 +1,22 @@
using SharpMetal.Metal;
using System;
using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
[SupportedOSPlatform("macos")]
readonly struct DisposableSampler : IDisposable
{
public MTLSamplerState Value { get; }
public DisposableSampler(MTLSamplerState sampler)
{
Value = sampler;
}
public void Dispose()
{
Value.Dispose();
}
}
}

View file

@ -55,10 +55,10 @@ namespace Ryujinx.Graphics.Metal
{ {
public ShaderStage Stage; public ShaderStage Stage;
public TextureBase Storage; public TextureBase Storage;
public Sampler Sampler; public Auto<DisposableSampler> Sampler;
public Format ImageFormat; public Format ImageFormat;
public TextureRef(ShaderStage stage, TextureBase storage, Sampler sampler) public TextureRef(ShaderStage stage, TextureBase storage, Auto<DisposableSampler> sampler)
{ {
Stage = stage; Stage = stage;
Storage = storage; Storage = storage;

View file

@ -865,11 +865,11 @@ namespace Ryujinx.Graphics.Metal
SignalDirty(DirtyFlags.StencilRef); SignalDirty(DirtyFlags.StencilRef);
} }
public readonly void UpdateTextureAndSampler(ShaderStage stage, int binding, TextureBase texture, Sampler sampler) public readonly void UpdateTextureAndSampler(ShaderStage stage, int binding, TextureBase texture, SamplerHolder samplerHolder)
{ {
if (texture != null) if (texture != null)
{ {
_currentState.TextureRefs[binding] = new(stage, texture, sampler); _currentState.TextureRefs[binding] = new(stage, texture, samplerHolder.GetSampler());
} }
else else
{ {
@ -1312,7 +1312,7 @@ namespace Ryujinx.Graphics.Metal
if (texture.Sampler != null) if (texture.Sampler != null)
{ {
vertResourceIds[vertResourceIdIndex] = texture.Sampler.GetSampler().GpuResourceID._impl; vertResourceIds[vertResourceIdIndex] = texture.Sampler.Get(_pipeline.Cbs).Value.GpuResourceID._impl;
vertResourceIdIndex++; vertResourceIdIndex++;
} }
@ -1326,7 +1326,7 @@ namespace Ryujinx.Graphics.Metal
if (texture.Sampler != null) if (texture.Sampler != null)
{ {
fragResourceIds[fragResourceIdIndex] = texture.Sampler.GetSampler().GpuResourceID._impl; fragResourceIds[fragResourceIdIndex] = texture.Sampler.Get(_pipeline.Cbs).Value.GpuResourceID._impl;
fragResourceIdIndex++; fragResourceIdIndex++;
} }
@ -1343,7 +1343,7 @@ namespace Ryujinx.Graphics.Metal
if (segment.Type != ResourceType.BufferTexture) if (segment.Type != ResourceType.BufferTexture)
{ {
var textures = textureArray.GetTextureRefs(); var textures = textureArray.GetTextureRefs();
var samplers = new Sampler[textures.Length]; var samplers = new Auto<DisposableSampler>[textures.Length];
for (int i = 0; i < textures.Length; i++) for (int i = 0; i < textures.Length; i++)
{ {
@ -1379,7 +1379,7 @@ namespace Ryujinx.Graphics.Metal
if (sampler != null) if (sampler != null)
{ {
gpuAddress = sampler.GetSampler().GpuResourceID._impl; gpuAddress = sampler.Get(_pipeline.Cbs).Value.GpuResourceID._impl;
} }
if ((segment.Stages & ResourceStages.Vertex) != 0) if ((segment.Stages & ResourceStages.Vertex) != 0)
@ -1612,7 +1612,7 @@ namespace Ryujinx.Graphics.Metal
if (texture.Sampler != null) if (texture.Sampler != null)
{ {
resourceIds[resourceIdIndex] = texture.Sampler.GetSampler().GpuResourceID._impl; resourceIds[resourceIdIndex] = texture.Sampler.Get(_pipeline.Cbs).Value.GpuResourceID._impl;
resourceIdIndex++; resourceIdIndex++;
} }
} }
@ -1625,7 +1625,7 @@ namespace Ryujinx.Graphics.Metal
if (segment.Type != ResourceType.BufferTexture) if (segment.Type != ResourceType.BufferTexture)
{ {
var textures = textureArray.GetTextureRefs(); var textures = textureArray.GetTextureRefs();
var samplers = new Sampler[textures.Length]; var samplers = new Auto<DisposableSampler>[textures.Length];
for (int i = 0; i < textures.Length; i++) for (int i = 0; i < textures.Length; i++)
{ {
@ -1646,7 +1646,7 @@ namespace Ryujinx.Graphics.Metal
{ {
if (sampler != null) if (sampler != null)
{ {
resourceIds[resourceIdIndex] = sampler.GetSampler().GpuResourceID._impl; resourceIds[resourceIdIndex] = sampler.Get(_pipeline.Cbs).Value.GpuResourceID._impl;
resourceIdIndex++; resourceIdIndex++;
} }
} }

View file

@ -47,8 +47,8 @@ namespace Ryujinx.Graphics.Metal
_renderer = renderer; _renderer = renderer;
_pipeline = pipeline; _pipeline = pipeline;
_samplerNearest = new Sampler(_device, SamplerCreateInfo.Create(MinFilter.Nearest, MagFilter.Nearest)); _samplerNearest = new SamplerHolder(renderer, _device, SamplerCreateInfo.Create(MinFilter.Nearest, MagFilter.Nearest));
_samplerLinear = new Sampler(_device, SamplerCreateInfo.Create(MinFilter.Linear, MagFilter.Linear)); _samplerLinear = new SamplerHolder(renderer, _device, SamplerCreateInfo.Create(MinFilter.Linear, MagFilter.Linear));
var blitResourceLayout = new ResourceLayoutBuilder() var blitResourceLayout = new ResourceLayoutBuilder()
.Add(ResourceStages.Vertex, ResourceType.UniformBuffer, 0) .Add(ResourceStages.Vertex, ResourceType.UniformBuffer, 0)

View file

@ -4,6 +4,7 @@ using Ryujinx.Graphics.Shader.Translation;
using SharpMetal.Metal; using SharpMetal.Metal;
using SharpMetal.QuartzCore; using SharpMetal.QuartzCore;
using System; using System;
using System.Collections.Generic;
using System.Runtime.Versioning; using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal namespace Ryujinx.Graphics.Metal
@ -33,9 +34,12 @@ namespace Ryujinx.Graphics.Metal
internal Action<Action> InterruptAction { get; private set; } internal Action<Action> InterruptAction { get; private set; }
internal SyncManager SyncManager { get; private set; } internal SyncManager SyncManager { get; private set; }
internal HashSet<SamplerHolder> Samplers { get; }
public MetalRenderer(Func<CAMetalLayer> metalLayer) public MetalRenderer(Func<CAMetalLayer> metalLayer)
{ {
_device = MTLDevice.CreateSystemDefaultDevice(); _device = MTLDevice.CreateSystemDefaultDevice();
Samplers = new HashSet<SamplerHolder>();
if (_device.ArgumentBuffersSupport != MTLArgumentBuffersTier.Tier2) if (_device.ArgumentBuffersSupport != MTLArgumentBuffersTier.Tier2)
{ {
@ -101,7 +105,7 @@ namespace Ryujinx.Graphics.Metal
public ISampler CreateSampler(SamplerCreateInfo info) public ISampler CreateSampler(SamplerCreateInfo info)
{ {
return new Sampler(_device, info); return new SamplerHolder(this, _device, info);
} }
public ITexture CreateTexture(TextureCreateInfo info) public ITexture CreateTexture(TextureCreateInfo info)
@ -281,6 +285,12 @@ namespace Ryujinx.Graphics.Metal
public void Dispose() public void Dispose()
{ {
BackgroundResources.Dispose(); BackgroundResources.Dispose();
foreach (var sampler in Samplers)
{
sampler.Dispose();
}
_pipeline.Dispose(); _pipeline.Dispose();
_window.Dispose(); _window.Dispose();
} }

View file

@ -768,9 +768,9 @@ namespace Ryujinx.Graphics.Metal
{ {
if (texture is TextureBase tex) if (texture is TextureBase tex)
{ {
if (sampler == null || sampler is Sampler) if (sampler == null || sampler is SamplerHolder)
{ {
_encoderStateManager.UpdateTextureAndSampler(stage, binding, tex, (Sampler)sampler); _encoderStateManager.UpdateTextureAndSampler(stage, binding, tex, (SamplerHolder)sampler);
} }
} }
} }

View file

@ -6,12 +6,17 @@ using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal namespace Ryujinx.Graphics.Metal
{ {
[SupportedOSPlatform("macos")] [SupportedOSPlatform("macos")]
class Sampler : ISampler class SamplerHolder : ISampler
{ {
private readonly MTLSamplerState _mtlSamplerState; private readonly MetalRenderer _renderer;
private readonly Auto<DisposableSampler> _sampler;
public Sampler(MTLDevice device, SamplerCreateInfo info) public SamplerHolder(MetalRenderer renderer, MTLDevice device, SamplerCreateInfo info)
{ {
_renderer = renderer;
renderer.Samplers.Add(this);
(MTLSamplerMinMagFilter minFilter, MTLSamplerMipFilter mipFilter) = info.MinFilter.Convert(); (MTLSamplerMinMagFilter minFilter, MTLSamplerMipFilter mipFilter) = info.MinFilter.Convert();
MTLSamplerBorderColor borderColor = GetConstrainedBorderColor(info.BorderColor, out _); MTLSamplerBorderColor borderColor = GetConstrainedBorderColor(info.BorderColor, out _);
@ -33,14 +38,9 @@ namespace Ryujinx.Graphics.Metal
SupportArgumentBuffers = true SupportArgumentBuffers = true
}; };
var samplerState = device.NewSamplerState(descriptor); var sampler = device.NewSamplerState(descriptor);
_mtlSamplerState = samplerState; _sampler = new Auto<DisposableSampler>(new DisposableSampler(sampler));
}
public Sampler(MTLSamplerState samplerState)
{
_mtlSamplerState = samplerState;
} }
private static MTLSamplerBorderColor GetConstrainedBorderColor(ColorF arbitraryBorderColor, out bool cantConstrain) private static MTLSamplerBorderColor GetConstrainedBorderColor(ColorF arbitraryBorderColor, out bool cantConstrain)
@ -74,14 +74,17 @@ namespace Ryujinx.Graphics.Metal
return MTLSamplerBorderColor.OpaqueBlack; return MTLSamplerBorderColor.OpaqueBlack;
} }
public MTLSamplerState GetSampler() public Auto<DisposableSampler> GetSampler()
{ {
return _mtlSamplerState; return _sampler;
} }
public void Dispose() public void Dispose()
{ {
_mtlSamplerState.Dispose(); if (_renderer.Samplers.Remove(this))
{
_sampler.Dispose();
}
} }
} }
} }

View file

@ -33,9 +33,9 @@ namespace Ryujinx.Graphics.Metal
{ {
ISampler sampler = samplers[i]; ISampler sampler = samplers[i];
if (sampler is Sampler samp) if (sampler is SamplerHolder samp)
{ {
_textureRefs[index + i].Sampler = samp; _textureRefs[index + i].Sampler = samp.GetSampler();
} }
else else
{ {