2019-10-12 23:02:07 -07:00
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Image
|
|
|
|
{
|
2019-12-29 15:26:37 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Cached sampler entry for sampler pools.
|
|
|
|
/// </summary>
|
2019-10-12 23:02:07 -07:00
|
|
|
class Sampler : IDisposable
|
|
|
|
{
|
2022-06-17 09:09:14 -07:00
|
|
|
/// <summary>
|
|
|
|
/// True if the sampler is disposed, false otherwise.
|
|
|
|
/// </summary>
|
|
|
|
public bool IsDisposed { get; private set; }
|
|
|
|
|
2019-12-29 15:26:37 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Host sampler object.
|
|
|
|
/// </summary>
|
2021-11-13 11:04:21 -08:00
|
|
|
private readonly ISampler _hostSampler;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Host sampler object, with anisotropy forced.
|
|
|
|
/// </summary>
|
|
|
|
private readonly ISampler _anisoSampler;
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2019-12-29 15:26:37 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the cached sampler.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">The GPU context the sampler belongs to</param>
|
|
|
|
/// <param name="descriptor">The Maxwell sampler descriptor</param>
|
2019-10-12 23:02:07 -07:00
|
|
|
public Sampler(GpuContext context, SamplerDescriptor descriptor)
|
|
|
|
{
|
|
|
|
MinFilter minFilter = descriptor.UnpackMinFilter();
|
|
|
|
MagFilter magFilter = descriptor.UnpackMagFilter();
|
|
|
|
|
2020-11-02 12:03:06 -08:00
|
|
|
bool seamlessCubemap = descriptor.UnpackSeamlessCubemap();
|
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
AddressMode addressU = descriptor.UnpackAddressU();
|
|
|
|
AddressMode addressV = descriptor.UnpackAddressV();
|
|
|
|
AddressMode addressP = descriptor.UnpackAddressP();
|
|
|
|
|
|
|
|
CompareMode compareMode = descriptor.UnpackCompareMode();
|
|
|
|
CompareOp compareOp = descriptor.UnpackCompareOp();
|
|
|
|
|
2020-01-17 00:55:38 -08:00
|
|
|
ColorF color = new ColorF(
|
|
|
|
descriptor.BorderColorR,
|
|
|
|
descriptor.BorderColorG,
|
|
|
|
descriptor.BorderColorB,
|
|
|
|
descriptor.BorderColorA);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
|
|
|
float minLod = descriptor.UnpackMinLod();
|
|
|
|
float maxLod = descriptor.UnpackMaxLod();
|
|
|
|
float mipLodBias = descriptor.UnpackMipLodBias();
|
|
|
|
|
2021-11-13 11:04:21 -08:00
|
|
|
float maxRequestedAnisotropy = descriptor.UnpackMaxAnisotropy();
|
2020-05-27 16:03:07 -07:00
|
|
|
float maxSupportedAnisotropy = context.Capabilities.MaximumSupportedAnisotropy;
|
2020-03-30 14:38:52 -07:00
|
|
|
|
2021-11-13 11:04:21 -08:00
|
|
|
_hostSampler = context.Renderer.CreateSampler(new SamplerCreateInfo(
|
2019-10-12 23:02:07 -07:00
|
|
|
minFilter,
|
|
|
|
magFilter,
|
2020-11-02 12:03:06 -08:00
|
|
|
seamlessCubemap,
|
2019-10-12 23:02:07 -07:00
|
|
|
addressU,
|
|
|
|
addressV,
|
|
|
|
addressP,
|
|
|
|
compareMode,
|
|
|
|
compareOp,
|
|
|
|
color,
|
|
|
|
minLod,
|
|
|
|
maxLod,
|
|
|
|
mipLodBias,
|
2021-11-13 11:04:21 -08:00
|
|
|
Math.Min(maxRequestedAnisotropy, maxSupportedAnisotropy)));
|
|
|
|
|
|
|
|
if (GraphicsConfig.MaxAnisotropy >= 0 && GraphicsConfig.MaxAnisotropy <= 16 && (minFilter == MinFilter.LinearMipmapNearest || minFilter == MinFilter.LinearMipmapLinear))
|
|
|
|
{
|
|
|
|
maxRequestedAnisotropy = GraphicsConfig.MaxAnisotropy;
|
|
|
|
|
|
|
|
_anisoSampler = context.Renderer.CreateSampler(new SamplerCreateInfo(
|
|
|
|
minFilter,
|
|
|
|
magFilter,
|
|
|
|
seamlessCubemap,
|
|
|
|
addressU,
|
|
|
|
addressV,
|
|
|
|
addressP,
|
|
|
|
compareMode,
|
|
|
|
compareOp,
|
|
|
|
color,
|
|
|
|
minLod,
|
|
|
|
maxLod,
|
|
|
|
mipLodBias,
|
|
|
|
Math.Min(maxRequestedAnisotropy, maxSupportedAnisotropy)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a host sampler for the given texture.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="texture">Texture to be sampled</param>
|
|
|
|
/// <returns>A host sampler</returns>
|
|
|
|
public ISampler GetHostSampler(Texture texture)
|
|
|
|
{
|
2021-12-08 13:09:36 -08:00
|
|
|
return _anisoSampler != null && texture?.CanForceAnisotropy == true ? _anisoSampler : _hostSampler;
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
|
2019-12-29 15:26:37 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Disposes the host sampler object.
|
|
|
|
/// </summary>
|
2019-10-12 23:02:07 -07:00
|
|
|
public void Dispose()
|
|
|
|
{
|
2022-06-17 09:09:14 -07:00
|
|
|
IsDisposed = true;
|
|
|
|
|
2021-11-13 11:04:21 -08:00
|
|
|
_hostSampler.Dispose();
|
|
|
|
_anisoSampler?.Dispose();
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|