1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-10-01 12:30:00 +02:00

GPU: Remove swizzle undefined matching and rework depth aliasing (#4896)

* GPU: Remove swizzle undefined matching and rework depth aliasing

@gdkchan pointed out that UI textures in TOTK seemed to be setting their texture swizzle incorrectly (texture was RGB but was sampling A, swizzle for A was wrong), so I determined that SwizzleComponentMatches was the problem and set on eliminating it. This PR combines existing work to select the most recently modified texture (now used when selecting which aliased texture to use) with some additional changes to remove the swizzle check and support aliased view creation.

The original observation (#1538) was that we wanted to match depth textures for the purposes of aliasing with color textures, but they often had different swizzle from what was sampled (as it's generally the identity swizzle once rendered). At the time, I decided to allow swizzles to match if only the defined components matched, which fixed the issue in all known cases but could easily be broken by a game _expecting_ a given swizzle, such as a 1/0 value on a component.

This error case could also occur in textures that don't even depth alias, such as R11G11B10, as the rule was created to generally apply to all cases.

The solution is now to fail this exact match test, and allow the search for an R32 texture to create a swizzled view of a D32 texture (and other such cases). This allows the creation of a view that mismatches the requested format, which wasn't present before and was the reason for the swizzle matching approach.

The exact match and view creation rules now follow the same rules over what textures to select when there are multiple options (such as a "perfect" match and an "aliased" match at the same time). It now selects the most recently modified texture, which is done with a new sequence number in the GpuContext (because we don't have enough of these).

Reportedly fixes UI having weird coloured backgrounds in TOTK. This also fixes an issue in MK8D where returning from a race resulted in the character selection cubemaps being broken. May work around issues introduced by the "short texture cache" PR due to modification ordering, though they won't be truly fixed.

Should allow (#4365) to avoid copies in more cases. Need to test that.

I tested a bunch of games #1538 originally affected and they seem to be fine. This change affects all games so it would be good to get some wide testing on it.

* Address feedback 1, fix an issue

* Workaround: Do not allow copies for format alias.

These should be removed when D32<->R32 copy dependencies become legal
This commit is contained in:
riperiperi 2023-05-12 01:30:47 +01:00 committed by GitHub
parent 49c63ea077
commit 95c06de4c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 125 additions and 106 deletions

View file

@ -98,6 +98,8 @@ namespace Ryujinx.Graphics.Gpu
private Thread _gpuThread; private Thread _gpuThread;
private bool _pendingSync; private bool _pendingSync;
private long _modifiedSequence;
/// <summary> /// <summary>
/// Creates a new instance of the GPU emulation context. /// Creates a new instance of the GPU emulation context.
/// </summary> /// </summary>
@ -200,6 +202,15 @@ namespace Ryujinx.Graphics.Gpu
return divided * NsToTicksFractionNumerator + errorBias; return divided * NsToTicksFractionNumerator + errorBias;
} }
/// <summary>
/// Gets a sequence number for resource modification ordering. This increments on each call.
/// </summary>
/// <returns>A sequence number for resource modification ordering</returns>
public long GetModifiedSequence()
{
return _modifiedSequence++;
}
/// <summary> /// <summary>
/// Gets the value of the GPU timer. /// Gets the value of the GPU timer.
/// </summary> /// </summary>

View file

@ -1170,6 +1170,7 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="caps">Host GPU capabilities</param> /// <param name="caps">Host GPU capabilities</param>
/// <param name="firstLayer">Texture view initial layer on this texture</param> /// <param name="firstLayer">Texture view initial layer on this texture</param>
/// <param name="firstLevel">Texture view first mipmap level on this texture</param> /// <param name="firstLevel">Texture view first mipmap level on this texture</param>
/// <param name="flags">Texture search flags</param>
/// <returns>The level of compatiblilty a view with the given parameters created from this texture has</returns> /// <returns>The level of compatiblilty a view with the given parameters created from this texture has</returns>
public TextureViewCompatibility IsViewCompatible( public TextureViewCompatibility IsViewCompatible(
TextureInfo info, TextureInfo info,
@ -1178,11 +1179,12 @@ namespace Ryujinx.Graphics.Gpu.Image
int layerSize, int layerSize,
Capabilities caps, Capabilities caps,
out int firstLayer, out int firstLayer,
out int firstLevel) out int firstLevel,
TextureSearchFlags flags = TextureSearchFlags.None)
{ {
TextureViewCompatibility result = TextureViewCompatibility.Full; TextureViewCompatibility result = TextureViewCompatibility.Full;
result = TextureCompatibility.PropagateViewCompatibility(result, TextureCompatibility.ViewFormatCompatible(Info, info, caps)); result = TextureCompatibility.PropagateViewCompatibility(result, TextureCompatibility.ViewFormatCompatible(Info, info, caps, flags));
if (result != TextureViewCompatibility.Incompatible) if (result != TextureViewCompatibility.Incompatible)
{ {
result = TextureCompatibility.PropagateViewCompatibility(result, TextureCompatibility.ViewTargetCompatible(Info, info, ref caps)); result = TextureCompatibility.PropagateViewCompatibility(result, TextureCompatibility.ViewTargetCompatible(Info, info, ref caps));

View file

@ -569,7 +569,7 @@ namespace Ryujinx.Graphics.Gpu.Image
Texture texture = null; Texture texture = null;
TextureMatchQuality bestQuality = TextureMatchQuality.NoMatch; long bestSequence = 0;
for (int index = 0; index < sameAddressOverlapsCount; index++) for (int index = 0; index < sameAddressOverlapsCount; index++)
{ {
@ -601,17 +601,12 @@ namespace Ryujinx.Graphics.Gpu.Image
continue; continue;
} }
} }
}
if (matchQuality == TextureMatchQuality.Perfect) if (texture == null || overlap.Group.ModifiedSequence - bestSequence > 0)
{ {
texture = overlap; texture = overlap;
break; bestSequence = overlap.Group.ModifiedSequence;
} }
else if (matchQuality > bestQuality)
{
texture = overlap;
bestQuality = matchQuality;
} }
} }
@ -664,6 +659,7 @@ namespace Ryujinx.Graphics.Gpu.Image
int fullyCompatible = 0; int fullyCompatible = 0;
// Evaluate compatibility of overlaps, add temporary references // Evaluate compatibility of overlaps, add temporary references
int preferredOverlap = -1;
for (int index = 0; index < overlapsCount; index++) for (int index = 0; index < overlapsCount; index++)
{ {
@ -675,17 +671,26 @@ namespace Ryujinx.Graphics.Gpu.Image
sizeInfo.LayerSize, sizeInfo.LayerSize,
_context.Capabilities, _context.Capabilities,
out int firstLayer, out int firstLayer,
out int firstLevel); out int firstLevel,
flags);
if (overlapCompatibility == TextureViewCompatibility.Full) if (overlapCompatibility >= TextureViewCompatibility.FormatAlias)
{ {
if (overlap.IsView) if (overlap.IsView)
{ {
overlapCompatibility = TextureViewCompatibility.CopyOnly; overlapCompatibility = overlapCompatibility == TextureViewCompatibility.FormatAlias ?
TextureViewCompatibility.Incompatible :
TextureViewCompatibility.CopyOnly;
} }
else else
{ {
fullyCompatible++; fullyCompatible++;
if (preferredOverlap == -1 || overlap.Group.ModifiedSequence - bestSequence > 0)
{
preferredOverlap = index;
bestSequence = overlap.Group.ModifiedSequence;
}
} }
} }
@ -695,37 +700,50 @@ namespace Ryujinx.Graphics.Gpu.Image
// Search through the overlaps to find a compatible view and establish any copy dependencies. // Search through the overlaps to find a compatible view and establish any copy dependencies.
for (int index = 0; index < overlapsCount; index++) if (preferredOverlap != -1)
{ {
Texture overlap = _textureOverlaps[index]; Texture overlap = _textureOverlaps[preferredOverlap];
OverlapInfo oInfo = _overlapInfo[index]; OverlapInfo oInfo = _overlapInfo[preferredOverlap];
if (oInfo.Compatibility == TextureViewCompatibility.Full) bool aliased = oInfo.Compatibility == TextureViewCompatibility.FormatAlias;
if (!isSamplerTexture)
{ {
if (!isSamplerTexture) // If this is not a sampler texture, the size might be different from the requested size,
{ // so we need to make sure the texture information has the correct size for this base texture,
// If this is not a sampler texture, the size might be different from the requested size, // before creating the view.
// so we need to make sure the texture information has the correct size for this base texture,
// before creating the view.
info = info.CreateInfoForLevelView(overlap, oInfo.FirstLevel);
}
texture = overlap.CreateView(info, sizeInfo, range.Value, oInfo.FirstLayer, oInfo.FirstLevel); info = info.CreateInfoForLevelView(overlap, oInfo.FirstLevel, aliased);
texture.SynchronizeMemory();
break;
} }
else if (oInfo.Compatibility == TextureViewCompatibility.CopyOnly && fullyCompatible == 0) else if (aliased)
{ {
// Only copy compatible. If there's another choice for a FULLY compatible texture, choose that instead. // The format must be changed to match the parent.
info = info.CreateInfoWithFormat(overlap.Info.FormatInfo);
}
texture = new Texture(_context, _physicalMemory, info, sizeInfo, range.Value, scaleMode); texture = overlap.CreateView(info, sizeInfo, range.Value, oInfo.FirstLayer, oInfo.FirstLevel);
texture.SynchronizeMemory();
}
else
{
for (int index = 0; index < overlapsCount; index++)
{
Texture overlap = _textureOverlaps[index];
OverlapInfo oInfo = _overlapInfo[index];
texture.InitializeGroup(true, true, new List<TextureIncompatibleOverlap>()); if (oInfo.Compatibility == TextureViewCompatibility.CopyOnly && fullyCompatible == 0)
texture.InitializeData(false, false); {
// Only copy compatible. If there's another choice for a FULLY compatible texture, choose that instead.
overlap.SynchronizeMemory(); texture = new Texture(_context, _physicalMemory, info, sizeInfo, range.Value, scaleMode);
overlap.CreateCopyDependency(texture, oInfo.FirstLayer, oInfo.FirstLevel, true);
break; texture.InitializeGroup(true, true, new List<TextureIncompatibleOverlap>());
texture.InitializeData(false, false);
overlap.SynchronizeMemory();
overlap.CreateCopyDependency(texture, oInfo.FirstLayer, oInfo.FirstLevel, true);
break;
}
} }
} }
@ -740,7 +758,7 @@ namespace Ryujinx.Graphics.Gpu.Image
Texture overlap = _textureOverlaps[index]; Texture overlap = _textureOverlaps[index];
OverlapInfo oInfo = _overlapInfo[index]; OverlapInfo oInfo = _overlapInfo[index];
if (oInfo.Compatibility <= TextureViewCompatibility.LayoutIncompatible) if (oInfo.Compatibility <= TextureViewCompatibility.LayoutIncompatible || oInfo.Compatibility == TextureViewCompatibility.FormatAlias)
{ {
if (!overlap.IsView && texture.DataOverlaps(overlap, oInfo.Compatibility)) if (!overlap.IsView && texture.DataOverlaps(overlap, oInfo.Compatibility))
{ {

View file

@ -291,22 +291,7 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <returns>The minimum compatibility level of two provided view compatibility results</returns> /// <returns>The minimum compatibility level of two provided view compatibility results</returns>
public static TextureViewCompatibility PropagateViewCompatibility(TextureViewCompatibility first, TextureViewCompatibility second) public static TextureViewCompatibility PropagateViewCompatibility(TextureViewCompatibility first, TextureViewCompatibility second)
{ {
if (first == TextureViewCompatibility.Incompatible || second == TextureViewCompatibility.Incompatible) return (TextureViewCompatibility)Math.Min((int)first, (int)second);
{
return TextureViewCompatibility.Incompatible;
}
else if (first == TextureViewCompatibility.LayoutIncompatible || second == TextureViewCompatibility.LayoutIncompatible)
{
return TextureViewCompatibility.LayoutIncompatible;
}
else if (first == TextureViewCompatibility.CopyOnly || second == TextureViewCompatibility.CopyOnly)
{
return TextureViewCompatibility.CopyOnly;
}
else
{
return TextureViewCompatibility.Full;
}
} }
/// <summary> /// <summary>
@ -628,15 +613,21 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="lhs">Texture information of the texture view</param> /// <param name="lhs">Texture information of the texture view</param>
/// <param name="rhs">Texture information of the texture view</param> /// <param name="rhs">Texture information of the texture view</param>
/// <param name="caps">Host GPU capabilities</param> /// <param name="caps">Host GPU capabilities</param>
/// <param name="flags">Texture search flags</param>
/// <returns>The view compatibility level of the texture formats</returns> /// <returns>The view compatibility level of the texture formats</returns>
public static TextureViewCompatibility ViewFormatCompatible(TextureInfo lhs, TextureInfo rhs, Capabilities caps) public static TextureViewCompatibility ViewFormatCompatible(TextureInfo lhs, TextureInfo rhs, Capabilities caps, TextureSearchFlags flags)
{ {
FormatInfo lhsFormat = lhs.FormatInfo; FormatInfo lhsFormat = lhs.FormatInfo;
FormatInfo rhsFormat = rhs.FormatInfo; FormatInfo rhsFormat = rhs.FormatInfo;
if (lhsFormat.Format.IsDepthOrStencil() || rhsFormat.Format.IsDepthOrStencil()) if (lhsFormat.Format.IsDepthOrStencil() || rhsFormat.Format.IsDepthOrStencil())
{ {
return lhsFormat.Format == rhsFormat.Format ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible; return FormatMatches(lhs, rhs, flags.HasFlag(TextureSearchFlags.ForSampler), flags.HasFlag(TextureSearchFlags.DepthAlias)) switch
{
TextureMatchQuality.Perfect => TextureViewCompatibility.Full,
TextureMatchQuality.FormatAlias => TextureViewCompatibility.FormatAlias,
_ => TextureViewCompatibility.Incompatible
};
} }
if (IsFormatHostIncompatible(lhs, caps) || IsFormatHostIncompatible(rhs, caps)) if (IsFormatHostIncompatible(lhs, caps) || IsFormatHostIncompatible(rhs, caps))
@ -754,49 +745,6 @@ namespace Ryujinx.Graphics.Gpu.Image
return result ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible; return result ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible;
} }
/// <summary>
/// Checks if a swizzle component in two textures functionally match, taking into account if the components are defined.
/// </summary>
/// <param name="lhs">Texture information to compare</param>
/// <param name="rhs">Texture information to compare with</param>
/// <param name="swizzleLhs">Swizzle component for the first texture</param>
/// <param name="swizzleRhs">Swizzle component for the second texture</param>
/// <param name="component">Component index, starting at 0 for red</param>
/// <returns>True if the swizzle components functionally match, false othersize</returns>
private static bool SwizzleComponentMatches(TextureInfo lhs, TextureInfo rhs, SwizzleComponent swizzleLhs, SwizzleComponent swizzleRhs, int component)
{
int lhsComponents = lhs.FormatInfo.Components;
int rhsComponents = rhs.FormatInfo.Components;
if (lhsComponents == 4 && rhsComponents == 4)
{
return swizzleLhs == swizzleRhs;
}
// Swizzles after the number of components a format defines are "undefined".
// We allow these to not be equal under certain circumstances.
// This can only happen when there are less than 4 components in a format.
// It tends to happen when float depth textures are sampled.
bool lhsDefined = (swizzleLhs - SwizzleComponent.Red) < lhsComponents;
bool rhsDefined = (swizzleRhs - SwizzleComponent.Red) < rhsComponents;
if (lhsDefined == rhsDefined)
{
// If both are undefined, return true. Otherwise just check if they're equal.
return lhsDefined ? swizzleLhs == swizzleRhs : true;
}
else
{
SwizzleComponent defined = lhsDefined ? swizzleLhs : swizzleRhs;
SwizzleComponent undefined = lhsDefined ? swizzleRhs : swizzleLhs;
// Undefined swizzle can be matched by a forced value (0, 1), exact equality, or expected value.
// For example, R___ matches R001, RGBA but not RBGA.
return defined == undefined || defined < SwizzleComponent.Red || defined == SwizzleComponent.Red + component;
}
}
/// <summary> /// <summary>
/// Checks if the texture shader sampling parameters of two texture informations match. /// Checks if the texture shader sampling parameters of two texture informations match.
/// </summary> /// </summary>
@ -806,10 +754,10 @@ namespace Ryujinx.Graphics.Gpu.Image
public static bool SamplerParamsMatches(TextureInfo lhs, TextureInfo rhs) public static bool SamplerParamsMatches(TextureInfo lhs, TextureInfo rhs)
{ {
return lhs.DepthStencilMode == rhs.DepthStencilMode && return lhs.DepthStencilMode == rhs.DepthStencilMode &&
SwizzleComponentMatches(lhs, rhs, lhs.SwizzleR, rhs.SwizzleR, 0) && lhs.SwizzleR == rhs.SwizzleR &&
SwizzleComponentMatches(lhs, rhs, lhs.SwizzleG, rhs.SwizzleG, 1) && lhs.SwizzleG == rhs.SwizzleG &&
SwizzleComponentMatches(lhs, rhs, lhs.SwizzleB, rhs.SwizzleB, 2) && lhs.SwizzleB == rhs.SwizzleB &&
SwizzleComponentMatches(lhs, rhs, lhs.SwizzleA, rhs.SwizzleA, 3); lhs.SwizzleA == rhs.SwizzleA;
} }
/// <summary> /// <summary>

View file

@ -68,6 +68,11 @@ namespace Ryujinx.Graphics.Gpu.Image
/// </summary> /// </summary>
public bool HasIncompatibleOverlaps => _incompatibleOverlaps.Count > 0; public bool HasIncompatibleOverlaps => _incompatibleOverlaps.Count > 0;
/// <summary>
/// Number indicating the order this texture group was modified relative to others.
/// </summary>
public long ModifiedSequence { get; private set; }
private readonly GpuContext _context; private readonly GpuContext _context;
private readonly PhysicalMemory _physicalMemory; private readonly PhysicalMemory _physicalMemory;
@ -664,6 +669,8 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="texture">The texture that has been modified</param> /// <param name="texture">The texture that has been modified</param>
public void SignalModified(Texture texture) public void SignalModified(Texture texture)
{ {
ModifiedSequence = _context.GetModifiedSequence();
ClearIncompatibleOverlaps(texture); ClearIncompatibleOverlaps(texture);
EvaluateRelevantHandles(texture, (baseHandle, regionCount, split) => EvaluateRelevantHandles(texture, (baseHandle, regionCount, split) =>
@ -684,6 +691,8 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <param name="bound">True if this texture is being bound, false if unbound</param> /// <param name="bound">True if this texture is being bound, false if unbound</param>
public void SignalModifying(Texture texture, bool bound) public void SignalModifying(Texture texture, bool bound)
{ {
ModifiedSequence = _context.GetModifiedSequence();
ClearIncompatibleOverlaps(texture); ClearIncompatibleOverlaps(texture);
EvaluateRelevantHandles(texture, (baseHandle, regionCount, split) => EvaluateRelevantHandles(texture, (baseHandle, regionCount, split) =>

View file

@ -300,8 +300,9 @@ namespace Ryujinx.Graphics.Gpu.Image
/// </summary> /// </summary>
/// <param name="parent">The parent texture</param> /// <param name="parent">The parent texture</param>
/// <param name="firstLevel">The first level of the texture view</param> /// <param name="firstLevel">The first level of the texture view</param>
/// <param name="parentFormat">True if the parent format should be inherited</param>
/// <returns>The adjusted texture information with the new size</returns> /// <returns>The adjusted texture information with the new size</returns>
public TextureInfo CreateInfoForLevelView(Texture parent, int firstLevel) public TextureInfo CreateInfoForLevelView(Texture parent, int firstLevel, bool parentFormat)
{ {
// When the texture is used as view of another texture, we must // When the texture is used as view of another texture, we must
// ensure that the sizes are valid, otherwise data uploads would fail // ensure that the sizes are valid, otherwise data uploads would fail
@ -370,7 +371,36 @@ namespace Ryujinx.Graphics.Gpu.Image
GobBlocksInZ, GobBlocksInZ,
GobBlocksInTileX, GobBlocksInTileX,
target, target,
FormatInfo, parentFormat ? parent.Info.FormatInfo : FormatInfo,
DepthStencilMode,
SwizzleR,
SwizzleG,
SwizzleB,
SwizzleA);
}
/// <summary>
/// Creates texture information for a given format and this information.
/// </summary>
/// <param name="formatInfo">Format for the new texture info</param>
/// <returns>New info with the specified format</returns>
public TextureInfo CreateInfoWithFormat(FormatInfo formatInfo)
{
return new TextureInfo(
GpuAddress,
Width,
Height,
DepthOrLayers,
Levels,
SamplesInX,
SamplesInY,
Stride,
IsLinear,
GobBlocksInY,
GobBlocksInZ,
GobBlocksInTileX,
Target,
formatInfo,
DepthStencilMode, DepthStencilMode,
SwizzleR, SwizzleR,
SwizzleG, SwizzleG,

View file

@ -9,6 +9,7 @@
Incompatible = 0, Incompatible = 0,
LayoutIncompatible, LayoutIncompatible,
CopyOnly, CopyOnly,
FormatAlias,
Full Full
} }
} }