Skip to content

Commit c6a1240

Browse files
committed
Bc improvements:
* Expose BlockSize as public constants * Refactor Bc7 to use more constants * Add a partial block test
1 parent 12c6750 commit c6a1240

8 files changed

Lines changed: 65 additions & 17 deletions

File tree

AssetRipper.TextureDecoder.Tests/BcTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using AssetRipper.TextureDecoder.Bc;
22
using AssetRipper.TextureDecoder.Dxt;
3+
using AssetRipper.TextureDecoder.Rgb.Formats;
4+
using System.Runtime.CompilerServices;
35

46
namespace AssetRipper.TextureDecoder.Tests
57
{
@@ -95,6 +97,19 @@ public void DecompressBC7Test(string fileName)
9597
AssertCorrectBC7Decompression(TestFileFolders.BcTestFiles + fileName, 512, 512);
9698
}
9799

100+
[Test]
101+
[Ignore("Not implemented")]
102+
public void BC7PartialBlockTest([Range(1, 4)] int width, [Range(1, 4)] int height)
103+
{
104+
Assert.Multiple(() =>
105+
{
106+
ReadOnlySpan<byte> data = File.ReadAllBytes(TestFileFolders.BcTestFiles + "test.bc7_best").AsSpan()[..Bc7.BlockSize];
107+
int bytesRead = Bc7.Decompress(data, width, height, out byte[] decodedData);
108+
Assert.That(bytesRead, Is.EqualTo(data.Length));
109+
Assert.That(decodedData, Has.Length.EqualTo(width * height * Unsafe.SizeOf<ColorBGRA32>()));
110+
});
111+
}
112+
98113
private static void AssertCorrectBC6HDecompression(string path, int width, int height, bool isSigned)
99114
{
100115
ReadOnlySpan<byte> data = File.ReadAllBytes(path);

AssetRipper.TextureDecoder/Bc/Bc1.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ namespace AssetRipper.TextureDecoder.Bc;
55

66
public static class Bc1
77
{
8-
internal const int BlockSize = 8;
8+
/// <summary>
9+
/// The size of an encoded block, in bytes.
10+
/// </summary>
11+
public const int BlockSize = 8;
912

1013
public static int Decompress(ReadOnlySpan<byte> input, int width, int height, out byte[] output)
1114
{
@@ -30,4 +33,4 @@ public static int Decompress(ReadOnlySpan<byte> input, int width, int height, Sp
3033
}
3134

3235
internal static int GetCompressedSize(int w, int h) => ((w) >> 2) * ((h) >> 2) * BlockSize;
33-
}
36+
}

AssetRipper.TextureDecoder/Bc/Bc2.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ namespace AssetRipper.TextureDecoder.Bc;
55

66
public static class Bc2
77
{
8-
internal const int BlockSize = 16;
8+
/// <summary>
9+
/// The size of an encoded block, in bytes.
10+
/// </summary>
11+
public const int BlockSize = 16;
912

1013
public static int Decompress(ReadOnlySpan<byte> input, int width, int height, out byte[] output)
1114
{
@@ -30,4 +33,4 @@ public static int Decompress(ReadOnlySpan<byte> input, int width, int height, Sp
3033
}
3134

3235
internal static int GetCompressedSize(int w, int h) => ((w) >> 2) * ((h) >> 2) * BlockSize;
33-
}
36+
}

AssetRipper.TextureDecoder/Bc/Bc3.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ namespace AssetRipper.TextureDecoder.Bc;
55

66
public static class Bc3
77
{
8-
internal const int BlockSize = 16;
8+
/// <summary>
9+
/// The size of an encoded block, in bytes.
10+
/// </summary>
11+
public const int BlockSize = 16;
912

1013
public static int Decompress(ReadOnlySpan<byte> input, int width, int height, out byte[] output)
1114
{
@@ -30,4 +33,4 @@ public static int Decompress(ReadOnlySpan<byte> input, int width, int height, Sp
3033
}
3134

3235
internal static int GetCompressedSize(int w, int h) => ((w) >> 2) * ((h) >> 2) * BlockSize;
33-
}
36+
}

AssetRipper.TextureDecoder/Bc/Bc4.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ namespace AssetRipper.TextureDecoder.Bc;
66

77
public static class Bc4
88
{
9-
internal const int BlockSize = 8;
9+
/// <summary>
10+
/// The size of an encoded block, in bytes.
11+
/// </summary>
12+
public const int BlockSize = 8;
1013

1114
public static int Decompress(ReadOnlySpan<byte> input, int width, int height, out byte[] output)
1215
{
@@ -35,4 +38,4 @@ public static int Decompress(ReadOnlySpan<byte> input, int width, int height, Sp
3538
}
3639

3740
internal static int GetCompressedSize(int w, int h) => ((w) >> 2) * ((h) >> 2) * BlockSize;
38-
}
41+
}

AssetRipper.TextureDecoder/Bc/Bc5.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ namespace AssetRipper.TextureDecoder.Bc;
66

77
public static class Bc5
88
{
9-
internal const int BlockSize = 16;
9+
/// <summary>
10+
/// The size of an encoded block, in bytes.
11+
/// </summary>
12+
public const int BlockSize = 16;
1013

1114
public static int Decompress(ReadOnlySpan<byte> input, int width, int height, out byte[] output)
1215
{
@@ -35,4 +38,4 @@ public static int Decompress(ReadOnlySpan<byte> input, int width, int height, Sp
3538
}
3639

3740
internal static int GetCompressedSize(int w, int h) => ((w) >> 2) * ((h) >> 2) * BlockSize;
38-
}
41+
}

AssetRipper.TextureDecoder/Bc/Bc6h.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ namespace AssetRipper.TextureDecoder.Bc;
66

77
public static class Bc6h
88
{
9-
internal const int BlockSize = 16;
9+
/// <summary>
10+
/// The size of an encoded block, in bytes.
11+
/// </summary>
12+
public const int BlockSize = 16;
1013

1114
public static int Decompress(ReadOnlySpan<byte> input, int width, int height, bool isSigned, out byte[] output)
1215
{
@@ -39,4 +42,4 @@ public static int Decompress(ReadOnlySpan<byte> input, int width, int height, bo
3942
}
4043

4144
internal static int GetCompressedSize(int w, int h) => ((w) >> 2) * ((h) >> 2) * BlockSize;
42-
}
45+
}

AssetRipper.TextureDecoder/Bc/Bc7.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,22 @@ namespace AssetRipper.TextureDecoder.Bc;
55

66
public static class Bc7
77
{
8-
internal const int BlockSize = 16;
8+
/// <summary>
9+
/// The size of an encoded block, in bytes.
10+
/// </summary>
11+
public const int BlockSize = 16;
12+
/// <summary>
13+
/// The width of a decoded block, in pixels.
14+
/// </summary>
15+
private const int BlockWidth = 4;
16+
/// <summary>
17+
/// The height of a decoded block, in pixels.
18+
/// </summary>
19+
private const int BlockHeight = 4;
20+
/// <summary>
21+
/// The size of the natural pixel type.
22+
/// </summary>
23+
private static int PixelSize => Unsafe.SizeOf<ColorRGBA<byte>>();
924

1025
public static int Decompress(ReadOnlySpan<byte> input, int width, int height, out byte[] output)
1126
{
@@ -16,13 +31,13 @@ public static int Decompress(ReadOnlySpan<byte> input, int width, int height, ou
1631
public static int Decompress(ReadOnlySpan<byte> input, int width, int height, Span<byte> output)
1732
{
1833
int inputOffset = 0;
19-
for (int i = 0; i < height; i += 4)
34+
for (int i = 0; i < height; i += BlockHeight)
2035
{
21-
for (int j = 0; j < width; j += 4)
36+
for (int j = 0; j < width; j += BlockWidth)
2237
{
23-
int outputOffset = ((i * width) + j) * Unsafe.SizeOf<ColorRGBA<byte>>();
38+
int outputOffset = ((i * width) + j) * PixelSize;
2439
BcHelpers.DecompressBc7(
25-
input.Slice(inputOffset, BlockSize),
40+
input.Slice(inputOffset, BlockSize),
2641
output.Slice(outputOffset),
2742
width * 4);
2843
inputOffset += BlockSize;

0 commit comments

Comments
 (0)