forked from Nominom/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBc6Encoder.cs
More file actions
326 lines (272 loc) · 8.39 KB
/
Copy pathBc6Encoder.cs
File metadata and controls
326 lines (272 loc) · 8.39 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
323
324
325
326
using System;
using System.Collections.Generic;
using BCnEncoder.Shared;
using BCnEncoder.Shared.Colors;
namespace BCnEncoder.Encoder.Bptc
{
internal class Bc6Encoder : BaseBcBlockEncoder<Bc6Block, RgbEncodingContext>
{
private readonly bool signed;
public Bc6Encoder(bool signed)
{
this.signed = signed;
}
public override Bc6Block EncodeBlock(in RgbEncodingContext context)
{
switch (context.Quality)
{
case CompressionQuality.Fast:
return Bc6EncoderFast.EncodeBlock(context, signed);
case CompressionQuality.Balanced:
return Bc6EncoderBalanced.EncodeBlock(context.RawBlock, context, signed);
case CompressionQuality.BestQuality:
return Bc6EncoderBestQuality.EncodeBlock(context.RawBlock, context, signed);
default:
throw new ArgumentOutOfRangeException(nameof(context.Quality), context.Quality, null);
}
}
internal static ClusterIndices4X4 CreateClusterIndexBlock(RawBlock4X4RgbaFloat raw, out int outputNumClusters,
int numClusters = 2)
{
var indexBlock = new ClusterIndices4X4();
var indices = LinearClustering.ClusterPixels(raw.AsSpan, 4, 4,
numClusters, 1, 10, false);
var output = indexBlock.AsSpan;
for (var i = 0; i < output.Length; i++)
{
output[i] = indices[i];
}
var nClusters = indexBlock.NumClusters;
if (nClusters < numClusters)
{
indexBlock = indexBlock.Reduce(out nClusters);
}
outputNumClusters = nClusters;
return indexBlock;
}
internal static class Bc6EncoderFast
{
internal static Bc6Block EncodeBlock(in RgbEncodingContext context, bool signed)
{
RgbBoundingBox.CreateFloat(context.RawBlock.AsSpan, out var min, out var max);
ColorRgbFloat minRgb = min, maxRgb = max;
LeastSquares.OptimizeEndpoints1Sub(context.RawBlock, ref minRgb, ref maxRgb);
return Bc6ModeEncoder.EncodeBlock1Sub(Bc6BlockType.Type3, context.RawBlock, min, max, signed, out _);
}
}
internal static class Bc6EncoderBalanced
{
private const float TargetError = 0.001f;
private const int MaxTries = 10;
private static IEnumerable<Bc6Block> GenerateCandidates(RawBlock4X4RgbaFloat block, RgbEncodingContext context, bool signed)
{
var candidates = 0;
Bc6EncodingHelpers.GetInitialUnscaledEndpoints(block, out var ep0Sub1, out var ep1Sub1, context.Weights);
if (!signed)
{
LeastSquares.OptimizeEndpoints1Sub(block, ref ep0Sub1, ref ep1Sub1);
}
ep0Sub1.ClampToHalf();
ep1Sub1.ClampToHalf();
if (!signed)
{
ep0Sub1.ClampToPositive();
ep1Sub1.ClampToPositive();
}
//Type3 Always ok!
yield return Bc6ModeEncoder.EncodeBlock1Sub(Bc6BlockType.Type3, block, ep0Sub1, ep1Sub1,
signed, out _);
candidates++;
var type15Block = Bc6ModeEncoder.EncodeBlock1Sub(Bc6BlockType.Type15, block, ep0Sub1, ep1Sub1,
signed, out var badType15);
candidates++;
if (!badType15)
{
yield return type15Block;
}
else
{
var indexBlock = CreateClusterIndexBlock(block, out var numClusters, 2);
var best2SubsetPartitions = BptcEncodingHelpers.Rank2SubsetPartitions(indexBlock, numClusters, true);
foreach (var subsetPartition in best2SubsetPartitions)
{
Bc6EncodingHelpers.GetInitialUnscaledEndpointsForSubset(block, out var ep0, out var ep1, subsetPartition, 0, context.Weights);
Bc6EncodingHelpers.GetInitialUnscaledEndpointsForSubset(block, out var ep2, out var ep3, subsetPartition, 1, context.Weights);
if (!signed)
{
LeastSquares.OptimizeEndpoints2Sub(block, ref ep0, ref ep1, subsetPartition, 0);
LeastSquares.OptimizeEndpoints2Sub(block, ref ep2, ref ep3, subsetPartition, 1);
}
ep0.ClampToHalf();
ep1.ClampToHalf();
ep2.ClampToHalf();
ep3.ClampToHalf();
if (!signed)
{
ep0.ClampToPositive();
ep1.ClampToPositive();
ep2.ClampToPositive();
ep3.ClampToPositive();
}
{
var type1Block = Bc6ModeEncoder.EncodeBlock2Sub(Bc6BlockType.Type1, block, ep0, ep1, ep2, ep3,
subsetPartition, signed, out var badType1);
candidates++;
if (!badType1)
{
yield return type1Block;
}
if (candidates >= MaxTries)
{
yield break;
}
}
{
var type14Block = Bc6ModeEncoder.EncodeBlock2Sub(Bc6BlockType.Type14, block, ep0, ep1, ep2, ep3,
subsetPartition, signed, out var badType14);
candidates++;
if (!badType14)
{
yield return type14Block;
}
if (candidates >= MaxTries)
{
yield break;
}
}
}
}
}
internal static Bc6Block EncodeBlock(RawBlock4X4RgbaFloat block, in RgbEncodingContext context, bool signed)
{
var result = new Bc6Block();
var bestError = 9999999f;
foreach (var candidate in GenerateCandidates(block, context, signed))
{
var error = block.CalculateError(candidate.Decode(signed));
if (error < bestError)
{
result = candidate;
bestError = error;
}
if (error <= TargetError)
{
break;
}
}
return result;
}
}
internal static class Bc6EncoderBestQuality
{
private const float TargetError = 0.0005f;
private const int MaxTries = 500;
private static IEnumerable<Bc6Block> GenerateCandidates(RawBlock4X4RgbaFloat block, RgbEncodingContext context, bool signed)
{
var candidates = 0;
Bc6EncodingHelpers.GetInitialUnscaledEndpoints(block, out var ep0Sub1, out var ep1Sub1, context.Weights);
if (!signed)
{
LeastSquares.OptimizeEndpoints1Sub(block, ref ep0Sub1, ref ep1Sub1);
}
ep0Sub1.ClampToHalf();
ep1Sub1.ClampToHalf();
if (!signed)
{
ep0Sub1.ClampToPositive();
ep1Sub1.ClampToPositive();
}
//Type3 Always ok!
yield return Bc6ModeEncoder.EncodeBlock1Sub(Bc6BlockType.Type3, block, ep0Sub1, ep1Sub1,
signed, out _);
candidates++;
//Type7
{
var type7Block = Bc6ModeEncoder.EncodeBlock1Sub(Bc6BlockType.Type7, block, ep0Sub1, ep1Sub1,
signed, out var badType7);
candidates++;
if (!badType7)
{
yield return type7Block;
}
}
//Type11
{
var type11Block = Bc6ModeEncoder.EncodeBlock1Sub(Bc6BlockType.Type11, block, ep0Sub1, ep1Sub1,
signed, out var badType11);
candidates++;
if (!badType11)
{
yield return type11Block;
}
}
//Type15
{
var type15Block = Bc6ModeEncoder.EncodeBlock1Sub(Bc6BlockType.Type15, block, ep0Sub1, ep1Sub1,
signed, out var badType15);
candidates++;
if (!badType15)
{
yield return type15Block;
}
}
var indexBlock = CreateClusterIndexBlock(block, out var numClusters, 2);
var best2SubsetPartitions = BptcEncodingHelpers.Rank2SubsetPartitions(indexBlock, numClusters, true);
foreach (var subsetPartition in best2SubsetPartitions)
{
Bc6EncodingHelpers.GetInitialUnscaledEndpointsForSubset(block, out var ep0, out var ep1, subsetPartition, 0, context.Weights);
Bc6EncodingHelpers.GetInitialUnscaledEndpointsForSubset(block, out var ep2, out var ep3, subsetPartition, 1, context.Weights);
if (!signed)
{
LeastSquares.OptimizeEndpoints2Sub(block, ref ep0, ref ep1, subsetPartition, 0);
LeastSquares.OptimizeEndpoints2Sub(block, ref ep2, ref ep3, subsetPartition, 1);
}
ep0.ClampToHalf();
ep1.ClampToHalf();
ep2.ClampToHalf();
ep3.ClampToHalf();
if (!signed)
{
ep0.ClampToPositive();
ep1.ClampToPositive();
ep2.ClampToPositive();
ep3.ClampToPositive();
}
foreach (var type in Bc6Block.Subsets2Types)
{
var sub2Block = Bc6ModeEncoder.EncodeBlock2Sub(type, block, ep0, ep1, ep2, ep3,
subsetPartition, signed, out var badTransform);
candidates++;
if (!badTransform)
{
yield return sub2Block;
}
if (candidates >= MaxTries)
{
yield break;
}
}
}
}
internal static Bc6Block EncodeBlock(RawBlock4X4RgbaFloat block, in RgbEncodingContext context, bool signed)
{
var result = new Bc6Block();
float bestError = 9999999;
foreach (var candidate in GenerateCandidates(block, context, signed))
{
var error = block.CalculateError(candidate.Decode(signed));
if (error < bestError)
{
result = candidate;
bestError = error;
}
if (error <= TargetError)
{
break;
}
}
return result;
}
}
}
}