-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBptcEncodingHelpers.cs
More file actions
322 lines (282 loc) · 7.89 KB
/
Copy pathBptcEncodingHelpers.cs
File metadata and controls
322 lines (282 loc) · 7.89 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using BCnEncoder.Shared;
#if NETSTANDARD2_0
using MemoryMarshal = BCnEncoder.Shared.MemoryMarshalPolyfills;
#endif
namespace BCnEncoder.Encoder.Bptc
{
internal static class BptcEncodingHelpers
{
private static readonly byte[] ColorInterpolationWeights2 = new byte[] { 0, 21, 43, 64 };
private static readonly byte[] ColorInterpolationWeights3 = new byte[] { 0, 9, 18, 27, 37, 46, 55, 64 };
private static readonly byte[] ColorInterpolationWeights4 = new byte[] { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 };
public static int InterpolateInt(int e0, int e1, int index, int indexPrecision)
{
if (indexPrecision == 0) return e0;
var aWeights2 = ColorInterpolationWeights2;
var aWeights3 = ColorInterpolationWeights3;
var aWeights4 = ColorInterpolationWeights4;
if (indexPrecision == 2)
return (((64 - aWeights2[index]) * e0 + aWeights2[index] * e1 + 32) >> 6);
if (indexPrecision == 3)
return ((64 - aWeights3[index]) * e0 + aWeights3[index] * e1 + 32) >> 6;
else // indexprecision == 4
return ((64 - aWeights4[index]) * e0 + aWeights4[index] * e1 + 32) >> 6;
}
public static byte InterpolateByte(byte e0, byte e1, int index, int indexPrecision)
{
if (indexPrecision == 0) return e0;
var aWeights2 = ColorInterpolationWeights2;
var aWeights3 = ColorInterpolationWeights3;
var aWeights4 = ColorInterpolationWeights4;
if (indexPrecision == 2)
return (byte)(((64 - aWeights2[index]) * e0 + aWeights2[index] * e1 + 32) >> 6);
if (indexPrecision == 3)
return (byte)(((64 - aWeights3[index]) * e0 + aWeights3[index] * e1 + 32) >> 6);
else // indexprecision == 4
return (byte)(((64 - aWeights4[index]) * e0 + aWeights4[index] * e1 + 32) >> 6);
}
public static int[] Rank2SubsetPartitions(ClusterIndices4X4 reducedIndicesBlock, int numDistinctClusters, bool smallIndex = false)
{
var output = Enumerable.Range(0, smallIndex ? 32 : 64).ToArray();
// Copy struct to array before the closure so that reducedIndicesBlock is not
// heap-captured. On .NET Framework, MemoryMarshalPolyfills.CreateSpan uses
// Unsafe.AsPointer, which is only GC-safe for stack-allocated structs.
#if NETSTANDARD2_0
var indices = new int[16];
reducedIndicesBlock.AsSpan.CopyTo(indices);
#endif
int CalculatePartitionError(int partitionIndex)
{
#if NETSTANDARD2_1
var indices = reducedIndicesBlock.AsSpan;
#endif
var error = 0;
ReadOnlySpan<int> partitionTable = Bc7Block.Subsets2PartitionTable[partitionIndex];
Span<int> subset0 = stackalloc int[numDistinctClusters];
Span<int> subset1 = stackalloc int[numDistinctClusters];
var max0Idx = 0;
var max1Idx = 0;
//Calculate largest cluster index for each subset
for (var i = 0; i < 16; i++)
{
if (partitionTable[i] == 0)
{
var r = indices[i];
subset0[r]++;
var count = subset0[r];
if (count > subset0[max0Idx])
{
max0Idx = r;
}
}
else
{
var r = indices[i];
subset1[r]++;
var count = subset1[r];
if (count > subset1[max1Idx])
{
max1Idx = r;
}
}
}
// Calculate error by counting as error everything that does not match the largest cluster
for (var i = 0; i < 16; i++)
{
if (partitionTable[i] == 0)
{
if (indices[i] != max0Idx) error++;
}
else
{
if (indices[i] != max1Idx) error++;
}
}
return error;
}
output = output.OrderBy(CalculatePartitionError).ToArray();
return output;
}
public static int[] Rank3SubsetPartitions(ClusterIndices4X4 reducedIndicesBlock, int numDistinctClusters)
{
var output = Enumerable.Range(0, 64).ToArray();
// Copy struct to array before the closure so that reducedIndicesBlock is not
// heap-captured. On .NET Framework, MemoryMarshalPolyfills.CreateSpan uses
// Unsafe.AsPointer, which is only GC-safe for stack-allocated structs.
#if NETSTANDARD2_0
var indices = new int[16];
reducedIndicesBlock.AsSpan.CopyTo(indices);
#endif
int CalculatePartitionError(int partitionIndex)
{
#if NETSTANDARD2_1
var indices = reducedIndicesBlock.AsSpan;
#endif
var error = 0;
ReadOnlySpan<int> partitionTable = Bc7Block.Subsets3PartitionTable[partitionIndex];
Span<int> subset0 = stackalloc int[numDistinctClusters];
Span<int> subset1 = stackalloc int[numDistinctClusters];
Span<int> subset2 = stackalloc int[numDistinctClusters];
var max0Idx = 0;
var max1Idx = 0;
var max2Idx = 0;
//Calculate largest cluster index for each subset
for (var i = 0; i < 16; i++)
{
if (partitionTable[i] == 0)
{
var r = indices[i];
subset0[r]++;
var count = subset0[r];
if (count > subset0[max0Idx])
{
max0Idx = r;
}
}
else if (partitionTable[i] == 1)
{
var r = indices[i];
subset1[r]++;
var count = subset1[r];
if (count > subset1[max1Idx])
{
max1Idx = r;
}
}
else
{
var r = indices[i];
subset2[r]++;
var count = subset2[r];
if (count > subset2[max2Idx])
{
max2Idx = r;
}
}
}
// Calculate error by counting as error everything that does not match the largest cluster
for (var i = 0; i < 16; i++)
{
if (partitionTable[i] == 0)
{
if (indices[i] != max0Idx) error++;
}
else if (partitionTable[i] == 1)
{
if (indices[i] != max1Idx) error++;
}
else
{
if (indices[i] != max2Idx) error++;
}
}
return error;
}
output = output.OrderBy(CalculatePartitionError).ToArray();
return output;
}
}
internal struct ClusterIndices4X4
{
public int i00, i10, i20, i30;
public int i01, i11, i21, i31;
public int i02, i12, i22, i32;
public int i03, i13, i23, i33;
public Span<int> AsSpan => MemoryMarshal.CreateSpan(ref i00, 16);
public int this[int x, int y]
{
get => AsSpan[x + y * 4];
set => AsSpan[x + y * 4] = value;
}
public int this[int index]
{
get => AsSpan[index];
set => AsSpan[index] = value;
}
public int NumClusters
{
get
{
var t = AsSpan;
Span<int> clusters = stackalloc int[16];
var distinct = 0;
for (var i = 0; i < 16; i++)
{
var cluster = t[i];
var found = false;
for (var j = 0; j < distinct; j++)
{
if (clusters[j] == cluster)
{
found = true;
break;
}
}
if (!found)
{
clusters[distinct] = cluster;
++distinct;
}
}
return distinct;
}
}
/// <summary>
/// Reduces block down to adjacent cluster indices. For example,
/// block that contains clusters 5, 16 and 77 will become a block that contains clusters 0, 1 and 2
/// </summary>
public ClusterIndices4X4 Reduce(out int numClusters)
{
var result = new ClusterIndices4X4();
numClusters = NumClusters;
Span<int> mapKey = stackalloc int[numClusters];
var indices = AsSpan;
var outIndices = result.AsSpan;
var next = 0;
for (var i = 0; i < 16; i++)
{
var cluster = indices[i];
var found = false;
for (var j = 0; j < next; j++)
{
if (mapKey[j] == cluster)
{
found = true;
outIndices[i] = j;
break;
}
}
if (!found)
{
outIndices[i] = next;
mapKey[next] = cluster;
++next;
}
}
return result;
}
}
internal struct IndexBlock4x4
{
public byte i00, i10, i20, i30;
public byte i01, i11, i21, i31;
public byte i02, i12, i22, i32;
public byte i03, i13, i23, i33;
public Span<byte> AsSpan => MemoryMarshal.CreateSpan(ref i00, 16);
public byte this[int x, int y]
{
get => AsSpan[x + y * 4];
set => AsSpan[x + y * 4] = value;
}
public byte this[int index]
{
get => AsSpan[index];
set => AsSpan[index] = value;
}
}
}