Skip to content

Commit

Permalink
Support 3D BC4 and BC5 compressed textures (#1655)
Browse files Browse the repository at this point in the history
* Support 3D BC4 and BC5 compressed textures

* PR feedback

* Fix some typos
  • Loading branch information
gdkchan authored Nov 1, 2020
1 parent 6222f17 commit 11a7c99
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 32 deletions.
20 changes: 20 additions & 0 deletions Ryujinx.Graphics.GAL/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,5 +341,25 @@ public static bool IsInteger(this Format format)
{
return format.IsUint() || format.IsSint();
}

/// <summary>
/// Checks if the texture format is a BC4 compressed format.
/// </summary>
/// <param name="format">Texture format</param>
/// <returns>True if the texture format is a BC4 compressed format, false otherwise</returns>
public static bool IsBc4(this Format format)
{
return format == Format.Bc4Unorm || format == Format.Bc4Snorm;
}

/// <summary>
/// Checks if the texture format is a BC5 compressed format.
/// </summary>
/// <param name="format">Texture format</param>
/// <returns>True if the texture format is a BC5 compressed format, false otherwise</returns>
public static bool IsBc5(this Format format)
{
return format == Format.Bc5Unorm || format == Format.Bc5Snorm;
}
}
}
19 changes: 14 additions & 5 deletions Ryujinx.Graphics.Gpu/Image/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,9 @@ private ReadOnlySpan<byte> ConvertToHostCompatibleFormat(ReadOnlySpan<byte> data
data);
}

// Handle compressed cases not supported by the host:
// - ASTC is usually not supported on desktop cards.
// - BC4/BC5 is not supported on 3D textures.
if (!_context.Capabilities.SupportsAstcCompression && Info.FormatInfo.Format.IsAstc())
{
if (!AstcDecoder.TryDecodeToRgba8(
Expand All @@ -691,6 +694,14 @@ private ReadOnlySpan<byte> ConvertToHostCompatibleFormat(ReadOnlySpan<byte> data

data = decoded;
}
else if (Info.Target == Target.Texture3D && Info.FormatInfo.Format.IsBc4())
{
data = BCnDecoder.DecodeBC4(data, Info.Width, Info.Height, _depth, Info.Levels, _layers, Info.FormatInfo.Format == Format.Bc4Snorm);
}
else if (Info.Target == Target.Texture3D && Info.FormatInfo.Format.IsBc5())
{
data = BCnDecoder.DecodeBC5(data, Info.Width, Info.Height, _depth, Info.Levels, _layers, Info.FormatInfo.Format == Format.Bc5Snorm);
}

return data;
}
Expand All @@ -707,8 +718,7 @@ private ReadOnlySpan<byte> ConvertToHostCompatibleFormat(ReadOnlySpan<byte> data
public void Flush(bool tracked = true)
{
IsModified = false;

if (Info.FormatInfo.Format.IsAstc())
if (TextureCompatibility.IsFormatHostIncompatible(Info, _context.Capabilities))
{
return; // Flushing this format is not supported, as it may have been converted to another host format.
}
Expand Down Expand Up @@ -739,10 +749,9 @@ public void ExternalFlush(ulong address, ulong size)
_context.Renderer.BackgroundContextAction(() =>
{
IsModified = false;
if (Info.FormatInfo.Format.IsAstc())
if (TextureCompatibility.IsFormatHostIncompatible(Info, _context.Capabilities))
{
// ASTC textures are not in their original format, so cannot be flushed.
return;
return; // Flushing this format is not supported, as it may have been converted to another host format.
}

ITexture texture = HostTexture;
Expand Down
62 changes: 62 additions & 0 deletions Ryujinx.Graphics.Gpu/Image/TextureCompatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,68 @@ private enum FormatClass
Bc7
}

/// <summary>
/// Checks if a format is host incompatible.
/// </summary>
/// <remarks>
/// Host incompatible formats can't be used directly, the texture data needs to be converted
/// to a compatible format first.
/// </remarks>
/// <param name="info">Texture information</param>
/// <param name="caps">Host GPU capabilities</param>
/// <returns>True if the format is incompatible, false otherwise</returns>
public static bool IsFormatHostIncompatible(TextureInfo info, Capabilities caps)
{
Format originalFormat = info.FormatInfo.Format;
return ToHostCompatibleFormat(info, caps).Format != originalFormat;
}

/// <summary>
/// Converts a incompatible format to a host compatible format, or return the format directly
/// if it is already host compatible.
/// </summary>
/// <remarks>
/// This can be used to convert a incompatible compressed format to the decompressor
/// output format.
/// </remarks>
/// <param name="info">Texture information</param>
/// <param name="caps">Host GPU capabilities</param>
/// <returns>A host compatible format</returns>
public static FormatInfo ToHostCompatibleFormat(TextureInfo info, Capabilities caps)
{
if (!caps.SupportsAstcCompression)
{
if (info.FormatInfo.Format.IsAstcUnorm())
{
return new FormatInfo(Format.R8G8B8A8Unorm, 1, 1, 4, 4);
}
else if (info.FormatInfo.Format.IsAstcSrgb())
{
return new FormatInfo(Format.R8G8B8A8Srgb, 1, 1, 4, 4);
}
}

if (info.Target == Target.Texture3D)
{
// The host API does not support 3D BC4/BC5 compressed formats.
// We assume software decompression will be done for those textures,
// and so we adjust the format here to match the decompressor output.
switch (info.FormatInfo.Format)
{
case Format.Bc4Unorm:
return new FormatInfo(Format.R8Unorm, 1, 1, 1, 1);
case Format.Bc4Snorm:
return new FormatInfo(Format.R8Snorm, 1, 1, 1, 1);
case Format.Bc5Unorm:
return new FormatInfo(Format.R8G8Unorm, 1, 1, 2, 2);
case Format.Bc5Snorm:
return new FormatInfo(Format.R8G8Snorm, 1, 1, 2, 2);
}
}

return info.FormatInfo;
}

/// <summary>
/// Finds the appropriate depth format for a copy texture if the source texture has a depth format.
/// </summary>
Expand Down
38 changes: 19 additions & 19 deletions Ryujinx.Graphics.Gpu/Image/TextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,19 +1058,7 @@ private static TextureInfo AdjustSizes(Texture parent, TextureInfo info, int fir
/// <returns>The texture creation information</returns>
public static TextureCreateInfo GetCreateInfo(TextureInfo info, Capabilities caps, float scale)
{
FormatInfo formatInfo = info.FormatInfo;

if (!caps.SupportsAstcCompression)
{
if (formatInfo.Format.IsAstcUnorm())
{
formatInfo = new FormatInfo(Format.R8G8B8A8Unorm, 1, 1, 4, 4);
}
else if (formatInfo.Format.IsAstcSrgb())
{
formatInfo = new FormatInfo(Format.R8G8B8A8Srgb, 1, 1, 4, 4);
}
}
FormatInfo formatInfo = TextureCompatibility.ToHostCompatibleFormat(info, caps);

if (info.Target == Target.TextureBuffer)
{
Expand All @@ -1079,12 +1067,24 @@ public static TextureCreateInfo GetCreateInfo(TextureInfo info, Capabilities cap
// The shader will need the appropriate conversion code to compensate.
switch (formatInfo.Format)
{
case Format.R8Snorm: formatInfo = new FormatInfo(Format.R8Sint, 1, 1, 1, 1); break;
case Format.R16Snorm: formatInfo = new FormatInfo(Format.R16Sint, 1, 1, 2, 1); break;
case Format.R8G8Snorm: formatInfo = new FormatInfo(Format.R8G8Sint, 1, 1, 2, 2); break;
case Format.R16G16Snorm: formatInfo = new FormatInfo(Format.R16G16Sint, 1, 1, 4, 2); break;
case Format.R8G8B8A8Snorm: formatInfo = new FormatInfo(Format.R8G8B8A8Sint, 1, 1, 4, 4); break;
case Format.R16G16B16A16Snorm: formatInfo = new FormatInfo(Format.R16G16B16A16Sint, 1, 1, 8, 4); break;
case Format.R8Snorm:
formatInfo = new FormatInfo(Format.R8Sint, 1, 1, 1, 1);
break;
case Format.R16Snorm:
formatInfo = new FormatInfo(Format.R16Sint, 1, 1, 2, 1);
break;
case Format.R8G8Snorm:
formatInfo = new FormatInfo(Format.R8G8Sint, 1, 1, 2, 2);
break;
case Format.R16G16Snorm:
formatInfo = new FormatInfo(Format.R16G16Sint, 1, 1, 4, 2);
break;
case Format.R8G8B8A8Snorm:
formatInfo = new FormatInfo(Format.R8G8B8A8Sint, 1, 1, 4, 4);
break;
case Format.R16G16B16A16Snorm:
formatInfo = new FormatInfo(Format.R16G16B16A16Sint, 1, 1, 8, 4);
break;
}
}

Expand Down
Loading

0 comments on commit 11a7c99

Please sign in to comment.