Ryujinx/src/Ryujinx.Graphics.Gpu/Image/TextureInfo.cs
riperiperi 95c06de4c1
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
2023-05-11 21:30:47 -03:00

411 lines
16 KiB
C#

using Ryujinx.Common;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Texture;
using System;
namespace Ryujinx.Graphics.Gpu.Image
{
/// <summary>
/// Texture information.
/// </summary>
readonly struct TextureInfo
{
/// <summary>
/// Address of the texture in GPU mapped memory.
/// </summary>
public ulong GpuAddress { get; }
/// <summary>
/// The width of the texture.
/// </summary>
public int Width { get; }
/// <summary>
/// The height of the texture, or layers count for 1D array textures.
/// </summary>
public int Height { get; }
/// <summary>
/// The depth of the texture (for 3D textures), or layers count for array textures.
/// </summary>
public int DepthOrLayers { get; }
/// <summary>
/// The number of mipmap levels of the texture.
/// </summary>
public int Levels { get; }
/// <summary>
/// The number of samples in the X direction for multisampled textures.
/// </summary>
public int SamplesInX { get; }
/// <summary>
/// The number of samples in the Y direction for multisampled textures.
/// </summary>
public int SamplesInY { get; }
/// <summary>
/// The number of bytes per line for linear textures.
/// </summary>
public int Stride { get; }
/// <summary>
/// Indicates whenever or not the texture is a linear texture.
/// </summary>
public bool IsLinear { get; }
/// <summary>
/// GOB blocks in the Y direction, for block linear textures.
/// </summary>
public int GobBlocksInY { get; }
/// <summary>
/// GOB blocks in the Z direction, for block linear textures.
/// </summary>
public int GobBlocksInZ { get; }
/// <summary>
/// Number of GOB blocks per tile in the X direction, for block linear textures.
/// </summary>
public int GobBlocksInTileX { get; }
/// <summary>
/// Total number of samples for multisampled textures.
/// </summary>
public int Samples => SamplesInX * SamplesInY;
/// <summary>
/// Texture target type.
/// </summary>
public Target Target { get; }
/// <summary>
/// Texture format information.
/// </summary>
public FormatInfo FormatInfo { get; }
/// <summary>
/// Depth-stencil mode of the texture. This defines whenever the depth or stencil value is read from shaders,
/// for depth-stencil texture formats.
/// </summary>
public DepthStencilMode DepthStencilMode { get; }
/// <summary>
/// Texture swizzle for the red color channel.
/// </summary>
public SwizzleComponent SwizzleR { get; }
/// <summary>
/// Texture swizzle for the green color channel.
/// </summary>
public SwizzleComponent SwizzleG { get; }
/// <summary>
/// Texture swizzle for the blue color channel.
/// </summary>
public SwizzleComponent SwizzleB { get; }
/// <summary>
/// Texture swizzle for the alpha color channel.
/// </summary>
public SwizzleComponent SwizzleA { get; }
/// <summary>
/// Constructs the texture information structure.
/// </summary>
/// <param name="gpuAddress">The GPU address of the texture</param>
/// <param name="width">The width of the texture</param>
/// <param name="height">The height or the texture</param>
/// <param name="depthOrLayers">The depth or layers count of the texture</param>
/// <param name="levels">The amount of mipmap levels of the texture</param>
/// <param name="samplesInX">The number of samples in the X direction for multisample textures, should be 1 otherwise</param>
/// <param name="samplesInY">The number of samples in the Y direction for multisample textures, should be 1 otherwise</param>
/// <param name="stride">The stride for linear textures</param>
/// <param name="isLinear">Whenever the texture is linear or block linear</param>
/// <param name="gobBlocksInY">Number of GOB blocks in the Y direction</param>
/// <param name="gobBlocksInZ">Number of GOB blocks in the Z direction</param>
/// <param name="gobBlocksInTileX">Number of GOB blocks per tile in the X direction</param>
/// <param name="target">Texture target type</param>
/// <param name="formatInfo">Texture format information</param>
/// <param name="depthStencilMode">Depth-stencil mode</param>
/// <param name="swizzleR">Swizzle for the red color channel</param>
/// <param name="swizzleG">Swizzle for the green color channel</param>
/// <param name="swizzleB">Swizzle for the blue color channel</param>
/// <param name="swizzleA">Swizzle for the alpha color channel</param>
public TextureInfo(
ulong gpuAddress,
int width,
int height,
int depthOrLayers,
int levels,
int samplesInX,
int samplesInY,
int stride,
bool isLinear,
int gobBlocksInY,
int gobBlocksInZ,
int gobBlocksInTileX,
Target target,
FormatInfo formatInfo,
DepthStencilMode depthStencilMode = DepthStencilMode.Depth,
SwizzleComponent swizzleR = SwizzleComponent.Red,
SwizzleComponent swizzleG = SwizzleComponent.Green,
SwizzleComponent swizzleB = SwizzleComponent.Blue,
SwizzleComponent swizzleA = SwizzleComponent.Alpha)
{
GpuAddress = gpuAddress;
Width = width;
Height = height;
DepthOrLayers = depthOrLayers;
Levels = levels;
SamplesInX = samplesInX;
SamplesInY = samplesInY;
Stride = stride;
IsLinear = isLinear;
GobBlocksInY = gobBlocksInY;
GobBlocksInZ = gobBlocksInZ;
GobBlocksInTileX = gobBlocksInTileX;
Target = target;
FormatInfo = formatInfo;
DepthStencilMode = depthStencilMode;
SwizzleR = swizzleR;
SwizzleG = swizzleG;
SwizzleB = swizzleB;
SwizzleA = swizzleA;
}
/// <summary>
/// Gets the real texture depth.
/// Returns 1 for any target other than 3D textures.
/// </summary>
/// <returns>Texture depth</returns>
public int GetDepth()
{
return GetDepth(Target, DepthOrLayers);
}
/// <summary>
/// Gets the real texture depth.
/// Returns 1 for any target other than 3D textures.
/// </summary>
/// <param name="target">Texture target</param>
/// <param name="depthOrLayers">Texture depth if the texture is 3D, otherwise ignored</param>
/// <returns>Texture depth</returns>
public static int GetDepth(Target target, int depthOrLayers)
{
return target == Target.Texture3D ? depthOrLayers : 1;
}
/// <summary>
/// Gets the number of layers of the texture.
/// Returns 1 for non-array textures, 6 for cubemap textures, and layer faces for cubemap array textures.
/// </summary>
/// <returns>The number of texture layers</returns>
public int GetLayers()
{
return GetLayers(Target, DepthOrLayers);
}
/// <summary>
/// Gets the number of layers of the texture.
/// Returns 1 for non-array textures, 6 for cubemap textures, and layer faces for cubemap array textures.
/// </summary>
/// <param name="target">Texture target</param>
/// <param name="depthOrLayers">Texture layers if the is a array texture, ignored otherwise</param>
/// <returns>The number of texture layers</returns>
public static int GetLayers(Target target, int depthOrLayers)
{
if (target == Target.Texture2DArray || target == Target.Texture2DMultisampleArray)
{
return depthOrLayers;
}
else if (target == Target.CubemapArray)
{
return depthOrLayers * 6;
}
else if (target == Target.Cubemap)
{
return 6;
}
else
{
return 1;
}
}
/// <summary>
/// Gets the number of 2D slices of the texture.
/// Returns 6 for cubemap textures, layer faces for cubemap array textures, and DepthOrLayers for everything else.
/// </summary>
/// <returns>The number of texture slices</returns>
public int GetSlices()
{
if (Target == Target.Texture3D || Target == Target.Texture2DArray || Target == Target.Texture2DMultisampleArray)
{
return DepthOrLayers;
}
else if (Target == Target.CubemapArray)
{
return DepthOrLayers * 6;
}
else if (Target == Target.Cubemap)
{
return 6;
}
else
{
return 1;
}
}
/// <summary>
/// Calculates the size information from the texture information.
/// </summary>
/// <param name="layerSize">Optional size of each texture layer in bytes</param>
/// <returns>Texture size information</returns>
public SizeInfo CalculateSizeInfo(int layerSize = 0)
{
if (Target == Target.TextureBuffer)
{
return new SizeInfo(Width * FormatInfo.BytesPerPixel);
}
else if (IsLinear)
{
return SizeCalculator.GetLinearTextureSize(
Stride,
Height,
FormatInfo.BlockHeight);
}
else
{
return SizeCalculator.GetBlockLinearTextureSize(
Width,
Height,
GetDepth(),
Levels,
GetLayers(),
FormatInfo.BlockWidth,
FormatInfo.BlockHeight,
FormatInfo.BytesPerPixel,
GobBlocksInY,
GobBlocksInZ,
GobBlocksInTileX,
layerSize);
}
}
/// <summary>
/// Creates texture information for a given mipmap level of the specified parent texture and this information.
/// </summary>
/// <param name="parent">The parent texture</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>
public TextureInfo CreateInfoForLevelView(Texture parent, int firstLevel, bool parentFormat)
{
// When the texture is used as view of another texture, we must
// ensure that the sizes are valid, otherwise data uploads would fail
// (and the size wouldn't match the real size used on the host API).
// Given a parent texture from where the view is created, we have the
// following rules:
// - The view size must be equal to the parent size, divided by (2 ^ l),
// where l is the first mipmap level of the view. The division result must
// be rounded down, and the result must be clamped to 1.
// - If the parent format is compressed, and the view format isn't, the
// view size is calculated as above, but the width and height of the
// view must be also divided by the compressed format block width and height.
// - If the parent format is not compressed, and the view is, the view
// size is calculated as described on the first point, but the width and height
// of the view must be also multiplied by the block width and height.
int width = Math.Max(1, parent.Info.Width >> firstLevel);
int height = Math.Max(1, parent.Info.Height >> firstLevel);
if (parent.Info.FormatInfo.IsCompressed && !FormatInfo.IsCompressed)
{
width = BitUtils.DivRoundUp(width, parent.Info.FormatInfo.BlockWidth);
height = BitUtils.DivRoundUp(height, parent.Info.FormatInfo.BlockHeight);
}
else if (!parent.Info.FormatInfo.IsCompressed && FormatInfo.IsCompressed)
{
width *= FormatInfo.BlockWidth;
height *= FormatInfo.BlockHeight;
}
int depthOrLayers;
if (Target == Target.Texture3D)
{
depthOrLayers = Math.Max(1, parent.Info.DepthOrLayers >> firstLevel);
}
else
{
depthOrLayers = DepthOrLayers;
}
// 2D and 2D multisample textures are not considered compatible.
// This specific case is required for copies, where the source texture might be multisample.
// In this case, we inherit the parent texture multisample state.
Target target = Target;
int samplesInX = SamplesInX;
int samplesInY = SamplesInY;
if (target == Target.Texture2D && parent.Target == Target.Texture2DMultisample)
{
target = Target.Texture2DMultisample;
samplesInX = parent.Info.SamplesInX;
samplesInY = parent.Info.SamplesInY;
}
return new TextureInfo(
GpuAddress,
width,
height,
depthOrLayers,
Levels,
samplesInX,
samplesInY,
Stride,
IsLinear,
GobBlocksInY,
GobBlocksInZ,
GobBlocksInTileX,
target,
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,
SwizzleR,
SwizzleG,
SwizzleB,
SwizzleA);
}
}
}