Rise
The Vieneo Province
Loading...
Searching...
No Matches
BC.h
Go to the documentation of this file.
1//-------------------------------------------------------------------------------------
2// BC.h
3//
4// Block-compression (BC) functionality
5//
6// Copyright (c) Microsoft Corporation.
7// Licensed under the MIT License.
8//
9// https://go.microsoft.com/fwlink/?LinkId=248926
10//-------------------------------------------------------------------------------------
11
12#pragma once
13
14#include <DirectXMath.h>
15#include <DirectXPackedVector.h>
16
17namespace DirectX
18{
19//-------------------------------------------------------------------------------------
20// Macros
21//-------------------------------------------------------------------------------------
22
23// Because these are used in SAL annotations, they need to remain macros rather than const values
24#define NUM_PIXELS_PER_BLOCK 16
25
26 //-------------------------------------------------------------------------------------
27 // Constants
28 //-------------------------------------------------------------------------------------
29
30 enum BC_FLAGS : uint32_t
31 {
33
35 // Enables dithering for RGB colors for BC1-3
36
38 // Enables dithering for Alpha channel for BC1-3
39
41 // By default, uses perceptual weighting for BC1-3; this flag makes it a uniform weighting
42
44 // By default, BC7 skips mode 0 & 2; this flag adds those modes back
45
47 // BC7 should only use mode 6; skip other modes
48 };
49
50 //-------------------------------------------------------------------------------------
51 // Structures
52 //-------------------------------------------------------------------------------------
53 class LDRColorA;
54
56 {
57 public:
58 float r, g, b, a;
59
60 public:
61 HDRColorA() = default;
62 HDRColorA(float _r, float _g, float _b, float _a) noexcept
63 : r(_r),
64 g(_g),
65 b(_b),
66 a(_a)
67 {}
68
69 HDRColorA(HDRColorA const&) = default;
70 HDRColorA& operator=(const HDRColorA&) = default;
71
72 HDRColorA(HDRColorA&&) = default;
74
75 // binary operators
76 HDRColorA operator+(const HDRColorA& c) const noexcept { return HDRColorA(r + c.r, g + c.g, b + c.b, a + c.a); }
77
78 HDRColorA operator-(const HDRColorA& c) const noexcept { return HDRColorA(r - c.r, g - c.g, b - c.b, a - c.a); }
79
80 HDRColorA operator*(float f) const noexcept { return HDRColorA(r * f, g * f, b * f, a * f); }
81
82 HDRColorA operator/(float f) const noexcept
83 {
84 const float fInv = 1.0f / f;
85 return HDRColorA(r * fInv, g * fInv, b * fInv, a * fInv);
86 }
87
88 float operator*(const HDRColorA& c) const noexcept { return r * c.r + g * c.g + b * c.b + a * c.a; }
89
90 // assignment operators
91 HDRColorA& operator+=(const HDRColorA& c) noexcept
92 {
93 r += c.r;
94 g += c.g;
95 b += c.b;
96 a += c.a;
97 return *this;
98 }
99
100 HDRColorA& operator-=(const HDRColorA& c) noexcept
101 {
102 r -= c.r;
103 g -= c.g;
104 b -= c.b;
105 a -= c.a;
106 return *this;
107 }
108
109 HDRColorA& operator*=(float f) noexcept
110 {
111 r *= f;
112 g *= f;
113 b *= f;
114 a *= f;
115 return *this;
116 }
117
118 HDRColorA& operator/=(float f) noexcept
119 {
120 const float fInv = 1.0f / f;
121 r *= fInv;
122 g *= fInv;
123 b *= fInv;
124 a *= fInv;
125 return *this;
126 }
127
128 HDRColorA& Clamp(_In_ float fMin, _In_ float fMax) noexcept
129 {
130 r = std::min<float>(fMax, std::max<float>(fMin, r));
131 g = std::min<float>(fMax, std::max<float>(fMin, g));
132 b = std::min<float>(fMax, std::max<float>(fMin, b));
133 a = std::min<float>(fMax, std::max<float>(fMin, a));
134 return *this;
135 }
136
137 HDRColorA(const LDRColorA& c) noexcept;
138 HDRColorA& operator=(const LDRColorA& c) noexcept;
139 LDRColorA ToLDRColorA() const noexcept;
140 };
141
142 inline HDRColorA* HDRColorALerp(_Out_ HDRColorA* pOut, _In_ const HDRColorA* pC1, _In_ const HDRColorA* pC2, _In_ float s) noexcept
143 {
144 pOut->r = pC1->r + s * (pC2->r - pC1->r);
145 pOut->g = pC1->g + s * (pC2->g - pC1->g);
146 pOut->b = pC1->b + s * (pC2->b - pC1->b);
147 pOut->a = pC1->a + s * (pC2->a - pC1->a);
148 return pOut;
149 }
150
151#pragma pack(push, 1)
152 // BC1/DXT1 compression (4 bits per texel)
153 struct D3DX_BC1
154 {
155 uint16_t rgb[2]; // 565 colors
156 uint32_t bitmap; // 2bpp rgb bitmap
157 };
158
159 // BC2/DXT2/3 compression (8 bits per texel)
160 struct D3DX_BC2
161 {
162 uint32_t bitmap[2]; // 4bpp alpha bitmap
163 D3DX_BC1 bc1; // BC1 rgb data
164 };
165
166 // BC3/DXT4/5 compression (8 bits per texel)
167 struct D3DX_BC3
168 {
169 uint8_t alpha[2]; // alpha values
170 uint8_t bitmap[6]; // 3bpp alpha bitmap
171 D3DX_BC1 bc1; // BC1 rgb data
172 };
173#pragma pack(pop)
174
175//-------------------------------------------------------------------------------------
176// Templates
177//-------------------------------------------------------------------------------------
178#pragma warning(push)
179#pragma warning(disable : 4127)
180 template<bool bRange>
181 void OptimizeAlpha(float* pX, float* pY, const float* pPoints, uint32_t cSteps) noexcept
182 {
183 static const float pC6[] = { 5.0f / 5.0f, 4.0f / 5.0f, 3.0f / 5.0f, 2.0f / 5.0f, 1.0f / 5.0f, 0.0f / 5.0f };
184 static const float pD6[] = { 0.0f / 5.0f, 1.0f / 5.0f, 2.0f / 5.0f, 3.0f / 5.0f, 4.0f / 5.0f, 5.0f / 5.0f };
185 static const float pC8[]
186 = { 7.0f / 7.0f, 6.0f / 7.0f, 5.0f / 7.0f, 4.0f / 7.0f, 3.0f / 7.0f, 2.0f / 7.0f, 1.0f / 7.0f, 0.0f / 7.0f };
187 static const float pD8[]
188 = { 0.0f / 7.0f, 1.0f / 7.0f, 2.0f / 7.0f, 3.0f / 7.0f, 4.0f / 7.0f, 5.0f / 7.0f, 6.0f / 7.0f, 7.0f / 7.0f };
189
190 const float* pC = (6 == cSteps) ? pC6 : pC8;
191 const float* pD = (6 == cSteps) ? pD6 : pD8;
192
193 constexpr float MAX_VALUE = 1.0f;
194 constexpr float MIN_VALUE = (bRange) ? -1.0f : 0.0f;
195
196 // Find Min and Max points, as starting point
197 float fX = MAX_VALUE;
198 float fY = MIN_VALUE;
199
200 if (8 == cSteps)
201 {
202 for (size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
203 {
204 if (pPoints[iPoint] < fX)
205 fX = pPoints[iPoint];
206
207 if (pPoints[iPoint] > fY)
208 fY = pPoints[iPoint];
209 }
210 }
211 else
212 {
213 for (size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
214 {
215 if (pPoints[iPoint] < fX && pPoints[iPoint] > MIN_VALUE)
216 fX = pPoints[iPoint];
217
218 if (pPoints[iPoint] > fY && pPoints[iPoint] < MAX_VALUE)
219 fY = pPoints[iPoint];
220 }
221
222 if (fX == fY)
223 {
224 fY = MAX_VALUE;
225 }
226 }
227
228 // Use Newton's Method to find local minima of sum-of-squares error.
229 const auto fSteps = static_cast<float>(cSteps - 1);
230
231 for (size_t iIteration = 0; iIteration < 8; iIteration++)
232 {
233 if ((fY - fX) < (1.0f / 256.0f))
234 break;
235
236 float const fScale = fSteps / (fY - fX);
237
238 // Calculate new steps
239 float pSteps[8];
240
241 for (size_t iStep = 0; iStep < cSteps; iStep++)
242 pSteps[iStep] = pC[iStep] * fX + pD[iStep] * fY;
243
244 if (6 == cSteps)
245 {
246 pSteps[6] = MIN_VALUE;
247 pSteps[7] = MAX_VALUE;
248 }
249
250 // Evaluate function, and derivatives
251 float dX = 0.0f;
252 float dY = 0.0f;
253 float d2X = 0.0f;
254 float d2Y = 0.0f;
255
256 for (size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
257 {
258 const float fDot = (pPoints[iPoint] - fX) * fScale;
259
260 uint32_t iStep;
261 if (fDot <= 0.0f)
262 {
263 // D3DX10 / D3DX11 didn't take into account the proper minimum value for the bRange (BC4S/BC5S) case
264 iStep = ((6 == cSteps) && (pPoints[iPoint] <= (fX + MIN_VALUE) * 0.5f)) ? 6u : 0u;
265 }
266 else if (fDot >= fSteps)
267 {
268 iStep = ((6 == cSteps) && (pPoints[iPoint] >= (fY + MAX_VALUE) * 0.5f)) ? 7u : (cSteps - 1);
269 }
270 else
271 {
272 iStep = uint32_t(fDot + 0.5f);
273 }
274
275 if (iStep < cSteps)
276 {
277 // D3DX had this computation backwards (pPoints[iPoint] - pSteps[iStep])
278 // this fix improves RMS of the alpha component
279 const float fDiff = pSteps[iStep] - pPoints[iPoint];
280
281 dX += pC[iStep] * fDiff;
282 d2X += pC[iStep] * pC[iStep];
283
284 dY += pD[iStep] * fDiff;
285 d2Y += pD[iStep] * pD[iStep];
286 }
287 }
288
289 // Move endpoints
290 if (d2X > 0.0f)
291 fX -= dX / d2X;
292
293 if (d2Y > 0.0f)
294 fY -= dY / d2Y;
295
296 if (fX > fY)
297 {
298 const float f = fX;
299 fX = fY;
300 fY = f;
301 }
302
303 if ((dX * dX < (1.0f / 64.0f)) && (dY * dY < (1.0f / 64.0f)))
304 break;
305 }
306
307 *pX = (fX < MIN_VALUE) ? MIN_VALUE : (fX > MAX_VALUE) ? MAX_VALUE : fX;
308 *pY = (fY < MIN_VALUE) ? MIN_VALUE : (fY > MAX_VALUE) ? MAX_VALUE : fY;
309 }
310#pragma warning(pop)
311
312 //-------------------------------------------------------------------------------------
313 // Functions
314 //-------------------------------------------------------------------------------------
315
316 typedef void (*BC_DECODE)(XMVECTOR* pColor, const uint8_t* pBC);
317 typedef void (*BC_ENCODE)(uint8_t* pDXT, const XMVECTOR* pColor, uint32_t flags);
318
319 void D3DXDecodeBC1(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_reads_(8) const uint8_t* pBC) noexcept;
320 void D3DXDecodeBC2(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_reads_(16) const uint8_t* pBC) noexcept;
321 void D3DXDecodeBC3(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_reads_(16) const uint8_t* pBC) noexcept;
322 void D3DXDecodeBC4U(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_reads_(8) const uint8_t* pBC) noexcept;
323 void D3DXDecodeBC4S(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_reads_(8) const uint8_t* pBC) noexcept;
324 void D3DXDecodeBC5U(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_reads_(16) const uint8_t* pBC) noexcept;
325 void D3DXDecodeBC5S(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_reads_(16) const uint8_t* pBC) noexcept;
326 void D3DXDecodeBC6HU(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_reads_(16) const uint8_t* pBC) noexcept;
327 void D3DXDecodeBC6HS(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_reads_(16) const uint8_t* pBC) noexcept;
328 void D3DXDecodeBC7(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR* pColor, _In_reads_(16) const uint8_t* pBC) noexcept;
329
330 void D3DXEncodeBC1(_Out_writes_(8) uint8_t* pBC,
331 _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR* pColor,
332 _In_ float threshold,
333 _In_ uint32_t flags) noexcept;
334 // BC1 requires one additional parameter, so it doesn't match signature of BC_ENCODE above
335
336 void
337 D3DXEncodeBC2(_Out_writes_(16) uint8_t* pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR* pColor, _In_ uint32_t flags) noexcept;
338 void
339 D3DXEncodeBC3(_Out_writes_(16) uint8_t* pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR* pColor, _In_ uint32_t flags) noexcept;
340 void
341 D3DXEncodeBC4U(_Out_writes_(8) uint8_t* pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR* pColor, _In_ uint32_t flags) noexcept;
342 void
343 D3DXEncodeBC4S(_Out_writes_(8) uint8_t* pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR* pColor, _In_ uint32_t flags) noexcept;
344 void
345 D3DXEncodeBC5U(_Out_writes_(16) uint8_t* pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR* pColor, _In_ uint32_t flags) noexcept;
346 void
347 D3DXEncodeBC5S(_Out_writes_(16) uint8_t* pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR* pColor, _In_ uint32_t flags) noexcept;
348 void
349 D3DXEncodeBC6HU(_Out_writes_(16) uint8_t* pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR* pColor, _In_ uint32_t flags) noexcept;
350 void
351 D3DXEncodeBC6HS(_Out_writes_(16) uint8_t* pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR* pColor, _In_ uint32_t flags) noexcept;
352 void
353 D3DXEncodeBC7(_Out_writes_(16) uint8_t* pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR* pColor, _In_ uint32_t flags) noexcept;
354
355} // namespace DirectX
#define NUM_PIXELS_PER_BLOCK
Definition BC.h:24
HDRColorA operator-(const HDRColorA &c) const noexcept
Definition BC.h:78
HDRColorA operator*(float f) const noexcept
Definition BC.h:80
HDRColorA operator/(float f) const noexcept
Definition BC.h:82
HDRColorA & operator*=(float f) noexcept
Definition BC.h:109
HDRColorA & operator-=(const HDRColorA &c) noexcept
Definition BC.h:100
HDRColorA & operator/=(float f) noexcept
Definition BC.h:118
float operator*(const HDRColorA &c) const noexcept
Definition BC.h:88
HDRColorA & operator=(HDRColorA &&)=default
HDRColorA(HDRColorA const &)=default
HDRColorA(const LDRColorA &c) noexcept
HDRColorA(float _r, float _g, float _b, float _a) noexcept
Definition BC.h:62
HDRColorA & operator=(const LDRColorA &c) noexcept
LDRColorA ToLDRColorA() const noexcept
HDRColorA(HDRColorA &&)=default
HDRColorA operator+(const HDRColorA &c) const noexcept
Definition BC.h:76
HDRColorA & Clamp(_In_ float fMin, _In_ float fMax) noexcept
Definition BC.h:128
HDRColorA & operator+=(const HDRColorA &c) noexcept
Definition BC.h:91
HDRColorA & operator=(const HDRColorA &)=default
Definition DDS.h:24
void D3DXDecodeBC6HU(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(16) const uint8_t *pBC) noexcept
void D3DXEncodeBC6HS(_Out_writes_(16) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void D3DXEncodeBC2(_Out_writes_(16) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void(*) BC_ENCODE(uint8_t *pDXT, const XMVECTOR *pColor, uint32_t flags)
Definition BC.h:317
void D3DXDecodeBC5U(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(16) const uint8_t *pBC) noexcept
void D3DXEncodeBC5U(_Out_writes_(16) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void D3DXDecodeBC6HS(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(16) const uint8_t *pBC) noexcept
void D3DXDecodeBC4S(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(8) const uint8_t *pBC) noexcept
void D3DXEncodeBC7(_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 D3DXEncodeBC6HU(_Out_writes_(16) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void D3DXDecodeBC3(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(16) const uint8_t *pBC) noexcept
BC_FLAGS
Definition BC.h:31
@ BC_FLAGS_FORCE_BC7_MODE6
Definition BC.h:46
@ BC_FLAGS_NONE
Definition BC.h:32
@ BC_FLAGS_USE_3SUBSETS
Definition BC.h:43
@ 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 D3DXDecodeBC5S(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(16) const uint8_t *pBC) noexcept
void D3DXEncodeBC5S(_Out_writes_(16) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void D3DXEncodeBC4S(_Out_writes_(8) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void D3DXDecodeBC4U(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(8) const uint8_t *pBC) 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(*) BC_DECODE(XMVECTOR *pColor, const uint8_t *pBC)
Definition BC.h:316
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
void D3DXEncodeBC4U(_Out_writes_(8) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void D3DXDecodeBC7(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(16) const uint8_t *pBC) noexcept
typedef void(S_CALLTYPE *SteamAPIWarningMessageHook_t)(int
uint16_t rgb[2]
Definition BC.h:155
uint32_t bitmap
Definition BC.h:156
uint32_t bitmap[2]
Definition BC.h:162
D3DX_BC1 bc1
Definition BC.h:163
D3DX_BC1 bc1
Definition BC.h:171
uint8_t alpha[2]
Definition BC.h:169
uint8_t bitmap[6]
Definition BC.h:170