Rise
The Vieneo Province
Loading...
Searching...
No Matches
BC.cpp
Go to the documentation of this file.
1//-------------------------------------------------------------------------------------
2// BC.cpp
3//
4// Block-compression (BC) functionality for BC1, BC2, BC3 (orginal DXTn formats)
5//
6// Copyright (c) Microsoft Corporation.
7// Licensed under the MIT License.
8//
9// https://go.microsoft.com/fwlink/?LinkId=248926
10//-------------------------------------------------------------------------------------
11
12#include <cstdint>
13#include <cfloat>
14#include <cassert>
15#include <cstring>
16#include <algorithm>
17
18// Experiemental encoding variants, not enabled by default
19// #define COLOR_WEIGHTS
20// #define COLOR_AVG_0WEIGHTS
21
22#include "BC.h"
23
24using namespace DirectX;
25using namespace DirectX::PackedVector;
26
27namespace
28{
29 //-------------------------------------------------------------------------------------
30 // Constants
31 //-------------------------------------------------------------------------------------
32
33 // Perceptual weightings for the importance of each channel.
34 const HDRColorA g_Luminance(0.2125f / 0.7154f, 1.0f, 0.0721f / 0.7154f, 1.0f);
35 const HDRColorA g_LuminanceInv(0.7154f / 0.2125f, 1.0f, 0.7154f / 0.0721f, 1.0f);
36
37 //-------------------------------------------------------------------------------------
38 // Decode/Encode RGB 5/6/5 colors
39 //-------------------------------------------------------------------------------------
40 inline void Decode565(_Out_ HDRColorA* pColor, _In_ const uint16_t w565) noexcept
41 {
42 pColor->r = static_cast<float>((w565 >> 11) & 31) * (1.0f / 31.0f);
43 pColor->g = static_cast<float>((w565 >> 5) & 63) * (1.0f / 63.0f);
44 pColor->b = static_cast<float>((w565 >> 0) & 31) * (1.0f / 31.0f);
45 pColor->a = 1.0f;
46 }
47
48 inline uint16_t Encode565(_In_ const HDRColorA* pColor) noexcept
49 {
51
52 Color.r = (pColor->r < 0.0f) ? 0.0f : (pColor->r > 1.0f) ? 1.0f : pColor->r;
53 Color.g = (pColor->g < 0.0f) ? 0.0f : (pColor->g > 1.0f) ? 1.0f : pColor->g;
54 Color.b = (pColor->b < 0.0f) ? 0.0f : (pColor->b > 1.0f) ? 1.0f : pColor->b;
55 Color.a = pColor->a;
56
57 uint16_t w;
58
59 w = static_cast<uint16_t>((static_cast<int32_t>(Color.r * 31.0f + 0.5f) << 11) | (static_cast<int32_t>(Color.g * 63.0f + 0.5f) << 5)
60 | (static_cast<int32_t>(Color.b * 31.0f + 0.5f) << 0));
61
62 return w;
63 }
64
65 //-------------------------------------------------------------------------------------
66 void OptimizeRGB(_Out_ HDRColorA* pX,
67 _Out_ HDRColorA* pY,
68 _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA* pPoints,
69 uint32_t cSteps,
70 uint32_t flags) noexcept
71 {
72 constexpr float fEpsilon = (0.25f / 64.0f) * (0.25f / 64.0f);
73 static const float pC3[] = { 2.0f / 2.0f, 1.0f / 2.0f, 0.0f / 2.0f };
74 static const float pD3[] = { 0.0f / 2.0f, 1.0f / 2.0f, 2.0f / 2.0f };
75 static const float pC4[] = { 3.0f / 3.0f, 2.0f / 3.0f, 1.0f / 3.0f, 0.0f / 3.0f };
76 static const float pD4[] = { 0.0f / 3.0f, 1.0f / 3.0f, 2.0f / 3.0f, 3.0f / 3.0f };
77
78 const float* pC = (3 == cSteps) ? pC3 : pC4;
79 const float* pD = (3 == cSteps) ? pD3 : pD4;
80
81 // Find Min and Max points, as starting point
82 HDRColorA X = (flags & BC_FLAGS_UNIFORM) ? HDRColorA(1.f, 1.f, 1.f, 1.f) : g_Luminance;
83 HDRColorA Y = HDRColorA(0.0f, 0.0f, 0.0f, 1.0f);
84
85 for (size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
86 {
87#ifdef COLOR_WEIGHTS
88 if (pPoints[iPoint].a > 0.0f)
89#endif // COLOR_WEIGHTS
90 {
91 if (pPoints[iPoint].r < X.r)
92 X.r = pPoints[iPoint].r;
93
94 if (pPoints[iPoint].g < X.g)
95 X.g = pPoints[iPoint].g;
96
97 if (pPoints[iPoint].b < X.b)
98 X.b = pPoints[iPoint].b;
99
100 if (pPoints[iPoint].r > Y.r)
101 Y.r = pPoints[iPoint].r;
102
103 if (pPoints[iPoint].g > Y.g)
104 Y.g = pPoints[iPoint].g;
105
106 if (pPoints[iPoint].b > Y.b)
107 Y.b = pPoints[iPoint].b;
108 }
109 }
110
111 // Diagonal axis
112 const HDRColorA AB(Y.r - X.r, Y.g - X.g, Y.b - X.b, 0.0f);
113
114 const float fAB = AB.r * AB.r + AB.g * AB.g + AB.b * AB.b;
115
116 // Single color block.. no need to root-find
117 if (fAB < FLT_MIN)
118 {
119 pX->r = X.r;
120 pX->g = X.g;
121 pX->b = X.b;
122 pX->a = 1.0f;
123 pY->r = Y.r;
124 pY->g = Y.g;
125 pY->b = Y.b;
126 pY->a = 1.0f;
127 return;
128 }
129
130 // Try all four axis directions, to determine which diagonal best fits data
131 const float fABInv = 1.0f / fAB;
132
133 HDRColorA Dir(AB.r * fABInv, AB.g * fABInv, AB.b * fABInv, 0.0f);
134
135 const HDRColorA Mid((X.r + Y.r) * 0.5f, (X.g + Y.g) * 0.5f, (X.b + Y.b) * 0.5f, 0.0f);
136
137 float fDir[4] = {};
138
139 for (size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
140 {
141 HDRColorA Pt;
142 Pt.r = (pPoints[iPoint].r - Mid.r) * Dir.r;
143 Pt.g = (pPoints[iPoint].g - Mid.g) * Dir.g;
144 Pt.b = (pPoints[iPoint].b - Mid.b) * Dir.b;
145 Pt.a = 0.0f;
146
147 float f;
148
149#ifdef COLOR_WEIGHTS
150 f = Pt.r + Pt.g + Pt.b;
151 fDir[0] += pPoints[iPoint].a * f * f;
152
153 f = Pt.r + Pt.g - Pt.b;
154 fDir[1] += pPoints[iPoint].a * f * f;
155
156 f = Pt.r - Pt.g + Pt.b;
157 fDir[2] += pPoints[iPoint].a * f * f;
158
159 f = Pt.r - Pt.g - Pt.b;
160 fDir[3] += pPoints[iPoint].a * f * f;
161#else
162 f = Pt.r + Pt.g + Pt.b;
163 fDir[0] += f * f;
164
165 f = Pt.r + Pt.g - Pt.b;
166 fDir[1] += f * f;
167
168 f = Pt.r - Pt.g + Pt.b;
169 fDir[2] += f * f;
170
171 f = Pt.r - Pt.g - Pt.b;
172 fDir[3] += f * f;
173#endif // COLOR_WEIGHTS
174 }
175
176 float fDirMax = fDir[0];
177 size_t iDirMax = 0;
178
179 for (size_t iDir = 1; iDir < 4; iDir++)
180 {
181 if (fDir[iDir] > fDirMax)
182 {
183 fDirMax = fDir[iDir];
184 iDirMax = iDir;
185 }
186 }
187
188 if (iDirMax & 2)
189 {
190 const float f = X.g;
191 X.g = Y.g;
192 Y.g = f;
193 }
194
195 if (iDirMax & 1)
196 {
197 const float f = X.b;
198 X.b = Y.b;
199 Y.b = f;
200 }
201
202 // Two color block.. no need to root-find
203 if (fAB < 1.0f / 4096.0f)
204 {
205 pX->r = X.r;
206 pX->g = X.g;
207 pX->b = X.b;
208 pX->a = 1.0f;
209 pY->r = Y.r;
210 pY->g = Y.g;
211 pY->b = Y.b;
212 pY->a = 1.0f;
213 return;
214 }
215
216 // Use Newton's Method to find local minima of sum-of-squares error.
217 const auto fSteps = static_cast<float>(cSteps - 1);
218
219 for (size_t iIteration = 0; iIteration < 8; iIteration++)
220 {
221 // Calculate new steps
222 HDRColorA pSteps[4];
223
224 for (size_t iStep = 0; iStep < cSteps; iStep++)
225 {
226 pSteps[iStep].r = X.r * pC[iStep] + Y.r * pD[iStep];
227 pSteps[iStep].g = X.g * pC[iStep] + Y.g * pD[iStep];
228 pSteps[iStep].b = X.b * pC[iStep] + Y.b * pD[iStep];
229 pSteps[iStep].a = 1.0f;
230 }
231
232 // Calculate color direction
233 Dir.r = Y.r - X.r;
234 Dir.g = Y.g - X.g;
235 Dir.b = Y.b - X.b;
236
237 const float fLen = (Dir.r * Dir.r + Dir.g * Dir.g + Dir.b * Dir.b);
238
239 if (fLen < (1.0f / 4096.0f))
240 break;
241
242 const float fScale = fSteps / fLen;
243
244 Dir.r *= fScale;
245 Dir.g *= fScale;
246 Dir.b *= fScale;
247
248 // Evaluate function, and derivatives
249 float d2X = 0.f;
250 float d2Y = 0.f;
251 HDRColorA dX = {};
252 HDRColorA dY = {};
253
254 for (size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
255 {
256 const float fDot
257 = (pPoints[iPoint].r - X.r) * Dir.r + (pPoints[iPoint].g - X.g) * Dir.g + (pPoints[iPoint].b - X.b) * Dir.b;
258
259 uint32_t iStep;
260 if (fDot <= 0.0f)
261 iStep = 0;
262 else if (fDot >= fSteps)
263 iStep = cSteps - 1;
264 else
265 iStep = uint32_t(fDot + 0.5f);
266
267 HDRColorA Diff;
268 Diff.r = pSteps[iStep].r - pPoints[iPoint].r;
269 Diff.g = pSteps[iStep].g - pPoints[iPoint].g;
270 Diff.b = pSteps[iStep].b - pPoints[iPoint].b;
271 Diff.a = 0.0f;
272
273#ifdef COLOR_WEIGHTS
274 const float fC = pC[iStep] * pPoints[iPoint].a * (1.0f / 8.0f);
275 const float fD = pD[iStep] * pPoints[iPoint].a * (1.0f / 8.0f);
276#else
277 const float fC = pC[iStep] * (1.0f / 8.0f);
278 const float fD = pD[iStep] * (1.0f / 8.0f);
279#endif // COLOR_WEIGHTS
280
281 d2X += fC * pC[iStep];
282 dX.r += fC * Diff.r;
283 dX.g += fC * Diff.g;
284 dX.b += fC * Diff.b;
285
286 d2Y += fD * pD[iStep];
287 dY.r += fD * Diff.r;
288 dY.g += fD * Diff.g;
289 dY.b += fD * Diff.b;
290 }
291
292 // Move endpoints
293 if (d2X > 0.0f)
294 {
295 const float f = -1.0f / d2X;
296
297 X.r += dX.r * f;
298 X.g += dX.g * f;
299 X.b += dX.b * f;
300 }
301
302 if (d2Y > 0.0f)
303 {
304 const float f = -1.0f / d2Y;
305
306 Y.r += dY.r * f;
307 Y.g += dY.g * f;
308 Y.b += dY.b * f;
309 }
310
311 if ((dX.r * dX.r < fEpsilon) && (dX.g * dX.g < fEpsilon) && (dX.b * dX.b < fEpsilon) && (dY.r * dY.r < fEpsilon)
312 && (dY.g * dY.g < fEpsilon) && (dY.b * dY.b < fEpsilon))
313 {
314 break;
315 }
316 }
317
318 pX->r = X.r;
319 pX->g = X.g;
320 pX->b = X.b;
321 pX->a = 1.0f;
322 pY->r = Y.r;
323 pY->g = Y.g;
324 pY->b = Y.b;
325 pY->a = 1.0f;
326 }
327
328 //-------------------------------------------------------------------------------------
329 inline void DecodeBC1(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_ const D3DX_BC1* pBC, bool isbc1) noexcept
330 {
331 assert(pColor && pBC);
332 static_assert(sizeof(D3DX_BC1) == 8, "D3DX_BC1 should be 8 bytes");
333
334 static XMVECTORF32 s_Scale = { { { 1.f / 31.f, 1.f / 63.f, 1.f / 31.f, 1.f } } };
335
336 XMVECTOR clr0 = XMLoadU565(reinterpret_cast<const XMU565*>(&pBC->rgb[0]));
337 XMVECTOR clr1 = XMLoadU565(reinterpret_cast<const XMU565*>(&pBC->rgb[1]));
338
339 clr0 = XMVectorMultiply(clr0, s_Scale);
340 clr1 = XMVectorMultiply(clr1, s_Scale);
341
342 clr0 = XMVectorSwizzle<2, 1, 0, 3>(clr0);
343 clr1 = XMVectorSwizzle<2, 1, 0, 3>(clr1);
344
345 clr0 = XMVectorSelect(g_XMIdentityR3, clr0, g_XMSelect1110);
346 clr1 = XMVectorSelect(g_XMIdentityR3, clr1, g_XMSelect1110);
347
348 XMVECTOR clr2, clr3;
349 if (isbc1 && (pBC->rgb[0] <= pBC->rgb[1]))
350 {
351 clr2 = XMVectorLerp(clr0, clr1, 0.5f);
352 clr3 = XMVectorZero(); // Alpha of 0
353 }
354 else
355 {
356 clr2 = XMVectorLerp(clr0, clr1, 1.f / 3.f);
357 clr3 = XMVectorLerp(clr0, clr1, 2.f / 3.f);
358 }
359
360 uint32_t dw = pBC->bitmap;
361
362 for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 2)
363 {
364 switch (dw & 3)
365 {
366 case 0: pColor[i] = clr0; break;
367 case 1: pColor[i] = clr1; break;
368 case 2: pColor[i] = clr2; break;
369
370 case 3:
371 default: pColor[i] = clr3; break;
372 }
373 }
374 }
375
376 //-------------------------------------------------------------------------------------
377 void EncodeBC1(_Out_ D3DX_BC1* pBC,
378 _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA* pColor,
379 bool bColorKey,
380 float threshold,
381 uint32_t flags) noexcept
382 {
383 assert(pBC && pColor);
384 static_assert(sizeof(D3DX_BC1) == 8, "D3DX_BC1 should be 8 bytes");
385
386 // Determine if we need to colorkey this block
387 uint32_t uSteps;
388
389 if (bColorKey)
390 {
391 size_t uColorKey = 0;
392
393 for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
394 {
395 if (pColor[i].a < threshold)
396 uColorKey++;
397 }
398
399 if (NUM_PIXELS_PER_BLOCK == uColorKey)
400 {
401 pBC->rgb[0] = 0x0000;
402 pBC->rgb[1] = 0xffff;
403 pBC->bitmap = 0xffffffff;
404 return;
405 }
406
407 uSteps = (uColorKey > 0) ? 3u : 4u;
408 }
409 else
410 {
411 uSteps = 4u;
412 }
413
414 // Quantize block to R56B5, using Floyd Stienberg error diffusion. This
415 // increases the chance that colors will map directly to the quantized
416 // axis endpoints.
419
420 if (flags & BC_FLAGS_DITHER_RGB)
421 memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
422
423 size_t i;
424 for (i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
425 {
426 HDRColorA Clr;
427 Clr.r = pColor[i].r;
428 Clr.g = pColor[i].g;
429 Clr.b = pColor[i].b;
430 Clr.a = 1.0f;
431
432 if (flags & BC_FLAGS_DITHER_RGB)
433 {
434 Clr.r += Error[i].r;
435 Clr.g += Error[i].g;
436 Clr.b += Error[i].b;
437 }
438
439 Color[i].r = static_cast<float>(static_cast<int32_t>(Clr.r * 31.0f + 0.5f)) * (1.0f / 31.0f);
440 Color[i].g = static_cast<float>(static_cast<int32_t>(Clr.g * 63.0f + 0.5f)) * (1.0f / 63.0f);
441 Color[i].b = static_cast<float>(static_cast<int32_t>(Clr.b * 31.0f + 0.5f)) * (1.0f / 31.0f);
442
443#ifdef COLOR_WEIGHTS
444 Color[i].a = pColor[i].a;
445#else
446 Color[i].a = 1.0f;
447#endif // COLOR_WEIGHTS
448
449 if (flags & BC_FLAGS_DITHER_RGB)
450 {
451 HDRColorA Diff;
452 Diff.r = Color[i].a * (Clr.r - Color[i].r);
453 Diff.g = Color[i].a * (Clr.g - Color[i].g);
454 Diff.b = Color[i].a * (Clr.b - Color[i].b);
455 Diff.a = 0.0f;
456
457 if (3 != (i & 3))
458 {
459 assert(i < 15);
460 _Analysis_assume_(i < 15);
461 Error[i + 1].r += Diff.r * (7.0f / 16.0f);
462 Error[i + 1].g += Diff.g * (7.0f / 16.0f);
463 Error[i + 1].b += Diff.b * (7.0f / 16.0f);
464 }
465
466 if (i < 12)
467 {
468 if (i & 3)
469 {
470 Error[i + 3].r += Diff.r * (3.0f / 16.0f);
471 Error[i + 3].g += Diff.g * (3.0f / 16.0f);
472 Error[i + 3].b += Diff.b * (3.0f / 16.0f);
473 }
474
475 Error[i + 4].r += Diff.r * (5.0f / 16.0f);
476 Error[i + 4].g += Diff.g * (5.0f / 16.0f);
477 Error[i + 4].b += Diff.b * (5.0f / 16.0f);
478
479 if (3 != (i & 3))
480 {
481 assert(i < 11);
482 _Analysis_assume_(i < 11);
483 Error[i + 5].r += Diff.r * (1.0f / 16.0f);
484 Error[i + 5].g += Diff.g * (1.0f / 16.0f);
485 Error[i + 5].b += Diff.b * (1.0f / 16.0f);
486 }
487 }
488 }
489
490 if (!(flags & BC_FLAGS_UNIFORM))
491 {
492 Color[i].r *= g_Luminance.r;
493 Color[i].g *= g_Luminance.g;
494 Color[i].b *= g_Luminance.b;
495 }
496 }
497
498 // Perform 6D root finding function to find two endpoints of color axis.
499 // Then quantize and sort the endpoints depending on mode.
500 HDRColorA ColorA, ColorB, ColorC, ColorD;
501
502 OptimizeRGB(&ColorA, &ColorB, Color, uSteps, flags);
503
504 if (flags & BC_FLAGS_UNIFORM)
505 {
506 ColorC = ColorA;
507 ColorD = ColorB;
508 }
509 else
510 {
511 ColorC.r = ColorA.r * g_LuminanceInv.r;
512 ColorC.g = ColorA.g * g_LuminanceInv.g;
513 ColorC.b = ColorA.b * g_LuminanceInv.b;
514 ColorC.a = ColorA.a;
515
516 ColorD.r = ColorB.r * g_LuminanceInv.r;
517 ColorD.g = ColorB.g * g_LuminanceInv.g;
518 ColorD.b = ColorB.b * g_LuminanceInv.b;
519 ColorD.a = ColorB.a;
520 }
521
522 const uint16_t wColorA = Encode565(&ColorC);
523 const uint16_t wColorB = Encode565(&ColorD);
524
525 if ((uSteps == 4) && (wColorA == wColorB))
526 {
527 pBC->rgb[0] = wColorA;
528 pBC->rgb[1] = wColorB;
529 pBC->bitmap = 0x00000000;
530 return;
531 }
532
533 Decode565(&ColorC, wColorA);
534 Decode565(&ColorD, wColorB);
535
536 if (flags & BC_FLAGS_UNIFORM)
537 {
538 ColorA = ColorC;
539 ColorB = ColorD;
540 }
541 else
542 {
543 ColorA.r = ColorC.r * g_Luminance.r;
544 ColorA.g = ColorC.g * g_Luminance.g;
545 ColorA.b = ColorC.b * g_Luminance.b;
546
547 ColorB.r = ColorD.r * g_Luminance.r;
548 ColorB.g = ColorD.g * g_Luminance.g;
549 ColorB.b = ColorD.b * g_Luminance.b;
550 }
551
552 // Calculate color steps
553 HDRColorA Step[4];
554
555 if ((3 == uSteps) == (wColorA <= wColorB))
556 {
557 pBC->rgb[0] = wColorA;
558 pBC->rgb[1] = wColorB;
559
560 Step[0] = ColorA;
561 Step[1] = ColorB;
562 }
563 else
564 {
565 pBC->rgb[0] = wColorB;
566 pBC->rgb[1] = wColorA;
567
568 Step[0] = ColorB;
569 Step[1] = ColorA;
570 }
571
572 static const size_t pSteps3[] = { 0, 2, 1 };
573 static const size_t pSteps4[] = { 0, 2, 3, 1 };
574 const size_t* pSteps;
575
576 if (3 == uSteps)
577 {
578 pSteps = pSteps3;
579
580 HDRColorALerp(&Step[2], &Step[0], &Step[1], 0.5f);
581 }
582 else
583 {
584 pSteps = pSteps4;
585
586 HDRColorALerp(&Step[2], &Step[0], &Step[1], 1.0f / 3.0f);
587 HDRColorALerp(&Step[3], &Step[0], &Step[1], 2.0f / 3.0f);
588 }
589
590 // Calculate color direction
591 HDRColorA Dir;
592 Dir.r = Step[1].r - Step[0].r;
593 Dir.g = Step[1].g - Step[0].g;
594 Dir.b = Step[1].b - Step[0].b;
595 Dir.a = 0.0f;
596
597 const auto fSteps = static_cast<float>(uSteps - 1);
598 const float fScale = (wColorA != wColorB) ? (fSteps / (Dir.r * Dir.r + Dir.g * Dir.g + Dir.b * Dir.b)) : 0.0f;
599
600 Dir.r *= fScale;
601 Dir.g *= fScale;
602 Dir.b *= fScale;
603
604 // Encode colors
605 uint32_t dw = 0;
606 if (flags & BC_FLAGS_DITHER_RGB)
607 memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
608
609 for (i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
610 {
611 if ((3 == uSteps) && (pColor[i].a < threshold))
612 {
613 dw = (3u << 30) | (dw >> 2);
614 }
615 else
616 {
617 HDRColorA Clr;
618 if (flags & BC_FLAGS_UNIFORM)
619 {
620 Clr.r = pColor[i].r;
621 Clr.g = pColor[i].g;
622 Clr.b = pColor[i].b;
623 }
624 else
625 {
626 Clr.r = pColor[i].r * g_Luminance.r;
627 Clr.g = pColor[i].g * g_Luminance.g;
628 Clr.b = pColor[i].b * g_Luminance.b;
629 }
630 Clr.a = 1.0f;
631
632 if (flags & BC_FLAGS_DITHER_RGB)
633 {
634 Clr.r += Error[i].r;
635 Clr.g += Error[i].g;
636 Clr.b += Error[i].b;
637 }
638
639 const float fDot = (Clr.r - Step[0].r) * Dir.r + (Clr.g - Step[0].g) * Dir.g + (Clr.b - Step[0].b) * Dir.b;
640
641 uint32_t iStep;
642 if (fDot <= 0.0f)
643 iStep = 0;
644 else if (fDot >= fSteps)
645 iStep = 1;
646 else
647 iStep = uint32_t(pSteps[uint32_t(fDot + 0.5f)]);
648
649 dw = (iStep << 30) | (dw >> 2);
650
651 if (flags & BC_FLAGS_DITHER_RGB)
652 {
653 HDRColorA Diff;
654 Diff.r = Color[i].a * (Clr.r - Step[iStep].r);
655 Diff.g = Color[i].a * (Clr.g - Step[iStep].g);
656 Diff.b = Color[i].a * (Clr.b - Step[iStep].b);
657 Diff.a = 0.0f;
658
659 if (3 != (i & 3))
660 {
661 Error[i + 1].r += Diff.r * (7.0f / 16.0f);
662 Error[i + 1].g += Diff.g * (7.0f / 16.0f);
663 Error[i + 1].b += Diff.b * (7.0f / 16.0f);
664 }
665
666 if (i < 12)
667 {
668 if (i & 3)
669 {
670 Error[i + 3].r += Diff.r * (3.0f / 16.0f);
671 Error[i + 3].g += Diff.g * (3.0f / 16.0f);
672 Error[i + 3].b += Diff.b * (3.0f / 16.0f);
673 }
674
675 Error[i + 4].r += Diff.r * (5.0f / 16.0f);
676 Error[i + 4].g += Diff.g * (5.0f / 16.0f);
677 Error[i + 4].b += Diff.b * (5.0f / 16.0f);
678
679 if (3 != (i & 3))
680 {
681 Error[i + 5].r += Diff.r * (1.0f / 16.0f);
682 Error[i + 5].g += Diff.g * (1.0f / 16.0f);
683 Error[i + 5].b += Diff.b * (1.0f / 16.0f);
684 }
685 }
686 }
687 }
688 }
689
690 pBC->bitmap = dw;
691 }
692
693 //-------------------------------------------------------------------------------------
694#ifdef COLOR_WEIGHTS
695 void EncodeSolidBC1(_Out_ D3DX_BC1* pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA* pColor)
696 {
697#ifdef COLOR_AVG_0WEIGHTS
698 // Compute avg color
700 Color.r = pColor[0].r;
701 Color.g = pColor[0].g;
702 Color.b = pColor[0].b;
703
704 for (size_t i = 1; i < NUM_PIXELS_PER_BLOCK; ++i)
705 {
706 Color.r += pColor[i].r;
707 Color.g += pColor[i].g;
708 Color.b += pColor[i].b;
709 }
710
711 Color.r *= 1.0f / 16.0f;
712 Color.g *= 1.0f / 16.0f;
713 Color.b *= 1.0f / 16.0f;
714
715 const uint16_t wColor = Encode565(&Color);
716#else
717 const uint16_t wColor = 0x0000;
718#endif // COLOR_AVG_0WEIGHTS
719
720 // Encode solid block
721 pBC->rgb[0] = wColor;
722 pBC->rgb[1] = wColor;
723 pBC->bitmap = 0x00000000;
724 }
725#endif // COLOR_WEIGHTS
726} // namespace
727
728//=====================================================================================
729// Entry points
730//=====================================================================================
731
732//-------------------------------------------------------------------------------------
733// BC1 Compression
734//-------------------------------------------------------------------------------------
735_Use_decl_annotations_ void DirectX::D3DXDecodeBC1(XMVECTOR* pColor, const uint8_t* pBC) noexcept
736{
737 auto pBC1 = reinterpret_cast<const D3DX_BC1*>(pBC);
738 DecodeBC1(pColor, pBC1, true);
739}
740
741_Use_decl_annotations_ void DirectX::D3DXEncodeBC1(uint8_t* pBC, const XMVECTOR* pColor, float threshold, uint32_t flags) noexcept
742{
743 assert(pBC && pColor);
744
746
747 if (flags & BC_FLAGS_DITHER_A)
748 {
749 float fError[NUM_PIXELS_PER_BLOCK] = {};
750
751 for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
752 {
753 HDRColorA clr;
754 XMStoreFloat4(reinterpret_cast<XMFLOAT4*>(&clr), pColor[i]);
755
756 const float fAlph = clr.a + fError[i];
757
758 Color[i].r = clr.r;
759 Color[i].g = clr.g;
760 Color[i].b = clr.b;
761 Color[i].a = static_cast<float>(static_cast<int32_t>(clr.a + fError[i] + 0.5f));
762
763 const float fDiff = fAlph - Color[i].a;
764
765 if (3 != (i & 3))
766 {
767 assert(i < 15);
768 _Analysis_assume_(i < 15);
769 fError[i + 1] += fDiff * (7.0f / 16.0f);
770 }
771
772 if (i < 12)
773 {
774 if (i & 3)
775 fError[i + 3] += fDiff * (3.0f / 16.0f);
776
777 fError[i + 4] += fDiff * (5.0f / 16.0f);
778
779 if (3 != (i & 3))
780 {
781 assert(i < 11);
782 _Analysis_assume_(i < 11);
783 fError[i + 5] += fDiff * (1.0f / 16.0f);
784 }
785 }
786 }
787 }
788 else
789 {
790 for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
791 {
792 XMStoreFloat4(reinterpret_cast<XMFLOAT4*>(&Color[i]), pColor[i]);
793 }
794 }
795
796 auto pBC1 = reinterpret_cast<D3DX_BC1*>(pBC);
797 EncodeBC1(pBC1, Color, true, threshold, flags);
798}
799
800//-------------------------------------------------------------------------------------
801// BC2 Compression
802//-------------------------------------------------------------------------------------
803_Use_decl_annotations_ void DirectX::D3DXDecodeBC2(XMVECTOR* pColor, const uint8_t* pBC) noexcept
804{
805 assert(pColor && pBC);
806 static_assert(sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes");
807
808 auto pBC2 = reinterpret_cast<const D3DX_BC2*>(pBC);
809
810 // RGB part
811 DecodeBC1(pColor, &pBC2->bc1, false);
812
813 // 4-bit alpha part
814 uint32_t dw = pBC2->bitmap[0];
815
816 for (size_t i = 0; i < 8; ++i, dw >>= 4)
817 {
818#pragma prefast(suppress : 22103, "writing blocks in two halves confuses tool")
819 pColor[i] = XMVectorSetW(pColor[i], static_cast<float>(dw & 0xf) * (1.0f / 15.0f));
820 }
821
822 dw = pBC2->bitmap[1];
823
824 for (size_t i = 8; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 4)
825 pColor[i] = XMVectorSetW(pColor[i], static_cast<float>(dw & 0xf) * (1.0f / 15.0f));
826}
827
828_Use_decl_annotations_ void DirectX::D3DXEncodeBC2(uint8_t* pBC, const XMVECTOR* pColor, uint32_t flags) noexcept
829{
830 assert(pBC && pColor);
831 static_assert(sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes");
832
834 for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
835 {
836 XMStoreFloat4(reinterpret_cast<XMFLOAT4*>(&Color[i]), pColor[i]);
837 }
838
839 auto pBC2 = reinterpret_cast<D3DX_BC2*>(pBC);
840
841 // 4-bit alpha part. Dithered using Floyd Stienberg error diffusion.
842 pBC2->bitmap[0] = 0;
843 pBC2->bitmap[1] = 0;
844
845 float fError[NUM_PIXELS_PER_BLOCK] = {};
846 for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
847 {
848 float fAlph = Color[i].a;
849 if (flags & BC_FLAGS_DITHER_A)
850 fAlph += fError[i];
851
852 const auto u = static_cast<uint32_t>(fAlph * 15.0f + 0.5f);
853
854 pBC2->bitmap[i >> 3] >>= 4;
855 pBC2->bitmap[i >> 3] |= (u << 28);
856
857 if (flags & BC_FLAGS_DITHER_A)
858 {
859 const float fDiff = fAlph - float(u) * (1.0f / 15.0f);
860
861 if (3 != (i & 3))
862 {
863 assert(i < 15);
864 _Analysis_assume_(i < 15);
865 fError[i + 1] += fDiff * (7.0f / 16.0f);
866 }
867
868 if (i < 12)
869 {
870 if (i & 3)
871 fError[i + 3] += fDiff * (3.0f / 16.0f);
872
873 fError[i + 4] += fDiff * (5.0f / 16.0f);
874
875 if (3 != (i & 3))
876 {
877 assert(i < 11);
878 _Analysis_assume_(i < 11);
879 fError[i + 5] += fDiff * (1.0f / 16.0f);
880 }
881 }
882 }
883 }
884
885 // RGB part
886#ifdef COLOR_WEIGHTS
887 if (!pBC2->bitmap[0] && !pBC2->bitmap[1])
888 {
889 EncodeSolidBC1(pBC2->dxt1, Color);
890 return;
891 }
892#endif // COLOR_WEIGHTS
893
894 EncodeBC1(&pBC2->bc1, Color, false, 0.f, flags);
895}
896
897//-------------------------------------------------------------------------------------
898// BC3 Compression
899//-------------------------------------------------------------------------------------
900_Use_decl_annotations_ void DirectX::D3DXDecodeBC3(XMVECTOR* pColor, const uint8_t* pBC) noexcept
901{
902 assert(pColor && pBC);
903 static_assert(sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes");
904
905 auto pBC3 = reinterpret_cast<const D3DX_BC3*>(pBC);
906
907 // RGB part
908 DecodeBC1(pColor, &pBC3->bc1, false);
909
910 // Adaptive 3-bit alpha part
911 float fAlpha[8];
912
913 fAlpha[0] = static_cast<float>(pBC3->alpha[0]) * (1.0f / 255.0f);
914 fAlpha[1] = static_cast<float>(pBC3->alpha[1]) * (1.0f / 255.0f);
915
916 if (pBC3->alpha[0] > pBC3->alpha[1])
917 {
918 for (size_t i = 1; i < 7; ++i)
919 fAlpha[i + 1] = (fAlpha[0] * float(7u - i) + fAlpha[1] * float(i)) * (1.0f / 7.0f);
920 }
921 else
922 {
923 for (size_t i = 1; i < 5; ++i)
924 fAlpha[i + 1] = (fAlpha[0] * float(5u - i) + fAlpha[1] * float(i)) * (1.0f / 5.0f);
925
926 fAlpha[6] = 0.0f;
927 fAlpha[7] = 1.0f;
928 }
929
930 uint32_t dw = uint32_t(pBC3->bitmap[0]) | uint32_t(pBC3->bitmap[1] << 8) | uint32_t(pBC3->bitmap[2] << 16);
931
932 for (size_t i = 0; i < 8; ++i, dw >>= 3)
933 pColor[i] = XMVectorSetW(pColor[i], fAlpha[dw & 0x7]);
934
935 dw = uint32_t(pBC3->bitmap[3]) | uint32_t(pBC3->bitmap[4] << 8) | uint32_t(pBC3->bitmap[5] << 16);
936
937 for (size_t i = 8; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 3)
938 pColor[i] = XMVectorSetW(pColor[i], fAlpha[dw & 0x7]);
939}
940
941_Use_decl_annotations_ void DirectX::D3DXEncodeBC3(uint8_t* pBC, const XMVECTOR* pColor, uint32_t flags) noexcept
942{
943 assert(pBC && pColor);
944 static_assert(sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes");
945
947 for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
948 {
949 XMStoreFloat4(reinterpret_cast<XMFLOAT4*>(&Color[i]), pColor[i]);
950 }
951
952 auto pBC3 = reinterpret_cast<D3DX_BC3*>(pBC);
953
954 // Quantize block to A8, using Floyd Stienberg error diffusion. This
955 // increases the chance that colors will map directly to the quantized
956 // axis endpoints.
957 float fAlpha[NUM_PIXELS_PER_BLOCK] = {};
958 float fError[NUM_PIXELS_PER_BLOCK] = {};
959
960 float fMinAlpha = Color[0].a;
961 float fMaxAlpha = Color[0].a;
962
963 for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
964 {
965 float fAlph = Color[i].a;
966 if (flags & BC_FLAGS_DITHER_A)
967 fAlph += fError[i];
968
969 fAlpha[i] = static_cast<float>(static_cast<int32_t>(fAlph * 255.0f + 0.5f)) * (1.0f / 255.0f);
970
971 if (fAlpha[i] < fMinAlpha)
972 fMinAlpha = fAlpha[i];
973 else if (fAlpha[i] > fMaxAlpha)
974 fMaxAlpha = fAlpha[i];
975
976 if (flags & BC_FLAGS_DITHER_A)
977 {
978 const float fDiff = fAlph - fAlpha[i];
979
980 if (3 != (i & 3))
981 {
982 assert(i < 15);
983 _Analysis_assume_(i < 15);
984 fError[i + 1] += fDiff * (7.0f / 16.0f);
985 }
986
987 if (i < 12)
988 {
989 if (i & 3)
990 fError[i + 3] += fDiff * (3.0f / 16.0f);
991
992 fError[i + 4] += fDiff * (5.0f / 16.0f);
993
994 if (3 != (i & 3))
995 {
996 assert(i < 11);
997 _Analysis_assume_(i < 11);
998 fError[i + 5] += fDiff * (1.0f / 16.0f);
999 }
1000 }
1001 }
1002 }
1003
1004#ifdef COLOR_WEIGHTS
1005 if (0.0f == fMaxAlpha)
1006 {
1007 EncodeSolidBC1(&pBC3->dxt1, Color);
1008 pBC3->alpha[0] = 0x00;
1009 pBC3->alpha[1] = 0x00;
1010 memset(pBC3->bitmap, 0x00, 6);
1011 }
1012#endif
1013
1014 // RGB part
1015 EncodeBC1(&pBC3->bc1, Color, false, 0.f, flags);
1016
1017 // Alpha part
1018 if (1.0f == fMinAlpha)
1019 {
1020 pBC3->alpha[0] = 0xff;
1021 pBC3->alpha[1] = 0xff;
1022 memset(pBC3->bitmap, 0x00, 6);
1023 return;
1024 }
1025
1026 // Optimize and Quantize Min and Max values
1027 const uint32_t uSteps = ((0.0f == fMinAlpha) || (1.0f == fMaxAlpha)) ? 6u : 8u;
1028
1029 float fAlphaA, fAlphaB;
1030 OptimizeAlpha<false>(&fAlphaA, &fAlphaB, fAlpha, uSteps);
1031
1032 const auto bAlphaA = static_cast<uint8_t>(static_cast<int32_t>(fAlphaA * 255.0f + 0.5f));
1033 const auto bAlphaB = static_cast<uint8_t>(static_cast<int32_t>(fAlphaB * 255.0f + 0.5f));
1034
1035 fAlphaA = static_cast<float>(bAlphaA) * (1.0f / 255.0f);
1036 fAlphaB = static_cast<float>(bAlphaB) * (1.0f / 255.0f);
1037
1038 // Setup block
1039 if ((8 == uSteps) && (bAlphaA == bAlphaB))
1040 {
1041 pBC3->alpha[0] = bAlphaA;
1042 pBC3->alpha[1] = bAlphaB;
1043 memset(pBC3->bitmap, 0x00, 6);
1044 return;
1045 }
1046
1047 static const size_t pSteps6[] = { 0, 2, 3, 4, 5, 1 };
1048 static const size_t pSteps8[] = { 0, 2, 3, 4, 5, 6, 7, 1 };
1049
1050 const size_t* pSteps;
1051 float fStep[8] = {};
1052
1053 if (6 == uSteps)
1054 {
1055 pBC3->alpha[0] = bAlphaA;
1056 pBC3->alpha[1] = bAlphaB;
1057
1058 fStep[0] = fAlphaA;
1059 fStep[1] = fAlphaB;
1060
1061 for (size_t i = 1; i < 5; ++i)
1062 fStep[i + 1] = (fStep[0] * float(5u - i) + fStep[1] * float(i)) * (1.0f / 5.0f);
1063
1064 fStep[6] = 0.0f;
1065 fStep[7] = 1.0f;
1066
1067 pSteps = pSteps6;
1068 }
1069 else
1070 {
1071 pBC3->alpha[0] = bAlphaB;
1072 pBC3->alpha[1] = bAlphaA;
1073
1074 fStep[0] = fAlphaB;
1075 fStep[1] = fAlphaA;
1076
1077 for (size_t i = 1; i < 7; ++i)
1078 fStep[i + 1] = (fStep[0] * float(7u - i) + fStep[1] * float(i)) * (1.0f / 7.0f);
1079
1080 pSteps = pSteps8;
1081 }
1082
1083 // Encode alpha bitmap
1084 const auto fSteps = static_cast<float>(uSteps - 1);
1085 const float fScale = (fStep[0] != fStep[1]) ? (fSteps / (fStep[1] - fStep[0])) : 0.0f;
1086
1087 if (flags & BC_FLAGS_DITHER_A)
1088 memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
1089
1090 for (size_t iSet = 0; iSet < 2; iSet++)
1091 {
1092 uint32_t dw = 0;
1093
1094 const size_t iMin = iSet * 8;
1095 const size_t iLim = iMin + 8;
1096
1097 for (size_t i = iMin; i < iLim; ++i)
1098 {
1099 float fAlph = Color[i].a;
1100 if (flags & BC_FLAGS_DITHER_A)
1101 fAlph += fError[i];
1102 const float fDot = (fAlph - fStep[0]) * fScale;
1103
1104 uint32_t iStep;
1105 if (fDot <= 0.0f)
1106 iStep = ((6 == uSteps) && (fAlph <= fStep[0] * 0.5f)) ? 6u : 0u;
1107 else if (fDot >= fSteps)
1108 iStep = ((6 == uSteps) && (fAlph >= (fStep[1] + 1.0f) * 0.5f)) ? 7u : 1u;
1109 else
1110 iStep = uint32_t(pSteps[uint32_t(fDot + 0.5f)]);
1111
1112 dw = (iStep << 21) | (dw >> 3);
1113
1114 if (flags & BC_FLAGS_DITHER_A)
1115 {
1116 const float fDiff = (fAlph - fStep[iStep]);
1117
1118 if (3 != (i & 3))
1119 fError[i + 1] += fDiff * (7.0f / 16.0f);
1120
1121 if (i < 12)
1122 {
1123 if (i & 3)
1124 fError[i + 3] += fDiff * (3.0f / 16.0f);
1125
1126 fError[i + 4] += fDiff * (5.0f / 16.0f);
1127
1128 if (3 != (i & 3))
1129 fError[i + 5] += fDiff * (1.0f / 16.0f);
1130 }
1131 }
1132 }
1133
1134 pBC3->bitmap[0 + iSet * 3] = reinterpret_cast<uint8_t*>(&dw)[0];
1135 pBC3->bitmap[1 + iSet * 3] = reinterpret_cast<uint8_t*>(&dw)[1];
1136 pBC3->bitmap[2 + iSet * 3] = reinterpret_cast<uint8_t*>(&dw)[2];
1137 }
1138}
#define NUM_PIXELS_PER_BLOCK
Definition BC.h:24
Definition DDS.h:24
void D3DXEncodeBC2(_Out_writes_(16) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void D3DXDecodeBC2(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(16) const uint8_t *pBC) noexcept
void D3DXDecodeBC1(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(8) const uint8_t *pBC) noexcept
void D3DXDecodeBC3(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(16) const uint8_t *pBC) noexcept
@ BC_FLAGS_DITHER_A
Definition BC.h:37
@ BC_FLAGS_UNIFORM
Definition BC.h:40
@ BC_FLAGS_DITHER_RGB
Definition BC.h:34
void D3DXEncodeBC3(_Out_writes_(16) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void OptimizeAlpha(float *pX, float *pY, const float *pPoints, uint32_t cSteps) noexcept
Definition BC.h:181
HDRColorA * HDRColorALerp(_Out_ HDRColorA *pOut, _In_ const HDRColorA *pC1, _In_ const HDRColorA *pC2, _In_ float s) noexcept
Definition BC.h:142
void D3DXEncodeBC1(_Out_writes_(8) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ float threshold, _In_ uint32_t flags) noexcept
uint32_t Color(Echo echo)
uint32_t bitmap[2]
Definition BC.h:162