1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-11-13 03:06:41 +00:00
Ryujinx/Ryujinx.Graphics.Gpu/Image/Sampler.cs
riperiperi 788aec511f
Limit Custom Anisotropic Filtering to mipmapped textures with many levels (#2832)
* Limit Custom Anisotropic Filtering to only fully mipmapped textures

There's a major flaw with the anisotropic filtering setting that causes @GamerzHell9137 to report graphical bugs that otherwise wouldn't be there, because he just won't set it to Auto. This should fix those issues, hopefully.

These bugs are generally because anisotropic filtering is enabled on something that it shouldn't be, such as a post process filter or some data texture. This PR maintains two host samplers when custom AF is enabled, and only uses the forced AF one when the texture is 2d and fully mipmapped (goes down to 1x1). This is because game textures are the ideal target for this filtering, and they are typically fully mipmapped, unlike things like screen render targets which usually have 1 or just a few levels.

This also only enables AF on mipmapped samplers where the filtering is bilinear or trilinear. This should be self explanatory.

This PR also allows the changing of Anisotropic Filtering at runtime, and you can immediately see the changes. All samplers are flushed from the cache if the setting changes, causing them to be recreated with the new custom AF value. This brings it in line with our resolution scale. 😌

* Expected minimum mip count for large textures rather than all, address feedback

* Use Target rather than Info.Target

* Retrigger build?

* Fix rebase
2021-11-13 16:04:21 -03:00

130 lines
No EOL
4.7 KiB
C#

using Ryujinx.Graphics.GAL;
using System;
using System.Numerics;
namespace Ryujinx.Graphics.Gpu.Image
{
/// <summary>
/// Cached sampler entry for sampler pools.
/// </summary>
class Sampler : IDisposable
{
private const int MinLevelsForAnisotropic = 5;
/// <summary>
/// Host sampler object.
/// </summary>
private readonly ISampler _hostSampler;
/// <summary>
/// Host sampler object, with anisotropy forced.
/// </summary>
private readonly ISampler _anisoSampler;
/// <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>
public Sampler(GpuContext context, SamplerDescriptor descriptor)
{
MinFilter minFilter = descriptor.UnpackMinFilter();
MagFilter magFilter = descriptor.UnpackMagFilter();
bool seamlessCubemap = descriptor.UnpackSeamlessCubemap();
AddressMode addressU = descriptor.UnpackAddressU();
AddressMode addressV = descriptor.UnpackAddressV();
AddressMode addressP = descriptor.UnpackAddressP();
CompareMode compareMode = descriptor.UnpackCompareMode();
CompareOp compareOp = descriptor.UnpackCompareOp();
ColorF color = new ColorF(
descriptor.BorderColorR,
descriptor.BorderColorG,
descriptor.BorderColorB,
descriptor.BorderColorA);
float minLod = descriptor.UnpackMinLod();
float maxLod = descriptor.UnpackMaxLod();
float mipLodBias = descriptor.UnpackMipLodBias();
float maxRequestedAnisotropy = descriptor.UnpackMaxAnisotropy();
float maxSupportedAnisotropy = context.Capabilities.MaximumSupportedAnisotropy;
_hostSampler = context.Renderer.CreateSampler(new SamplerCreateInfo(
minFilter,
magFilter,
seamlessCubemap,
addressU,
addressV,
addressP,
compareMode,
compareOp,
color,
minLod,
maxLod,
mipLodBias,
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)
{
return _anisoSampler != null && AllowForceAnisotropy(texture) ? _anisoSampler : _hostSampler;
}
/// <summary>
/// Determine if the given texture can have anisotropic filtering forced.
/// Filtered textures that we might want to force anisotropy on should have a lot of mip levels.
/// </summary>
/// <param name="texture">The texture</param>
/// <returns>True if anisotropic filtering can be forced, false otherwise</returns>
private static bool AllowForceAnisotropy(Texture texture)
{
if (texture == null || !(texture.Target == Target.Texture2D || texture.Target == Target.Texture2DArray))
{
return false;
}
int maxSize = Math.Max(texture.Info.Width, texture.Info.Height);
int maxLevels = BitOperations.Log2((uint)maxSize) + 1;
return texture.Info.Levels >= Math.Min(MinLevelsForAnisotropic, maxLevels);
}
/// <summary>
/// Disposes the host sampler object.
/// </summary>
public void Dispose()
{
_hostSampler.Dispose();
_anisoSampler?.Dispose();
}
}
}