1 | // File: basisu_bc7enc.cpp |
2 | // Copyright (C) 2019-2021 Binomial LLC. All Rights Reserved. |
3 | // |
4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | // you may not use this file except in compliance with the License. |
6 | // You may obtain a copy of the License at |
7 | // |
8 | // http://www.apache.org/licenses/LICENSE-2.0 |
9 | // |
10 | // Unless required by applicable law or agreed to in writing, software |
11 | // distributed under the License is distributed on an "AS IS" BASIS, |
12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | // See the License for the specific language governing permissions and |
14 | // limitations under the License. |
15 | #include "basisu_bc7enc.h" |
16 | |
17 | #ifdef _DEBUG |
18 | #define BC7ENC_CHECK_OVERALL_ERROR 1 |
19 | #else |
20 | #define BC7ENC_CHECK_OVERALL_ERROR 0 |
21 | #endif |
22 | |
23 | using namespace basist; |
24 | |
25 | namespace basisu |
26 | { |
27 | |
28 | // Helpers |
29 | static inline color_quad_u8 *color_quad_u8_set_clamped(color_quad_u8 *pRes, int32_t r, int32_t g, int32_t b, int32_t a) { pRes->m_c[0] = (uint8_t)clampi(r, 0, 255); pRes->m_c[1] = (uint8_t)clampi(g, 0, 255); pRes->m_c[2] = (uint8_t)clampi(b, 0, 255); pRes->m_c[3] = (uint8_t)clampi(a, 0, 255); return pRes; } |
30 | static inline color_quad_u8 *color_quad_u8_set(color_quad_u8 *pRes, int32_t r, int32_t g, int32_t b, int32_t a) { assert((uint32_t)(r | g | b | a) <= 255); pRes->m_c[0] = (uint8_t)r; pRes->m_c[1] = (uint8_t)g; pRes->m_c[2] = (uint8_t)b; pRes->m_c[3] = (uint8_t)a; return pRes; } |
31 | static inline bc7enc_bool color_quad_u8_notequals(const color_quad_u8 *pLHS, const color_quad_u8 *pRHS) { return (pLHS->m_c[0] != pRHS->m_c[0]) || (pLHS->m_c[1] != pRHS->m_c[1]) || (pLHS->m_c[2] != pRHS->m_c[2]) || (pLHS->m_c[3] != pRHS->m_c[3]); } |
32 | static inline bc7enc_vec4F*vec4F_set_scalar(bc7enc_vec4F*pV, float x) { pV->m_c[0] = x; pV->m_c[1] = x; pV->m_c[2] = x; pV->m_c[3] = x; return pV; } |
33 | static inline bc7enc_vec4F*vec4F_set(bc7enc_vec4F*pV, float x, float y, float z, float w) { pV->m_c[0] = x; pV->m_c[1] = y; pV->m_c[2] = z; pV->m_c[3] = w; return pV; } |
34 | static inline bc7enc_vec4F*vec4F_saturate_in_place(bc7enc_vec4F*pV) { pV->m_c[0] = saturate(pV->m_c[0]); pV->m_c[1] = saturate(pV->m_c[1]); pV->m_c[2] = saturate(pV->m_c[2]); pV->m_c[3] = saturate(pV->m_c[3]); return pV; } |
35 | static inline bc7enc_vec4F vec4F_saturate(const bc7enc_vec4F*pV) { bc7enc_vec4F res; res.m_c[0] = saturate(pV->m_c[0]); res.m_c[1] = saturate(pV->m_c[1]); res.m_c[2] = saturate(pV->m_c[2]); res.m_c[3] = saturate(pV->m_c[3]); return res; } |
36 | static inline bc7enc_vec4F vec4F_from_color(const color_quad_u8 *pC) { bc7enc_vec4F res; vec4F_set(&res, pC->m_c[0], pC->m_c[1], pC->m_c[2], pC->m_c[3]); return res; } |
37 | static inline bc7enc_vec4F vec4F_add(const bc7enc_vec4F*pLHS, const bc7enc_vec4F*pRHS) { bc7enc_vec4F res; vec4F_set(&res, pLHS->m_c[0] + pRHS->m_c[0], pLHS->m_c[1] + pRHS->m_c[1], pLHS->m_c[2] + pRHS->m_c[2], pLHS->m_c[3] + pRHS->m_c[3]); return res; } |
38 | static inline bc7enc_vec4F vec4F_sub(const bc7enc_vec4F*pLHS, const bc7enc_vec4F*pRHS) { bc7enc_vec4F res; vec4F_set(&res, pLHS->m_c[0] - pRHS->m_c[0], pLHS->m_c[1] - pRHS->m_c[1], pLHS->m_c[2] - pRHS->m_c[2], pLHS->m_c[3] - pRHS->m_c[3]); return res; } |
39 | static inline float vec4F_dot(const bc7enc_vec4F*pLHS, const bc7enc_vec4F*pRHS) { return pLHS->m_c[0] * pRHS->m_c[0] + pLHS->m_c[1] * pRHS->m_c[1] + pLHS->m_c[2] * pRHS->m_c[2] + pLHS->m_c[3] * pRHS->m_c[3]; } |
40 | static inline bc7enc_vec4F vec4F_mul(const bc7enc_vec4F*pLHS, float s) { bc7enc_vec4F res; vec4F_set(&res, pLHS->m_c[0] * s, pLHS->m_c[1] * s, pLHS->m_c[2] * s, pLHS->m_c[3] * s); return res; } |
41 | static inline bc7enc_vec4F* vec4F_normalize_in_place(bc7enc_vec4F*pV) { float s = pV->m_c[0] * pV->m_c[0] + pV->m_c[1] * pV->m_c[1] + pV->m_c[2] * pV->m_c[2] + pV->m_c[3] * pV->m_c[3]; if (s != 0.0f) { s = 1.0f / sqrtf(s); pV->m_c[0] *= s; pV->m_c[1] *= s; pV->m_c[2] *= s; pV->m_c[3] *= s; } return pV; } |
42 | |
43 | // Precomputed weight constants used during least fit determination. For each entry in g_bc7_weights[]: w * w, (1.0f - w) * w, (1.0f - w) * (1.0f - w), w |
44 | const float g_bc7_weights1x[2 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 1.000000f, 0.000000f, 0.000000f, 1.000000f }; |
45 | |
46 | const float g_bc7_weights2x[4 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.107666f, 0.220459f, 0.451416f, 0.328125f, 0.451416f, 0.220459f, 0.107666f, 0.671875f, 1.000000f, 0.000000f, 0.000000f, 1.000000f }; |
47 | |
48 | const float g_bc7_weights3x[8 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.019775f, 0.120850f, 0.738525f, 0.140625f, 0.079102f, 0.202148f, 0.516602f, 0.281250f, 0.177979f, 0.243896f, 0.334229f, 0.421875f, 0.334229f, 0.243896f, 0.177979f, 0.578125f, 0.516602f, 0.202148f, |
49 | 0.079102f, 0.718750f, 0.738525f, 0.120850f, 0.019775f, 0.859375f, 1.000000f, 0.000000f, 0.000000f, 1.000000f }; |
50 | |
51 | const float g_bc7_weights4x[16 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.003906f, 0.058594f, 0.878906f, 0.062500f, 0.019775f, 0.120850f, 0.738525f, 0.140625f, 0.041260f, 0.161865f, 0.635010f, 0.203125f, 0.070557f, 0.195068f, 0.539307f, 0.265625f, 0.107666f, 0.220459f, |
52 | 0.451416f, 0.328125f, 0.165039f, 0.241211f, 0.352539f, 0.406250f, 0.219727f, 0.249023f, 0.282227f, 0.468750f, 0.282227f, 0.249023f, 0.219727f, 0.531250f, 0.352539f, 0.241211f, 0.165039f, 0.593750f, 0.451416f, 0.220459f, 0.107666f, 0.671875f, 0.539307f, 0.195068f, 0.070557f, 0.734375f, |
53 | 0.635010f, 0.161865f, 0.041260f, 0.796875f, 0.738525f, 0.120850f, 0.019775f, 0.859375f, 0.878906f, 0.058594f, 0.003906f, 0.937500f, 1.000000f, 0.000000f, 0.000000f, 1.000000f }; |
54 | |
55 | const float g_astc_weights4x[16 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.003906f, 0.058594f, 0.878906f, 0.062500f, 0.015625f, 0.109375f, 0.765625f, 0.125000f, 0.035156f, 0.152344f, 0.660156f, 0.187500f, 0.070557f, 0.195068f, 0.539307f, 0.265625f, 0.107666f, 0.220459f, |
56 | 0.451416f, 0.328125f, 0.152588f, 0.238037f, 0.371338f, 0.390625f, 0.205322f, 0.247803f, 0.299072f, 0.453125f, 0.299072f, 0.247803f, 0.205322f, 0.546875f, 0.371338f, 0.238037f, 0.152588f, 0.609375f, 0.451416f, 0.220459f, 0.107666f, 0.671875f, 0.539307f, 0.195068f, 0.070557f, 0.734375f, |
57 | 0.660156f, 0.152344f, 0.035156f, 0.812500f, 0.765625f, 0.109375f, 0.015625f, 0.875000f, 0.878906f, 0.058594f, 0.003906f, 0.937500f, 1.000000f, 0.000000f, 0.000000f, 1.000000f }; |
58 | |
59 | const float g_astc_weights5x[32 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.000977f, 0.030273f, 0.938477f, 0.031250f, 0.003906f, 0.058594f, 0.878906f, 0.062500f, 0.008789f, 0.084961f, 0.821289f, |
60 | 0.093750f, 0.015625f, 0.109375f, 0.765625f, 0.125000f, 0.024414f, 0.131836f, 0.711914f, 0.156250f, 0.035156f, 0.152344f, 0.660156f, 0.187500f, 0.047852f, 0.170898f, 0.610352f, 0.218750f, 0.062500f, 0.187500f, |
61 | 0.562500f, 0.250000f, 0.079102f, 0.202148f, 0.516602f, 0.281250f, 0.097656f, 0.214844f, 0.472656f, 0.312500f, 0.118164f, 0.225586f, 0.430664f, 0.343750f, 0.140625f, 0.234375f, 0.390625f, 0.375000f, 0.165039f, |
62 | 0.241211f, 0.352539f, 0.406250f, 0.191406f, 0.246094f, 0.316406f, 0.437500f, 0.219727f, 0.249023f, 0.282227f, 0.468750f, 0.282227f, 0.249023f, 0.219727f, 0.531250f, 0.316406f, 0.246094f, 0.191406f, 0.562500f, |
63 | 0.352539f, 0.241211f, 0.165039f, 0.593750f, 0.390625f, 0.234375f, 0.140625f, 0.625000f, 0.430664f, 0.225586f, 0.118164f, 0.656250f, 0.472656f, 0.214844f, 0.097656f, 0.687500f, 0.516602f, 0.202148f, 0.079102f, |
64 | 0.718750f, 0.562500f, 0.187500f, 0.062500f, 0.750000f, 0.610352f, 0.170898f, 0.047852f, 0.781250f, 0.660156f, 0.152344f, 0.035156f, 0.812500f, 0.711914f, 0.131836f, 0.024414f, 0.843750f, 0.765625f, 0.109375f, |
65 | 0.015625f, 0.875000f, 0.821289f, 0.084961f, 0.008789f, 0.906250f, 0.878906f, 0.058594f, 0.003906f, 0.937500f, 0.938477f, 0.030273f, 0.000977f, 0.968750f, 1.000000f, 0.000000f, 0.000000f, 1.000000f }; |
66 | |
67 | const float g_astc_weights_3levelsx[3 * 4] = { |
68 | 0.000000f, 0.000000f, 1.000000f, 0.000000f, |
69 | .5f * .5f, (1.0f - .5f) * .5f, (1.0f - .5f) * (1.0f - .5f), .5f, |
70 | 1.000000f, 0.000000f, 0.000000f, 1.000000f }; |
71 | |
72 | static endpoint_err g_bc7_mode_1_optimal_endpoints[256][2]; // [c][pbit] |
73 | static const uint32_t BC7ENC_MODE_1_OPTIMAL_INDEX = 2; |
74 | |
75 | static endpoint_err g_astc_4bit_3bit_optimal_endpoints[256]; // [c] |
76 | static const uint32_t BC7ENC_ASTC_4BIT_3BIT_OPTIMAL_INDEX = 2; |
77 | |
78 | static endpoint_err g_astc_4bit_2bit_optimal_endpoints[256]; // [c] |
79 | static const uint32_t BC7ENC_ASTC_4BIT_2BIT_OPTIMAL_INDEX = 1; |
80 | |
81 | static endpoint_err g_astc_range7_2bit_optimal_endpoints[256]; // [c] |
82 | static const uint32_t BC7ENC_ASTC_RANGE7_2BIT_OPTIMAL_INDEX = 1; |
83 | |
84 | static endpoint_err g_astc_range13_4bit_optimal_endpoints[256]; // [c] |
85 | static const uint32_t BC7ENC_ASTC_RANGE13_4BIT_OPTIMAL_INDEX = 2; |
86 | |
87 | static endpoint_err g_astc_range13_2bit_optimal_endpoints[256]; // [c] |
88 | static const uint32_t BC7ENC_ASTC_RANGE13_2BIT_OPTIMAL_INDEX = 1; |
89 | |
90 | static endpoint_err g_astc_range11_5bit_optimal_endpoints[256]; // [c] |
91 | static const uint32_t BC7ENC_ASTC_RANGE11_5BIT_OPTIMAL_INDEX = 13; // not 1, which is optimal, because 26 losslessly maps to BC7 4-bit weights |
92 | |
93 | astc_quant_bin g_astc_sorted_order_unquant[BC7ENC_TOTAL_ASTC_RANGES][256]; // [sorted unquantized order] |
94 | |
95 | static uint8_t g_astc_nearest_sorted_index[BC7ENC_TOTAL_ASTC_RANGES][256]; |
96 | |
97 | static void astc_init() |
98 | { |
99 | for (uint32_t range = 0; range < BC7ENC_TOTAL_ASTC_RANGES; range++) |
100 | { |
101 | if (!astc_is_valid_endpoint_range(range)) |
102 | continue; |
103 | |
104 | const uint32_t levels = astc_get_levels(range); |
105 | |
106 | uint32_t vals[256]; |
107 | // TODO |
108 | for (uint32_t i = 0; i < levels; i++) |
109 | vals[i] = (unquant_astc_endpoint_val(i, range) << 8) | i; |
110 | |
111 | std::sort(vals, vals + levels); |
112 | |
113 | for (uint32_t i = 0; i < levels; i++) |
114 | { |
115 | uint32_t order = vals[i] & 0xFF; |
116 | uint32_t unq = vals[i] >> 8; |
117 | |
118 | g_astc_sorted_order_unquant[range][i].m_unquant = (uint8_t)unq; |
119 | g_astc_sorted_order_unquant[range][i].m_index = (uint8_t)order; |
120 | |
121 | } // i |
122 | |
123 | #if 0 |
124 | if (g_astc_bise_range_table[range][1] || g_astc_bise_range_table[range][2]) |
125 | { |
126 | printf("// Range: %u, Levels: %u, Bits: %u, Trits: %u, Quints: %u\n" , range, levels, g_astc_bise_range_table[range][0], g_astc_bise_range_table[range][1], g_astc_bise_range_table[range][2]); |
127 | |
128 | printf("{" ); |
129 | for (uint32_t i = 0; i < levels; i++) |
130 | { |
131 | printf("{%u,%u}" , g_astc_sorted_order_unquant[range][i].m_index, g_astc_sorted_order_unquant[range][i].m_unquant); |
132 | if (i != (levels - 1)) |
133 | printf("," ); |
134 | } |
135 | printf("}\n" ); |
136 | } |
137 | #endif |
138 | |
139 | #if 0 |
140 | if (g_astc_bise_range_table[range][1] || g_astc_bise_range_table[range][2]) |
141 | { |
142 | printf("// Range: %u, Levels: %u, Bits: %u, Trits: %u, Quints: %u\n" , range, levels, g_astc_bise_range_table[range][0], g_astc_bise_range_table[range][1], g_astc_bise_range_table[range][2]); |
143 | |
144 | printf("{" ); |
145 | for (uint32_t i = 0; i < levels; i++) |
146 | { |
147 | printf("{%u,%u}" , g_astc_unquant[range][i].m_index, g_astc_unquant[range][i].m_unquant); |
148 | if (i != (levels - 1)) |
149 | printf("," ); |
150 | } |
151 | printf("}\n" ); |
152 | } |
153 | #endif |
154 | |
155 | for (uint32_t i = 0; i < 256; i++) |
156 | { |
157 | uint32_t best_index = 0; |
158 | int best_err = INT32_MAX; |
159 | |
160 | for (uint32_t j = 0; j < levels; j++) |
161 | { |
162 | int err = g_astc_sorted_order_unquant[range][j].m_unquant - i; |
163 | if (err < 0) |
164 | err = -err; |
165 | if (err < best_err) |
166 | { |
167 | best_err = err; |
168 | best_index = j; |
169 | } |
170 | } |
171 | |
172 | g_astc_nearest_sorted_index[range][i] = (uint8_t)best_index; |
173 | } // i |
174 | } // range |
175 | } |
176 | |
177 | static inline uint32_t astc_interpolate_linear(uint32_t l, uint32_t h, uint32_t w) |
178 | { |
179 | l = (l << 8) | l; |
180 | h = (h << 8) | h; |
181 | uint32_t k = (l * (64 - w) + h * w + 32) >> 6; |
182 | return k >> 8; |
183 | } |
184 | |
185 | // Initialize the lookup table used for optimal single color compression in mode 1. Must be called before encoding. |
186 | void bc7enc_compress_block_init() |
187 | { |
188 | astc_init(); |
189 | |
190 | // BC7 666.1 |
191 | for (int c = 0; c < 256; c++) |
192 | { |
193 | for (uint32_t lp = 0; lp < 2; lp++) |
194 | { |
195 | endpoint_err best; |
196 | best.m_error = (uint16_t)UINT16_MAX; |
197 | for (uint32_t l = 0; l < 64; l++) |
198 | { |
199 | uint32_t low = ((l << 1) | lp) << 1; |
200 | low |= (low >> 7); |
201 | for (uint32_t h = 0; h < 64; h++) |
202 | { |
203 | uint32_t high = ((h << 1) | lp) << 1; |
204 | high |= (high >> 7); |
205 | const int k = (low * (64 - g_bc7_weights3[BC7ENC_MODE_1_OPTIMAL_INDEX]) + high * g_bc7_weights3[BC7ENC_MODE_1_OPTIMAL_INDEX] + 32) >> 6; |
206 | const int err = (k - c) * (k - c); |
207 | if (err < best.m_error) |
208 | { |
209 | best.m_error = (uint16_t)err; |
210 | best.m_lo = (uint8_t)l; |
211 | best.m_hi = (uint8_t)h; |
212 | } |
213 | } // h |
214 | } // l |
215 | g_bc7_mode_1_optimal_endpoints[c][lp] = best; |
216 | } // lp |
217 | } // c |
218 | |
219 | // ASTC [0,15] 3-bit |
220 | for (int c = 0; c < 256; c++) |
221 | { |
222 | endpoint_err best; |
223 | best.m_error = (uint16_t)UINT16_MAX; |
224 | for (uint32_t l = 0; l < 16; l++) |
225 | { |
226 | uint32_t low = (l << 4) | l; |
227 | |
228 | for (uint32_t h = 0; h < 16; h++) |
229 | { |
230 | uint32_t high = (h << 4) | h; |
231 | |
232 | const int k = astc_interpolate_linear(low, high, g_bc7_weights3[BC7ENC_ASTC_4BIT_3BIT_OPTIMAL_INDEX]); |
233 | const int err = (k - c) * (k - c); |
234 | |
235 | if (err < best.m_error) |
236 | { |
237 | best.m_error = (uint16_t)err; |
238 | best.m_lo = (uint8_t)l; |
239 | best.m_hi = (uint8_t)h; |
240 | } |
241 | } // h |
242 | } // l |
243 | |
244 | g_astc_4bit_3bit_optimal_endpoints[c] = best; |
245 | |
246 | } // c |
247 | |
248 | // ASTC [0,15] 2-bit |
249 | for (int c = 0; c < 256; c++) |
250 | { |
251 | endpoint_err best; |
252 | best.m_error = (uint16_t)UINT16_MAX; |
253 | for (uint32_t l = 0; l < 16; l++) |
254 | { |
255 | uint32_t low = (l << 4) | l; |
256 | |
257 | for (uint32_t h = 0; h < 16; h++) |
258 | { |
259 | uint32_t high = (h << 4) | h; |
260 | |
261 | const int k = astc_interpolate_linear(low, high, g_bc7_weights2[BC7ENC_ASTC_4BIT_2BIT_OPTIMAL_INDEX]); |
262 | const int err = (k - c) * (k - c); |
263 | |
264 | if (err < best.m_error) |
265 | { |
266 | best.m_error = (uint16_t)err; |
267 | best.m_lo = (uint8_t)l; |
268 | best.m_hi = (uint8_t)h; |
269 | } |
270 | } // h |
271 | } // l |
272 | |
273 | g_astc_4bit_2bit_optimal_endpoints[c] = best; |
274 | |
275 | } // c |
276 | |
277 | // ASTC range 7 [0,11] 2-bit |
278 | for (int c = 0; c < 256; c++) |
279 | { |
280 | endpoint_err best; |
281 | best.m_error = (uint16_t)UINT16_MAX; |
282 | for (uint32_t l = 0; l < 12; l++) |
283 | { |
284 | uint32_t low = g_astc_sorted_order_unquant[7][l].m_unquant; |
285 | |
286 | for (uint32_t h = 0; h < 12; h++) |
287 | { |
288 | uint32_t high = g_astc_sorted_order_unquant[7][h].m_unquant; |
289 | |
290 | const int k = astc_interpolate_linear(low, high, g_bc7_weights2[BC7ENC_ASTC_RANGE7_2BIT_OPTIMAL_INDEX]); |
291 | const int err = (k - c) * (k - c); |
292 | |
293 | if (err < best.m_error) |
294 | { |
295 | best.m_error = (uint16_t)err; |
296 | best.m_lo = (uint8_t)l; |
297 | best.m_hi = (uint8_t)h; |
298 | } |
299 | } // h |
300 | } // l |
301 | |
302 | g_astc_range7_2bit_optimal_endpoints[c] = best; |
303 | |
304 | } // c |
305 | |
306 | // ASTC range 13 [0,47] 4-bit |
307 | for (int c = 0; c < 256; c++) |
308 | { |
309 | endpoint_err best; |
310 | best.m_error = (uint16_t)UINT16_MAX; |
311 | for (uint32_t l = 0; l < 48; l++) |
312 | { |
313 | uint32_t low = g_astc_sorted_order_unquant[13][l].m_unquant; |
314 | |
315 | for (uint32_t h = 0; h < 48; h++) |
316 | { |
317 | uint32_t high = g_astc_sorted_order_unquant[13][h].m_unquant; |
318 | |
319 | const int k = astc_interpolate_linear(low, high, g_astc_weights4[BC7ENC_ASTC_RANGE13_4BIT_OPTIMAL_INDEX]); |
320 | const int err = (k - c) * (k - c); |
321 | |
322 | if (err < best.m_error) |
323 | { |
324 | best.m_error = (uint16_t)err; |
325 | best.m_lo = (uint8_t)l; |
326 | best.m_hi = (uint8_t)h; |
327 | } |
328 | } // h |
329 | } // l |
330 | |
331 | g_astc_range13_4bit_optimal_endpoints[c] = best; |
332 | |
333 | } // c |
334 | |
335 | // ASTC range 13 [0,47] 2-bit |
336 | for (int c = 0; c < 256; c++) |
337 | { |
338 | endpoint_err best; |
339 | best.m_error = (uint16_t)UINT16_MAX; |
340 | for (uint32_t l = 0; l < 48; l++) |
341 | { |
342 | uint32_t low = g_astc_sorted_order_unquant[13][l].m_unquant; |
343 | |
344 | for (uint32_t h = 0; h < 48; h++) |
345 | { |
346 | uint32_t high = g_astc_sorted_order_unquant[13][h].m_unquant; |
347 | |
348 | const int k = astc_interpolate_linear(low, high, g_bc7_weights2[BC7ENC_ASTC_RANGE13_2BIT_OPTIMAL_INDEX]); |
349 | const int err = (k - c) * (k - c); |
350 | |
351 | if (err < best.m_error) |
352 | { |
353 | best.m_error = (uint16_t)err; |
354 | best.m_lo = (uint8_t)l; |
355 | best.m_hi = (uint8_t)h; |
356 | } |
357 | } // h |
358 | } // l |
359 | |
360 | g_astc_range13_2bit_optimal_endpoints[c] = best; |
361 | |
362 | } // c |
363 | |
364 | // ASTC range 11 [0,31] 5-bit |
365 | for (int c = 0; c < 256; c++) |
366 | { |
367 | endpoint_err best; |
368 | best.m_error = (uint16_t)UINT16_MAX; |
369 | for (uint32_t l = 0; l < 32; l++) |
370 | { |
371 | uint32_t low = g_astc_sorted_order_unquant[11][l].m_unquant; |
372 | |
373 | for (uint32_t h = 0; h < 32; h++) |
374 | { |
375 | uint32_t high = g_astc_sorted_order_unquant[11][h].m_unquant; |
376 | |
377 | const int k = astc_interpolate_linear(low, high, g_astc_weights5[BC7ENC_ASTC_RANGE11_5BIT_OPTIMAL_INDEX]); |
378 | const int err = (k - c) * (k - c); |
379 | |
380 | if (err < best.m_error) |
381 | { |
382 | best.m_error = (uint16_t)err; |
383 | best.m_lo = (uint8_t)l; |
384 | best.m_hi = (uint8_t)h; |
385 | } |
386 | } // h |
387 | } // l |
388 | |
389 | g_astc_range11_5bit_optimal_endpoints[c] = best; |
390 | |
391 | } // c |
392 | } |
393 | |
394 | static void compute_least_squares_endpoints_rgba(uint32_t N, const uint8_t *pSelectors, const bc7enc_vec4F* pSelector_weights, bc7enc_vec4F* pXl, bc7enc_vec4F* pXh, const color_quad_u8 *pColors) |
395 | { |
396 | // Least squares using normal equations: http://www.cs.cornell.edu/~bindel/class/cs3220-s12/notes/lec10.pdf |
397 | // I did this in matrix form first, expanded out all the ops, then optimized it a bit. |
398 | double z00 = 0.0f, z01 = 0.0f, z10 = 0.0f, z11 = 0.0f; |
399 | double q00_r = 0.0f, q10_r = 0.0f, t_r = 0.0f; |
400 | double q00_g = 0.0f, q10_g = 0.0f, t_g = 0.0f; |
401 | double q00_b = 0.0f, q10_b = 0.0f, t_b = 0.0f; |
402 | double q00_a = 0.0f, q10_a = 0.0f, t_a = 0.0f; |
403 | |
404 | for (uint32_t i = 0; i < N; i++) |
405 | { |
406 | const uint32_t sel = pSelectors[i]; |
407 | z00 += pSelector_weights[sel].m_c[0]; |
408 | z10 += pSelector_weights[sel].m_c[1]; |
409 | z11 += pSelector_weights[sel].m_c[2]; |
410 | float w = pSelector_weights[sel].m_c[3]; |
411 | q00_r += w * pColors[i].m_c[0]; t_r += pColors[i].m_c[0]; |
412 | q00_g += w * pColors[i].m_c[1]; t_g += pColors[i].m_c[1]; |
413 | q00_b += w * pColors[i].m_c[2]; t_b += pColors[i].m_c[2]; |
414 | q00_a += w * pColors[i].m_c[3]; t_a += pColors[i].m_c[3]; |
415 | } |
416 | |
417 | q10_r = t_r - q00_r; |
418 | q10_g = t_g - q00_g; |
419 | q10_b = t_b - q00_b; |
420 | q10_a = t_a - q00_a; |
421 | |
422 | z01 = z10; |
423 | |
424 | double det = z00 * z11 - z01 * z10; |
425 | if (det != 0.0f) |
426 | det = 1.0f / det; |
427 | |
428 | double iz00, iz01, iz10, iz11; |
429 | iz00 = z11 * det; |
430 | iz01 = -z01 * det; |
431 | iz10 = -z10 * det; |
432 | iz11 = z00 * det; |
433 | |
434 | pXl->m_c[0] = (float)(iz00 * q00_r + iz01 * q10_r); pXh->m_c[0] = (float)(iz10 * q00_r + iz11 * q10_r); |
435 | pXl->m_c[1] = (float)(iz00 * q00_g + iz01 * q10_g); pXh->m_c[1] = (float)(iz10 * q00_g + iz11 * q10_g); |
436 | pXl->m_c[2] = (float)(iz00 * q00_b + iz01 * q10_b); pXh->m_c[2] = (float)(iz10 * q00_b + iz11 * q10_b); |
437 | pXl->m_c[3] = (float)(iz00 * q00_a + iz01 * q10_a); pXh->m_c[3] = (float)(iz10 * q00_a + iz11 * q10_a); |
438 | |
439 | for (uint32_t c = 0; c < 4; c++) |
440 | { |
441 | if ((pXl->m_c[c] < 0.0f) || (pXh->m_c[c] > 255.0f)) |
442 | { |
443 | uint32_t lo_v = UINT32_MAX, hi_v = 0; |
444 | for (uint32_t i = 0; i < N; i++) |
445 | { |
446 | lo_v = minimumu(lo_v, pColors[i].m_c[c]); |
447 | hi_v = maximumu(hi_v, pColors[i].m_c[c]); |
448 | } |
449 | |
450 | if (lo_v == hi_v) |
451 | { |
452 | pXl->m_c[c] = (float)lo_v; |
453 | pXh->m_c[c] = (float)hi_v; |
454 | } |
455 | } |
456 | } |
457 | } |
458 | |
459 | static void compute_least_squares_endpoints_rgb(uint32_t N, const uint8_t *pSelectors, const bc7enc_vec4F*pSelector_weights, bc7enc_vec4F*pXl, bc7enc_vec4F*pXh, const color_quad_u8 *pColors) |
460 | { |
461 | double z00 = 0.0f, z01 = 0.0f, z10 = 0.0f, z11 = 0.0f; |
462 | double q00_r = 0.0f, q10_r = 0.0f, t_r = 0.0f; |
463 | double q00_g = 0.0f, q10_g = 0.0f, t_g = 0.0f; |
464 | double q00_b = 0.0f, q10_b = 0.0f, t_b = 0.0f; |
465 | |
466 | for (uint32_t i = 0; i < N; i++) |
467 | { |
468 | const uint32_t sel = pSelectors[i]; |
469 | z00 += pSelector_weights[sel].m_c[0]; |
470 | z10 += pSelector_weights[sel].m_c[1]; |
471 | z11 += pSelector_weights[sel].m_c[2]; |
472 | float w = pSelector_weights[sel].m_c[3]; |
473 | q00_r += w * pColors[i].m_c[0]; t_r += pColors[i].m_c[0]; |
474 | q00_g += w * pColors[i].m_c[1]; t_g += pColors[i].m_c[1]; |
475 | q00_b += w * pColors[i].m_c[2]; t_b += pColors[i].m_c[2]; |
476 | } |
477 | |
478 | q10_r = t_r - q00_r; |
479 | q10_g = t_g - q00_g; |
480 | q10_b = t_b - q00_b; |
481 | |
482 | z01 = z10; |
483 | |
484 | double det = z00 * z11 - z01 * z10; |
485 | if (det != 0.0f) |
486 | det = 1.0f / det; |
487 | |
488 | double iz00, iz01, iz10, iz11; |
489 | iz00 = z11 * det; |
490 | iz01 = -z01 * det; |
491 | iz10 = -z10 * det; |
492 | iz11 = z00 * det; |
493 | |
494 | pXl->m_c[0] = (float)(iz00 * q00_r + iz01 * q10_r); pXh->m_c[0] = (float)(iz10 * q00_r + iz11 * q10_r); |
495 | pXl->m_c[1] = (float)(iz00 * q00_g + iz01 * q10_g); pXh->m_c[1] = (float)(iz10 * q00_g + iz11 * q10_g); |
496 | pXl->m_c[2] = (float)(iz00 * q00_b + iz01 * q10_b); pXh->m_c[2] = (float)(iz10 * q00_b + iz11 * q10_b); |
497 | pXl->m_c[3] = 255.0f; pXh->m_c[3] = 255.0f; |
498 | |
499 | for (uint32_t c = 0; c < 3; c++) |
500 | { |
501 | if ((pXl->m_c[c] < 0.0f) || (pXh->m_c[c] > 255.0f)) |
502 | { |
503 | uint32_t lo_v = UINT32_MAX, hi_v = 0; |
504 | for (uint32_t i = 0; i < N; i++) |
505 | { |
506 | lo_v = minimumu(lo_v, pColors[i].m_c[c]); |
507 | hi_v = maximumu(hi_v, pColors[i].m_c[c]); |
508 | } |
509 | |
510 | if (lo_v == hi_v) |
511 | { |
512 | pXl->m_c[c] = (float)lo_v; |
513 | pXh->m_c[c] = (float)hi_v; |
514 | } |
515 | } |
516 | } |
517 | } |
518 | |
519 | static inline color_quad_u8 scale_color(const color_quad_u8* pC, const color_cell_compressor_params* pParams) |
520 | { |
521 | color_quad_u8 results; |
522 | |
523 | if (pParams->m_astc_endpoint_range) |
524 | { |
525 | for (uint32_t i = 0; i < 4; i++) |
526 | { |
527 | results.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pC->m_c[i]].m_unquant; |
528 | } |
529 | } |
530 | else |
531 | { |
532 | const uint32_t n = pParams->m_comp_bits + (pParams->m_has_pbits ? 1 : 0); |
533 | assert((n >= 4) && (n <= 8)); |
534 | |
535 | for (uint32_t i = 0; i < 4; i++) |
536 | { |
537 | uint32_t v = pC->m_c[i] << (8 - n); |
538 | v |= (v >> n); |
539 | assert(v <= 255); |
540 | results.m_c[i] = (uint8_t)(v); |
541 | } |
542 | } |
543 | |
544 | return results; |
545 | } |
546 | |
547 | static inline uint64_t compute_color_distance_rgb(const color_quad_u8 *pE1, const color_quad_u8 *pE2, bc7enc_bool perceptual, const uint32_t weights[4]) |
548 | { |
549 | int dr, dg, db; |
550 | |
551 | if (perceptual) |
552 | { |
553 | const int l1 = pE1->m_c[0] * 109 + pE1->m_c[1] * 366 + pE1->m_c[2] * 37; |
554 | const int cr1 = ((int)pE1->m_c[0] << 9) - l1; |
555 | const int cb1 = ((int)pE1->m_c[2] << 9) - l1; |
556 | const int l2 = pE2->m_c[0] * 109 + pE2->m_c[1] * 366 + pE2->m_c[2] * 37; |
557 | const int cr2 = ((int)pE2->m_c[0] << 9) - l2; |
558 | const int cb2 = ((int)pE2->m_c[2] << 9) - l2; |
559 | dr = (l1 - l2) >> 8; |
560 | dg = (cr1 - cr2) >> 8; |
561 | db = (cb1 - cb2) >> 8; |
562 | } |
563 | else |
564 | { |
565 | dr = (int)pE1->m_c[0] - (int)pE2->m_c[0]; |
566 | dg = (int)pE1->m_c[1] - (int)pE2->m_c[1]; |
567 | db = (int)pE1->m_c[2] - (int)pE2->m_c[2]; |
568 | } |
569 | |
570 | return weights[0] * (uint32_t)(dr * dr) + weights[1] * (uint32_t)(dg * dg) + weights[2] * (uint32_t)(db * db); |
571 | } |
572 | |
573 | static inline uint64_t compute_color_distance_rgba(const color_quad_u8 *pE1, const color_quad_u8 *pE2, bc7enc_bool perceptual, const uint32_t weights[4]) |
574 | { |
575 | int da = (int)pE1->m_c[3] - (int)pE2->m_c[3]; |
576 | return compute_color_distance_rgb(pE1, pE2, perceptual, weights) + (weights[3] * (uint32_t)(da * da)); |
577 | } |
578 | |
579 | static uint64_t pack_mode1_to_one_color(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, uint32_t r, uint32_t g, uint32_t b, uint8_t *pSelectors) |
580 | { |
581 | uint32_t best_err = UINT_MAX; |
582 | uint32_t best_p = 0; |
583 | |
584 | for (uint32_t p = 0; p < 2; p++) |
585 | { |
586 | uint32_t err = g_bc7_mode_1_optimal_endpoints[r][p].m_error + g_bc7_mode_1_optimal_endpoints[g][p].m_error + g_bc7_mode_1_optimal_endpoints[b][p].m_error; |
587 | if (err < best_err) |
588 | { |
589 | best_err = err; |
590 | best_p = p; |
591 | } |
592 | } |
593 | |
594 | const endpoint_err *pEr = &g_bc7_mode_1_optimal_endpoints[r][best_p]; |
595 | const endpoint_err *pEg = &g_bc7_mode_1_optimal_endpoints[g][best_p]; |
596 | const endpoint_err *pEb = &g_bc7_mode_1_optimal_endpoints[b][best_p]; |
597 | |
598 | color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 0); |
599 | color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, 0); |
600 | pResults->m_pbits[0] = best_p; |
601 | pResults->m_pbits[1] = 0; |
602 | |
603 | memset(pSelectors, BC7ENC_MODE_1_OPTIMAL_INDEX, pParams->m_num_pixels); |
604 | |
605 | color_quad_u8 p; |
606 | for (uint32_t i = 0; i < 3; i++) |
607 | { |
608 | uint32_t low = ((pResults->m_low_endpoint.m_c[i] << 1) | pResults->m_pbits[0]) << 1; |
609 | low |= (low >> 7); |
610 | |
611 | uint32_t high = ((pResults->m_high_endpoint.m_c[i] << 1) | pResults->m_pbits[0]) << 1; |
612 | high |= (high >> 7); |
613 | |
614 | p.m_c[i] = (uint8_t)((low * (64 - g_bc7_weights3[BC7ENC_MODE_1_OPTIMAL_INDEX]) + high * g_bc7_weights3[BC7ENC_MODE_1_OPTIMAL_INDEX] + 32) >> 6); |
615 | } |
616 | p.m_c[3] = 255; |
617 | |
618 | uint64_t total_err = 0; |
619 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
620 | total_err += compute_color_distance_rgb(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights); |
621 | |
622 | pResults->m_best_overall_err = total_err; |
623 | |
624 | return total_err; |
625 | } |
626 | |
627 | static uint64_t pack_astc_4bit_3bit_to_one_color(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, uint32_t r, uint32_t g, uint32_t b, uint8_t *pSelectors) |
628 | { |
629 | const endpoint_err *pEr = &g_astc_4bit_3bit_optimal_endpoints[r]; |
630 | const endpoint_err *pEg = &g_astc_4bit_3bit_optimal_endpoints[g]; |
631 | const endpoint_err *pEb = &g_astc_4bit_3bit_optimal_endpoints[b]; |
632 | |
633 | color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 0); |
634 | color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, 0); |
635 | pResults->m_pbits[0] = 0; |
636 | pResults->m_pbits[1] = 0; |
637 | |
638 | for (uint32_t i = 0; i < 4; i++) |
639 | { |
640 | pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index; |
641 | pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index; |
642 | } |
643 | |
644 | memset(pSelectors, BC7ENC_ASTC_4BIT_3BIT_OPTIMAL_INDEX, pParams->m_num_pixels); |
645 | |
646 | color_quad_u8 p; |
647 | for (uint32_t i = 0; i < 3; i++) |
648 | { |
649 | uint32_t low = (pResults->m_low_endpoint.m_c[i] << 4) | pResults->m_low_endpoint.m_c[i]; |
650 | uint32_t high = (pResults->m_high_endpoint.m_c[i] << 4) | pResults->m_high_endpoint.m_c[i]; |
651 | |
652 | p.m_c[i] = (uint8_t)astc_interpolate_linear(low, high, g_bc7_weights3[BC7ENC_ASTC_4BIT_3BIT_OPTIMAL_INDEX]); |
653 | } |
654 | p.m_c[3] = 255; |
655 | |
656 | uint64_t total_err = 0; |
657 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
658 | total_err += compute_color_distance_rgb(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights); |
659 | |
660 | pResults->m_best_overall_err = total_err; |
661 | |
662 | return total_err; |
663 | } |
664 | |
665 | static uint64_t pack_astc_4bit_2bit_to_one_color_rgba(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, uint32_t r, uint32_t g, uint32_t b, uint32_t a, uint8_t *pSelectors) |
666 | { |
667 | const endpoint_err *pEr = &g_astc_4bit_2bit_optimal_endpoints[r]; |
668 | const endpoint_err *pEg = &g_astc_4bit_2bit_optimal_endpoints[g]; |
669 | const endpoint_err *pEb = &g_astc_4bit_2bit_optimal_endpoints[b]; |
670 | const endpoint_err *pEa = &g_astc_4bit_2bit_optimal_endpoints[a]; |
671 | |
672 | color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, pEa->m_lo); |
673 | color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, pEa->m_hi); |
674 | pResults->m_pbits[0] = 0; |
675 | pResults->m_pbits[1] = 0; |
676 | |
677 | for (uint32_t i = 0; i < 4; i++) |
678 | { |
679 | pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index; |
680 | pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index; |
681 | } |
682 | |
683 | memset(pSelectors, BC7ENC_ASTC_4BIT_2BIT_OPTIMAL_INDEX, pParams->m_num_pixels); |
684 | |
685 | color_quad_u8 p; |
686 | for (uint32_t i = 0; i < 4; i++) |
687 | { |
688 | uint32_t low = (pResults->m_low_endpoint.m_c[i] << 4) | pResults->m_low_endpoint.m_c[i]; |
689 | uint32_t high = (pResults->m_high_endpoint.m_c[i] << 4) | pResults->m_high_endpoint.m_c[i]; |
690 | |
691 | p.m_c[i] = (uint8_t)astc_interpolate_linear(low, high, g_bc7_weights2[BC7ENC_ASTC_4BIT_2BIT_OPTIMAL_INDEX]); |
692 | } |
693 | |
694 | uint64_t total_err = 0; |
695 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
696 | total_err += compute_color_distance_rgba(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights); |
697 | |
698 | pResults->m_best_overall_err = total_err; |
699 | |
700 | return total_err; |
701 | } |
702 | |
703 | static uint64_t pack_astc_range7_2bit_to_one_color(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, uint32_t r, uint32_t g, uint32_t b, uint8_t *pSelectors) |
704 | { |
705 | assert(pParams->m_astc_endpoint_range == 7 && pParams->m_num_selector_weights == 4); |
706 | |
707 | const endpoint_err *pEr = &g_astc_range7_2bit_optimal_endpoints[r]; |
708 | const endpoint_err *pEg = &g_astc_range7_2bit_optimal_endpoints[g]; |
709 | const endpoint_err *pEb = &g_astc_range7_2bit_optimal_endpoints[b]; |
710 | |
711 | color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 0); |
712 | color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, 0); |
713 | pResults->m_pbits[0] = 0; |
714 | pResults->m_pbits[1] = 0; |
715 | |
716 | for (uint32_t i = 0; i < 4; i++) |
717 | { |
718 | pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index; |
719 | pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index; |
720 | } |
721 | |
722 | memset(pSelectors, BC7ENC_ASTC_RANGE7_2BIT_OPTIMAL_INDEX, pParams->m_num_pixels); |
723 | |
724 | color_quad_u8 p; |
725 | for (uint32_t i = 0; i < 3; i++) |
726 | { |
727 | uint32_t low = g_astc_sorted_order_unquant[7][pResults->m_low_endpoint.m_c[i]].m_unquant; |
728 | uint32_t high = g_astc_sorted_order_unquant[7][pResults->m_high_endpoint.m_c[i]].m_unquant; |
729 | |
730 | p.m_c[i] = (uint8_t)astc_interpolate_linear(low, high, g_bc7_weights2[BC7ENC_ASTC_RANGE7_2BIT_OPTIMAL_INDEX]); |
731 | } |
732 | p.m_c[3] = 255; |
733 | |
734 | uint64_t total_err = 0; |
735 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
736 | total_err += compute_color_distance_rgb(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights); |
737 | |
738 | pResults->m_best_overall_err = total_err; |
739 | |
740 | return total_err; |
741 | } |
742 | |
743 | static uint64_t pack_astc_range13_2bit_to_one_color(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, uint32_t r, uint32_t g, uint32_t b, uint8_t *pSelectors) |
744 | { |
745 | assert(pParams->m_astc_endpoint_range == 13 && pParams->m_num_selector_weights == 4 && !pParams->m_has_alpha); |
746 | |
747 | const endpoint_err *pEr = &g_astc_range13_2bit_optimal_endpoints[r]; |
748 | const endpoint_err *pEg = &g_astc_range13_2bit_optimal_endpoints[g]; |
749 | const endpoint_err *pEb = &g_astc_range13_2bit_optimal_endpoints[b]; |
750 | |
751 | color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 47); |
752 | color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, 47); |
753 | pResults->m_pbits[0] = 0; |
754 | pResults->m_pbits[1] = 0; |
755 | |
756 | for (uint32_t i = 0; i < 4; i++) |
757 | { |
758 | pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index; |
759 | pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index; |
760 | } |
761 | |
762 | memset(pSelectors, BC7ENC_ASTC_RANGE13_2BIT_OPTIMAL_INDEX, pParams->m_num_pixels); |
763 | |
764 | color_quad_u8 p; |
765 | for (uint32_t i = 0; i < 4; i++) |
766 | { |
767 | uint32_t low = g_astc_sorted_order_unquant[13][pResults->m_low_endpoint.m_c[i]].m_unquant; |
768 | uint32_t high = g_astc_sorted_order_unquant[13][pResults->m_high_endpoint.m_c[i]].m_unquant; |
769 | |
770 | p.m_c[i] = (uint8_t)astc_interpolate_linear(low, high, g_bc7_weights2[BC7ENC_ASTC_RANGE13_2BIT_OPTIMAL_INDEX]); |
771 | } |
772 | |
773 | uint64_t total_err = 0; |
774 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
775 | total_err += compute_color_distance_rgb(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights); |
776 | |
777 | pResults->m_best_overall_err = total_err; |
778 | |
779 | return total_err; |
780 | } |
781 | |
782 | static uint64_t pack_astc_range11_5bit_to_one_color(const color_cell_compressor_params* pParams, color_cell_compressor_results* pResults, uint32_t r, uint32_t g, uint32_t b, uint8_t* pSelectors) |
783 | { |
784 | assert(pParams->m_astc_endpoint_range == 11 && pParams->m_num_selector_weights == 32 && !pParams->m_has_alpha); |
785 | |
786 | const endpoint_err* pEr = &g_astc_range11_5bit_optimal_endpoints[r]; |
787 | const endpoint_err* pEg = &g_astc_range11_5bit_optimal_endpoints[g]; |
788 | const endpoint_err* pEb = &g_astc_range11_5bit_optimal_endpoints[b]; |
789 | |
790 | color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 31); |
791 | color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, 31); |
792 | pResults->m_pbits[0] = 0; |
793 | pResults->m_pbits[1] = 0; |
794 | |
795 | for (uint32_t i = 0; i < 4; i++) |
796 | { |
797 | pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index; |
798 | pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index; |
799 | } |
800 | |
801 | memset(pSelectors, BC7ENC_ASTC_RANGE11_5BIT_OPTIMAL_INDEX, pParams->m_num_pixels); |
802 | |
803 | color_quad_u8 p; |
804 | for (uint32_t i = 0; i < 4; i++) |
805 | { |
806 | uint32_t low = g_astc_sorted_order_unquant[11][pResults->m_low_endpoint.m_c[i]].m_unquant; |
807 | uint32_t high = g_astc_sorted_order_unquant[11][pResults->m_high_endpoint.m_c[i]].m_unquant; |
808 | |
809 | p.m_c[i] = (uint8_t)astc_interpolate_linear(low, high, g_astc_weights5[BC7ENC_ASTC_RANGE11_5BIT_OPTIMAL_INDEX]); |
810 | } |
811 | |
812 | uint64_t total_err = 0; |
813 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
814 | total_err += compute_color_distance_rgb(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights); |
815 | |
816 | pResults->m_best_overall_err = total_err; |
817 | |
818 | return total_err; |
819 | } |
820 | |
821 | static uint64_t evaluate_solution(const color_quad_u8 *pLow, const color_quad_u8 *pHigh, const uint32_t pbits[2], const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults) |
822 | { |
823 | color_quad_u8 quantMinColor = *pLow; |
824 | color_quad_u8 quantMaxColor = *pHigh; |
825 | |
826 | if (pParams->m_has_pbits) |
827 | { |
828 | uint32_t minPBit, maxPBit; |
829 | |
830 | if (pParams->m_endpoints_share_pbit) |
831 | maxPBit = minPBit = pbits[0]; |
832 | else |
833 | { |
834 | minPBit = pbits[0]; |
835 | maxPBit = pbits[1]; |
836 | } |
837 | |
838 | quantMinColor.m_c[0] = (uint8_t)((pLow->m_c[0] << 1) | minPBit); |
839 | quantMinColor.m_c[1] = (uint8_t)((pLow->m_c[1] << 1) | minPBit); |
840 | quantMinColor.m_c[2] = (uint8_t)((pLow->m_c[2] << 1) | minPBit); |
841 | quantMinColor.m_c[3] = (uint8_t)((pLow->m_c[3] << 1) | minPBit); |
842 | |
843 | quantMaxColor.m_c[0] = (uint8_t)((pHigh->m_c[0] << 1) | maxPBit); |
844 | quantMaxColor.m_c[1] = (uint8_t)((pHigh->m_c[1] << 1) | maxPBit); |
845 | quantMaxColor.m_c[2] = (uint8_t)((pHigh->m_c[2] << 1) | maxPBit); |
846 | quantMaxColor.m_c[3] = (uint8_t)((pHigh->m_c[3] << 1) | maxPBit); |
847 | } |
848 | |
849 | color_quad_u8 actualMinColor = scale_color(&quantMinColor, pParams); |
850 | color_quad_u8 actualMaxColor = scale_color(&quantMaxColor, pParams); |
851 | |
852 | const uint32_t N = pParams->m_num_selector_weights; |
853 | assert(N >= 1 && N <= 32); |
854 | |
855 | color_quad_u8 weightedColors[32]; |
856 | weightedColors[0] = actualMinColor; |
857 | weightedColors[N - 1] = actualMaxColor; |
858 | |
859 | const uint32_t nc = pParams->m_has_alpha ? 4 : 3; |
860 | if (pParams->m_astc_endpoint_range) |
861 | { |
862 | for (uint32_t i = 1; i < (N - 1); i++) |
863 | { |
864 | for (uint32_t j = 0; j < nc; j++) |
865 | weightedColors[i].m_c[j] = (uint8_t)(astc_interpolate_linear(actualMinColor.m_c[j], actualMaxColor.m_c[j], pParams->m_pSelector_weights[i])); |
866 | } |
867 | } |
868 | else |
869 | { |
870 | for (uint32_t i = 1; i < (N - 1); i++) |
871 | for (uint32_t j = 0; j < nc; j++) |
872 | weightedColors[i].m_c[j] = (uint8_t)((actualMinColor.m_c[j] * (64 - pParams->m_pSelector_weights[i]) + actualMaxColor.m_c[j] * pParams->m_pSelector_weights[i] + 32) >> 6); |
873 | } |
874 | |
875 | const int lr = actualMinColor.m_c[0]; |
876 | const int lg = actualMinColor.m_c[1]; |
877 | const int lb = actualMinColor.m_c[2]; |
878 | const int dr = actualMaxColor.m_c[0] - lr; |
879 | const int dg = actualMaxColor.m_c[1] - lg; |
880 | const int db = actualMaxColor.m_c[2] - lb; |
881 | |
882 | uint64_t total_err = 0; |
883 | |
884 | if (pParams->m_pForce_selectors) |
885 | { |
886 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
887 | { |
888 | const color_quad_u8* pC = &pParams->m_pPixels[i]; |
889 | |
890 | const uint8_t sel = pParams->m_pForce_selectors[i]; |
891 | assert(sel < N); |
892 | |
893 | total_err += (pParams->m_has_alpha ? compute_color_distance_rgba : compute_color_distance_rgb)(&weightedColors[sel], pC, pParams->m_perceptual, pParams->m_weights); |
894 | |
895 | pResults->m_pSelectors_temp[i] = sel; |
896 | } |
897 | } |
898 | else if (!pParams->m_perceptual) |
899 | { |
900 | if (pParams->m_has_alpha) |
901 | { |
902 | const int la = actualMinColor.m_c[3]; |
903 | const int da = actualMaxColor.m_c[3] - la; |
904 | |
905 | const float f = N / (float)(squarei(dr) + squarei(dg) + squarei(db) + squarei(da) + .00000125f); |
906 | |
907 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
908 | { |
909 | const color_quad_u8 *pC = &pParams->m_pPixels[i]; |
910 | int r = pC->m_c[0]; |
911 | int g = pC->m_c[1]; |
912 | int b = pC->m_c[2]; |
913 | int a = pC->m_c[3]; |
914 | |
915 | int best_sel = (int)((float)((r - lr) * dr + (g - lg) * dg + (b - lb) * db + (a - la) * da) * f + .5f); |
916 | best_sel = clampi(best_sel, 1, N - 1); |
917 | |
918 | uint64_t err0 = compute_color_distance_rgba(&weightedColors[best_sel - 1], pC, BC7ENC_FALSE, pParams->m_weights); |
919 | uint64_t err1 = compute_color_distance_rgba(&weightedColors[best_sel], pC, BC7ENC_FALSE, pParams->m_weights); |
920 | |
921 | if (err0 == err1) |
922 | { |
923 | // Prefer non-interpolation |
924 | if ((best_sel - 1) == 0) |
925 | best_sel = 0; |
926 | } |
927 | else if (err1 > err0) |
928 | { |
929 | err1 = err0; |
930 | --best_sel; |
931 | } |
932 | total_err += err1; |
933 | |
934 | pResults->m_pSelectors_temp[i] = (uint8_t)best_sel; |
935 | } |
936 | } |
937 | else |
938 | { |
939 | const float f = N / (float)(squarei(dr) + squarei(dg) + squarei(db) + .00000125f); |
940 | |
941 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
942 | { |
943 | const color_quad_u8 *pC = &pParams->m_pPixels[i]; |
944 | int r = pC->m_c[0]; |
945 | int g = pC->m_c[1]; |
946 | int b = pC->m_c[2]; |
947 | |
948 | int sel = (int)((float)((r - lr) * dr + (g - lg) * dg + (b - lb) * db) * f + .5f); |
949 | sel = clampi(sel, 1, N - 1); |
950 | |
951 | uint64_t err0 = compute_color_distance_rgb(&weightedColors[sel - 1], pC, BC7ENC_FALSE, pParams->m_weights); |
952 | uint64_t err1 = compute_color_distance_rgb(&weightedColors[sel], pC, BC7ENC_FALSE, pParams->m_weights); |
953 | |
954 | int best_sel = sel; |
955 | uint64_t best_err = err1; |
956 | if (err0 == err1) |
957 | { |
958 | // Prefer non-interpolation |
959 | if ((best_sel - 1) == 0) |
960 | best_sel = 0; |
961 | } |
962 | else if (err0 < best_err) |
963 | { |
964 | best_err = err0; |
965 | best_sel = sel - 1; |
966 | } |
967 | |
968 | total_err += best_err; |
969 | |
970 | pResults->m_pSelectors_temp[i] = (uint8_t)best_sel; |
971 | } |
972 | } |
973 | } |
974 | else |
975 | { |
976 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
977 | { |
978 | uint64_t best_err = UINT64_MAX; |
979 | uint32_t best_sel = 0; |
980 | |
981 | if (pParams->m_has_alpha) |
982 | { |
983 | for (uint32_t j = 0; j < N; j++) |
984 | { |
985 | uint64_t err = compute_color_distance_rgba(&weightedColors[j], &pParams->m_pPixels[i], BC7ENC_TRUE, pParams->m_weights); |
986 | if (err < best_err) |
987 | { |
988 | best_err = err; |
989 | best_sel = j; |
990 | } |
991 | // Prefer non-interpolation |
992 | else if ((err == best_err) && (j == (N - 1))) |
993 | best_sel = j; |
994 | } |
995 | } |
996 | else |
997 | { |
998 | for (uint32_t j = 0; j < N; j++) |
999 | { |
1000 | uint64_t err = compute_color_distance_rgb(&weightedColors[j], &pParams->m_pPixels[i], BC7ENC_TRUE, pParams->m_weights); |
1001 | if (err < best_err) |
1002 | { |
1003 | best_err = err; |
1004 | best_sel = j; |
1005 | } |
1006 | // Prefer non-interpolation |
1007 | else if ((err == best_err) && (j == (N - 1))) |
1008 | best_sel = j; |
1009 | } |
1010 | } |
1011 | |
1012 | total_err += best_err; |
1013 | |
1014 | pResults->m_pSelectors_temp[i] = (uint8_t)best_sel; |
1015 | } |
1016 | } |
1017 | |
1018 | if (total_err < pResults->m_best_overall_err) |
1019 | { |
1020 | pResults->m_best_overall_err = total_err; |
1021 | |
1022 | pResults->m_low_endpoint = *pLow; |
1023 | pResults->m_high_endpoint = *pHigh; |
1024 | |
1025 | pResults->m_pbits[0] = pbits[0]; |
1026 | pResults->m_pbits[1] = pbits[1]; |
1027 | |
1028 | memcpy(pResults->m_pSelectors, pResults->m_pSelectors_temp, sizeof(pResults->m_pSelectors[0]) * pParams->m_num_pixels); |
1029 | } |
1030 | |
1031 | return total_err; |
1032 | } |
1033 | |
1034 | static bool areDegenerateEndpoints(color_quad_u8* pTrialMinColor, color_quad_u8* pTrialMaxColor, const bc7enc_vec4F* pXl, const bc7enc_vec4F* pXh) |
1035 | { |
1036 | for (uint32_t i = 0; i < 3; i++) |
1037 | { |
1038 | if (pTrialMinColor->m_c[i] == pTrialMaxColor->m_c[i]) |
1039 | { |
1040 | if (fabs(pXl->m_c[i] - pXh->m_c[i]) > 0.0f) |
1041 | return true; |
1042 | } |
1043 | } |
1044 | |
1045 | return false; |
1046 | } |
1047 | |
1048 | static void fixDegenerateEndpoints(uint32_t mode, color_quad_u8 *pTrialMinColor, color_quad_u8 *pTrialMaxColor, const bc7enc_vec4F*pXl, const bc7enc_vec4F*pXh, uint32_t iscale, int flags) |
1049 | { |
1050 | if (mode == 255) |
1051 | { |
1052 | for (uint32_t i = 0; i < 3; i++) |
1053 | { |
1054 | if (pTrialMinColor->m_c[i] == pTrialMaxColor->m_c[i]) |
1055 | { |
1056 | if (fabs(pXl->m_c[i] - pXh->m_c[i]) > 0.000125f) |
1057 | { |
1058 | if (flags & 1) |
1059 | { |
1060 | if (pTrialMinColor->m_c[i] > 0) |
1061 | pTrialMinColor->m_c[i]--; |
1062 | } |
1063 | if (flags & 2) |
1064 | { |
1065 | if (pTrialMaxColor->m_c[i] < iscale) |
1066 | pTrialMaxColor->m_c[i]++; |
1067 | } |
1068 | } |
1069 | } |
1070 | } |
1071 | } |
1072 | else if (mode == 1) |
1073 | { |
1074 | // fix degenerate case where the input collapses to a single colorspace voxel, and we loose all freedom (test with grayscale ramps) |
1075 | for (uint32_t i = 0; i < 3; i++) |
1076 | { |
1077 | if (pTrialMinColor->m_c[i] == pTrialMaxColor->m_c[i]) |
1078 | { |
1079 | if (fabs(pXl->m_c[i] - pXh->m_c[i]) > 0.000125f) |
1080 | { |
1081 | if (pTrialMinColor->m_c[i] > (iscale >> 1)) |
1082 | { |
1083 | if (pTrialMinColor->m_c[i] > 0) |
1084 | pTrialMinColor->m_c[i]--; |
1085 | else |
1086 | if (pTrialMaxColor->m_c[i] < iscale) |
1087 | pTrialMaxColor->m_c[i]++; |
1088 | } |
1089 | else |
1090 | { |
1091 | if (pTrialMaxColor->m_c[i] < iscale) |
1092 | pTrialMaxColor->m_c[i]++; |
1093 | else if (pTrialMinColor->m_c[i] > 0) |
1094 | pTrialMinColor->m_c[i]--; |
1095 | } |
1096 | } |
1097 | } |
1098 | } |
1099 | } |
1100 | } |
1101 | |
1102 | static uint64_t find_optimal_solution(uint32_t mode, bc7enc_vec4F xl, bc7enc_vec4F xh, const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults) |
1103 | { |
1104 | vec4F_saturate_in_place(&xl); vec4F_saturate_in_place(&xh); |
1105 | |
1106 | if (pParams->m_astc_endpoint_range) |
1107 | { |
1108 | const uint32_t levels = astc_get_levels(pParams->m_astc_endpoint_range); |
1109 | |
1110 | const float scale = 255.0f; |
1111 | |
1112 | color_quad_u8 trialMinColor8Bit, trialMaxColor8Bit; |
1113 | color_quad_u8_set_clamped(&trialMinColor8Bit, (int)(xl.m_c[0] * scale + .5f), (int)(xl.m_c[1] * scale + .5f), (int)(xl.m_c[2] * scale + .5f), (int)(xl.m_c[3] * scale + .5f)); |
1114 | color_quad_u8_set_clamped(&trialMaxColor8Bit, (int)(xh.m_c[0] * scale + .5f), (int)(xh.m_c[1] * scale + .5f), (int)(xh.m_c[2] * scale + .5f), (int)(xh.m_c[3] * scale + .5f)); |
1115 | |
1116 | color_quad_u8 trialMinColor, trialMaxColor; |
1117 | for (uint32_t i = 0; i < 4; i++) |
1118 | { |
1119 | trialMinColor.m_c[i] = g_astc_nearest_sorted_index[pParams->m_astc_endpoint_range][trialMinColor8Bit.m_c[i]]; |
1120 | trialMaxColor.m_c[i] = g_astc_nearest_sorted_index[pParams->m_astc_endpoint_range][trialMaxColor8Bit.m_c[i]]; |
1121 | } |
1122 | |
1123 | if (areDegenerateEndpoints(&trialMinColor, &trialMaxColor, &xl, &xh)) |
1124 | { |
1125 | color_quad_u8 trialMinColorOrig(trialMinColor), trialMaxColorOrig(trialMaxColor); |
1126 | |
1127 | fixDegenerateEndpoints(mode, &trialMinColor, &trialMaxColor, &xl, &xh, levels - 1, 1); |
1128 | if ((pResults->m_best_overall_err == UINT64_MAX) || color_quad_u8_notequals(&trialMinColor, &pResults->m_low_endpoint) || color_quad_u8_notequals(&trialMaxColor, &pResults->m_high_endpoint)) |
1129 | evaluate_solution(&trialMinColor, &trialMaxColor, pResults->m_pbits, pParams, pResults); |
1130 | |
1131 | trialMinColor = trialMinColorOrig; |
1132 | trialMaxColor = trialMaxColorOrig; |
1133 | fixDegenerateEndpoints(mode, &trialMinColor, &trialMaxColor, &xl, &xh, levels - 1, 0); |
1134 | if ((pResults->m_best_overall_err == UINT64_MAX) || color_quad_u8_notequals(&trialMinColor, &pResults->m_low_endpoint) || color_quad_u8_notequals(&trialMaxColor, &pResults->m_high_endpoint)) |
1135 | evaluate_solution(&trialMinColor, &trialMaxColor, pResults->m_pbits, pParams, pResults); |
1136 | |
1137 | trialMinColor = trialMinColorOrig; |
1138 | trialMaxColor = trialMaxColorOrig; |
1139 | fixDegenerateEndpoints(mode, &trialMinColor, &trialMaxColor, &xl, &xh, levels - 1, 2); |
1140 | if ((pResults->m_best_overall_err == UINT64_MAX) || color_quad_u8_notequals(&trialMinColor, &pResults->m_low_endpoint) || color_quad_u8_notequals(&trialMaxColor, &pResults->m_high_endpoint)) |
1141 | evaluate_solution(&trialMinColor, &trialMaxColor, pResults->m_pbits, pParams, pResults); |
1142 | |
1143 | trialMinColor = trialMinColorOrig; |
1144 | trialMaxColor = trialMaxColorOrig; |
1145 | fixDegenerateEndpoints(mode, &trialMinColor, &trialMaxColor, &xl, &xh, levels - 1, 3); |
1146 | if ((pResults->m_best_overall_err == UINT64_MAX) || color_quad_u8_notequals(&trialMinColor, &pResults->m_low_endpoint) || color_quad_u8_notequals(&trialMaxColor, &pResults->m_high_endpoint)) |
1147 | evaluate_solution(&trialMinColor, &trialMaxColor, pResults->m_pbits, pParams, pResults); |
1148 | } |
1149 | else |
1150 | { |
1151 | if ((pResults->m_best_overall_err == UINT64_MAX) || color_quad_u8_notequals(&trialMinColor, &pResults->m_low_endpoint) || color_quad_u8_notequals(&trialMaxColor, &pResults->m_high_endpoint)) |
1152 | { |
1153 | evaluate_solution(&trialMinColor, &trialMaxColor, pResults->m_pbits, pParams, pResults); |
1154 | } |
1155 | } |
1156 | |
1157 | for (uint32_t i = 0; i < 4; i++) |
1158 | { |
1159 | pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index; |
1160 | pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index; |
1161 | } |
1162 | } |
1163 | else if (pParams->m_has_pbits) |
1164 | { |
1165 | const int iscalep = (1 << (pParams->m_comp_bits + 1)) - 1; |
1166 | const float scalep = (float)iscalep; |
1167 | |
1168 | const int32_t totalComps = pParams->m_has_alpha ? 4 : 3; |
1169 | |
1170 | uint32_t best_pbits[2]; |
1171 | color_quad_u8 bestMinColor, bestMaxColor; |
1172 | |
1173 | if (!pParams->m_endpoints_share_pbit) |
1174 | { |
1175 | float best_err0 = 1e+9; |
1176 | float best_err1 = 1e+9; |
1177 | |
1178 | for (int p = 0; p < 2; p++) |
1179 | { |
1180 | color_quad_u8 xMinColor, xMaxColor; |
1181 | |
1182 | // Notes: The pbit controls which quantization intervals are selected. |
1183 | // total_levels=2^(comp_bits+1), where comp_bits=4 for mode 0, etc. |
1184 | // pbit 0: v=(b*2)/(total_levels-1), pbit 1: v=(b*2+1)/(total_levels-1) where b is the component bin from [0,total_levels/2-1] and v is the [0,1] component value |
1185 | // rearranging you get for pbit 0: b=floor(v*(total_levels-1)/2+.5) |
1186 | // rearranging you get for pbit 1: b=floor((v*(total_levels-1)-1)/2+.5) |
1187 | for (uint32_t c = 0; c < 4; c++) |
1188 | { |
1189 | xMinColor.m_c[c] = (uint8_t)(clampi(((int)((xl.m_c[c] * scalep - p) / 2.0f + .5f)) * 2 + p, p, iscalep - 1 + p)); |
1190 | xMaxColor.m_c[c] = (uint8_t)(clampi(((int)((xh.m_c[c] * scalep - p) / 2.0f + .5f)) * 2 + p, p, iscalep - 1 + p)); |
1191 | } |
1192 | |
1193 | color_quad_u8 scaledLow = scale_color(&xMinColor, pParams); |
1194 | color_quad_u8 scaledHigh = scale_color(&xMaxColor, pParams); |
1195 | |
1196 | float err0 = 0, err1 = 0; |
1197 | for (int i = 0; i < totalComps; i++) |
1198 | { |
1199 | err0 += squaref(scaledLow.m_c[i] - xl.m_c[i] * 255.0f); |
1200 | err1 += squaref(scaledHigh.m_c[i] - xh.m_c[i] * 255.0f); |
1201 | } |
1202 | |
1203 | if (err0 < best_err0) |
1204 | { |
1205 | best_err0 = err0; |
1206 | best_pbits[0] = p; |
1207 | |
1208 | bestMinColor.m_c[0] = xMinColor.m_c[0] >> 1; |
1209 | bestMinColor.m_c[1] = xMinColor.m_c[1] >> 1; |
1210 | bestMinColor.m_c[2] = xMinColor.m_c[2] >> 1; |
1211 | bestMinColor.m_c[3] = xMinColor.m_c[3] >> 1; |
1212 | } |
1213 | |
1214 | if (err1 < best_err1) |
1215 | { |
1216 | best_err1 = err1; |
1217 | best_pbits[1] = p; |
1218 | |
1219 | bestMaxColor.m_c[0] = xMaxColor.m_c[0] >> 1; |
1220 | bestMaxColor.m_c[1] = xMaxColor.m_c[1] >> 1; |
1221 | bestMaxColor.m_c[2] = xMaxColor.m_c[2] >> 1; |
1222 | bestMaxColor.m_c[3] = xMaxColor.m_c[3] >> 1; |
1223 | } |
1224 | } |
1225 | } |
1226 | else |
1227 | { |
1228 | // Endpoints share pbits |
1229 | float best_err = 1e+9; |
1230 | |
1231 | for (int p = 0; p < 2; p++) |
1232 | { |
1233 | color_quad_u8 xMinColor, xMaxColor; |
1234 | for (uint32_t c = 0; c < 4; c++) |
1235 | { |
1236 | xMinColor.m_c[c] = (uint8_t)(clampi(((int)((xl.m_c[c] * scalep - p) / 2.0f + .5f)) * 2 + p, p, iscalep - 1 + p)); |
1237 | xMaxColor.m_c[c] = (uint8_t)(clampi(((int)((xh.m_c[c] * scalep - p) / 2.0f + .5f)) * 2 + p, p, iscalep - 1 + p)); |
1238 | } |
1239 | |
1240 | color_quad_u8 scaledLow = scale_color(&xMinColor, pParams); |
1241 | color_quad_u8 scaledHigh = scale_color(&xMaxColor, pParams); |
1242 | |
1243 | float err = 0; |
1244 | for (int i = 0; i < totalComps; i++) |
1245 | err += squaref((scaledLow.m_c[i] / 255.0f) - xl.m_c[i]) + squaref((scaledHigh.m_c[i] / 255.0f) - xh.m_c[i]); |
1246 | |
1247 | if (err < best_err) |
1248 | { |
1249 | best_err = err; |
1250 | best_pbits[0] = p; |
1251 | best_pbits[1] = p; |
1252 | for (uint32_t j = 0; j < 4; j++) |
1253 | { |
1254 | bestMinColor.m_c[j] = xMinColor.m_c[j] >> 1; |
1255 | bestMaxColor.m_c[j] = xMaxColor.m_c[j] >> 1; |
1256 | } |
1257 | } |
1258 | } |
1259 | } |
1260 | |
1261 | fixDegenerateEndpoints(mode, &bestMinColor, &bestMaxColor, &xl, &xh, iscalep >> 1, 0); |
1262 | |
1263 | if ((pResults->m_best_overall_err == UINT64_MAX) || color_quad_u8_notequals(&bestMinColor, &pResults->m_low_endpoint) || color_quad_u8_notequals(&bestMaxColor, &pResults->m_high_endpoint) || (best_pbits[0] != pResults->m_pbits[0]) || (best_pbits[1] != pResults->m_pbits[1])) |
1264 | evaluate_solution(&bestMinColor, &bestMaxColor, best_pbits, pParams, pResults); |
1265 | } |
1266 | else |
1267 | { |
1268 | const int iscale = (1 << pParams->m_comp_bits) - 1; |
1269 | const float scale = (float)iscale; |
1270 | |
1271 | color_quad_u8 trialMinColor, trialMaxColor; |
1272 | color_quad_u8_set_clamped(&trialMinColor, (int)(xl.m_c[0] * scale + .5f), (int)(xl.m_c[1] * scale + .5f), (int)(xl.m_c[2] * scale + .5f), (int)(xl.m_c[3] * scale + .5f)); |
1273 | color_quad_u8_set_clamped(&trialMaxColor, (int)(xh.m_c[0] * scale + .5f), (int)(xh.m_c[1] * scale + .5f), (int)(xh.m_c[2] * scale + .5f), (int)(xh.m_c[3] * scale + .5f)); |
1274 | |
1275 | fixDegenerateEndpoints(mode, &trialMinColor, &trialMaxColor, &xl, &xh, iscale, 0); |
1276 | |
1277 | if ((pResults->m_best_overall_err == UINT64_MAX) || color_quad_u8_notequals(&trialMinColor, &pResults->m_low_endpoint) || color_quad_u8_notequals(&trialMaxColor, &pResults->m_high_endpoint)) |
1278 | evaluate_solution(&trialMinColor, &trialMaxColor, pResults->m_pbits, pParams, pResults); |
1279 | } |
1280 | |
1281 | return pResults->m_best_overall_err; |
1282 | } |
1283 | |
1284 | void check_best_overall_error(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults) |
1285 | { |
1286 | const uint32_t n = pParams->m_num_selector_weights; |
1287 | |
1288 | assert(n <= 32); |
1289 | |
1290 | color_quad_u8 colors[32]; |
1291 | for (uint32_t c = 0; c < 4; c++) |
1292 | { |
1293 | colors[0].m_c[c] = g_astc_unquant[pParams->m_astc_endpoint_range][pResults->m_astc_low_endpoint.m_c[c]].m_unquant; |
1294 | assert(colors[0].m_c[c] == g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[c]].m_unquant); |
1295 | |
1296 | colors[n-1].m_c[c] = g_astc_unquant[pParams->m_astc_endpoint_range][pResults->m_astc_high_endpoint.m_c[c]].m_unquant; |
1297 | assert(colors[n-1].m_c[c] == g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[c]].m_unquant); |
1298 | } |
1299 | |
1300 | for (uint32_t i = 1; i < pParams->m_num_selector_weights - 1; i++) |
1301 | for (uint32_t c = 0; c < 4; c++) |
1302 | colors[i].m_c[c] = (uint8_t)astc_interpolate_linear(colors[0].m_c[c], colors[n - 1].m_c[c], pParams->m_pSelector_weights[i]); |
1303 | |
1304 | uint64_t total_err = 0; |
1305 | for (uint32_t p = 0; p < pParams->m_num_pixels; p++) |
1306 | { |
1307 | const color_quad_u8 &orig = pParams->m_pPixels[p]; |
1308 | const color_quad_u8 &packed = colors[pResults->m_pSelectors[p]]; |
1309 | |
1310 | if (pParams->m_has_alpha) |
1311 | total_err += compute_color_distance_rgba(&orig, &packed, pParams->m_perceptual, pParams->m_weights); |
1312 | else |
1313 | total_err += compute_color_distance_rgb(&orig, &packed, pParams->m_perceptual, pParams->m_weights); |
1314 | } |
1315 | assert(total_err == pResults->m_best_overall_err); |
1316 | |
1317 | // HACK HACK |
1318 | //if (total_err != pResults->m_best_overall_err) |
1319 | // printf("X"); |
1320 | } |
1321 | |
1322 | static bool is_solid_rgb(const color_cell_compressor_params *pParams, uint32_t &r, uint32_t &g, uint32_t &b) |
1323 | { |
1324 | r = pParams->m_pPixels[0].m_c[0]; |
1325 | g = pParams->m_pPixels[0].m_c[1]; |
1326 | b = pParams->m_pPixels[0].m_c[2]; |
1327 | |
1328 | bool allSame = true; |
1329 | for (uint32_t i = 1; i < pParams->m_num_pixels; i++) |
1330 | { |
1331 | if ((r != pParams->m_pPixels[i].m_c[0]) || (g != pParams->m_pPixels[i].m_c[1]) || (b != pParams->m_pPixels[i].m_c[2])) |
1332 | { |
1333 | allSame = false; |
1334 | break; |
1335 | } |
1336 | } |
1337 | |
1338 | return allSame; |
1339 | } |
1340 | |
1341 | static bool is_solid_rgba(const color_cell_compressor_params *pParams, uint32_t &r, uint32_t &g, uint32_t &b, uint32_t &a) |
1342 | { |
1343 | r = pParams->m_pPixels[0].m_c[0]; |
1344 | g = pParams->m_pPixels[0].m_c[1]; |
1345 | b = pParams->m_pPixels[0].m_c[2]; |
1346 | a = pParams->m_pPixels[0].m_c[3]; |
1347 | |
1348 | bool allSame = true; |
1349 | for (uint32_t i = 1; i < pParams->m_num_pixels; i++) |
1350 | { |
1351 | if ((r != pParams->m_pPixels[i].m_c[0]) || (g != pParams->m_pPixels[i].m_c[1]) || (b != pParams->m_pPixels[i].m_c[2]) || (a != pParams->m_pPixels[i].m_c[3])) |
1352 | { |
1353 | allSame = false; |
1354 | break; |
1355 | } |
1356 | } |
1357 | |
1358 | return allSame; |
1359 | } |
1360 | |
1361 | uint64_t color_cell_compression(uint32_t mode, const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, const bc7enc_compress_block_params *pComp_params) |
1362 | { |
1363 | if (!pParams->m_astc_endpoint_range) |
1364 | { |
1365 | assert((mode == 6) || (!pParams->m_has_alpha)); |
1366 | } |
1367 | assert(pParams->m_num_selector_weights >= 1 && pParams->m_num_selector_weights <= 32); |
1368 | assert(pParams->m_pSelector_weights[0] == 0); |
1369 | assert(pParams->m_pSelector_weights[pParams->m_num_selector_weights - 1] == 64); |
1370 | |
1371 | pResults->m_best_overall_err = UINT64_MAX; |
1372 | |
1373 | uint32_t cr, cg, cb, ca; |
1374 | |
1375 | // If the partition's colors are all the same, then just pack them as a single color. |
1376 | if (!pParams->m_pForce_selectors) |
1377 | { |
1378 | if (mode == 1) |
1379 | { |
1380 | if (is_solid_rgb(pParams, cr, cg, cb)) |
1381 | return pack_mode1_to_one_color(pParams, pResults, cr, cg, cb, pResults->m_pSelectors); |
1382 | } |
1383 | else if ((pParams->m_astc_endpoint_range == 8) && (pParams->m_num_selector_weights == 8) && (!pParams->m_has_alpha)) |
1384 | { |
1385 | if (is_solid_rgb(pParams, cr, cg, cb)) |
1386 | return pack_astc_4bit_3bit_to_one_color(pParams, pResults, cr, cg, cb, pResults->m_pSelectors); |
1387 | } |
1388 | else if ((pParams->m_astc_endpoint_range == 7) && (pParams->m_num_selector_weights == 4) && (!pParams->m_has_alpha)) |
1389 | { |
1390 | if (is_solid_rgb(pParams, cr, cg, cb)) |
1391 | return pack_astc_range7_2bit_to_one_color(pParams, pResults, cr, cg, cb, pResults->m_pSelectors); |
1392 | } |
1393 | else if ((pParams->m_astc_endpoint_range == 8) && (pParams->m_num_selector_weights == 4) && (pParams->m_has_alpha)) |
1394 | { |
1395 | if (is_solid_rgba(pParams, cr, cg, cb, ca)) |
1396 | return pack_astc_4bit_2bit_to_one_color_rgba(pParams, pResults, cr, cg, cb, ca, pResults->m_pSelectors); |
1397 | } |
1398 | else if ((pParams->m_astc_endpoint_range == 13) && (pParams->m_num_selector_weights == 4) && (!pParams->m_has_alpha)) |
1399 | { |
1400 | if (is_solid_rgb(pParams, cr, cg, cb)) |
1401 | return pack_astc_range13_2bit_to_one_color(pParams, pResults, cr, cg, cb, pResults->m_pSelectors); |
1402 | } |
1403 | else if ((pParams->m_astc_endpoint_range == 11) && (pParams->m_num_selector_weights == 32) && (!pParams->m_has_alpha)) |
1404 | { |
1405 | if (is_solid_rgb(pParams, cr, cg, cb)) |
1406 | return pack_astc_range11_5bit_to_one_color(pParams, pResults, cr, cg, cb, pResults->m_pSelectors); |
1407 | } |
1408 | } |
1409 | |
1410 | // Compute partition's mean color and principle axis. |
1411 | bc7enc_vec4F meanColor, axis; |
1412 | vec4F_set_scalar(&meanColor, 0.0f); |
1413 | |
1414 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
1415 | { |
1416 | bc7enc_vec4F color = vec4F_from_color(&pParams->m_pPixels[i]); |
1417 | meanColor = vec4F_add(&meanColor, &color); |
1418 | } |
1419 | |
1420 | bc7enc_vec4F meanColorScaled = vec4F_mul(&meanColor, 1.0f / (float)(pParams->m_num_pixels)); |
1421 | |
1422 | meanColor = vec4F_mul(&meanColor, 1.0f / (float)(pParams->m_num_pixels * 255.0f)); |
1423 | vec4F_saturate_in_place(&meanColor); |
1424 | |
1425 | if (pParams->m_has_alpha) |
1426 | { |
1427 | // Use incremental PCA for RGBA PCA, because it's simple. |
1428 | vec4F_set_scalar(&axis, 0.0f); |
1429 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
1430 | { |
1431 | bc7enc_vec4F color = vec4F_from_color(&pParams->m_pPixels[i]); |
1432 | color = vec4F_sub(&color, &meanColorScaled); |
1433 | bc7enc_vec4F a = vec4F_mul(&color, color.m_c[0]); |
1434 | bc7enc_vec4F b = vec4F_mul(&color, color.m_c[1]); |
1435 | bc7enc_vec4F c = vec4F_mul(&color, color.m_c[2]); |
1436 | bc7enc_vec4F d = vec4F_mul(&color, color.m_c[3]); |
1437 | bc7enc_vec4F n = i ? axis : color; |
1438 | vec4F_normalize_in_place(&n); |
1439 | axis.m_c[0] += vec4F_dot(&a, &n); |
1440 | axis.m_c[1] += vec4F_dot(&b, &n); |
1441 | axis.m_c[2] += vec4F_dot(&c, &n); |
1442 | axis.m_c[3] += vec4F_dot(&d, &n); |
1443 | } |
1444 | vec4F_normalize_in_place(&axis); |
1445 | } |
1446 | else |
1447 | { |
1448 | // Use covar technique for RGB PCA, because it doesn't require per-pixel normalization. |
1449 | float cov[6] = { 0, 0, 0, 0, 0, 0 }; |
1450 | |
1451 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
1452 | { |
1453 | const color_quad_u8 *pV = &pParams->m_pPixels[i]; |
1454 | float r = pV->m_c[0] - meanColorScaled.m_c[0]; |
1455 | float g = pV->m_c[1] - meanColorScaled.m_c[1]; |
1456 | float b = pV->m_c[2] - meanColorScaled.m_c[2]; |
1457 | cov[0] += r*r; cov[1] += r*g; cov[2] += r*b; cov[3] += g*g; cov[4] += g*b; cov[5] += b*b; |
1458 | } |
1459 | |
1460 | float xr = .9f, xg = 1.0f, xb = .7f; |
1461 | for (uint32_t iter = 0; iter < 3; iter++) |
1462 | { |
1463 | float r = xr * cov[0] + xg * cov[1] + xb * cov[2]; |
1464 | float g = xr * cov[1] + xg * cov[3] + xb * cov[4]; |
1465 | float b = xr * cov[2] + xg * cov[4] + xb * cov[5]; |
1466 | |
1467 | float m = maximumf(maximumf(fabsf(r), fabsf(g)), fabsf(b)); |
1468 | if (m > 1e-10f) |
1469 | { |
1470 | m = 1.0f / m; |
1471 | r *= m; g *= m; b *= m; |
1472 | } |
1473 | |
1474 | xr = r; xg = g; xb = b; |
1475 | } |
1476 | |
1477 | float len = xr * xr + xg * xg + xb * xb; |
1478 | if (len < 1e-10f) |
1479 | vec4F_set_scalar(&axis, 0.0f); |
1480 | else |
1481 | { |
1482 | len = 1.0f / sqrtf(len); |
1483 | xr *= len; xg *= len; xb *= len; |
1484 | vec4F_set(&axis, xr, xg, xb, 0); |
1485 | } |
1486 | } |
1487 | |
1488 | if (vec4F_dot(&axis, &axis) < .5f) |
1489 | { |
1490 | if (pParams->m_perceptual) |
1491 | vec4F_set(&axis, .213f, .715f, .072f, pParams->m_has_alpha ? .715f : 0); |
1492 | else |
1493 | vec4F_set(&axis, 1.0f, 1.0f, 1.0f, pParams->m_has_alpha ? 1.0f : 0); |
1494 | vec4F_normalize_in_place(&axis); |
1495 | } |
1496 | |
1497 | bc7enc_vec4F minColor, maxColor; |
1498 | |
1499 | float l = 1e+9f, h = -1e+9f; |
1500 | |
1501 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
1502 | { |
1503 | bc7enc_vec4F color = vec4F_from_color(&pParams->m_pPixels[i]); |
1504 | |
1505 | bc7enc_vec4F q = vec4F_sub(&color, &meanColorScaled); |
1506 | float d = vec4F_dot(&q, &axis); |
1507 | |
1508 | l = minimumf(l, d); |
1509 | h = maximumf(h, d); |
1510 | } |
1511 | |
1512 | l *= (1.0f / 255.0f); |
1513 | h *= (1.0f / 255.0f); |
1514 | |
1515 | bc7enc_vec4F b0 = vec4F_mul(&axis, l); |
1516 | bc7enc_vec4F b1 = vec4F_mul(&axis, h); |
1517 | bc7enc_vec4F c0 = vec4F_add(&meanColor, &b0); |
1518 | bc7enc_vec4F c1 = vec4F_add(&meanColor, &b1); |
1519 | minColor = vec4F_saturate(&c0); |
1520 | maxColor = vec4F_saturate(&c1); |
1521 | |
1522 | bc7enc_vec4F whiteVec; |
1523 | vec4F_set_scalar(&whiteVec, 1.0f); |
1524 | if (vec4F_dot(&minColor, &whiteVec) > vec4F_dot(&maxColor, &whiteVec)) |
1525 | { |
1526 | #if 1 |
1527 | std::swap(minColor.m_c[0], maxColor.m_c[0]); |
1528 | std::swap(minColor.m_c[1], maxColor.m_c[1]); |
1529 | std::swap(minColor.m_c[2], maxColor.m_c[2]); |
1530 | std::swap(minColor.m_c[3], maxColor.m_c[3]); |
1531 | #elif 0 |
1532 | // Fails to compile correctly with MSVC 2019 (code generation bug) |
1533 | std::swap(minColor, maxColor); |
1534 | #else |
1535 | // Fails with MSVC 2019 |
1536 | bc7enc_vec4F temp = minColor; |
1537 | minColor = maxColor; |
1538 | maxColor = temp; |
1539 | #endif |
1540 | } |
1541 | |
1542 | // First find a solution using the block's PCA. |
1543 | if (!find_optimal_solution(mode, minColor, maxColor, pParams, pResults)) |
1544 | return 0; |
1545 | |
1546 | for (uint32_t i = 0; i < pComp_params->m_least_squares_passes; i++) |
1547 | { |
1548 | // Now try to refine the solution using least squares by computing the optimal endpoints from the current selectors. |
1549 | bc7enc_vec4F xl, xh; |
1550 | vec4F_set_scalar(&xl, 0.0f); |
1551 | vec4F_set_scalar(&xh, 0.0f); |
1552 | if (pParams->m_has_alpha) |
1553 | compute_least_squares_endpoints_rgba(pParams->m_num_pixels, pResults->m_pSelectors, pParams->m_pSelector_weightsx, &xl, &xh, pParams->m_pPixels); |
1554 | else |
1555 | compute_least_squares_endpoints_rgb(pParams->m_num_pixels, pResults->m_pSelectors, pParams->m_pSelector_weightsx, &xl, &xh, pParams->m_pPixels); |
1556 | |
1557 | xl = vec4F_mul(&xl, (1.0f / 255.0f)); |
1558 | xh = vec4F_mul(&xh, (1.0f / 255.0f)); |
1559 | |
1560 | if (!find_optimal_solution(mode, xl, xh, pParams, pResults)) |
1561 | return 0; |
1562 | } |
1563 | |
1564 | if ((!pParams->m_pForce_selectors) && (pComp_params->m_uber_level > 0)) |
1565 | { |
1566 | // In uber level 1, try varying the selectors a little, somewhat like cluster fit would. First try incrementing the minimum selectors, |
1567 | // then try decrementing the selectrors, then try both. |
1568 | uint8_t selectors_temp[16], selectors_temp1[16]; |
1569 | memcpy(selectors_temp, pResults->m_pSelectors, pParams->m_num_pixels); |
1570 | |
1571 | const int max_selector = pParams->m_num_selector_weights - 1; |
1572 | |
1573 | uint32_t min_sel = 256; |
1574 | uint32_t max_sel = 0; |
1575 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
1576 | { |
1577 | uint32_t sel = selectors_temp[i]; |
1578 | min_sel = minimumu(min_sel, sel); |
1579 | max_sel = maximumu(max_sel, sel); |
1580 | } |
1581 | |
1582 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
1583 | { |
1584 | uint32_t sel = selectors_temp[i]; |
1585 | if ((sel == min_sel) && (sel < (pParams->m_num_selector_weights - 1))) |
1586 | sel++; |
1587 | selectors_temp1[i] = (uint8_t)sel; |
1588 | } |
1589 | |
1590 | bc7enc_vec4F xl, xh; |
1591 | vec4F_set_scalar(&xl, 0.0f); |
1592 | vec4F_set_scalar(&xh, 0.0f); |
1593 | if (pParams->m_has_alpha) |
1594 | compute_least_squares_endpoints_rgba(pParams->m_num_pixels, selectors_temp1, pParams->m_pSelector_weightsx, &xl, &xh, pParams->m_pPixels); |
1595 | else |
1596 | compute_least_squares_endpoints_rgb(pParams->m_num_pixels, selectors_temp1, pParams->m_pSelector_weightsx, &xl, &xh, pParams->m_pPixels); |
1597 | |
1598 | xl = vec4F_mul(&xl, (1.0f / 255.0f)); |
1599 | xh = vec4F_mul(&xh, (1.0f / 255.0f)); |
1600 | |
1601 | if (!find_optimal_solution(mode, xl, xh, pParams, pResults)) |
1602 | return 0; |
1603 | |
1604 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
1605 | { |
1606 | uint32_t sel = selectors_temp[i]; |
1607 | if ((sel == max_sel) && (sel > 0)) |
1608 | sel--; |
1609 | selectors_temp1[i] = (uint8_t)sel; |
1610 | } |
1611 | |
1612 | if (pParams->m_has_alpha) |
1613 | compute_least_squares_endpoints_rgba(pParams->m_num_pixels, selectors_temp1, pParams->m_pSelector_weightsx, &xl, &xh, pParams->m_pPixels); |
1614 | else |
1615 | compute_least_squares_endpoints_rgb(pParams->m_num_pixels, selectors_temp1, pParams->m_pSelector_weightsx, &xl, &xh, pParams->m_pPixels); |
1616 | |
1617 | xl = vec4F_mul(&xl, (1.0f / 255.0f)); |
1618 | xh = vec4F_mul(&xh, (1.0f / 255.0f)); |
1619 | |
1620 | if (!find_optimal_solution(mode, xl, xh, pParams, pResults)) |
1621 | return 0; |
1622 | |
1623 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
1624 | { |
1625 | uint32_t sel = selectors_temp[i]; |
1626 | if ((sel == min_sel) && (sel < (pParams->m_num_selector_weights - 1))) |
1627 | sel++; |
1628 | else if ((sel == max_sel) && (sel > 0)) |
1629 | sel--; |
1630 | selectors_temp1[i] = (uint8_t)sel; |
1631 | } |
1632 | |
1633 | if (pParams->m_has_alpha) |
1634 | compute_least_squares_endpoints_rgba(pParams->m_num_pixels, selectors_temp1, pParams->m_pSelector_weightsx, &xl, &xh, pParams->m_pPixels); |
1635 | else |
1636 | compute_least_squares_endpoints_rgb(pParams->m_num_pixels, selectors_temp1, pParams->m_pSelector_weightsx, &xl, &xh, pParams->m_pPixels); |
1637 | |
1638 | xl = vec4F_mul(&xl, (1.0f / 255.0f)); |
1639 | xh = vec4F_mul(&xh, (1.0f / 255.0f)); |
1640 | |
1641 | if (!find_optimal_solution(mode, xl, xh, pParams, pResults)) |
1642 | return 0; |
1643 | |
1644 | // In uber levels 2+, try taking more advantage of endpoint extrapolation by scaling the selectors in one direction or another. |
1645 | const uint32_t uber_err_thresh = (pParams->m_num_pixels * 56) >> 4; |
1646 | if ((pComp_params->m_uber_level >= 2) && (pResults->m_best_overall_err > uber_err_thresh)) |
1647 | { |
1648 | const int Q = (pComp_params->m_uber_level >= 4) ? (pComp_params->m_uber_level - 2) : 1; |
1649 | for (int ly = -Q; ly <= 1; ly++) |
1650 | { |
1651 | for (int hy = max_selector - 1; hy <= (max_selector + Q); hy++) |
1652 | { |
1653 | if ((ly == 0) && (hy == max_selector)) |
1654 | continue; |
1655 | |
1656 | for (uint32_t i = 0; i < pParams->m_num_pixels; i++) |
1657 | selectors_temp1[i] = (uint8_t)clampf(floorf((float)max_selector * ((float)selectors_temp[i] - (float)ly) / ((float)hy - (float)ly) + .5f), 0, (float)max_selector); |
1658 | |
1659 | //bc7enc_vec4F xl, xh; |
1660 | vec4F_set_scalar(&xl, 0.0f); |
1661 | vec4F_set_scalar(&xh, 0.0f); |
1662 | if (pParams->m_has_alpha) |
1663 | compute_least_squares_endpoints_rgba(pParams->m_num_pixels, selectors_temp1, pParams->m_pSelector_weightsx, &xl, &xh, pParams->m_pPixels); |
1664 | else |
1665 | compute_least_squares_endpoints_rgb(pParams->m_num_pixels, selectors_temp1, pParams->m_pSelector_weightsx, &xl, &xh, pParams->m_pPixels); |
1666 | |
1667 | xl = vec4F_mul(&xl, (1.0f / 255.0f)); |
1668 | xh = vec4F_mul(&xh, (1.0f / 255.0f)); |
1669 | |
1670 | if (!find_optimal_solution(mode, xl, xh, pParams, pResults)) |
1671 | return 0; |
1672 | } |
1673 | } |
1674 | } |
1675 | } |
1676 | |
1677 | if (!pParams->m_pForce_selectors) |
1678 | { |
1679 | // Try encoding the partition as a single color by using the optimal single colors tables to encode the block to its mean. |
1680 | if (mode == 1) |
1681 | { |
1682 | color_cell_compressor_results avg_results = *pResults; |
1683 | const uint32_t r = (int)(.5f + meanColor.m_c[0] * 255.0f), g = (int)(.5f + meanColor.m_c[1] * 255.0f), b = (int)(.5f + meanColor.m_c[2] * 255.0f); |
1684 | uint64_t avg_err = pack_mode1_to_one_color(pParams, &avg_results, r, g, b, pResults->m_pSelectors_temp); |
1685 | if (avg_err < pResults->m_best_overall_err) |
1686 | { |
1687 | *pResults = avg_results; |
1688 | memcpy(pResults->m_pSelectors, pResults->m_pSelectors_temp, sizeof(pResults->m_pSelectors[0]) * pParams->m_num_pixels); |
1689 | pResults->m_best_overall_err = avg_err; |
1690 | } |
1691 | } |
1692 | else if ((pParams->m_astc_endpoint_range == 8) && (pParams->m_num_selector_weights == 8) && (!pParams->m_has_alpha)) |
1693 | { |
1694 | color_cell_compressor_results avg_results = *pResults; |
1695 | const uint32_t r = (int)(.5f + meanColor.m_c[0] * 255.0f), g = (int)(.5f + meanColor.m_c[1] * 255.0f), b = (int)(.5f + meanColor.m_c[2] * 255.0f); |
1696 | uint64_t avg_err = pack_astc_4bit_3bit_to_one_color(pParams, &avg_results, r, g, b, pResults->m_pSelectors_temp); |
1697 | if (avg_err < pResults->m_best_overall_err) |
1698 | { |
1699 | *pResults = avg_results; |
1700 | memcpy(pResults->m_pSelectors, pResults->m_pSelectors_temp, sizeof(pResults->m_pSelectors[0]) * pParams->m_num_pixels); |
1701 | pResults->m_best_overall_err = avg_err; |
1702 | } |
1703 | } |
1704 | else if ((pParams->m_astc_endpoint_range == 7) && (pParams->m_num_selector_weights == 4) && (!pParams->m_has_alpha)) |
1705 | { |
1706 | color_cell_compressor_results avg_results = *pResults; |
1707 | const uint32_t r = (int)(.5f + meanColor.m_c[0] * 255.0f), g = (int)(.5f + meanColor.m_c[1] * 255.0f), b = (int)(.5f + meanColor.m_c[2] * 255.0f); |
1708 | uint64_t avg_err = pack_astc_range7_2bit_to_one_color(pParams, &avg_results, r, g, b, pResults->m_pSelectors_temp); |
1709 | if (avg_err < pResults->m_best_overall_err) |
1710 | { |
1711 | *pResults = avg_results; |
1712 | memcpy(pResults->m_pSelectors, pResults->m_pSelectors_temp, sizeof(pResults->m_pSelectors[0]) * pParams->m_num_pixels); |
1713 | pResults->m_best_overall_err = avg_err; |
1714 | } |
1715 | } |
1716 | else if ((pParams->m_astc_endpoint_range == 8) && (pParams->m_num_selector_weights == 4) && (pParams->m_has_alpha)) |
1717 | { |
1718 | color_cell_compressor_results avg_results = *pResults; |
1719 | const uint32_t r = (int)(.5f + meanColor.m_c[0] * 255.0f), g = (int)(.5f + meanColor.m_c[1] * 255.0f), b = (int)(.5f + meanColor.m_c[2] * 255.0f), a = (int)(.5f + meanColor.m_c[3] * 255.0f); |
1720 | uint64_t avg_err = pack_astc_4bit_2bit_to_one_color_rgba(pParams, &avg_results, r, g, b, a, pResults->m_pSelectors_temp); |
1721 | if (avg_err < pResults->m_best_overall_err) |
1722 | { |
1723 | *pResults = avg_results; |
1724 | memcpy(pResults->m_pSelectors, pResults->m_pSelectors_temp, sizeof(pResults->m_pSelectors[0]) * pParams->m_num_pixels); |
1725 | pResults->m_best_overall_err = avg_err; |
1726 | } |
1727 | } |
1728 | else if ((pParams->m_astc_endpoint_range == 13) && (pParams->m_num_selector_weights == 4) && (!pParams->m_has_alpha)) |
1729 | { |
1730 | color_cell_compressor_results avg_results = *pResults; |
1731 | const uint32_t r = (int)(.5f + meanColor.m_c[0] * 255.0f), g = (int)(.5f + meanColor.m_c[1] * 255.0f), b = (int)(.5f + meanColor.m_c[2] * 255.0f); |
1732 | uint64_t avg_err = pack_astc_range13_2bit_to_one_color(pParams, &avg_results, r, g, b, pResults->m_pSelectors_temp); |
1733 | if (avg_err < pResults->m_best_overall_err) |
1734 | { |
1735 | *pResults = avg_results; |
1736 | memcpy(pResults->m_pSelectors, pResults->m_pSelectors_temp, sizeof(pResults->m_pSelectors[0]) * pParams->m_num_pixels); |
1737 | pResults->m_best_overall_err = avg_err; |
1738 | } |
1739 | } |
1740 | else if ((pParams->m_astc_endpoint_range == 11) && (pParams->m_num_selector_weights == 32) && (!pParams->m_has_alpha)) |
1741 | { |
1742 | color_cell_compressor_results avg_results = *pResults; |
1743 | const uint32_t r = (int)(.5f + meanColor.m_c[0] * 255.0f), g = (int)(.5f + meanColor.m_c[1] * 255.0f), b = (int)(.5f + meanColor.m_c[2] * 255.0f); |
1744 | uint64_t avg_err = pack_astc_range11_5bit_to_one_color(pParams, &avg_results, r, g, b, pResults->m_pSelectors_temp); |
1745 | if (avg_err < pResults->m_best_overall_err) |
1746 | { |
1747 | *pResults = avg_results; |
1748 | memcpy(pResults->m_pSelectors, pResults->m_pSelectors_temp, sizeof(pResults->m_pSelectors[0]) * pParams->m_num_pixels); |
1749 | pResults->m_best_overall_err = avg_err; |
1750 | } |
1751 | } |
1752 | } |
1753 | |
1754 | #if BC7ENC_CHECK_OVERALL_ERROR |
1755 | check_best_overall_error(pParams, pResults); |
1756 | #endif |
1757 | |
1758 | return pResults->m_best_overall_err; |
1759 | } |
1760 | |
1761 | uint64_t color_cell_compression_est_astc( |
1762 | uint32_t num_weights, uint32_t num_comps, const uint32_t *pWeight_table, |
1763 | uint32_t num_pixels, const color_quad_u8* pPixels, |
1764 | uint64_t best_err_so_far, const uint32_t weights[4]) |
1765 | { |
1766 | assert(num_comps == 3 || num_comps == 4); |
1767 | assert(num_weights >= 1 && num_weights <= 32); |
1768 | assert(pWeight_table[0] == 0 && pWeight_table[num_weights - 1] == 64); |
1769 | |
1770 | // Find RGB bounds as an approximation of the block's principle axis |
1771 | uint32_t lr = 255, lg = 255, lb = 255, la = 255; |
1772 | uint32_t hr = 0, hg = 0, hb = 0, ha = 0; |
1773 | if (num_comps == 4) |
1774 | { |
1775 | for (uint32_t i = 0; i < num_pixels; i++) |
1776 | { |
1777 | const color_quad_u8* pC = &pPixels[i]; |
1778 | if (pC->m_c[0] < lr) lr = pC->m_c[0]; |
1779 | if (pC->m_c[1] < lg) lg = pC->m_c[1]; |
1780 | if (pC->m_c[2] < lb) lb = pC->m_c[2]; |
1781 | if (pC->m_c[3] < la) la = pC->m_c[3]; |
1782 | |
1783 | if (pC->m_c[0] > hr) hr = pC->m_c[0]; |
1784 | if (pC->m_c[1] > hg) hg = pC->m_c[1]; |
1785 | if (pC->m_c[2] > hb) hb = pC->m_c[2]; |
1786 | if (pC->m_c[3] > ha) ha = pC->m_c[3]; |
1787 | } |
1788 | } |
1789 | else |
1790 | { |
1791 | for (uint32_t i = 0; i < num_pixels; i++) |
1792 | { |
1793 | const color_quad_u8* pC = &pPixels[i]; |
1794 | if (pC->m_c[0] < lr) lr = pC->m_c[0]; |
1795 | if (pC->m_c[1] < lg) lg = pC->m_c[1]; |
1796 | if (pC->m_c[2] < lb) lb = pC->m_c[2]; |
1797 | |
1798 | if (pC->m_c[0] > hr) hr = pC->m_c[0]; |
1799 | if (pC->m_c[1] > hg) hg = pC->m_c[1]; |
1800 | if (pC->m_c[2] > hb) hb = pC->m_c[2]; |
1801 | } |
1802 | la = 255; |
1803 | ha = 255; |
1804 | } |
1805 | |
1806 | color_quad_u8 lowColor, highColor; |
1807 | color_quad_u8_set(&lowColor, lr, lg, lb, la); |
1808 | color_quad_u8_set(&highColor, hr, hg, hb, ha); |
1809 | |
1810 | // Place endpoints at bbox diagonals and compute interpolated colors |
1811 | color_quad_u8 weightedColors[32]; |
1812 | |
1813 | weightedColors[0] = lowColor; |
1814 | weightedColors[num_weights - 1] = highColor; |
1815 | for (uint32_t i = 1; i < (num_weights - 1); i++) |
1816 | { |
1817 | weightedColors[i].m_c[0] = (uint8_t)astc_interpolate_linear(lowColor.m_c[0], highColor.m_c[0], pWeight_table[i]); |
1818 | weightedColors[i].m_c[1] = (uint8_t)astc_interpolate_linear(lowColor.m_c[1], highColor.m_c[1], pWeight_table[i]); |
1819 | weightedColors[i].m_c[2] = (uint8_t)astc_interpolate_linear(lowColor.m_c[2], highColor.m_c[2], pWeight_table[i]); |
1820 | weightedColors[i].m_c[3] = (num_comps == 4) ? (uint8_t)astc_interpolate_linear(lowColor.m_c[3], highColor.m_c[3], pWeight_table[i]) : 255; |
1821 | } |
1822 | |
1823 | // Compute dots and thresholds |
1824 | const int ar = highColor.m_c[0] - lowColor.m_c[0]; |
1825 | const int ag = highColor.m_c[1] - lowColor.m_c[1]; |
1826 | const int ab = highColor.m_c[2] - lowColor.m_c[2]; |
1827 | const int aa = highColor.m_c[3] - lowColor.m_c[3]; |
1828 | |
1829 | int dots[32]; |
1830 | if (num_comps == 4) |
1831 | { |
1832 | for (uint32_t i = 0; i < num_weights; i++) |
1833 | dots[i] = weightedColors[i].m_c[0] * ar + weightedColors[i].m_c[1] * ag + weightedColors[i].m_c[2] * ab + weightedColors[i].m_c[3] * aa; |
1834 | } |
1835 | else |
1836 | { |
1837 | assert(aa == 0); |
1838 | for (uint32_t i = 0; i < num_weights; i++) |
1839 | dots[i] = weightedColors[i].m_c[0] * ar + weightedColors[i].m_c[1] * ag + weightedColors[i].m_c[2] * ab; |
1840 | } |
1841 | |
1842 | int thresh[32 - 1]; |
1843 | for (uint32_t i = 0; i < (num_weights - 1); i++) |
1844 | thresh[i] = (dots[i] + dots[i + 1] + 1) >> 1; |
1845 | |
1846 | uint64_t total_err = 0; |
1847 | if ((weights[0] | weights[1] | weights[2] | weights[3]) == 1) |
1848 | { |
1849 | if (num_comps == 4) |
1850 | { |
1851 | for (uint32_t i = 0; i < num_pixels; i++) |
1852 | { |
1853 | const color_quad_u8* pC = &pPixels[i]; |
1854 | |
1855 | int d = ar * pC->m_c[0] + ag * pC->m_c[1] + ab * pC->m_c[2] + aa * pC->m_c[3]; |
1856 | |
1857 | // Find approximate selector |
1858 | uint32_t s = 0; |
1859 | for (int j = num_weights - 2; j >= 0; j--) |
1860 | { |
1861 | if (d >= thresh[j]) |
1862 | { |
1863 | s = j + 1; |
1864 | break; |
1865 | } |
1866 | } |
1867 | |
1868 | // Compute error |
1869 | const color_quad_u8* pE1 = &weightedColors[s]; |
1870 | |
1871 | int dr = (int)pE1->m_c[0] - (int)pC->m_c[0]; |
1872 | int dg = (int)pE1->m_c[1] - (int)pC->m_c[1]; |
1873 | int db = (int)pE1->m_c[2] - (int)pC->m_c[2]; |
1874 | int da = (int)pE1->m_c[3] - (int)pC->m_c[3]; |
1875 | |
1876 | total_err += (dr * dr) + (dg * dg) + (db * db) + (da * da); |
1877 | if (total_err > best_err_so_far) |
1878 | break; |
1879 | } |
1880 | } |
1881 | else |
1882 | { |
1883 | for (uint32_t i = 0; i < num_pixels; i++) |
1884 | { |
1885 | const color_quad_u8* pC = &pPixels[i]; |
1886 | |
1887 | int d = ar * pC->m_c[0] + ag * pC->m_c[1] + ab * pC->m_c[2]; |
1888 | |
1889 | // Find approximate selector |
1890 | uint32_t s = 0; |
1891 | for (int j = num_weights - 2; j >= 0; j--) |
1892 | { |
1893 | if (d >= thresh[j]) |
1894 | { |
1895 | s = j + 1; |
1896 | break; |
1897 | } |
1898 | } |
1899 | |
1900 | // Compute error |
1901 | const color_quad_u8* pE1 = &weightedColors[s]; |
1902 | |
1903 | int dr = (int)pE1->m_c[0] - (int)pC->m_c[0]; |
1904 | int dg = (int)pE1->m_c[1] - (int)pC->m_c[1]; |
1905 | int db = (int)pE1->m_c[2] - (int)pC->m_c[2]; |
1906 | |
1907 | total_err += (dr * dr) + (dg * dg) + (db * db); |
1908 | if (total_err > best_err_so_far) |
1909 | break; |
1910 | } |
1911 | } |
1912 | } |
1913 | else |
1914 | { |
1915 | if (num_comps == 4) |
1916 | { |
1917 | for (uint32_t i = 0; i < num_pixels; i++) |
1918 | { |
1919 | const color_quad_u8* pC = &pPixels[i]; |
1920 | |
1921 | int d = ar * pC->m_c[0] + ag * pC->m_c[1] + ab * pC->m_c[2] + aa * pC->m_c[3]; |
1922 | |
1923 | // Find approximate selector |
1924 | uint32_t s = 0; |
1925 | for (int j = num_weights - 2; j >= 0; j--) |
1926 | { |
1927 | if (d >= thresh[j]) |
1928 | { |
1929 | s = j + 1; |
1930 | break; |
1931 | } |
1932 | } |
1933 | |
1934 | // Compute error |
1935 | const color_quad_u8* pE1 = &weightedColors[s]; |
1936 | |
1937 | int dr = (int)pE1->m_c[0] - (int)pC->m_c[0]; |
1938 | int dg = (int)pE1->m_c[1] - (int)pC->m_c[1]; |
1939 | int db = (int)pE1->m_c[2] - (int)pC->m_c[2]; |
1940 | int da = (int)pE1->m_c[3] - (int)pC->m_c[3]; |
1941 | |
1942 | total_err += weights[0] * (dr * dr) + weights[1] * (dg * dg) + weights[2] * (db * db) + weights[3] * (da * da); |
1943 | if (total_err > best_err_so_far) |
1944 | break; |
1945 | } |
1946 | } |
1947 | else |
1948 | { |
1949 | for (uint32_t i = 0; i < num_pixels; i++) |
1950 | { |
1951 | const color_quad_u8* pC = &pPixels[i]; |
1952 | |
1953 | int d = ar * pC->m_c[0] + ag * pC->m_c[1] + ab * pC->m_c[2]; |
1954 | |
1955 | // Find approximate selector |
1956 | uint32_t s = 0; |
1957 | for (int j = num_weights - 2; j >= 0; j--) |
1958 | { |
1959 | if (d >= thresh[j]) |
1960 | { |
1961 | s = j + 1; |
1962 | break; |
1963 | } |
1964 | } |
1965 | |
1966 | // Compute error |
1967 | const color_quad_u8* pE1 = &weightedColors[s]; |
1968 | |
1969 | int dr = (int)pE1->m_c[0] - (int)pC->m_c[0]; |
1970 | int dg = (int)pE1->m_c[1] - (int)pC->m_c[1]; |
1971 | int db = (int)pE1->m_c[2] - (int)pC->m_c[2]; |
1972 | |
1973 | total_err += weights[0] * (dr * dr) + weights[1] * (dg * dg) + weights[2] * (db * db); |
1974 | if (total_err > best_err_so_far) |
1975 | break; |
1976 | } |
1977 | } |
1978 | } |
1979 | |
1980 | return total_err; |
1981 | } |
1982 | |
1983 | } // namespace basisu |
1984 | |