Ryujinx/src/Ryujinx.Graphics.Metal/Sampler.cs

86 lines
2.7 KiB
C#
Raw Normal View History

using Ryujinx.Graphics.GAL;
using SharpMetal.Metal;
using System;
2024-05-15 13:03:53 +00:00
using System.Runtime.Versioning;
namespace Ryujinx.Graphics.Metal
{
2024-03-19 18:05:09 +00:00
[SupportedOSPlatform("macos")]
2023-08-03 18:50:49 +00:00
class Sampler : ISampler
{
2024-03-19 18:05:09 +00:00
private readonly MTLSamplerState _mtlSamplerState;
2024-03-19 18:05:09 +00:00
public Sampler(MTLDevice device, SamplerCreateInfo info)
{
2024-03-19 18:05:09 +00:00
(MTLSamplerMinMagFilter minFilter, MTLSamplerMipFilter mipFilter) = info.MinFilter.Convert();
MTLSamplerBorderColor borderColor = GetConstrainedBorderColor(info.BorderColor, out _);
2024-03-19 18:05:09 +00:00
var samplerState = device.NewSamplerState(new MTLSamplerDescriptor
{
BorderColor = borderColor,
2024-03-19 18:05:09 +00:00
MinFilter = minFilter,
MagFilter = info.MagFilter.Convert(),
MipFilter = mipFilter,
CompareFunction = info.CompareOp.Convert(),
LodMinClamp = info.MinLod,
LodMaxClamp = info.MaxLod,
LodAverage = false,
MaxAnisotropy = Math.Max((uint)info.MaxAnisotropy, 1),
2024-03-19 18:05:09 +00:00
SAddressMode = info.AddressU.Convert(),
TAddressMode = info.AddressV.Convert(),
RAddressMode = info.AddressP.Convert(),
SupportArgumentBuffers = true
2024-03-19 18:05:09 +00:00
});
_mtlSamplerState = samplerState;
}
2024-05-18 22:54:55 +00:00
public Sampler(MTLSamplerState samplerState)
{
_mtlSamplerState = samplerState;
}
private static MTLSamplerBorderColor GetConstrainedBorderColor(ColorF arbitraryBorderColor, out bool cantConstrain)
{
float r = arbitraryBorderColor.Red;
float g = arbitraryBorderColor.Green;
float b = arbitraryBorderColor.Blue;
float a = arbitraryBorderColor.Alpha;
if (r == 0f && g == 0f && b == 0f)
{
if (a == 1f)
{
cantConstrain = false;
return MTLSamplerBorderColor.OpaqueBlack;
}
if (a == 0f)
{
cantConstrain = false;
return MTLSamplerBorderColor.TransparentBlack;
}
}
else if (r == 1f && g == 1f && b == 1f && a == 1f)
{
cantConstrain = false;
return MTLSamplerBorderColor.OpaqueWhite;
}
cantConstrain = true;
return MTLSamplerBorderColor.OpaqueBlack;
}
2024-03-19 18:05:09 +00:00
public MTLSamplerState GetSampler()
{
return _mtlSamplerState;
}
public void Dispose()
{
2024-05-23 17:15:23 +00:00
_mtlSamplerState.Dispose();
}
}
}