Avoid deleting textures when their data does not overlap. (#2601)

* Avoid deleting textures when their data does not overlap.

It's possible that while two textures start and end addresses indicate an overlap, that the actual data contained within them is sparse due to a layer stride. One such possibility is array slices of a cubemap at different mip levels - they overlap on a whole, but the actual texture data fills the gaps between each other's layers rather than actually overlapping.

This fixes issues with UE4 games having incorrect lighting (solid white screen or really dark shadows). There are still remaining issues with games that use the 3D texture prebaked lighting, such as THPS1+2.

This PR also fixes a bug with TexturePool's resized texture handling where the base level in the descriptor was not considered.

* AllRegions granularity for 3d textures is now by level rather than by slice.

* Address feedback
This commit is contained in:
riperiperi 2021-08-29 20:22:13 +01:00 committed by GitHub
parent 54adc5f9fb
commit 15e7fe3ac9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 2 deletions

View file

@ -9,6 +9,7 @@ using Ryujinx.Memory.Range;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Ryujinx.Graphics.Gpu.Image
{
@ -1295,6 +1296,37 @@ namespace Ryujinx.Graphics.Gpu.Image
return false;
}
/// <summary>
/// Determine if any of this texture's data overlaps with another.
/// </summary>
/// <param name="texture">The texture to check against</param>
/// <returns>True if any slice of the textures overlap, false otherwise</returns>
public bool DataOverlaps(Texture texture)
{
if (texture._sizeInfo.AllOffsets.Length == 1 && _sizeInfo.AllOffsets.Length == 1)
{
return Range.OverlapsWith(texture.Range);
}
MultiRange otherRange = texture.Range;
IEnumerable<MultiRange> regions = _sizeInfo.AllRegions().Select((region) => Range.GetSlice((ulong)region.Offset, (ulong)region.Size));
IEnumerable<MultiRange> otherRegions = texture._sizeInfo.AllRegions().Select((region) => otherRange.GetSlice((ulong)region.Offset, (ulong)region.Size));
foreach (MultiRange region in regions)
{
foreach (MultiRange otherRegion in otherRegions)
{
if (region.OverlapsWith(otherRegion))
{
return true;
}
}
}
return false;
}
/// <summary>
/// Increments the texture reference count.
/// </summary>

View file

@ -636,6 +636,13 @@ namespace Ryujinx.Graphics.Gpu.Image
continue;
}
if (!texture.DataOverlaps(overlap))
{
// Allow textures to overlap if their data does not actually overlap.
// This typically happens when mip level subranges of a layered texture are used. (each texture fills the gaps of the others)
continue;
}
// The overlap texture is going to contain garbage data after we draw, or is generally incompatible.
// If the texture cannot be entirely contained in the new address space, and one of its view children is compatible with us,
// it must be flushed before removal, so that the data is not lost.

View file

@ -85,8 +85,9 @@ namespace Ryujinx.Graphics.Gpu.Image
TextureDescriptor descriptor = GetDescriptor(id);
int width = descriptor.UnpackWidth();
int height = descriptor.UnpackHeight();
int baseLevel = descriptor.UnpackBaseLevel();
int width = Math.Max(1, descriptor.UnpackWidth() >> baseLevel);
int height = Math.Max(1, descriptor.UnpackHeight() >> baseLevel);
if (texture.Info.Width != width || texture.Info.Height != height)
{

View file

@ -0,0 +1,14 @@
namespace Ryujinx.Graphics.Texture
{
public struct Region
{
public int Offset { get; }
public int Size { get; }
public Region(int offset, int size)
{
Offset = offset;
Size = size;
}
}
}

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Texture
{
@ -91,5 +92,23 @@ namespace Ryujinx.Graphics.Texture
return true;
}
public IEnumerable<Region> AllRegions()
{
if (_is3D)
{
for (int i = 0; i < _mipOffsets.Length; i++)
{
yield return new Region(_mipOffsets[i], SliceSizes[i]);
}
}
else
{
for (int i = 0; i < AllOffsets.Length; i++)
{
yield return new Region(AllOffsets[i], SliceSizes[i % _levels]);
}
}
}
}
}