2023-07-28 01:51:20 +00:00
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using SharpMetal.Metal;
|
2024-05-14 18:51:53 +00:00
|
|
|
using System;
|
2024-05-15 13:03:53 +00:00
|
|
|
using System.Runtime.Versioning;
|
2023-07-28 01:51:20 +00:00
|
|
|
|
|
|
|
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
|
2023-07-28 01:51:20 +00:00
|
|
|
{
|
2024-03-19 18:05:09 +00:00
|
|
|
private readonly MTLSamplerState _mtlSamplerState;
|
2023-07-28 01:51:20 +00:00
|
|
|
|
2024-03-19 18:05:09 +00:00
|
|
|
public Sampler(MTLDevice device, SamplerCreateInfo info)
|
2023-07-28 01:51:20 +00:00
|
|
|
{
|
2024-03-19 18:05:09 +00:00
|
|
|
(MTLSamplerMinMagFilter minFilter, MTLSamplerMipFilter mipFilter) = info.MinFilter.Convert();
|
|
|
|
|
2024-06-26 23:20:00 +00:00
|
|
|
MTLSamplerBorderColor borderColor = GetConstrainedBorderColor(info.BorderColor, out _);
|
|
|
|
|
2024-03-19 18:05:09 +00:00
|
|
|
var samplerState = device.NewSamplerState(new MTLSamplerDescriptor
|
|
|
|
{
|
2024-06-26 23:20:00 +00:00
|
|
|
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,
|
2024-05-14 18:51:53 +00:00
|
|
|
MaxAnisotropy = Math.Max((uint)info.MaxAnisotropy, 1),
|
2024-03-19 18:05:09 +00:00
|
|
|
SAddressMode = info.AddressU.Convert(),
|
|
|
|
TAddressMode = info.AddressV.Convert(),
|
2024-06-25 13:25:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-26 23:20:00 +00:00
|
|
|
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;
|
2023-07-28 01:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2024-05-23 17:15:23 +00:00
|
|
|
_mtlSamplerState.Dispose();
|
2023-07-28 01:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|