mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2024-11-10 04:21:45 +00:00
Support 3D BC4 and BC5 compressed textures (#1655)
* Support 3D BC4 and BC5 compressed textures * PR feedback * Fix some typos
This commit is contained in:
parent
6222f173f0
commit
11a7c99764
6 changed files with 357 additions and 32 deletions
|
@ -341,5 +341,25 @@ namespace Ryujinx.Graphics.GAL
|
|||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -671,6 +671,9 @@ namespace Ryujinx.Graphics.Gpu.Image
|
|||
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(
|
||||
|
@ -691,6 +694,14 @@ namespace Ryujinx.Graphics.Gpu.Image
|
|||
|
||||
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;
|
||||
}
|
||||
|
@ -707,8 +718,7 @@ namespace Ryujinx.Graphics.Gpu.Image
|
|||
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.
|
||||
}
|
||||
|
@ -739,10 +749,9 @@ namespace Ryujinx.Graphics.Gpu.Image
|
|||
_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;
|
||||
|
|
|
@ -26,6 +26,68 @@ namespace Ryujinx.Graphics.Gpu.Image
|
|||
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>
|
||||
|
|
|
@ -1058,19 +1058,7 @@ namespace Ryujinx.Graphics.Gpu.Image
|
|||
/// <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)
|
||||
{
|
||||
|
@ -1079,12 +1067,24 @@ namespace Ryujinx.Graphics.Gpu.Image
|
|||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
242
Ryujinx.Graphics.Texture/BCnDecoder.cs
Normal file
242
Ryujinx.Graphics.Texture/BCnDecoder.cs
Normal file
|
@ -0,0 +1,242 @@
|
|||
using Ryujinx.Common;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Texture
|
||||
{
|
||||
public static class BCnDecoder
|
||||
{
|
||||
private const int BlockWidth = 4;
|
||||
private const int BlockHeight = 4;
|
||||
|
||||
public static byte[] DecodeBC4(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers, bool signed)
|
||||
{
|
||||
int size = 0;
|
||||
|
||||
for (int l = 0; l < levels; l++)
|
||||
{
|
||||
size += Math.Max(1, width >> l) * Math.Max(1, height >> l) * Math.Max(1, depth >> l) * layers;
|
||||
}
|
||||
|
||||
byte[] output = new byte[size];
|
||||
|
||||
ReadOnlySpan<ulong> data64 = MemoryMarshal.Cast<byte, ulong>(data);
|
||||
|
||||
Span<byte> rPal = stackalloc byte[8];
|
||||
|
||||
int baseOOffs = 0;
|
||||
|
||||
for (int l = 0; l < levels; l++)
|
||||
{
|
||||
int w = BitUtils.DivRoundUp(width, BlockWidth);
|
||||
int h = BitUtils.DivRoundUp(height, BlockHeight);
|
||||
|
||||
for (int l2 = 0; l2 < layers; l2++)
|
||||
{
|
||||
for (int z = 0; z < depth; z++)
|
||||
{
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
int baseY = y * BlockHeight;
|
||||
|
||||
for (int x = 0; x < w; x++)
|
||||
{
|
||||
int baseX = x * BlockWidth;
|
||||
int lineBaseOOffs = baseOOffs + baseX;
|
||||
|
||||
ulong block = data64[0];
|
||||
|
||||
rPal[0] = (byte)block;
|
||||
rPal[1] = (byte)(block >> 8);
|
||||
|
||||
if (signed)
|
||||
{
|
||||
CalculateBC3AlphaS(rPal);
|
||||
}
|
||||
else
|
||||
{
|
||||
CalculateBC3Alpha(rPal);
|
||||
}
|
||||
|
||||
ulong rI = block >> 16;
|
||||
|
||||
for (int texel = 0; texel < BlockWidth * BlockHeight; texel++)
|
||||
{
|
||||
int tX = texel & 3;
|
||||
int tY = texel >> 2;
|
||||
|
||||
if (baseX + tX >= width || baseY + tY >= height)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
int shift = texel * 3;
|
||||
|
||||
byte r = rPal[(int)((rI >> shift) & 7)];
|
||||
|
||||
int oOffs = lineBaseOOffs + tY * width + tX;
|
||||
|
||||
output[oOffs] = r;
|
||||
}
|
||||
|
||||
data64 = data64.Slice(1);
|
||||
}
|
||||
|
||||
baseOOffs += width * (baseY + BlockHeight > height ? (height & (BlockHeight - 1)) : BlockHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
width = Math.Max(1, width >> 1);
|
||||
height = Math.Max(1, height >> 1);
|
||||
depth = Math.Max(1, depth >> 1);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public static byte[] DecodeBC5(ReadOnlySpan<byte> data, int width, int height, int depth, int levels, int layers, bool signed)
|
||||
{
|
||||
int size = 0;
|
||||
|
||||
for (int l = 0; l < levels; l++)
|
||||
{
|
||||
size += Math.Max(1, width >> l) * Math.Max(1, height >> l) * Math.Max(1, depth >> l) * layers * 2;
|
||||
}
|
||||
|
||||
byte[] output = new byte[size];
|
||||
|
||||
ReadOnlySpan<ulong> data64 = MemoryMarshal.Cast<byte, ulong>(data);
|
||||
|
||||
Span<byte> rPal = stackalloc byte[8];
|
||||
Span<byte> gPal = stackalloc byte[8];
|
||||
|
||||
int baseOOffs = 0;
|
||||
|
||||
for (int l = 0; l < levels; l++)
|
||||
{
|
||||
int w = BitUtils.DivRoundUp(width, BlockWidth);
|
||||
int h = BitUtils.DivRoundUp(height, BlockHeight);
|
||||
|
||||
for (int l2 = 0; l2 < layers; l2++)
|
||||
{
|
||||
for (int z = 0; z < depth; z++)
|
||||
{
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
int baseY = y * BlockHeight;
|
||||
|
||||
for (int x = 0; x < w; x++)
|
||||
{
|
||||
int baseX = x * BlockWidth;
|
||||
int lineBaseOOffs = baseOOffs + baseX;
|
||||
|
||||
ulong blockL = data64[0];
|
||||
ulong blockH = data64[1];
|
||||
|
||||
rPal[0] = (byte)blockL;
|
||||
rPal[1] = (byte)(blockL >> 8);
|
||||
gPal[0] = (byte)blockH;
|
||||
gPal[1] = (byte)(blockH >> 8);
|
||||
|
||||
if (signed)
|
||||
{
|
||||
CalculateBC3AlphaS(rPal);
|
||||
CalculateBC3AlphaS(gPal);
|
||||
}
|
||||
else
|
||||
{
|
||||
CalculateBC3Alpha(rPal);
|
||||
CalculateBC3Alpha(gPal);
|
||||
}
|
||||
|
||||
ulong rI = blockL >> 16;
|
||||
ulong gI = blockH >> 16;
|
||||
|
||||
for (int texel = 0; texel < BlockWidth * BlockHeight; texel++)
|
||||
{
|
||||
int tX = texel & 3;
|
||||
int tY = texel >> 2;
|
||||
|
||||
if (baseX + tX >= width || baseY + tY >= height)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
int shift = texel * 3;
|
||||
|
||||
byte r = rPal[(int)((rI >> shift) & 7)];
|
||||
byte g = gPal[(int)((gI >> shift) & 7)];
|
||||
|
||||
int oOffs = (lineBaseOOffs + tY * width + tX) * 2;
|
||||
|
||||
output[oOffs + 0] = r;
|
||||
output[oOffs + 1] = g;
|
||||
}
|
||||
|
||||
data64 = data64.Slice(2);
|
||||
}
|
||||
|
||||
baseOOffs += width * (baseY + BlockHeight > height ? (height & (BlockHeight - 1)) : BlockHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
width = Math.Max(1, width >> 1);
|
||||
height = Math.Max(1, height >> 1);
|
||||
depth = Math.Max(1, depth >> 1);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void CalculateBC3Alpha(Span<byte> alpha)
|
||||
{
|
||||
for (int i = 2; i < 8; i++)
|
||||
{
|
||||
if (alpha[0] > alpha[1])
|
||||
{
|
||||
alpha[i] = (byte)(((8 - i) * alpha[0] + (i - 1) * alpha[1]) / 7);
|
||||
}
|
||||
else if (i < 6)
|
||||
{
|
||||
alpha[i] = (byte)(((6 - i) * alpha[0] + (i - 1) * alpha[1]) / 7);
|
||||
}
|
||||
else if (i == 6)
|
||||
{
|
||||
alpha[i] = 0;
|
||||
}
|
||||
else /* i == 7 */
|
||||
{
|
||||
alpha[i] = 0xff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void CalculateBC3AlphaS(Span<byte> alpha)
|
||||
{
|
||||
for (int i = 2; i < 8; i++)
|
||||
{
|
||||
if ((sbyte)alpha[0] > (sbyte)alpha[1])
|
||||
{
|
||||
alpha[i] = (byte)(((8 - i) * (sbyte)alpha[0] + (i - 1) * (sbyte)alpha[1]) / 7);
|
||||
}
|
||||
else if (i < 6)
|
||||
{
|
||||
alpha[i] = (byte)(((6 - i) * (sbyte)alpha[0] + (i - 1) * (sbyte)alpha[1]) / 7);
|
||||
}
|
||||
else if (i == 6)
|
||||
{
|
||||
alpha[i] = 0x80;
|
||||
}
|
||||
else /* i == 7 */
|
||||
{
|
||||
alpha[i] = 0x7f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -87,14 +87,6 @@ Global
|
|||
{3AB294D0-2230-468F-9EB3-BDFCAEAE99A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3AB294D0-2230-468F-9EB3-BDFCAEAE99A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3AB294D0-2230-468F-9EB3-BDFCAEAE99A5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{8E7D36DD-9626-47E2-8EF5-8F2F66751C9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8E7D36DD-9626-47E2-8EF5-8F2F66751C9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8E7D36DD-9626-47E2-8EF5-8F2F66751C9C}.Profile Debug|Any CPU.ActiveCfg = Profile Debug|Any CPU
|
||||
{8E7D36DD-9626-47E2-8EF5-8F2F66751C9C}.Profile Debug|Any CPU.Build.0 = Profile Debug|Any CPU
|
||||
{8E7D36DD-9626-47E2-8EF5-8F2F66751C9C}.Profile Release|Any CPU.ActiveCfg = Profile Release|Any CPU
|
||||
{8E7D36DD-9626-47E2-8EF5-8F2F66751C9C}.Profile Release|Any CPU.Build.0 = Profile Release|Any CPU
|
||||
{8E7D36DD-9626-47E2-8EF5-8F2F66751C9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8E7D36DD-9626-47E2-8EF5-8F2F66751C9C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5FD4E4F6-8928-4B3C-BE07-28A675C17226}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{5FD4E4F6-8928-4B3C-BE07-28A675C17226}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5FD4E4F6-8928-4B3C-BE07-28A675C17226}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
|
|
Loading…
Reference in a new issue