/src/astc-encoder/Source/astcenc_decompress_symbolic.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // ---------------------------------------------------------------------------- |
3 | | // Copyright 2011-2025 Arm Limited |
4 | | // |
5 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
6 | | // use this file except in compliance with the License. You may obtain a copy |
7 | | // of the License at: |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, software |
12 | | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
13 | | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
14 | | // License for the specific language governing permissions and limitations |
15 | | // under the License. |
16 | | // ---------------------------------------------------------------------------- |
17 | | |
18 | | /** |
19 | | * @brief Functions to decompress a symbolic block. |
20 | | */ |
21 | | |
22 | | #include "astcenc_internal.h" |
23 | | |
24 | | #include <stdio.h> |
25 | | #include <assert.h> |
26 | | |
27 | | /** |
28 | | * @brief Compute the integer linear interpolation of two color endpoints. |
29 | | * |
30 | | * @param u8_mask The mask for lanes using decode_unorm8 rather than decode_f16. |
31 | | * @param color0 The endpoint0 color. |
32 | | * @param color1 The endpoint1 color. |
33 | | * @param weights The interpolation weight (between 0 and 64). |
34 | | * |
35 | | * @return The interpolated color. |
36 | | */ |
37 | | static vint4 lerp_color_int( |
38 | | vmask4 u8_mask, |
39 | | vint4 color0, |
40 | | vint4 color1, |
41 | | vint4 weights |
42 | 1.57M | ) { |
43 | 1.57M | vint4 weight1 = weights; |
44 | 1.57M | vint4 weight0 = vint4(64) - weight1; |
45 | | |
46 | 1.57M | vint4 color = (color0 * weight0) + (color1 * weight1) + vint4(32); |
47 | 1.57M | color = asr<6>(color); |
48 | | |
49 | | // For decode_unorm8 values force the codec to bit replicate. This allows the |
50 | | // rest of the codec to assume the full 0xFFFF range for everything and ignore |
51 | | // the decode_mode setting |
52 | 1.57M | vint4 color_u8 = asr<8>(color) * vint4(257); |
53 | 1.57M | color = select(color, color_u8, u8_mask); |
54 | | |
55 | 1.57M | return color; |
56 | 1.57M | } |
57 | | |
58 | | /** |
59 | | * @brief Convert integer color value into a float value for the decoder. |
60 | | * |
61 | | * @param data The integer color value post-interpolation. |
62 | | * @param lns_mask If set treat lane as HDR (LNS) else LDR (unorm16). |
63 | | * |
64 | | * @return The float color value. |
65 | | */ |
66 | | static inline vfloat4 decode_texel( |
67 | | vint4 data, |
68 | | vmask4 lns_mask |
69 | 191k | ) { |
70 | 191k | vint4 color_lns = vint4::zero(); |
71 | 191k | vint4 color_unorm = vint4::zero(); |
72 | | |
73 | 191k | if (any(lns_mask)) |
74 | 41.3k | { |
75 | 41.3k | color_lns = lns_to_sf16(data); |
76 | 41.3k | } |
77 | | |
78 | 191k | if (!all(lns_mask)) |
79 | 166k | { |
80 | 166k | color_unorm = unorm16_to_sf16(data); |
81 | 166k | } |
82 | | |
83 | | // Pick components and then convert to FP16 |
84 | 191k | vint4 datai = select(color_unorm, color_lns, lns_mask); |
85 | 191k | return float16_to_float(datai); |
86 | 191k | } |
87 | | |
88 | | /* See header for documentation. */ |
89 | | void unpack_weights( |
90 | | const block_size_descriptor& bsd, |
91 | | const symbolic_compressed_block& scb, |
92 | | const decimation_info& di, |
93 | | bool is_dual_plane, |
94 | | int weights_plane1[BLOCK_MAX_TEXELS], |
95 | | int weights_plane2[BLOCK_MAX_TEXELS] |
96 | 88.3k | ) { |
97 | | // Safe to overshoot as all arrays are allocated to full size |
98 | 88.3k | if (!is_dual_plane) |
99 | 58.3k | { |
100 | | // Build full 64-entry weight lookup table |
101 | 58.3k | vtable_64x8 table; |
102 | 58.3k | vtable_prepare(table, scb.weights); |
103 | | |
104 | 457k | for (unsigned int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH) |
105 | 399k | { |
106 | 399k | vint summed_value(8); |
107 | 399k | vint weight_count(di.texel_weight_count + i); |
108 | 399k | int max_weight_count = hmax_s(weight_count); |
109 | | |
110 | 399k | promise(max_weight_count > 0); |
111 | 1.07M | for (int j = 0; j < max_weight_count; j++) |
112 | 677k | { |
113 | 677k | vint texel_weights(di.texel_weights_tr[j] + i); |
114 | 677k | vint texel_weights_int(di.texel_weight_contribs_int_tr[j] + i); |
115 | | |
116 | 677k | summed_value += vtable_lookup_32bit(table, texel_weights) * texel_weights_int; |
117 | 677k | } |
118 | | |
119 | 399k | store(lsr<4>(summed_value), weights_plane1 + i); |
120 | 399k | } |
121 | 58.3k | } |
122 | 30.0k | else |
123 | 30.0k | { |
124 | | // Build a 32-entry weight lookup table per plane |
125 | | // Plane 1 |
126 | 30.0k | vtable_32x8 tab_plane1; |
127 | 30.0k | vtable_prepare(tab_plane1, scb.weights); |
128 | | |
129 | | // Plane 2 |
130 | 30.0k | vtable_32x8 tab_plane2; |
131 | 30.0k | vtable_prepare(tab_plane2, scb.weights + 32); |
132 | | |
133 | 229k | for (unsigned int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH) |
134 | 199k | { |
135 | 199k | vint sum_plane1(8); |
136 | 199k | vint sum_plane2(8); |
137 | | |
138 | 199k | vint weight_count(di.texel_weight_count + i); |
139 | 199k | int max_weight_count = hmax_s(weight_count); |
140 | | |
141 | 199k | promise(max_weight_count > 0); |
142 | 663k | for (int j = 0; j < max_weight_count; j++) |
143 | 463k | { |
144 | 463k | vint texel_weights(di.texel_weights_tr[j] + i); |
145 | 463k | vint texel_weights_int(di.texel_weight_contribs_int_tr[j] + i); |
146 | | |
147 | 463k | sum_plane1 += vtable_lookup_32bit(tab_plane1, texel_weights) * texel_weights_int; |
148 | 463k | sum_plane2 += vtable_lookup_32bit(tab_plane2, texel_weights) * texel_weights_int; |
149 | 463k | } |
150 | | |
151 | 199k | store(lsr<4>(sum_plane1), weights_plane1 + i); |
152 | 199k | store(lsr<4>(sum_plane2), weights_plane2 + i); |
153 | 199k | } |
154 | 30.0k | } |
155 | 88.3k | } |
156 | | |
157 | | /** |
158 | | * @brief Return an FP32 NaN value for use in error colors. |
159 | | * |
160 | | * This NaN encoding will turn into 0xFFFF when converted to an FP16 NaN. |
161 | | * |
162 | | * @return The float color value. |
163 | | */ |
164 | | static float error_color_nan() |
165 | 253k | { |
166 | 253k | return astc::uint_as_float(0xFFFFE000u); |
167 | 253k | } |
168 | | |
169 | | /* See header for documentation. */ |
170 | | void decompress_symbolic_block( |
171 | | astcenc_profile decode_mode, |
172 | | const block_size_descriptor& bsd, |
173 | | size_t pos_x, |
174 | | size_t pos_y, |
175 | | size_t pos_z, |
176 | | const symbolic_compressed_block& scb, |
177 | | image_block& blk |
178 | 3.91k | ) { |
179 | 3.91k | blk.pos_x = pos_x; |
180 | 3.91k | blk.pos_y = pos_y; |
181 | 3.91k | blk.pos_z = pos_z; |
182 | | |
183 | 3.91k | blk.data_min = vfloat4::zero(); |
184 | 3.91k | blk.data_mean = vfloat4::zero(); |
185 | 3.91k | blk.data_max = vfloat4::zero(); |
186 | 3.91k | blk.grayscale = false; |
187 | | |
188 | | // If we detected an error-block, blow up immediately. |
189 | 3.91k | if (scb.block_type == SYM_BTYPE_ERROR) |
190 | 978 | { |
191 | 64.4k | for (unsigned int i = 0; i < bsd.texel_count; i++) |
192 | 63.4k | { |
193 | 63.4k | blk.data_r[i] = error_color_nan(); |
194 | 63.4k | blk.data_g[i] = error_color_nan(); |
195 | 63.4k | blk.data_b[i] = error_color_nan(); |
196 | 63.4k | blk.data_a[i] = error_color_nan(); |
197 | 63.4k | blk.rgb_lns[i] = 0; |
198 | 63.4k | blk.alpha_lns[i] = 0; |
199 | 63.4k | } |
200 | | |
201 | 978 | return; |
202 | 978 | } |
203 | | |
204 | 2.93k | if ((scb.block_type == SYM_BTYPE_CONST_F16) || |
205 | 2.84k | (scb.block_type == SYM_BTYPE_CONST_U16)) |
206 | 395 | { |
207 | 395 | vfloat4 color; |
208 | 395 | uint8_t use_lns = 0; |
209 | | |
210 | | // UNORM16 constant color block |
211 | 395 | if (scb.block_type == SYM_BTYPE_CONST_U16) |
212 | 300 | { |
213 | 300 | vint4 colori(scb.constant_color); |
214 | | |
215 | | // Determine the UNORM8 rounding on the decode |
216 | 300 | vmask4 u8_mask = get_u8_component_mask(decode_mode, blk); |
217 | | |
218 | | // The real decoder would just use the top 8 bits, but we rescale |
219 | | // into a 16-bit value that rounds correctly. |
220 | 300 | vint4 colori_u8 = asr<8>(colori) * 257; |
221 | 300 | colori = select(colori, colori_u8, u8_mask); |
222 | | |
223 | 300 | vint4 colorf16 = unorm16_to_sf16(colori); |
224 | 300 | color = float16_to_float(colorf16); |
225 | 300 | } |
226 | | // FLOAT16 constant color block |
227 | 95 | else |
228 | 95 | { |
229 | 95 | switch (decode_mode) |
230 | 95 | { |
231 | 19 | case ASTCENC_PRF_LDR_SRGB: |
232 | 20 | case ASTCENC_PRF_LDR: |
233 | 20 | color = vfloat4(error_color_nan()); |
234 | 20 | break; |
235 | 23 | case ASTCENC_PRF_HDR_RGB_LDR_A: |
236 | 75 | case ASTCENC_PRF_HDR: |
237 | | // Constant-color block; unpack from FP16 to FP32. |
238 | 75 | color = float16_to_float(vint4(scb.constant_color)); |
239 | 75 | use_lns = 1; |
240 | 75 | break; |
241 | 95 | } |
242 | 95 | } |
243 | | |
244 | 24.6k | for (unsigned int i = 0; i < bsd.texel_count; i++) |
245 | 24.2k | { |
246 | 24.2k | blk.data_r[i] = color.lane<0>(); |
247 | 24.2k | blk.data_g[i] = color.lane<1>(); |
248 | 24.2k | blk.data_b[i] = color.lane<2>(); |
249 | 24.2k | blk.data_a[i] = color.lane<3>(); |
250 | 24.2k | blk.rgb_lns[i] = use_lns; |
251 | 24.2k | blk.alpha_lns[i] = use_lns; |
252 | 24.2k | } |
253 | | |
254 | 395 | return; |
255 | 395 | } |
256 | | |
257 | | // Get the appropriate partition-table entry |
258 | 2.54k | int partition_count = scb.partition_count; |
259 | 2.54k | const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index); |
260 | | |
261 | | // Get the appropriate block descriptors |
262 | 2.54k | const auto& bm = bsd.get_block_mode(scb.block_mode); |
263 | 2.54k | const auto& di = bsd.get_decimation_info(bm.decimation_mode); |
264 | | |
265 | 2.54k | bool is_dual_plane = static_cast<bool>(bm.is_dual_plane); |
266 | | |
267 | | // Unquantize and undecimate the weights |
268 | 2.54k | int plane1_weights[BLOCK_MAX_TEXELS]; |
269 | 2.54k | int plane2_weights[BLOCK_MAX_TEXELS]; |
270 | 2.54k | unpack_weights(bsd, scb, di, is_dual_plane, plane1_weights, plane2_weights); |
271 | | |
272 | | // Now that we have endpoint colors and weights, we can unpack texel colors |
273 | 2.54k | int plane2_component = scb.plane2_component; |
274 | 2.54k | vmask4 plane2_mask = vint4::lane_id() == vint4(plane2_component); |
275 | | |
276 | 2.54k | vmask4 u8_mask = get_u8_component_mask(decode_mode, blk); |
277 | | |
278 | 9.54k | for (int i = 0; i < partition_count; i++) |
279 | 7.00k | { |
280 | | // Decode the color endpoints for this partition |
281 | 7.00k | vint4 ep0; |
282 | 7.00k | vint4 ep1; |
283 | 7.00k | bool rgb_lns; |
284 | 7.00k | bool a_lns; |
285 | | |
286 | 7.00k | unpack_color_endpoints(decode_mode, |
287 | 7.00k | scb.color_formats[i], |
288 | 7.00k | scb.color_values[i], |
289 | 7.00k | rgb_lns, a_lns, |
290 | 7.00k | ep0, ep1); |
291 | | |
292 | 7.00k | vmask4 lns_mask(rgb_lns, rgb_lns, rgb_lns, a_lns); |
293 | | |
294 | 7.00k | int texel_count = pi.partition_texel_count[i]; |
295 | 198k | for (int j = 0; j < texel_count; j++) |
296 | 191k | { |
297 | 191k | int tix = pi.texels_of_partition[i][j]; |
298 | 191k | vint4 weight = select(vint4(plane1_weights[tix]), vint4(plane2_weights[tix]), plane2_mask); |
299 | 191k | vint4 color = lerp_color_int(u8_mask, ep0, ep1, weight); |
300 | 191k | vfloat4 colorf = decode_texel(color, lns_mask); |
301 | | |
302 | 191k | blk.data_r[tix] = colorf.lane<0>(); |
303 | 191k | blk.data_g[tix] = colorf.lane<1>(); |
304 | 191k | blk.data_b[tix] = colorf.lane<2>(); |
305 | 191k | blk.data_a[tix] = colorf.lane<3>(); |
306 | 191k | } |
307 | 7.00k | } |
308 | 2.54k | } |
309 | | |
310 | | #if !defined(ASTCENC_DECOMPRESS_ONLY) |
311 | | |
312 | | /* See header for documentation. */ |
313 | | float compute_symbolic_block_difference_2plane( |
314 | | const astcenc_config& config, |
315 | | const block_size_descriptor& bsd, |
316 | | const symbolic_compressed_block& scb, |
317 | | const image_block& blk |
318 | 34.2k | ) { |
319 | | // If we detected an error-block, blow up immediately. |
320 | 34.2k | if (scb.block_type == SYM_BTYPE_ERROR) |
321 | 4.81k | { |
322 | 4.81k | return ERROR_CALC_DEFAULT; |
323 | 4.81k | } |
324 | | |
325 | 34.2k | assert(scb.block_mode >= 0); |
326 | 29.4k | assert(scb.partition_count == 1); |
327 | 29.4k | assert(bsd.get_block_mode(scb.block_mode).is_dual_plane == 1); |
328 | | |
329 | | // Get the appropriate block descriptor |
330 | 29.4k | const block_mode& bm = bsd.get_block_mode(scb.block_mode); |
331 | 29.4k | const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode); |
332 | | |
333 | | // Unquantize and undecimate the weights |
334 | 29.4k | int plane1_weights[BLOCK_MAX_TEXELS]; |
335 | 29.4k | int plane2_weights[BLOCK_MAX_TEXELS]; |
336 | 29.4k | unpack_weights(bsd, scb, di, true, plane1_weights, plane2_weights); |
337 | | |
338 | 29.4k | vmask4 plane2_mask = vint4::lane_id() == vint4(scb.plane2_component); |
339 | | |
340 | 29.4k | vfloat4 summa = vfloat4::zero(); |
341 | | |
342 | | // Decode the color endpoints for this partition |
343 | 29.4k | vint4 ep0; |
344 | 29.4k | vint4 ep1; |
345 | 29.4k | bool rgb_lns; |
346 | 29.4k | bool a_lns; |
347 | | |
348 | 29.4k | unpack_color_endpoints(config.profile, |
349 | 29.4k | scb.color_formats[0], |
350 | 29.4k | scb.color_values[0], |
351 | 29.4k | rgb_lns, a_lns, |
352 | 29.4k | ep0, ep1); |
353 | | |
354 | 29.4k | vmask4 u8_mask = get_u8_component_mask(config.profile, blk); |
355 | | |
356 | | // Unpack and compute error for each texel in the partition |
357 | 29.4k | unsigned int texel_count = bsd.texel_count; |
358 | 512k | for (unsigned int i = 0; i < texel_count; i++) |
359 | 495k | { |
360 | 495k | vint4 weight = select(vint4(plane1_weights[i]), vint4(plane2_weights[i]), plane2_mask); |
361 | 495k | vint4 colori = lerp_color_int(u8_mask, ep0, ep1, weight); |
362 | | |
363 | 495k | vfloat4 color = int_to_float(colori); |
364 | 495k | vfloat4 oldColor = blk.texel(i); |
365 | | |
366 | | // Compare error using a perceptual decode metric for RGBM textures |
367 | 495k | if (config.flags & ASTCENC_FLG_MAP_RGBM) |
368 | 88.7k | { |
369 | | // Fail encodings that result in zero weight M pixels. Note that this can cause |
370 | | // "interesting" artifacts if we reject all useful encodings - we typically get max |
371 | | // brightness encodings instead which look just as bad. We recommend users apply a |
372 | | // bias to their stored M value, limiting the lower value to 16 or 32 to avoid |
373 | | // getting small M values post-quantization, but we can't prove it would never |
374 | | // happen, especially at low bit rates ... |
375 | 88.7k | if (color.lane<3>() == 0.0f) |
376 | 11.9k | { |
377 | 11.9k | return -ERROR_CALC_DEFAULT; |
378 | 11.9k | } |
379 | | |
380 | | // Compute error based on decoded RGBM color |
381 | 76.7k | color = vfloat4( |
382 | 76.7k | color.lane<0>() * color.lane<3>() * config.rgbm_m_scale, |
383 | 76.7k | color.lane<1>() * color.lane<3>() * config.rgbm_m_scale, |
384 | 76.7k | color.lane<2>() * color.lane<3>() * config.rgbm_m_scale, |
385 | 76.7k | 1.0f |
386 | 76.7k | ); |
387 | | |
388 | 76.7k | oldColor = vfloat4( |
389 | 76.7k | oldColor.lane<0>() * oldColor.lane<3>() * config.rgbm_m_scale, |
390 | 76.7k | oldColor.lane<1>() * oldColor.lane<3>() * config.rgbm_m_scale, |
391 | 76.7k | oldColor.lane<2>() * oldColor.lane<3>() * config.rgbm_m_scale, |
392 | 76.7k | 1.0f |
393 | 76.7k | ); |
394 | 76.7k | } |
395 | | |
396 | 483k | vfloat4 error = oldColor - color; |
397 | 483k | error = min(abs(error), 1e15f); |
398 | 483k | error = error * error; |
399 | | |
400 | 483k | summa += min(dot(error, blk.channel_weight), ERROR_CALC_DEFAULT); |
401 | 483k | } |
402 | | |
403 | 17.4k | return summa.lane<0>(); |
404 | 29.4k | } |
405 | | |
406 | | /* See header for documentation. */ |
407 | | float compute_symbolic_block_difference_1plane( |
408 | | const astcenc_config& config, |
409 | | const block_size_descriptor& bsd, |
410 | | const symbolic_compressed_block& scb, |
411 | | const image_block& blk |
412 | 50.4k | ) { |
413 | 50.4k | assert(bsd.get_block_mode(scb.block_mode).is_dual_plane == 0); |
414 | | |
415 | | // If we detected an error-block, blow up immediately. |
416 | 50.4k | if (scb.block_type == SYM_BTYPE_ERROR) |
417 | 5.00k | { |
418 | 5.00k | return ERROR_CALC_DEFAULT; |
419 | 5.00k | } |
420 | | |
421 | 50.4k | assert(scb.block_mode >= 0); |
422 | | |
423 | | // Get the appropriate partition-table entry |
424 | 45.4k | unsigned int partition_count = scb.partition_count; |
425 | 45.4k | const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index); |
426 | | |
427 | | // Get the appropriate block descriptor |
428 | 45.4k | const block_mode& bm = bsd.get_block_mode(scb.block_mode); |
429 | 45.4k | const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode); |
430 | | |
431 | | // Unquantize and undecimate the weights |
432 | 45.4k | int plane1_weights[BLOCK_MAX_TEXELS]; |
433 | 45.4k | unpack_weights(bsd, scb, di, false, plane1_weights, nullptr); |
434 | | |
435 | 45.4k | vmask4 u8_mask = get_u8_component_mask(config.profile, blk); |
436 | | |
437 | 45.4k | vfloat4 summa = vfloat4::zero(); |
438 | 134k | for (unsigned int i = 0; i < partition_count; i++) |
439 | 101k | { |
440 | | // Decode the color endpoints for this partition |
441 | 101k | vint4 ep0; |
442 | 101k | vint4 ep1; |
443 | 101k | bool rgb_lns; |
444 | 101k | bool a_lns; |
445 | | |
446 | 101k | unpack_color_endpoints(config.profile, |
447 | 101k | scb.color_formats[i], |
448 | 101k | scb.color_values[i], |
449 | 101k | rgb_lns, a_lns, |
450 | 101k | ep0, ep1); |
451 | | |
452 | | // Unpack and compute error for each texel in the partition |
453 | 101k | unsigned int texel_count = pi.partition_texel_count[i]; |
454 | 979k | for (unsigned int j = 0; j < texel_count; j++) |
455 | 890k | { |
456 | 890k | unsigned int tix = pi.texels_of_partition[i][j]; |
457 | 890k | vint4 colori = lerp_color_int(u8_mask, ep0, ep1, |
458 | 890k | vint4(plane1_weights[tix])); |
459 | | |
460 | 890k | vfloat4 color = int_to_float(colori); |
461 | 890k | vfloat4 oldColor = blk.texel(tix); |
462 | | |
463 | | // Compare error using a perceptual decode metric for RGBM textures |
464 | 890k | if (config.flags & ASTCENC_FLG_MAP_RGBM) |
465 | 304k | { |
466 | | // Fail encodings that result in zero weight M pixels. Note that this can cause |
467 | | // "interesting" artifacts if we reject all useful encodings - we typically get max |
468 | | // brightness encodings instead which look just as bad. We recommend users apply a |
469 | | // bias to their stored M value, limiting the lower value to 16 or 32 to avoid |
470 | | // getting small M values post-quantization, but we can't prove it would never |
471 | | // happen, especially at low bit rates ... |
472 | 304k | if (color.lane<3>() == 0.0f) |
473 | 12.8k | { |
474 | 12.8k | return -ERROR_CALC_DEFAULT; |
475 | 12.8k | } |
476 | | |
477 | | // Compute error based on decoded RGBM color |
478 | 291k | color = vfloat4( |
479 | 291k | color.lane<0>() * color.lane<3>() * config.rgbm_m_scale, |
480 | 291k | color.lane<1>() * color.lane<3>() * config.rgbm_m_scale, |
481 | 291k | color.lane<2>() * color.lane<3>() * config.rgbm_m_scale, |
482 | 291k | 1.0f |
483 | 291k | ); |
484 | | |
485 | 291k | oldColor = vfloat4( |
486 | 291k | oldColor.lane<0>() * oldColor.lane<3>() * config.rgbm_m_scale, |
487 | 291k | oldColor.lane<1>() * oldColor.lane<3>() * config.rgbm_m_scale, |
488 | 291k | oldColor.lane<2>() * oldColor.lane<3>() * config.rgbm_m_scale, |
489 | 291k | 1.0f |
490 | 291k | ); |
491 | 291k | } |
492 | | |
493 | 877k | vfloat4 error = oldColor - color; |
494 | 877k | error = min(abs(error), 1e15f); |
495 | 877k | error = error * error; |
496 | | |
497 | 877k | summa += min(dot(error, blk.channel_weight), ERROR_CALC_DEFAULT); |
498 | 877k | } |
499 | 101k | } |
500 | | |
501 | 32.6k | return summa.lane<0>(); |
502 | 45.4k | } |
503 | | |
504 | | /* See header for documentation. */ |
505 | | float compute_symbolic_block_difference_1plane_1partition( |
506 | | const astcenc_config& config, |
507 | | const block_size_descriptor& bsd, |
508 | | const symbolic_compressed_block& scb, |
509 | | const image_block& blk |
510 | 10.9k | ) { |
511 | | // If we detected an error-block, blow up immediately. |
512 | 10.9k | if (scb.block_type == SYM_BTYPE_ERROR) |
513 | 0 | { |
514 | 0 | return ERROR_CALC_DEFAULT; |
515 | 0 | } |
516 | | |
517 | 10.9k | assert(scb.block_mode >= 0); |
518 | 10.9k | assert(bsd.get_partition_info(scb.partition_count, scb.partition_index).partition_count == 1); |
519 | | |
520 | | // Get the appropriate block descriptor |
521 | 10.9k | const block_mode& bm = bsd.get_block_mode(scb.block_mode); |
522 | 10.9k | const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode); |
523 | | |
524 | | // Unquantize and undecimate the weights |
525 | 10.9k | ASTCENC_ALIGNAS int plane1_weights[BLOCK_MAX_TEXELS]; |
526 | 10.9k | unpack_weights(bsd, scb, di, false, plane1_weights, nullptr); |
527 | | |
528 | | // Decode the color endpoints for this partition |
529 | 10.9k | vint4 ep0; |
530 | 10.9k | vint4 ep1; |
531 | 10.9k | bool rgb_lns; |
532 | 10.9k | bool a_lns; |
533 | | |
534 | 10.9k | unpack_color_endpoints(config.profile, |
535 | 10.9k | scb.color_formats[0], |
536 | 10.9k | scb.color_values[0], |
537 | 10.9k | rgb_lns, a_lns, |
538 | 10.9k | ep0, ep1); |
539 | | |
540 | 10.9k | vmask4 u8_mask = get_u8_component_mask(config.profile, blk); |
541 | | |
542 | | // Unpack and compute error for each texel in the partition |
543 | 10.9k | vfloatacc summav = vfloatacc::zero(); |
544 | | |
545 | 10.9k | vint lane_id = vint::lane_id(); |
546 | | |
547 | 10.9k | unsigned int texel_count = bsd.texel_count; |
548 | 94.4k | for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH) |
549 | 83.5k | { |
550 | | // Compute EP1 contribution |
551 | 83.5k | vint weight1 = vint::loada(plane1_weights + i); |
552 | 83.5k | vint ep1_r = vint(ep1.lane<0>()) * weight1; |
553 | 83.5k | vint ep1_g = vint(ep1.lane<1>()) * weight1; |
554 | 83.5k | vint ep1_b = vint(ep1.lane<2>()) * weight1; |
555 | 83.5k | vint ep1_a = vint(ep1.lane<3>()) * weight1; |
556 | | |
557 | | // Compute EP0 contribution |
558 | 83.5k | vint weight0 = vint(64) - weight1; |
559 | 83.5k | vint ep0_r = vint(ep0.lane<0>()) * weight0; |
560 | 83.5k | vint ep0_g = vint(ep0.lane<1>()) * weight0; |
561 | 83.5k | vint ep0_b = vint(ep0.lane<2>()) * weight0; |
562 | 83.5k | vint ep0_a = vint(ep0.lane<3>()) * weight0; |
563 | | |
564 | | // Combine contributions |
565 | 83.5k | vint colori_r = asr<6>(ep0_r + ep1_r + vint(32)); |
566 | 83.5k | vint colori_g = asr<6>(ep0_g + ep1_g + vint(32)); |
567 | 83.5k | vint colori_b = asr<6>(ep0_b + ep1_b + vint(32)); |
568 | 83.5k | vint colori_a = asr<6>(ep0_a + ep1_a + vint(32)); |
569 | | |
570 | | // If using a U8 decode mode bit replicate top 8 bits |
571 | | // so rest of codec can assume 0xFFFF max range everywhere |
572 | 83.5k | vint colori_r8 = asr<8>(colori_r) * vint(257); |
573 | 83.5k | colori_r = select(colori_r, colori_r8, vmask(u8_mask.lane<0>())); |
574 | | |
575 | 83.5k | vint colori_g8 = asr<8>(colori_g) * vint(257); |
576 | 83.5k | colori_g = select(colori_g, colori_g8, vmask(u8_mask.lane<1>())); |
577 | | |
578 | 83.5k | vint colori_b8 = asr<8>(colori_b) * vint(257); |
579 | 83.5k | colori_b = select(colori_b, colori_b8, vmask(u8_mask.lane<2>())); |
580 | | |
581 | 83.5k | vint colori_a8 = asr<8>(colori_a) * vint(257); |
582 | 83.5k | colori_a = select(colori_a, colori_a8, vmask(u8_mask.lane<3>())); |
583 | | |
584 | | // Compute color diff |
585 | 83.5k | vfloat color_r = int_to_float(colori_r); |
586 | 83.5k | vfloat color_g = int_to_float(colori_g); |
587 | 83.5k | vfloat color_b = int_to_float(colori_b); |
588 | 83.5k | vfloat color_a = int_to_float(colori_a); |
589 | | |
590 | 83.5k | vfloat color_orig_r = loada(blk.data_r + i); |
591 | 83.5k | vfloat color_orig_g = loada(blk.data_g + i); |
592 | 83.5k | vfloat color_orig_b = loada(blk.data_b + i); |
593 | 83.5k | vfloat color_orig_a = loada(blk.data_a + i); |
594 | | |
595 | 83.5k | vfloat color_error_r = min(abs(color_orig_r - color_r), vfloat(1e15f)); |
596 | 83.5k | vfloat color_error_g = min(abs(color_orig_g - color_g), vfloat(1e15f)); |
597 | 83.5k | vfloat color_error_b = min(abs(color_orig_b - color_b), vfloat(1e15f)); |
598 | 83.5k | vfloat color_error_a = min(abs(color_orig_a - color_a), vfloat(1e15f)); |
599 | | |
600 | | // Compute squared error metric |
601 | 83.5k | color_error_r = color_error_r * color_error_r; |
602 | 83.5k | color_error_g = color_error_g * color_error_g; |
603 | 83.5k | color_error_b = color_error_b * color_error_b; |
604 | 83.5k | color_error_a = color_error_a * color_error_a; |
605 | | |
606 | 83.5k | vfloat metric = color_error_r * blk.channel_weight.lane<0>() |
607 | 83.5k | + color_error_g * blk.channel_weight.lane<1>() |
608 | 83.5k | + color_error_b * blk.channel_weight.lane<2>() |
609 | 83.5k | + color_error_a * blk.channel_weight.lane<3>(); |
610 | | |
611 | | // Mask off bad lanes |
612 | 83.5k | vmask mask = lane_id < vint(texel_count); |
613 | 83.5k | lane_id += vint(ASTCENC_SIMD_WIDTH); |
614 | 83.5k | haccumulate(summav, metric, mask); |
615 | 83.5k | } |
616 | | |
617 | 10.9k | return hadd_s(summav); |
618 | 10.9k | } |
619 | | |
620 | | #endif |