-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockCompressionNewShader.hlsl
540 lines (448 loc) · 15 KB
/
BlockCompressionNewShader.hlsl
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#include "UHInputs.hlsli"
#include "UHCommon.hlsli"
// shader implementation of new block compression (e.g. BC6H) in UHE
#define BLOCK_SIZE 4
// 128-bit output
struct UHBlockCompressionOutput
{
uint LowBits0;
uint LowBits1;
uint HighBits0;
uint HighBits1;
};
struct UHBC6HData
{
uint LowBits;
uint HighBits;
uint3 Color0;
uint3 Color1;
};
RWStructuredBuffer<UHBlockCompressionOutput> GResult : register(u0);
// input should be int, note this already stores a direct binary value of 16-bit float
StructuredBuffer<uint3> GColorInput : register(t1);
cbuffer BlockCompressionConstant : register(b2)
{
uint GWidth;
uint GHeight;
}
groupshared uint3 GBlockColors[16];
groupshared float GMinError[16];
groupshared UHBC6HData GMinResult[16];
void GetBC6HPalette(int3 Color0, int3 Color1, inout float3 OutPalette[16])
{
int Weights[] = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 };
// interpolate 16 values for comparison
for (int Idx = 0; Idx < 16; Idx++)
{
int3 Palette = (Color0 * (64 - Weights[Idx]) + Color1 * Weights[Idx] + 32) >> 6;
OutPalette[Idx] = Palette;
}
}
// BC6H references:
// https://learn.microsoft.com/en-us/windows/win32/direct3d11/bc6h-format
// Color0 and Color1 might be swapped during the process
UHBC6HData EvaluateBC6H(uint3 Color0, uint3 Color1, out float OutMinDiff)
{
// setup palette for BC6H
float3 Palette[16];
GetBC6HPalette(Color0, Color1, Palette);
OutMinDiff = 0;
int BitShiftStart = 0;
UHBC6HData Data = (UHBC6HData) 0;
for (uint Idx = 0; Idx < 16; Idx++)
{
float3 BlockColor = GBlockColors[Idx];
float MinDiff = UH_FLOAT_MAX;
uint ClosestIdx = 0;
for (uint Jdx = 0; Jdx < 16; Jdx++)
{
float Diff = length(BlockColor - Palette[Jdx]);
if (Diff < MinDiff)
{
MinDiff = Diff;
ClosestIdx = Jdx;
}
}
// since the MSB of the first index will be discarded, if closest index for the first is larger than 3-bit range
// swap the reference color and search again
if (ClosestIdx > 7 && Idx == 0)
{
uint3 Temp = Color0;
Color0 = Color1;
Color1 = Temp;
GetBC6HPalette(Color0, Color1, Palette);
ClosestIdx = 16 - ClosestIdx - 1;
}
OutMinDiff += MinDiff;
if (Idx < 8)
{
Data.LowBits |= ClosestIdx << BitShiftStart;
}
else if (Idx == 8)
{
// across the boundary of low bits and high bits
Data.LowBits |= ClosestIdx << BitShiftStart;
Data.HighBits = ClosestIdx >> 1;
BitShiftStart = 3;
}
else
{
Data.HighBits |= ClosestIdx << BitShiftStart;
}
if (Idx != 8)
{
BitShiftStart += (Idx == 0) ? 3 : 4;
}
}
Data.Color0 = Color0;
Data.Color1 = Color1;
return Data;
}
// all quantize below is assumed as signed
int QuantizeAsNBit(int InVal, int InBit)
{
int S = 0;
if (InVal < 0)
{
S = 1;
InVal = -InVal;
}
int Q = (InVal << (InBit - 1)) / (0x7bff + 1);
if (S)
{
Q = -Q;
}
return Q;
}
int UnquantizeFromNBit(int InVal, int InBit)
{
int S = 0;
if (InVal < 0)
{
S = 1;
InVal = -InVal;
}
int Q;
if (InVal == 0)
{
Q = 0;
}
else if (InVal >= ((1U << (InBit - 1)) - 1))
{
Q = 0x7FFF;
}
else
{
Q = ((InVal << 15) + 0x4000) >> (InBit - 1);
}
if (S)
{
Q = -Q;
}
return Q;
}
bool StoreBC6HMode14(UHBC6HData Data, out UHBlockCompressionOutput OutResult)
{
// the first endpoint and the delta
// 16 bits for the endpoint and 4 bits for the delta
int RW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.r, 16), 16);
int GW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.g, 16), 16);
int BW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.b, 16), 16);
int RX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.r - Data.Color0.r, 16), 16);
int GX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.g - Data.Color0.g, 16), 16);
int BX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.b - Data.Color0.b, 16), 16);
if (!(RX >= -8 && RX < 7
&& GX >= -8 && GX < 7
&& BX >= -8 && BX < 7))
{
return false;
}
// store result from LSB to MSB
int BitShiftStart = 0;
UHBlockCompressionOutput Result = (UHBlockCompressionOutput)0;
// first 5 bits for mode, mode 14 is 01111
Result.LowBits0 = 15;
BitShiftStart += 5;
// store RW/GW/BW 10-bit [9:0]
Result.LowBits0 |= (RW & 1023) << BitShiftStart;
BitShiftStart += 10;
Result.LowBits0 |= (GW & 1023) << BitShiftStart;
BitShiftStart += 10;
// across boundary!
Result.LowBits0 |= (BW & 127) << BitShiftStart;
BitShiftStart += 7;
Result.LowBits1 |= (BW >> 7) & 7;
BitShiftStart = 3;
// store RX[3:0]
Result.LowBits1 |= (RX & 15) << BitShiftStart;
BitShiftStart += 4;
// store RW[10:15], note this is reversed!
for (int Idx = 15; Idx >= 10; Idx--)
{
Result.LowBits1 |= (RW >> Idx & 1) << BitShiftStart;
BitShiftStart++;
}
// store GX[3:0]
Result.LowBits1 |= (GX & 15) << BitShiftStart;
BitShiftStart += 4;
// store GW[10:15], note this is reversed!
for (Idx = 15; Idx >= 10; Idx--)
{
Result.LowBits1 |= (GW >> Idx & 1) << BitShiftStart;
BitShiftStart++;
}
// store BX[3:0]
Result.LowBits1 |= (BX & 15) << BitShiftStart;
BitShiftStart += 4;
// store BW[11:15], note this is reversed and across the low-high bits!
for (Idx = 15; Idx >= 11; Idx--)
{
Result.LowBits1 |= (BW >> Idx & 1) << BitShiftStart;
BitShiftStart++;
}
Result.HighBits0 |= (BW >> 10 & 1);
BitShiftStart = 1;
// now stores the indices, and acrossing boundary!
Result.HighBits0 |= Data.LowBits << BitShiftStart;
Result.HighBits1 |= (Data.LowBits >> 31);
Result.HighBits1 |= Data.HighBits << 1;
OutResult = Result;
return true;
}
bool StoreBC6HMode13(UHBC6HData Data, out UHBlockCompressionOutput OutResult)
{
// the first endpoint and the delta
// 12 bits for the endpoint and 8 bits for the delta
uint RW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.r, 12), 12) >> 4;
uint GW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.g, 12), 12) >> 4;
uint BW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.b, 12), 12) >> 4;
int RX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.r - Data.Color0.r, 12), 12) >> 4;
int GX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.g - Data.Color0.g, 12), 12) >> 4;
int BX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.b - Data.Color0.b, 12), 12) >> 4;
if (!(RX >= -128 && RX < 127
&& GX >= -128 && GX < 127
&& BX >= -128 && BX < 127))
{
return false;
}
// store result from LSB to MSB
int BitShiftStart = 0;
UHBlockCompressionOutput Result = (UHBlockCompressionOutput)0;
// first 5 bits for mode, mode 13 is 01011
Result.LowBits0 = 11;
BitShiftStart += 5;
// store RW/GW/BW 10-bit [9:0]
Result.LowBits0 |= (RW & 1023) << BitShiftStart;
BitShiftStart += 10;
Result.LowBits0 |= (GW & 1023) << BitShiftStart;
BitShiftStart += 10;
Result.LowBits0 |= (BW & 127) << BitShiftStart;
BitShiftStart += 7;
Result.LowBits1 |= (BW >> 7) & 7;
BitShiftStart = 3;
// store RX[7:0]
Result.LowBits1 |= (RX & 255) << BitShiftStart;
BitShiftStart += 8;
// store RW[10:11], note this is reversed!
Result.LowBits1 |= (RW >> 11 & 1) << BitShiftStart;
BitShiftStart++;
Result.LowBits1 |= (RW >> 10 & 1) << BitShiftStart;
BitShiftStart++;
// store GX[7:0]
Result.LowBits1 |= (GX & 255) << BitShiftStart;
BitShiftStart += 8;
// store GW[10:11], note this is reversed!
Result.LowBits1 |= (GW >> 11 & 1) << BitShiftStart;
BitShiftStart++;
Result.LowBits1 |= (GW >> 10 & 1) << BitShiftStart;
BitShiftStart++;
// store BX[7:0]
Result.LowBits1 |= (BX & 255) << BitShiftStart;
BitShiftStart += 8;
// store BW[10:11], note this is reversed and across the low-high bits!
Result.LowBits1 |= (BW >> 11 & 1) << BitShiftStart;
Result.HighBits0 |= (BW >> 10 & 1);
BitShiftStart = 1;
// now stores the indices, and acrossing boundary!
Result.HighBits0 |= Data.LowBits << BitShiftStart;
Result.HighBits1 |= (Data.LowBits >> 31);
Result.HighBits1 |= Data.HighBits << 1;
OutResult = Result;
return true;
}
bool StoreBC6HMode12(UHBC6HData Data, out UHBlockCompressionOutput OutResult)
{
// the first endpoint and the delta
// 11 bits for the endpoint and 9 bits for the delta
// for the odd bits, round up the value before shift
uint RW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.r, 11), 11 + 32) >> 5;
uint GW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.g, 11), 11 + 32) >> 5;
uint BW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.b, 11), 11 + 32) >> 5;
int RX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.r - Data.Color0.r, 11), 11 + 32) >> 5;
int GX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.g - Data.Color0.g, 11), 11 + 32) >> 5;
int BX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.b - Data.Color0.b, 11), 11 + 32) >> 5;
if (!(RX >= -256 && RX < 255
&& GX >= -256 && GX < 255
&& BX >= -256 && BX < 255))
{
return false;
}
// store result from LSB to MSB
int BitShiftStart = 0;
UHBlockCompressionOutput Result = (UHBlockCompressionOutput)0;
// first 5 bits for mode, mode 12 is 00111
Result.LowBits0 = 7;
BitShiftStart += 5;
// store RW/GW/BW 10-bit [9:0]
Result.LowBits0 |= (RW & 1023) << BitShiftStart;
BitShiftStart += 10;
Result.LowBits0 |= (GW & 1023) << BitShiftStart;
BitShiftStart += 10;
// across boundary!
Result.LowBits0 |= (BW & 127) << BitShiftStart;
BitShiftStart += 7;
Result.LowBits1 |= (BW >> 7) & 7;
BitShiftStart = 3;
// store RX[8:0]
Result.LowBits1 |= (RX & 511) << BitShiftStart;
BitShiftStart += 9;
// store RW[10]
Result.LowBits1 |= (RW & 1024) << BitShiftStart;
BitShiftStart++;
// store GX[8:0]
Result.LowBits1 |= (GX & 511) << BitShiftStart;
BitShiftStart += 9;
// store GW[10]
Result.LowBits1 |= (GW & 1024) << BitShiftStart;
BitShiftStart++;
// store BX[8:0]
Result.LowBits1 |= (BX & 511) << BitShiftStart;
BitShiftStart += 9;
// store BW[10]
Result.HighBits0 |= (BW & 1024);
BitShiftStart++;
// now stores the indices, and acrossing boundary!
Result.HighBits0 |= Data.LowBits << BitShiftStart;
Result.HighBits1 |= (Data.LowBits >> 31);
Result.HighBits1 |= Data.HighBits << 1;
OutResult = Result;
return true;
}
UHBlockCompressionOutput StoreBC6HMode11(UHBC6HData Data)
{
// store result from LSB to MSB
int BitShiftStart = 0;
UHBlockCompressionOutput Result = (UHBlockCompressionOutput)0;
// quantize and unquantize back for the consistency
uint RW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.r, 10), 10) >> 6;
uint GW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.g, 10), 10) >> 6;
uint BW = UnquantizeFromNBit(QuantizeAsNBit(Data.Color0.b, 10), 10) >> 6;
uint RX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.r, 10), 10) >> 6;
uint GX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.g, 10), 10) >> 6;
uint BX = UnquantizeFromNBit(QuantizeAsNBit(Data.Color1.b, 10), 10) >> 6;
// first 5 bits for mode, mode 11 is corresponding to 00011
Result.LowBits0 = 3;
BitShiftStart += 5;
// next 60 bits for reference colors, 10 bits for each color component
Result.LowBits0 |= RW << BitShiftStart;
BitShiftStart += 10;
Result.LowBits0 |= GW << BitShiftStart;
BitShiftStart += 10;
// acrossing boundary!
Result.LowBits0 |= BW << BitShiftStart;
BitShiftStart += 7;
Result.LowBits1 |= (BW >> 7);
BitShiftStart = 3;
Result.LowBits1 |= RX << BitShiftStart;
BitShiftStart += 10;
Result.LowBits1 |= GX << BitShiftStart;
BitShiftStart += 10;
// acrossing boundary!
Result.LowBits1 |= BX << BitShiftStart;
BitShiftStart += 9;
Result.HighBits0 = BX >> 9;
BitShiftStart = 1;
// now stores the indices, and acrossing boundary!
Result.HighBits0 |= Data.LowBits << BitShiftStart;
Result.HighBits1 |= (Data.LowBits >> 31);
Result.HighBits1 |= Data.HighBits << 1;
return Result;
}
[numthreads(BLOCK_SIZE, BLOCK_SIZE, 1)]
void BlockCompressHDR(uint3 DTid : SV_DispatchThreadID, uint GIndex : SV_GroupIndex, uint3 Gid : SV_GroupID)
{
// store block colors and init caches
uint TextureIndex = DTid.x + DTid.y * GWidth;
GBlockColors[GIndex] = GColorInput[TextureIndex];
GMinError[GIndex] = UH_FLOAT_MAX;
GroupMemoryBarrierWithGroupSync();
if (DTid.x >= GWidth || DTid.y >= GHeight)
{
return;
}
// find min/max color
uint3 MaxColor = 0;
uint3 MinColor = 65504;
for (uint Idx = 0; Idx < 16; Idx++)
{
MaxColor = max(MaxColor, GBlockColors[Idx]);
MinColor = min(MinColor, GBlockColors[Idx]);
}
// Test the pairs that has the minimal errors except itself
float BC6HMinError = UH_FLOAT_MAX;
float MinError = 0;
UHBC6HData Result = (UHBC6HData) 0;
UHBC6HData FinalResult = (UHBC6HData) 0;
// on CPU side a thread runs 120 tests
// here, only need to run 15 tests for each thread, I'll choose the minimal result across all threads later
for (Idx = 0; Idx < 16; Idx++)
{
if (Idx == GIndex)
{
continue;
}
// call EvaluateBC6H
Result = EvaluateBC6H(GBlockColors[GIndex], GBlockColors[Idx], MinError);
if (MinError < BC6HMinError)
{
BC6HMinError = MinError;
FinalResult = Result;
}
}
// in case the search doesn't work well, try the min/max color once
Result = EvaluateBC6H(MaxColor, MinColor, MinError);
if (MinError < BC6HMinError)
{
BC6HMinError = MinError;
FinalResult = Result;
}
GMinError[GIndex] = BC6HMinError;
GMinResult[GIndex] = FinalResult;
GroupMemoryBarrierWithGroupSync();
// each thread hold a minimal result now, find the real minimal one for this block and output
if (GIndex == 0)
{
int MinIdx = 0;
float MinError = UH_FLOAT_MAX;
for (int Idx = 0; Idx < 16; Idx++)
{
if (GMinError[Idx] < MinError)
{
MinIdx = Idx;
MinError = GMinError[Idx];
}
}
// select the data output mode
UHBC6HData Data = GMinResult[MinIdx];
int OutputIdx = Gid.x + Gid.y * (GWidth / 4);
UHBlockCompressionOutput SelectedResult;
if (StoreBC6HMode14(Data, SelectedResult)) {}
else if (StoreBC6HMode13(Data, SelectedResult)) {}
else if (StoreBC6HMode12(Data, SelectedResult)) {}
else
{
SelectedResult = StoreBC6HMode11(Data);
}
GResult[OutputIdx] = SelectedResult;
}
}