Skip to content

Commit c4831f6

Browse files
Merge pull request #37 from Erik-White/implement-astc-decoding
Implement support for ASTC compressed KTX textures
2 parents 22cfefd + 8cf9fc3 commit c4831f6

511 files changed

Lines changed: 17530 additions & 999 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitattributes‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
*.dds filter=lfs diff=lfs merge=lfs -text
127127
*.ktx filter=lfs diff=lfs merge=lfs -text
128128
*.ktx2 filter=lfs diff=lfs merge=lfs -text
129+
*.astc filter=lfs diff=lfs merge=lfs -text
129130
*.pam filter=lfs diff=lfs merge=lfs -text
130131
*.pbm filter=lfs diff=lfs merge=lfs -text
131132
*.pgm filter=lfs diff=lfs merge=lfs -text

‎ImageSharp.Textures.sln‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29613.14
3+
# Visual Studio Version 18
4+
VisualStudioVersion = 18.5.11716.220
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageSharp.Textures", "src\ImageSharp.Textures\ImageSharp.Textures.csproj", "{1588F6C4-2186-4A35-9693-E9F296791393}"
77
EndProject
@@ -50,34 +50,63 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
5050
EndProjectSection
5151
EndProject
5252
Global
53-
GlobalSection(SharedMSBuildProjectFiles) = preSolution
54-
shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems*{1588f6c4-2186-4a35-9693-e9f296791393}*SharedItemsImports = 5
55-
tests\Images\Images.projitems*{17fcbd4d-d232-45e8-876f-dfbc2fad52cf}*SharedItemsImports = 5
56-
tests\Images\Images.projitems*{18be79b6-6b95-4ed7-a963-ad75f6cb9f3c}*SharedItemsImports = 5
57-
tests\Images\Images.projitems*{68a8cc40-6aed-4e96-b524-31b1158fdeea}*SharedItemsImports = 13
58-
tests\Images\Images.projitems*{b159ffd1-e646-42d0-892c-4abf69103712}*SharedItemsImports = 5
59-
EndGlobalSection
6053
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6154
Debug|Any CPU = Debug|Any CPU
55+
Debug|x64 = Debug|x64
56+
Debug|x86 = Debug|x86
6257
Release|Any CPU = Release|Any CPU
58+
Release|x64 = Release|x64
59+
Release|x86 = Release|x86
6360
EndGlobalSection
6461
GlobalSection(ProjectConfigurationPlatforms) = postSolution
6562
{1588F6C4-2186-4A35-9693-E9F296791393}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
6663
{1588F6C4-2186-4A35-9693-E9F296791393}.Debug|Any CPU.Build.0 = Debug|Any CPU
64+
{1588F6C4-2186-4A35-9693-E9F296791393}.Debug|x64.ActiveCfg = Debug|Any CPU
65+
{1588F6C4-2186-4A35-9693-E9F296791393}.Debug|x64.Build.0 = Debug|Any CPU
66+
{1588F6C4-2186-4A35-9693-E9F296791393}.Debug|x86.ActiveCfg = Debug|Any CPU
67+
{1588F6C4-2186-4A35-9693-E9F296791393}.Debug|x86.Build.0 = Debug|Any CPU
6768
{1588F6C4-2186-4A35-9693-E9F296791393}.Release|Any CPU.ActiveCfg = Release|Any CPU
6869
{1588F6C4-2186-4A35-9693-E9F296791393}.Release|Any CPU.Build.0 = Release|Any CPU
70+
{1588F6C4-2186-4A35-9693-E9F296791393}.Release|x64.ActiveCfg = Release|Any CPU
71+
{1588F6C4-2186-4A35-9693-E9F296791393}.Release|x64.Build.0 = Release|Any CPU
72+
{1588F6C4-2186-4A35-9693-E9F296791393}.Release|x86.ActiveCfg = Release|Any CPU
73+
{1588F6C4-2186-4A35-9693-E9F296791393}.Release|x86.Build.0 = Release|Any CPU
6974
{B159FFD1-E646-42D0-892C-4ABF69103712}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7075
{B159FFD1-E646-42D0-892C-4ABF69103712}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{B159FFD1-E646-42D0-892C-4ABF69103712}.Debug|x64.ActiveCfg = Debug|Any CPU
77+
{B159FFD1-E646-42D0-892C-4ABF69103712}.Debug|x64.Build.0 = Debug|Any CPU
78+
{B159FFD1-E646-42D0-892C-4ABF69103712}.Debug|x86.ActiveCfg = Debug|Any CPU
79+
{B159FFD1-E646-42D0-892C-4ABF69103712}.Debug|x86.Build.0 = Debug|Any CPU
7180
{B159FFD1-E646-42D0-892C-4ABF69103712}.Release|Any CPU.ActiveCfg = Release|Any CPU
7281
{B159FFD1-E646-42D0-892C-4ABF69103712}.Release|Any CPU.Build.0 = Release|Any CPU
82+
{B159FFD1-E646-42D0-892C-4ABF69103712}.Release|x64.ActiveCfg = Release|Any CPU
83+
{B159FFD1-E646-42D0-892C-4ABF69103712}.Release|x64.Build.0 = Release|Any CPU
84+
{B159FFD1-E646-42D0-892C-4ABF69103712}.Release|x86.ActiveCfg = Release|Any CPU
85+
{B159FFD1-E646-42D0-892C-4ABF69103712}.Release|x86.Build.0 = Release|Any CPU
7386
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7487
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
88+
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Debug|x64.ActiveCfg = Debug|Any CPU
89+
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Debug|x64.Build.0 = Debug|Any CPU
90+
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Debug|x86.ActiveCfg = Debug|Any CPU
91+
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Debug|x86.Build.0 = Debug|Any CPU
7592
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
7693
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Release|Any CPU.Build.0 = Release|Any CPU
94+
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Release|x64.ActiveCfg = Release|Any CPU
95+
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Release|x64.Build.0 = Release|Any CPU
96+
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Release|x86.ActiveCfg = Release|Any CPU
97+
{18BE79B6-6B95-4ED7-A963-AD75F6CB9F3C}.Release|x86.Build.0 = Release|Any CPU
7798
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7899
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
100+
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Debug|x64.ActiveCfg = Debug|Any CPU
101+
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Debug|x64.Build.0 = Debug|Any CPU
102+
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Debug|x86.ActiveCfg = Debug|Any CPU
103+
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Debug|x86.Build.0 = Debug|Any CPU
79104
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
80105
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Release|Any CPU.Build.0 = Release|Any CPU
106+
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Release|x64.ActiveCfg = Release|Any CPU
107+
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Release|x64.Build.0 = Release|Any CPU
108+
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Release|x86.ActiveCfg = Release|Any CPU
109+
{17FCBD4D-D232-45E8-876F-DFBC2FAD52CF}.Release|x86.Build.0 = Release|Any CPU
81110
EndGlobalSection
82111
GlobalSection(SolutionProperties) = preSolution
83112
HideSolutionNode = FALSE
@@ -94,4 +123,11 @@ Global
94123
GlobalSection(ExtensibilityGlobals) = postSolution
95124
SolutionGuid = {F1762A0D-74C4-454A-BCB7-C010BB067E58}
96125
EndGlobalSection
126+
GlobalSection(SharedMSBuildProjectFiles) = preSolution
127+
shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems*{1588f6c4-2186-4a35-9693-e9f296791393}*SharedItemsImports = 5
128+
tests\Images\Images.projitems*{17fcbd4d-d232-45e8-876f-dfbc2fad52cf}*SharedItemsImports = 5
129+
tests\Images\Images.projitems*{18be79b6-6b95-4ed7-a963-ad75f6cb9f3c}*SharedItemsImports = 5
130+
tests\Images\Images.projitems*{68a8cc40-6aed-4e96-b524-31b1158fdeea}*SharedItemsImports = 13
131+
tests\Images\Images.projitems*{b159ffd1-e646-42d0-892c-4abf69103712}*SharedItemsImports = 5
132+
EndGlobalSection
97133
EndGlobal

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SixLabors.ImageSharp.Textures
99

1010
[![Build Status](https://img.shields.io/github/actions/workflow/status/SixLabors/ImageSharp.Textures/build-and-test.yml?branch=main)](https://github.com/SixLabors/ImageSharp.Textures/actions)
1111
[![Code coverage](https://codecov.io/gh/SixLabors/ImageSharp.Textures/branch/main/graph/badge.svg)](https://codecov.io/gh/SixLabors/ImageSharp)
12-
[![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
12+
[![License: Six Labors Split](https://img.shields.io/badge/license-Six%20Labors%20Split-%23e30183)](https://github.com/SixLabors/ImageSharp/blob/main/LICENSE)
1313
[![Twitter](https://img.shields.io/twitter/url/http/shields.io.svg?style=flat&logo=twitter)](https://twitter.com/intent/tweet?hashtags=imagesharp,dotnet,oss&text=ImageSharp.+A+new+cross-platform+2D+graphics+API+in+C%23&url=https%3a%2f%2fgithub.com%2fSixLabors%2fImageSharp&via=sixlabors)
1414

1515
</div>
@@ -33,6 +33,7 @@ with the following compressions:
3333
- BC5
3434
- BC6H
3535
- BC7
36+
- ASTC
3637

3738
Encoding textures is **not** yet supported. PR are of course very welcome.
3839

‎shared-infrastructure‎

‎src/Directory.Build.props‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
-->
1212

1313
<!-- Import the shared src .props file -->
14-
<Import Project="$(MSBuildThisFileDirectory)..\shared-infrastructure\msbuild\props\SixLabors.Src.props" />
14+
<Import Project="$(MSBuildThisFileDirectory)../shared-infrastructure/msbuild/props/SixLabors.Src.props" />
1515

1616
<!-- Import the solution .props file. -->
1717
<Import Project="$(MSBuildThisFileDirectory)..\Directory.Build.props" />
@@ -22,7 +22,7 @@
2222
</PropertyGroup>
2323

2424
<ItemGroup>
25-
<InternalsVisibleTo Include="SixLabors.ImageSharp.Textures.Benchmarks" Key="$(SixLaborsPublicKey)" />
25+
<InternalsVisibleTo Include="ImageSharp.Textures.Benchmarks" Key="$(SixLaborsPublicKey)" />
2626
<InternalsVisibleTo Include="SixLabors.ImageSharp.Textures.InteractiveTest" Key="$(SixLaborsPublicKey)" />
2727
<InternalsVisibleTo Include="SixLabors.ImageSharp.Textures.Tests" Key="$(SixLaborsPublicKey)" />
2828
</ItemGroup>
Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,88 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System;
45
using System.Runtime.CompilerServices;
56

6-
namespace SixLabors.ImageSharp.Textures.Common.Helpers
7+
namespace SixLabors.ImageSharp.Textures.Common.Helpers;
8+
9+
internal static class FloatHelper
710
{
8-
internal static class FloatHelper
9-
{
10-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11-
public static float UnpackFloat32ToFloat(uint value) => Unsafe.As<uint, float>(ref value);
11+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12+
public static float UnpackFloat32ToFloat(uint value) => Unsafe.As<uint, float>(ref value);
1213

13-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14-
public static uint PackFloatToFloat32(float value) => Unsafe.As<float, uint>(ref value);
14+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
15+
public static uint PackFloatToFloat32(float value) => Unsafe.As<float, uint>(ref value);
1516

16-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17-
public static float UnpackFloat16ToFloat(uint value)
18-
{
19-
uint result =
20-
((value >> 15) << 31) |
21-
((((value >> 10) & 0x1f) - 15 + 127) << 23) |
22-
((value & 0x3ff) << 13);
23-
return Unsafe.As<uint, float>(ref result);
24-
}
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
public static float UnpackFloat16ToFloat(uint value) => (float)BitConverter.UInt16BitsToHalf((ushort)value);
2519

26-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
27-
public static uint PackFloatToFloat16(float value)
28-
{
29-
uint temp = Unsafe.As<float, uint>(ref value);
30-
return
31-
((temp >> 31) << 15) |
32-
((((temp >> 23) & 0xff) - 127 + 15) << 10) |
33-
((temp & 0x7fffff) >> 13);
34-
}
20+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
21+
public static uint PackFloatToFloat16(float value) => BitConverter.HalfToUInt16Bits((Half)value);
3522

36-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37-
public static float UnpackFloat10ToFloat(uint value)
38-
{
39-
uint result =
40-
((((value >> 5) & 0x1f) - 10 + 127) << 23) |
41-
((value & 0x1f) << 18);
42-
return Unsafe.As<uint, float>(ref result);
43-
}
23+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24+
public static float UnpackFloat10ToFloat(uint value, uint bias = 10)
25+
{
26+
uint e = (value >> 5) & 0x1Fu;
27+
uint m = value & 0x1Fu;
4428

45-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
46-
public static uint PackFloatToFloat10(float value)
29+
return e switch
4730
{
48-
uint temp = Unsafe.As<float, uint>(ref value);
49-
return
50-
((((temp >> 23) & 0xff) - 127 + 10) << 5) |
51-
((temp & 0x7fffff) >> 18);
52-
}
31+
// Zero
32+
0 when m == 0 => 0f,
33+
34+
// Denormalized
35+
0 => m * BitConverter.UInt32BitsToSingle((128u - bias - 5u) << 23),
36+
37+
// Inf/NaN
38+
31 => BitConverter.UInt32BitsToSingle((0xFFu << 23) | (m << 18)),
5339

54-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
55-
public static float UnpackFloat11ToFloat(uint value)
40+
// Normalized
41+
_ => BitConverter.UInt32BitsToSingle(((e + 127u - bias) << 23) | (m << 18)),
42+
};
43+
}
44+
45+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
46+
public static uint PackFloatToFloat10(float value)
47+
{
48+
uint temp = Unsafe.As<float, uint>(ref value);
49+
return
50+
((((temp >> 23) & 0xff) - 127 + 10) << 5) |
51+
((temp & 0x7fffff) >> 18);
52+
}
53+
54+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
55+
public static float UnpackFloat11ToFloat(uint value, uint bias = 11)
56+
{
57+
uint e = (value >> 6) & 0x1Fu;
58+
uint m = value & 0x3Fu;
59+
60+
if (e == 0)
5661
{
57-
uint result =
58-
((((value >> 6) & 0x1f) - 11 + 127) << 23) |
59-
((value & 0x3f) << 17);
60-
return Unsafe.As<uint, float>(ref result);
62+
if (m == 0)
63+
{
64+
return 0f;
65+
}
66+
67+
// Denormalized: m * 2^(1 - bias - 6)
68+
return m * BitConverter.UInt32BitsToSingle((128u - bias - 6u) << 23);
6169
}
6270

63-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
64-
public static uint PackFloatToFloat11(float value)
71+
if (e == 31)
6572
{
66-
uint temp = Unsafe.As<float, uint>(ref value);
67-
return
68-
((((temp >> 23) & 0xff) - 127 + 11) << 6) |
69-
((temp & 0x7fffff) >> 17);
73+
uint ieee = (0xFFu << 23) | (m << 17);
74+
return BitConverter.UInt32BitsToSingle(ieee);
7075
}
76+
77+
return BitConverter.UInt32BitsToSingle(((e + 127u - bias) << 23) | (m << 17));
78+
}
79+
80+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
81+
public static uint PackFloatToFloat11(float value)
82+
{
83+
uint temp = Unsafe.As<float, uint>(ref value);
84+
return
85+
((((temp >> 23) & 0xff) - 127 + 11) << 6) |
86+
((temp & 0x7fffff) >> 17);
7187
}
7288
}

0 commit comments

Comments
 (0)