forked from crskycode/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBc7Mode2Encoder.cs
More file actions
78 lines (63 loc) · 2.4 KB
/
Copy pathBc7Mode2Encoder.cs
File metadata and controls
78 lines (63 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using BCnEncoder.Shared;
namespace BCnEncoder.Encoder.Bptc
{
internal static class Bc7Mode2Encoder
{
public static Bc7Block EncodeBlock(RawBlock4X4Rgba32 block, int startingVariation, int bestPartition)
{
var output = new Bc7Block();
const Bc7BlockType type = Bc7BlockType.Type2;
var endpoints = new ColorRgba32[6];
ReadOnlySpan<int> partitionTable = Bc7Block.Subsets3PartitionTable[bestPartition];
var indices = new byte[16];
var anchorIndices = new int[] {
0,
Bc7Block.Subsets3AnchorIndices2[bestPartition],
Bc7Block.Subsets3AnchorIndices3[bestPartition]
};
for (var subset = 0; subset < 3; subset++) {
Bc7EncodingHelpers.GetInitialUnscaledEndpointsForSubset(block, out var ep0, out var ep1,
partitionTable, subset);
var scaledEp0 =
Bc7EncodingHelpers.ScaleDownEndpoint(ep0, type, true, out var _);
var scaledEp1 =
Bc7EncodingHelpers.ScaleDownEndpoint(ep1, type, true, out var _);
byte pBit = 0;
Bc7EncodingHelpers.OptimizeSubsetEndpointsWithPBit(type, block, ref scaledEp0,
ref scaledEp1, ref pBit, ref pBit, startingVariation, partitionTable, subset, false, false);
ep0 = Bc7EncodingHelpers.ExpandEndpoint(type, scaledEp0, 0);
ep1 = Bc7EncodingHelpers.ExpandEndpoint(type, scaledEp1, 0);
Bc7EncodingHelpers.FillSubsetIndices(type, block,
ep0,
ep1,
partitionTable, subset, indices);
if ((indices[anchorIndices[subset]] & 0b10) > 0) //If anchor index most significant bit is 1, switch endpoints
{
var c = scaledEp0;
scaledEp0 = scaledEp1;
scaledEp1 = c;
//redo indices
ep0 = Bc7EncodingHelpers.ExpandEndpoint(type, scaledEp0, 0);
ep1 = Bc7EncodingHelpers.ExpandEndpoint(type, scaledEp1, 0);
Bc7EncodingHelpers.FillSubsetIndices(type, block,
ep0,
ep1,
partitionTable, subset, indices);
}
endpoints[subset * 2] = scaledEp0;
endpoints[subset * 2 + 1] = scaledEp1;
}
output.PackType2(bestPartition, new[]{
new byte[]{endpoints[0].r, endpoints[0].g, endpoints[0].b},
new byte[]{endpoints[1].r, endpoints[1].g, endpoints[1].b},
new byte[]{endpoints[2].r, endpoints[2].g, endpoints[2].b},
new byte[]{endpoints[3].r, endpoints[3].g, endpoints[3].b},
new byte[]{endpoints[4].r, endpoints[4].g, endpoints[4].b},
new byte[]{endpoints[5].r, endpoints[5].g, endpoints[5].b}
},
indices);
return output;
}
}
}