/src/astc-encoder/Source/astcenc_compress_symbolic.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // ---------------------------------------------------------------------------- |
3 | | // Copyright 2011-2026 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 | | #if !defined(ASTCENC_DECOMPRESS_ONLY) |
19 | | |
20 | | /** |
21 | | * @brief Functions to compress a symbolic block. |
22 | | */ |
23 | | |
24 | | #include "astcenc_internal.h" |
25 | | #include "astcenc_diagnostic_trace.h" |
26 | | |
27 | | #include <cassert> |
28 | | |
29 | | /** |
30 | | * @brief Merge two planes of endpoints into a single vector. |
31 | | * |
32 | | * @param ep_plane1 The endpoints for plane 1. |
33 | | * @param ep_plane2 The endpoints for plane 2. |
34 | | * @param component_plane2 The color component for plane 2. |
35 | | * @param[out] result The merged output. |
36 | | */ |
37 | | static void merge_endpoints( |
38 | | const endpoints& ep_plane1, |
39 | | const endpoints& ep_plane2, |
40 | | unsigned int component_plane2, |
41 | | endpoints& result |
42 | 7.02k | ) { |
43 | 7.02k | unsigned int partition_count = ep_plane1.partition_count; |
44 | 7.02k | assert(partition_count == 1); |
45 | | |
46 | 7.02k | vmask4 sep_mask = vint4::lane_id() == vint4(component_plane2); |
47 | | |
48 | 7.02k | result.partition_count = partition_count; |
49 | 7.02k | result.endpt0[0] = select(ep_plane1.endpt0[0], ep_plane2.endpt0[0], sep_mask); |
50 | 7.02k | result.endpt1[0] = select(ep_plane1.endpt1[0], ep_plane2.endpt1[0], sep_mask); |
51 | 7.02k | } |
52 | | |
53 | | /** |
54 | | * @brief Attempt to improve weights given a chosen configuration. |
55 | | * |
56 | | * Given a fixed weight grid decimation and weight value quantization, iterate over all weights (per |
57 | | * partition and per plane) and attempt to improve image quality by moving each weight up by one or |
58 | | * down by one quantization step. |
59 | | * |
60 | | * This is a specialized function which only supports operating on undecimated weight grids, |
61 | | * therefore primarily improving the performance of 4x4 and 5x5 blocks where grid decimation |
62 | | * is needed less often. |
63 | | * |
64 | | * @param decode_mode The decode mode (LDR, HDR). |
65 | | * @param bsd The block size information. |
66 | | * @param blk The image block color data to compress. |
67 | | * @param[out] scb The symbolic compressed block output. |
68 | | */ |
69 | | static bool realign_weights_undecimated( |
70 | | astcenc_profile decode_mode, |
71 | | const block_size_descriptor& bsd, |
72 | | const image_block& blk, |
73 | | symbolic_compressed_block& scb |
74 | 34.5k | ) { |
75 | | // Get the partition descriptor |
76 | 34.5k | unsigned int partition_count = scb.partition_count; |
77 | 34.5k | const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index); |
78 | | |
79 | | // Get the quantization table |
80 | 34.5k | const block_mode& bm = bsd.get_block_mode(scb.block_mode); |
81 | 34.5k | unsigned int weight_quant_level = bm.quant_mode; |
82 | 34.5k | const quant_and_transfer_table& qat = quant_and_xfer_tables[weight_quant_level]; |
83 | | |
84 | 34.5k | unsigned int max_plane = bm.is_dual_plane; |
85 | 34.5k | int plane2_component = scb.plane2_component; |
86 | 34.5k | vmask4 plane_mask = vint4::lane_id() == vint4(plane2_component); |
87 | | |
88 | | // Decode the color endpoints |
89 | 34.5k | bool rgb_hdr; |
90 | 34.5k | bool alpha_hdr; |
91 | 34.5k | vint4 endpnt0[BLOCK_MAX_PARTITIONS]; |
92 | 34.5k | vint4 endpnt1[BLOCK_MAX_PARTITIONS]; |
93 | 34.5k | vfloat4 endpnt0f[BLOCK_MAX_PARTITIONS]; |
94 | 34.5k | vfloat4 offset[BLOCK_MAX_PARTITIONS]; |
95 | | |
96 | 34.5k | promise(partition_count > 0); |
97 | | |
98 | 94.8k | for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++) |
99 | 60.2k | { |
100 | 60.2k | unpack_color_endpoints(decode_mode, |
101 | 60.2k | scb.color_formats[pa_idx], |
102 | 60.2k | scb.color_values[pa_idx], |
103 | 60.2k | rgb_hdr, alpha_hdr, |
104 | 60.2k | endpnt0[pa_idx], |
105 | 60.2k | endpnt1[pa_idx]); |
106 | 60.2k | } |
107 | | |
108 | 34.5k | uint8_t* dec_weights_uquant = scb.weights; |
109 | 34.5k | bool adjustments = false; |
110 | | |
111 | | // For each plane and partition ... |
112 | 77.9k | for (unsigned int pl_idx = 0; pl_idx <= max_plane; pl_idx++) |
113 | 43.3k | { |
114 | 112k | for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++) |
115 | 69.0k | { |
116 | | // Compute the endpoint delta for all components in current plane |
117 | 69.0k | vint4 epd = endpnt1[pa_idx] - endpnt0[pa_idx]; |
118 | 69.0k | epd = select(epd, vint4::zero(), plane_mask); |
119 | | |
120 | 69.0k | endpnt0f[pa_idx] = int_to_float(endpnt0[pa_idx]); |
121 | 69.0k | offset[pa_idx] = int_to_float(epd) * (1.0f / 64.0f); |
122 | 69.0k | } |
123 | | |
124 | | // For each weight compute previous, current, and next errors |
125 | 43.3k | promise(bsd.texel_count > 0); |
126 | 804k | for (unsigned int texel = 0; texel < bsd.texel_count; texel++) |
127 | 761k | { |
128 | 761k | int uqw = dec_weights_uquant[texel]; |
129 | | |
130 | 761k | uint32_t prev_and_next = qat.prev_next_values[uqw]; |
131 | 761k | int uqw_down = prev_and_next & 0xFF; |
132 | 761k | int uqw_up = (prev_and_next >> 8) & 0xFF; |
133 | | |
134 | | // Interpolate the colors to create the diffs |
135 | 761k | float weight_base = static_cast<float>(uqw); |
136 | 761k | float weight_down = static_cast<float>(uqw_down - uqw); |
137 | 761k | float weight_up = static_cast<float>(uqw_up - uqw); |
138 | | |
139 | 761k | unsigned int partition = pi.partition_of_texel[texel]; |
140 | 761k | vfloat4 color_offset = offset[partition]; |
141 | 761k | vfloat4 color_base = endpnt0f[partition]; |
142 | | |
143 | 761k | vfloat4 color = color_base + color_offset * weight_base; |
144 | 761k | vfloat4 orig_color = blk.texel(texel); |
145 | 761k | vfloat4 error_weight = blk.channel_weight; |
146 | | |
147 | 761k | vfloat4 color_diff = color - orig_color; |
148 | 761k | vfloat4 color_diff_down = color_diff + color_offset * weight_down; |
149 | 761k | vfloat4 color_diff_up = color_diff + color_offset * weight_up; |
150 | | |
151 | 761k | float error_base = dot_s(color_diff * color_diff, error_weight); |
152 | 761k | float error_down = dot_s(color_diff_down * color_diff_down, error_weight); |
153 | 761k | float error_up = dot_s(color_diff_up * color_diff_up, error_weight); |
154 | | |
155 | | // Check if the prev or next error is better, and if so use it |
156 | 761k | if ((error_up < error_base) && (error_up < error_down) && (uqw < 64)) |
157 | 55.0k | { |
158 | 55.0k | dec_weights_uquant[texel] = static_cast<uint8_t>(uqw_up); |
159 | 55.0k | adjustments = true; |
160 | 55.0k | } |
161 | 706k | else if ((error_down < error_base) && (uqw > 0)) |
162 | 57.2k | { |
163 | 57.2k | dec_weights_uquant[texel] = static_cast<uint8_t>(uqw_down); |
164 | 57.2k | adjustments = true; |
165 | 57.2k | } |
166 | 761k | } |
167 | | |
168 | | // Prepare iteration for plane 2 |
169 | 43.3k | dec_weights_uquant += WEIGHTS_PLANE2_OFFSET; |
170 | 43.3k | plane_mask = ~plane_mask; |
171 | 43.3k | } |
172 | | |
173 | 34.5k | return adjustments; |
174 | 34.5k | } |
175 | | |
176 | | /** |
177 | | * @brief Attempt to improve weights given a chosen configuration. |
178 | | * |
179 | | * Given a fixed weight grid decimation and weight value quantization, iterate over all weights (per |
180 | | * partition and per plane) and attempt to improve image quality by moving each weight up by one or |
181 | | * down by one quantization step. |
182 | | * |
183 | | * @param decode_mode The decode mode (LDR, HDR). |
184 | | * @param bsd The block size information. |
185 | | * @param blk The image block color data to compress. |
186 | | * @param[out] scb The symbolic compressed block output. |
187 | | */ |
188 | | static bool realign_weights_decimated( |
189 | | astcenc_profile decode_mode, |
190 | | const block_size_descriptor& bsd, |
191 | | const image_block& blk, |
192 | | symbolic_compressed_block& scb |
193 | 12.4k | ) { |
194 | | // Get the partition descriptor |
195 | 12.4k | unsigned int partition_count = scb.partition_count; |
196 | 12.4k | const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index); |
197 | | |
198 | | // Get the quantization table |
199 | 12.4k | const block_mode& bm = bsd.get_block_mode(scb.block_mode); |
200 | 12.4k | unsigned int weight_quant_level = bm.quant_mode; |
201 | 12.4k | const quant_and_transfer_table& qat = quant_and_xfer_tables[weight_quant_level]; |
202 | | |
203 | | // Get the decimation table |
204 | 12.4k | const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode); |
205 | 12.4k | unsigned int weight_count = di.weight_count; |
206 | 12.4k | assert(weight_count != bsd.texel_count); |
207 | | |
208 | 12.4k | unsigned int max_plane = bm.is_dual_plane; |
209 | 12.4k | int plane2_component = scb.plane2_component; |
210 | 12.4k | vmask4 plane_mask = vint4::lane_id() == vint4(plane2_component); |
211 | | |
212 | | // Decode the color endpoints |
213 | 12.4k | bool rgb_hdr; |
214 | 12.4k | bool alpha_hdr; |
215 | 12.4k | vint4 endpnt0[BLOCK_MAX_PARTITIONS]; |
216 | 12.4k | vint4 endpnt1[BLOCK_MAX_PARTITIONS]; |
217 | 12.4k | vfloat4 endpnt0f[BLOCK_MAX_PARTITIONS]; |
218 | 12.4k | vfloat4 offset[BLOCK_MAX_PARTITIONS]; |
219 | | |
220 | 12.4k | promise(partition_count > 0); |
221 | 12.4k | promise(weight_count > 0); |
222 | | |
223 | 29.0k | for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++) |
224 | 16.5k | { |
225 | 16.5k | unpack_color_endpoints(decode_mode, |
226 | 16.5k | scb.color_formats[pa_idx], |
227 | 16.5k | scb.color_values[pa_idx], |
228 | 16.5k | rgb_hdr, alpha_hdr, |
229 | 16.5k | endpnt0[pa_idx], |
230 | 16.5k | endpnt1[pa_idx]); |
231 | 16.5k | } |
232 | | |
233 | 12.4k | uint8_t* dec_weights_uquant = scb.weights; |
234 | 12.4k | bool adjustments = false; |
235 | | |
236 | | // For each plane and partition ... |
237 | 32.6k | for (unsigned int pl_idx = 0; pl_idx <= max_plane; pl_idx++) |
238 | 20.2k | { |
239 | 44.5k | for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++) |
240 | 24.3k | { |
241 | | // Compute the endpoint delta for all components in current plane |
242 | 24.3k | vint4 epd = endpnt1[pa_idx] - endpnt0[pa_idx]; |
243 | 24.3k | epd = select(epd, vint4::zero(), plane_mask); |
244 | | |
245 | 24.3k | endpnt0f[pa_idx] = int_to_float(endpnt0[pa_idx]); |
246 | 24.3k | offset[pa_idx] = int_to_float(epd) * (1.0f / 64.0f); |
247 | 24.3k | } |
248 | | |
249 | | // Create an unquantized weight grid for this decimation level |
250 | 20.2k | ASTCENC_ALIGNAS float uq_weightsf[BLOCK_MAX_WEIGHTS]; |
251 | 120k | for (unsigned int we_idx = 0; we_idx < weight_count; we_idx += ASTCENC_SIMD_WIDTH) |
252 | 100k | { |
253 | 100k | vint unquant_value(dec_weights_uquant + we_idx); |
254 | 100k | vfloat unquant_valuef = int_to_float(unquant_value); |
255 | 100k | storea(unquant_valuef, uq_weightsf + we_idx); |
256 | 100k | } |
257 | | |
258 | | // For each weight compute previous, current, and next errors |
259 | 412k | for (unsigned int we_idx = 0; we_idx < weight_count; we_idx++) |
260 | 392k | { |
261 | 392k | int uqw = dec_weights_uquant[we_idx]; |
262 | 392k | uint32_t prev_and_next = qat.prev_next_values[uqw]; |
263 | | |
264 | 392k | float uqw_base = uq_weightsf[we_idx]; |
265 | 392k | float uqw_down = static_cast<float>(prev_and_next & 0xFF); |
266 | 392k | float uqw_up = static_cast<float>((prev_and_next >> 8) & 0xFF); |
267 | | |
268 | 392k | float uqw_diff_down = uqw_down - uqw_base; |
269 | 392k | float uqw_diff_up = uqw_up - uqw_base; |
270 | | |
271 | 392k | vfloat4 error_basev = vfloat4::zero(); |
272 | 392k | vfloat4 error_downv = vfloat4::zero(); |
273 | 392k | vfloat4 error_upv = vfloat4::zero(); |
274 | | |
275 | | // Interpolate the colors to create the diffs |
276 | 392k | unsigned int texels_to_evaluate = di.weight_texel_count[we_idx]; |
277 | 392k | promise(texels_to_evaluate > 0); |
278 | 2.21M | for (unsigned int te_idx = 0; te_idx < texels_to_evaluate; te_idx++) |
279 | 1.82M | { |
280 | 1.82M | unsigned int texel = di.weight_texels_tr[te_idx][we_idx]; |
281 | | |
282 | 1.82M | float tw_base = di.texel_contrib_for_weight[te_idx][we_idx]; |
283 | | |
284 | 1.82M | float weight_base = (uq_weightsf[di.texel_weights_tr[0][texel]] * di.texel_weight_contribs_float_tr[0][texel] |
285 | 1.82M | + uq_weightsf[di.texel_weights_tr[1][texel]] * di.texel_weight_contribs_float_tr[1][texel]) |
286 | 1.82M | + (uq_weightsf[di.texel_weights_tr[2][texel]] * di.texel_weight_contribs_float_tr[2][texel] |
287 | 1.82M | + uq_weightsf[di.texel_weights_tr[3][texel]] * di.texel_weight_contribs_float_tr[3][texel]); |
288 | | |
289 | | // Ideally this is integer rounded, but IQ gain it isn't worth the overhead |
290 | | // float weight = astc::flt_rd(weight_base + 0.5f); |
291 | | // float weight_down = astc::flt_rd(weight_base + 0.5f + uqw_diff_down * tw_base) - weight; |
292 | | // float weight_up = astc::flt_rd(weight_base + 0.5f + uqw_diff_up * tw_base) - weight; |
293 | 1.82M | float weight_down = weight_base + uqw_diff_down * tw_base - weight_base; |
294 | 1.82M | float weight_up = weight_base + uqw_diff_up * tw_base - weight_base; |
295 | | |
296 | 1.82M | unsigned int partition = pi.partition_of_texel[texel]; |
297 | 1.82M | vfloat4 color_offset = offset[partition]; |
298 | 1.82M | vfloat4 color_base = endpnt0f[partition]; |
299 | | |
300 | 1.82M | vfloat4 color = color_base + color_offset * weight_base; |
301 | 1.82M | vfloat4 orig_color = blk.texel(texel); |
302 | | |
303 | 1.82M | vfloat4 color_diff = color - orig_color; |
304 | 1.82M | vfloat4 color_down_diff = color_diff + color_offset * weight_down; |
305 | 1.82M | vfloat4 color_up_diff = color_diff + color_offset * weight_up; |
306 | | |
307 | 1.82M | error_basev += color_diff * color_diff; |
308 | 1.82M | error_downv += color_down_diff * color_down_diff; |
309 | 1.82M | error_upv += color_up_diff * color_up_diff; |
310 | 1.82M | } |
311 | | |
312 | 392k | vfloat4 error_weight = blk.channel_weight; |
313 | 392k | float error_base = hadd_s(error_basev * error_weight); |
314 | 392k | float error_down = hadd_s(error_downv * error_weight); |
315 | 392k | float error_up = hadd_s(error_upv * error_weight); |
316 | | |
317 | | // Check if the prev or next error is better, and if so use it |
318 | 392k | if ((error_up < error_base) && (error_up < error_down) && (uqw < 64)) |
319 | 18.6k | { |
320 | 18.6k | uq_weightsf[we_idx] = uqw_up; |
321 | 18.6k | dec_weights_uquant[we_idx] = static_cast<uint8_t>(uqw_up); |
322 | 18.6k | adjustments = true; |
323 | 18.6k | } |
324 | 374k | else if ((error_down < error_base) && (uqw > 0)) |
325 | 32.6k | { |
326 | 32.6k | uq_weightsf[we_idx] = uqw_down; |
327 | 32.6k | dec_weights_uquant[we_idx] = static_cast<uint8_t>(uqw_down); |
328 | 32.6k | adjustments = true; |
329 | 32.6k | } |
330 | 392k | } |
331 | | |
332 | | // Prepare iteration for plane 2 |
333 | 20.2k | dec_weights_uquant += WEIGHTS_PLANE2_OFFSET; |
334 | 20.2k | plane_mask = ~plane_mask; |
335 | 20.2k | } |
336 | | |
337 | 12.4k | return adjustments; |
338 | 12.4k | } |
339 | | |
340 | | /** |
341 | | * @brief Compress a block using a chosen partitioning and 1 plane of weights. |
342 | | * |
343 | | * @param config The compressor configuration. |
344 | | * @param bsd The block size information. |
345 | | * @param blk The image block color data to compress. |
346 | | * @param only_always True if we only use "always" percentile block modes. |
347 | | * @param tune_errorval_threshold The error value threshold. |
348 | | * @param partition_count The partition count. |
349 | | * @param partition_index The partition index if @c partition_count is 2-4. |
350 | | * @param[out] scb The symbolic compressed block output. |
351 | | * @param[out] tmpbuf The quantized weights for plane 1. |
352 | | */ |
353 | | static float compress_symbolic_block_for_partition_1plane( |
354 | | const astcenc_config& config, |
355 | | const block_size_descriptor& bsd, |
356 | | const image_block& blk, |
357 | | bool only_always, |
358 | | float tune_errorval_threshold, |
359 | | unsigned int partition_count, |
360 | | unsigned int partition_index, |
361 | | symbolic_compressed_block& scb, |
362 | | compression_working_buffers& tmpbuf, |
363 | | int quant_limit |
364 | 11.0k | ) { |
365 | 11.0k | promise(partition_count > 0); |
366 | 11.0k | promise(config.tune_candidate_limit > 0); |
367 | 11.0k | promise(config.tune_refinement_limit > 0); |
368 | | |
369 | 11.0k | int max_weight_quant = astc::min(static_cast<int>(QUANT_32), quant_limit); |
370 | | |
371 | 11.0k | auto compute_difference = &compute_symbolic_block_difference_1plane; |
372 | 11.0k | if ((partition_count == 1) && !(config.flags & ASTCENC_FLG_MAP_RGBM)) |
373 | 1.76k | { |
374 | 1.76k | compute_difference = &compute_symbolic_block_difference_1plane_1partition; |
375 | 1.76k | } |
376 | | |
377 | 11.0k | const auto& pi = bsd.get_partition_info(partition_count, partition_index); |
378 | | |
379 | | // Compute ideal weights and endpoint colors, with no quantization or decimation |
380 | 11.0k | endpoints_and_weights& ei = tmpbuf.ei1; |
381 | 11.0k | compute_ideal_colors_and_weights_1plane(blk, pi, ei); |
382 | | |
383 | | // Compute ideal weights and endpoint colors for every decimation |
384 | 11.0k | float* dec_weights_ideal = tmpbuf.dec_weights_ideal; |
385 | 11.0k | uint8_t* dec_weights_uquant = tmpbuf.dec_weights_uquant; |
386 | | |
387 | | // For each decimation mode, compute an ideal set of weights with no quantization |
388 | 11.0k | unsigned int max_decimation_modes = only_always ? bsd.decimation_mode_count_always |
389 | 11.0k | : bsd.decimation_mode_count_selected; |
390 | 11.0k | promise(max_decimation_modes > 0); |
391 | 117k | for (unsigned int i = 0; i < max_decimation_modes; i++) |
392 | 106k | { |
393 | 106k | const auto& dm = bsd.get_decimation_mode(i); |
394 | 106k | if (!dm.is_ref_1plane(static_cast<quant_method>(max_weight_quant))) |
395 | 34.0k | { |
396 | 34.0k | continue; |
397 | 34.0k | } |
398 | | |
399 | 72.3k | const auto& di = bsd.get_decimation_info(i); |
400 | | |
401 | 72.3k | compute_ideal_weights_for_decimation( |
402 | 72.3k | ei, |
403 | 72.3k | di, |
404 | 72.3k | dec_weights_ideal + i * BLOCK_MAX_WEIGHTS); |
405 | 72.3k | } |
406 | | |
407 | | // Compute maximum colors for the endpoints and ideal weights, then for each endpoint and ideal |
408 | | // weight pair, compute the smallest weight that will result in a color value greater than 1 |
409 | 11.0k | vfloat4 min_ep(10.0f); |
410 | 35.8k | for (unsigned int i = 0; i < partition_count; i++) |
411 | 24.7k | { |
412 | 24.7k | vfloat4 ep = (vfloat4(1.0f) - ei.ep.endpt0[i]) / (ei.ep.endpt1[i] - ei.ep.endpt0[i]); |
413 | | |
414 | 24.7k | vmask4 use_ep = (ep > vfloat4(0.5f)) & (ep < min_ep); |
415 | 24.7k | min_ep = select(min_ep, ep, use_ep); |
416 | 24.7k | } |
417 | | |
418 | 11.0k | float min_wt_cutoff = hmin_s(min_ep); |
419 | | |
420 | | // For each mode, use the angular method to compute a shift |
421 | 11.0k | compute_angular_endpoints_1plane( |
422 | 11.0k | only_always, bsd, dec_weights_ideal, max_weight_quant, tmpbuf); |
423 | | |
424 | 11.0k | float* weight_low_value = tmpbuf.weight_low_value1; |
425 | 11.0k | float* weight_high_value = tmpbuf.weight_high_value1; |
426 | 11.0k | int8_t* qwt_bitcounts = tmpbuf.qwt_bitcounts; |
427 | 11.0k | float* qwt_errors = tmpbuf.qwt_errors; |
428 | | |
429 | | // For each mode (which specifies a decimation and a quantization): |
430 | | // * Compute number of bits needed for the quantized weights |
431 | | // * Generate an optimized set of quantized weights |
432 | | // * Compute quantization errors for the mode |
433 | | |
434 | 11.0k | static const int8_t free_bits_for_partition_count[4] { |
435 | 11.0k | 115 - 4, 111 - 4 - PARTITION_INDEX_BITS, 108 - 4 - PARTITION_INDEX_BITS, 105 - 4 - PARTITION_INDEX_BITS |
436 | 11.0k | }; |
437 | | |
438 | 11.0k | unsigned int max_block_modes = only_always ? bsd.block_mode_count_1plane_always |
439 | 11.0k | : bsd.block_mode_count_1plane_selected; |
440 | 11.0k | promise(max_block_modes > 0); |
441 | 282k | for (unsigned int i = 0; i < max_block_modes; i++) |
442 | 271k | { |
443 | 271k | const block_mode& bm = bsd.block_modes[i]; |
444 | | |
445 | 271k | if (bm.quant_mode > max_weight_quant) |
446 | 89.9k | { |
447 | 89.9k | qwt_errors[i] = 1e38f; |
448 | 89.9k | continue; |
449 | 89.9k | } |
450 | | |
451 | 271k | assert(!bm.is_dual_plane); |
452 | 181k | int bitcount = free_bits_for_partition_count[partition_count - 1] - bm.weight_bits; |
453 | 181k | if (bitcount <= 0) |
454 | 1.38k | { |
455 | 1.38k | qwt_errors[i] = 1e38f; |
456 | 1.38k | continue; |
457 | 1.38k | } |
458 | | |
459 | 180k | if (weight_high_value[i] > 1.02f * min_wt_cutoff) |
460 | 34.7k | { |
461 | 34.7k | weight_high_value[i] = 1.0f; |
462 | 34.7k | } |
463 | | |
464 | 180k | int decimation_mode = bm.decimation_mode; |
465 | 180k | const auto& di = bsd.get_decimation_info(decimation_mode); |
466 | | |
467 | 180k | qwt_bitcounts[i] = static_cast<int8_t>(bitcount); |
468 | | |
469 | 180k | ASTCENC_ALIGNAS float dec_weights_uquantf[BLOCK_MAX_WEIGHTS]; |
470 | | |
471 | | // Generate the optimized set of weights for the weight mode |
472 | 180k | compute_quantized_weights_for_decimation( |
473 | 180k | di, |
474 | 180k | weight_low_value[i], weight_high_value[i], |
475 | 180k | dec_weights_ideal + BLOCK_MAX_WEIGHTS * decimation_mode, |
476 | 180k | dec_weights_uquantf, |
477 | 180k | dec_weights_uquant + BLOCK_MAX_WEIGHTS * i, |
478 | 180k | bm.get_weight_quant_mode()); |
479 | | |
480 | | // Compute weight quantization errors for the block mode |
481 | 180k | qwt_errors[i] = compute_error_of_weight_set_1plane( |
482 | 180k | ei, |
483 | 180k | di, |
484 | 180k | dec_weights_uquantf); |
485 | 180k | } |
486 | | |
487 | | // Decide the optimal combination of color endpoint encodings and weight encodings |
488 | 11.0k | uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS]; |
489 | 11.0k | int block_mode_index[TUNE_MAX_TRIAL_CANDIDATES]; |
490 | | |
491 | 11.0k | quant_method color_quant_level[TUNE_MAX_TRIAL_CANDIDATES]; |
492 | 11.0k | quant_method color_quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES]; |
493 | | |
494 | 11.0k | unsigned int candidate_count = compute_ideal_endpoint_formats( |
495 | 11.0k | pi, blk, ei.ep, qwt_bitcounts, qwt_errors, |
496 | 11.0k | config.tune_candidate_limit, 0, max_block_modes, |
497 | 11.0k | partition_format_specifiers, block_mode_index, |
498 | 11.0k | color_quant_level, color_quant_level_mod, tmpbuf); |
499 | | |
500 | | // Iterate over the N believed-to-be-best modes to find out which one is actually best |
501 | 11.0k | float best_errorval_in_mode = ERROR_CALC_DEFAULT; |
502 | 11.0k | float best_errorval_in_scb = scb.errorval; |
503 | | |
504 | 42.1k | for (unsigned int i = 0; i < candidate_count; i++) |
505 | 31.1k | { |
506 | 31.1k | TRACE_NODE(node0, "candidate"); |
507 | | |
508 | 31.1k | const int bm_packed_index = block_mode_index[i]; |
509 | 31.1k | assert(bm_packed_index >= 0 && bm_packed_index < static_cast<int>(bsd.block_mode_count_1plane_selected)); |
510 | 31.1k | const block_mode& qw_bm = bsd.block_modes[bm_packed_index]; |
511 | | |
512 | 31.1k | int decimation_mode = qw_bm.decimation_mode; |
513 | 31.1k | const auto& di = bsd.get_decimation_info(decimation_mode); |
514 | 31.1k | promise(di.weight_count > 0); |
515 | | |
516 | 31.1k | trace_add_data("weight_x", di.weight_x); |
517 | 31.1k | trace_add_data("weight_y", di.weight_y); |
518 | 31.1k | trace_add_data("weight_z", di.weight_z); |
519 | 31.1k | trace_add_data("weight_quant", qw_bm.quant_mode); |
520 | | |
521 | | // Recompute the ideal color endpoints before storing them |
522 | 31.1k | vfloat4 rgbs_colors[BLOCK_MAX_PARTITIONS]; |
523 | 31.1k | vfloat4 rgbo_colors[BLOCK_MAX_PARTITIONS]; |
524 | | |
525 | 31.1k | symbolic_compressed_block workscb; |
526 | 31.1k | endpoints workep = ei.ep; |
527 | | |
528 | 31.1k | uint8_t* u8_weight_src = dec_weights_uquant + BLOCK_MAX_WEIGHTS * bm_packed_index; |
529 | | |
530 | 698k | for (unsigned int j = 0; j < di.weight_count; j++) |
531 | 666k | { |
532 | 666k | workscb.weights[j] = u8_weight_src[j]; |
533 | 666k | } |
534 | | |
535 | 49.1k | for (unsigned int l = 0; l < config.tune_refinement_limit; l++) |
536 | 46.2k | { |
537 | 46.2k | recompute_ideal_colors_1plane( |
538 | 46.2k | blk, pi, di, workscb.weights, |
539 | 46.2k | workep, rgbs_colors, rgbo_colors); |
540 | | |
541 | | // Quantize the chosen color, tracking if worth trying the mod value |
542 | 46.2k | bool all_same = color_quant_level[i] != color_quant_level_mod[i]; |
543 | 146k | for (unsigned int j = 0; j < partition_count; j++) |
544 | 100k | { |
545 | 100k | workscb.color_formats[j] = pack_color_endpoints( |
546 | 100k | workep.endpt0[j], |
547 | 100k | workep.endpt1[j], |
548 | 100k | rgbs_colors[j], |
549 | 100k | rgbo_colors[j], |
550 | 100k | partition_format_specifiers[i][j], |
551 | 100k | workscb.color_values[j], |
552 | 100k | color_quant_level[i]); |
553 | | |
554 | 100k | all_same = all_same && workscb.color_formats[j] == workscb.color_formats[0]; |
555 | 100k | } |
556 | | |
557 | | // If all the color endpoint modes are the same, we get a few more bits to store colors; |
558 | | // let's see if we can take advantage of this: requantize all the colors and see if the |
559 | | // endpoint modes remain the same. |
560 | 46.2k | workscb.color_formats_matched = 0; |
561 | 46.2k | if (partition_count >= 2 && all_same) |
562 | 9.06k | { |
563 | 9.06k | uint8_t colorvals[BLOCK_MAX_PARTITIONS][8]; |
564 | 9.06k | uint8_t color_formats_mod[BLOCK_MAX_PARTITIONS] { 0 }; |
565 | 9.06k | bool all_same_mod = true; |
566 | 33.9k | for (unsigned int j = 0; j < partition_count; j++) |
567 | 25.3k | { |
568 | 25.3k | color_formats_mod[j] = pack_color_endpoints( |
569 | 25.3k | workep.endpt0[j], |
570 | 25.3k | workep.endpt1[j], |
571 | 25.3k | rgbs_colors[j], |
572 | 25.3k | rgbo_colors[j], |
573 | 25.3k | partition_format_specifiers[i][j], |
574 | 25.3k | colorvals[j], |
575 | 25.3k | color_quant_level_mod[i]); |
576 | | |
577 | | // Early out as soon as it's no longer possible to use mod |
578 | 25.3k | if (color_formats_mod[j] != color_formats_mod[0]) |
579 | 448 | { |
580 | 448 | all_same_mod = false; |
581 | 448 | break; |
582 | 448 | } |
583 | 25.3k | } |
584 | | |
585 | 9.06k | if (all_same_mod) |
586 | 8.61k | { |
587 | 8.61k | workscb.color_formats_matched = 1; |
588 | 43.0k | for (unsigned int j = 0; j < BLOCK_MAX_PARTITIONS; j++) |
589 | 34.4k | { |
590 | 310k | for (unsigned int k = 0; k < 8; k++) |
591 | 275k | { |
592 | 275k | workscb.color_values[j][k] = colorvals[j][k]; |
593 | 275k | } |
594 | | |
595 | 34.4k | workscb.color_formats[j] = color_formats_mod[j]; |
596 | 34.4k | } |
597 | 8.61k | } |
598 | 9.06k | } |
599 | | |
600 | | // Store header fields |
601 | 46.2k | workscb.partition_count = static_cast<uint8_t>(partition_count); |
602 | 46.2k | workscb.partition_index = static_cast<uint16_t>(partition_index); |
603 | 46.2k | workscb.plane2_component = -1; |
604 | 46.2k | workscb.quant_mode = workscb.color_formats_matched ? color_quant_level_mod[i] : color_quant_level[i]; |
605 | 46.2k | workscb.block_mode = qw_bm.mode_index; |
606 | 46.2k | workscb.block_type = SYM_BTYPE_NONCONST; |
607 | | |
608 | | // Pre-realign test |
609 | 46.2k | if (l == 0) |
610 | 31.1k | { |
611 | 31.1k | float errorval = compute_difference(config, bsd, workscb, blk); |
612 | 31.1k | if (errorval == -ERROR_CALC_DEFAULT) |
613 | 6.06k | { |
614 | 6.06k | errorval = -errorval; |
615 | 6.06k | workscb.block_type = SYM_BTYPE_ERROR; |
616 | 6.06k | } |
617 | | |
618 | 31.1k | trace_add_data("error_prerealign", errorval); |
619 | 31.1k | best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode); |
620 | | |
621 | | // Average refinement improvement is 3.5% per iteration (allow 4.5%), but the first |
622 | | // iteration can help more so we give it a extra 8% leeway. Use this knowledge to |
623 | | // drive a heuristic to skip blocks that are unlikely to catch up with the best |
624 | | // block we have already. |
625 | 31.1k | unsigned int iters_remaining = config.tune_refinement_limit - l; |
626 | 31.1k | float threshold = (0.045f * static_cast<float>(iters_remaining)) + 1.08f; |
627 | 31.1k | if (errorval > (threshold * best_errorval_in_scb)) |
628 | 15.7k | { |
629 | 15.7k | break; |
630 | 15.7k | } |
631 | | |
632 | 15.3k | if (errorval < best_errorval_in_scb) |
633 | 3.90k | { |
634 | 3.90k | best_errorval_in_scb = errorval; |
635 | 3.90k | workscb.errorval = errorval; |
636 | 3.90k | scb = workscb; |
637 | | |
638 | 3.90k | if (errorval < tune_errorval_threshold) |
639 | 20 | { |
640 | | // Skip remaining candidates - this is "good enough" |
641 | 20 | i = candidate_count; |
642 | 20 | break; |
643 | 20 | } |
644 | 3.90k | } |
645 | 15.3k | } |
646 | | |
647 | 30.4k | bool adjustments; |
648 | 30.4k | if (di.weight_count != bsd.texel_count) |
649 | 4.68k | { |
650 | 4.68k | adjustments = realign_weights_decimated( |
651 | 4.68k | config.profile, bsd, blk, workscb); |
652 | 4.68k | } |
653 | 25.8k | else |
654 | 25.8k | { |
655 | 25.8k | adjustments = realign_weights_undecimated( |
656 | 25.8k | config.profile, bsd, blk, workscb); |
657 | 25.8k | } |
658 | | |
659 | | // Post-realign test |
660 | 30.4k | float errorval = compute_difference(config, bsd, workscb, blk); |
661 | 30.4k | if (errorval == -ERROR_CALC_DEFAULT) |
662 | 5.52k | { |
663 | 5.52k | errorval = -errorval; |
664 | 5.52k | workscb.block_type = SYM_BTYPE_ERROR; |
665 | 5.52k | } |
666 | | |
667 | 30.4k | trace_add_data("error_postrealign", errorval); |
668 | 30.4k | best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode); |
669 | | |
670 | | // Average refinement improvement is 3.5% per iteration, so skip blocks that are |
671 | | // unlikely to catch up with the best block we have already. Assume a 4.5% per step to |
672 | | // give benefit of the doubt ... |
673 | 30.4k | unsigned int iters_remaining = config.tune_refinement_limit - 1 - l; |
674 | 30.4k | float threshold = (0.045f * static_cast<float>(iters_remaining)) + 1.0f; |
675 | 30.4k | if (errorval > (threshold * best_errorval_in_scb)) |
676 | 4.46k | { |
677 | 4.46k | break; |
678 | 4.46k | } |
679 | | |
680 | 26.0k | if (errorval < best_errorval_in_scb) |
681 | 6.55k | { |
682 | 6.55k | best_errorval_in_scb = errorval; |
683 | 6.55k | workscb.errorval = errorval; |
684 | 6.55k | scb = workscb; |
685 | | |
686 | 6.55k | if (errorval < tune_errorval_threshold) |
687 | 6 | { |
688 | | // Skip remaining candidates - this is "good enough" |
689 | 6 | i = candidate_count; |
690 | 6 | break; |
691 | 6 | } |
692 | 6.55k | } |
693 | | |
694 | 26.0k | if (!adjustments) |
695 | 8.02k | { |
696 | 8.02k | break; |
697 | 8.02k | } |
698 | 26.0k | } |
699 | 31.1k | } |
700 | | |
701 | 11.0k | return best_errorval_in_mode; |
702 | 11.0k | } |
703 | | |
704 | | /** |
705 | | * @brief Compress a block using a chosen partitioning and 2 planes of weights. |
706 | | * |
707 | | * @param config The compressor configuration. |
708 | | * @param bsd The block size information. |
709 | | * @param blk The image block color data to compress. |
710 | | * @param tune_errorval_threshold The error value threshold. |
711 | | * @param plane2_component The component index for the second plane of weights. |
712 | | * @param[out] scb The symbolic compressed block output. |
713 | | * @param[out] tmpbuf The quantized weights for plane 1. |
714 | | */ |
715 | | static float compress_symbolic_block_for_partition_2planes( |
716 | | const astcenc_config& config, |
717 | | const block_size_descriptor& bsd, |
718 | | const image_block& blk, |
719 | | float tune_errorval_threshold, |
720 | | unsigned int plane2_component, |
721 | | symbolic_compressed_block& scb, |
722 | | compression_working_buffers& tmpbuf, |
723 | | int quant_limit |
724 | 7.02k | ) { |
725 | 7.02k | promise(config.tune_candidate_limit > 0); |
726 | 7.02k | promise(config.tune_refinement_limit > 0); |
727 | 7.02k | promise(bsd.decimation_mode_count_selected > 0); |
728 | | |
729 | 7.02k | int max_weight_quant = astc::min(static_cast<int>(QUANT_32), quant_limit); |
730 | | |
731 | | // Compute ideal weights and endpoint colors, with no quantization or decimation |
732 | 7.02k | endpoints_and_weights& ei1 = tmpbuf.ei1; |
733 | 7.02k | endpoints_and_weights& ei2 = tmpbuf.ei2; |
734 | | |
735 | 7.02k | compute_ideal_colors_and_weights_2planes(bsd, blk, plane2_component, ei1, ei2); |
736 | | |
737 | | // Compute ideal weights and endpoint colors for every decimation |
738 | 7.02k | float* dec_weights_ideal = tmpbuf.dec_weights_ideal; |
739 | 7.02k | uint8_t* dec_weights_uquant = tmpbuf.dec_weights_uquant; |
740 | | |
741 | | // For each decimation mode, compute an ideal set of weights with no quantization |
742 | 72.0k | for (unsigned int i = 0; i < bsd.decimation_mode_count_selected; i++) |
743 | 65.0k | { |
744 | 65.0k | const auto& dm = bsd.get_decimation_mode(i); |
745 | 65.0k | if (!dm.is_ref_2plane(static_cast<quant_method>(max_weight_quant))) |
746 | 25.9k | { |
747 | 25.9k | continue; |
748 | 25.9k | } |
749 | | |
750 | 39.0k | const auto& di = bsd.get_decimation_info(i); |
751 | | |
752 | 39.0k | compute_ideal_weights_for_decimation( |
753 | 39.0k | ei1, |
754 | 39.0k | di, |
755 | 39.0k | dec_weights_ideal + i * BLOCK_MAX_WEIGHTS); |
756 | | |
757 | 39.0k | compute_ideal_weights_for_decimation( |
758 | 39.0k | ei2, |
759 | 39.0k | di, |
760 | 39.0k | dec_weights_ideal + i * BLOCK_MAX_WEIGHTS + WEIGHTS_PLANE2_OFFSET); |
761 | 39.0k | } |
762 | | |
763 | | // Compute maximum colors for the endpoints and ideal weights, then for each endpoint and ideal |
764 | | // weight pair, compute the smallest weight that will result in a color value greater than 1 |
765 | 7.02k | vfloat4 min_ep1(10.0f); |
766 | 7.02k | vfloat4 min_ep2(10.0f); |
767 | | |
768 | 7.02k | vfloat4 ep1 = (vfloat4(1.0f) - ei1.ep.endpt0[0]) / (ei1.ep.endpt1[0] - ei1.ep.endpt0[0]); |
769 | 7.02k | vmask4 use_ep1 = (ep1 > vfloat4(0.5f)) & (ep1 < min_ep1); |
770 | 7.02k | min_ep1 = select(min_ep1, ep1, use_ep1); |
771 | | |
772 | 7.02k | vfloat4 ep2 = (vfloat4(1.0f) - ei2.ep.endpt0[0]) / (ei2.ep.endpt1[0] - ei2.ep.endpt0[0]); |
773 | 7.02k | vmask4 use_ep2 = (ep2 > vfloat4(0.5f)) & (ep2 < min_ep2); |
774 | 7.02k | min_ep2 = select(min_ep2, ep2, use_ep2); |
775 | | |
776 | 7.02k | vfloat4 err_max(ERROR_CALC_DEFAULT); |
777 | 7.02k | vmask4 err_mask = vint4::lane_id() == vint4(plane2_component); |
778 | | |
779 | | // Set the plane2 component to max error in ep1 |
780 | 7.02k | min_ep1 = select(min_ep1, err_max, err_mask); |
781 | | |
782 | 7.02k | float min_wt_cutoff1 = hmin_s(min_ep1); |
783 | | |
784 | | // Set the minwt2 to the plane2 component min in ep2 |
785 | 7.02k | float min_wt_cutoff2 = hmin_s(select(err_max, min_ep2, err_mask)); |
786 | | |
787 | 7.02k | compute_angular_endpoints_2planes( |
788 | 7.02k | bsd, dec_weights_ideal, max_weight_quant, tmpbuf); |
789 | | |
790 | | // For each mode (which specifies a decimation and a quantization): |
791 | | // * Compute number of bits needed for the quantized weights |
792 | | // * Generate an optimized set of quantized weights |
793 | | // * Compute quantization errors for the mode |
794 | | |
795 | 7.02k | float* weight_low_value1 = tmpbuf.weight_low_value1; |
796 | 7.02k | float* weight_high_value1 = tmpbuf.weight_high_value1; |
797 | 7.02k | float* weight_low_value2 = tmpbuf.weight_low_value2; |
798 | 7.02k | float* weight_high_value2 = tmpbuf.weight_high_value2; |
799 | | |
800 | 7.02k | int8_t* qwt_bitcounts = tmpbuf.qwt_bitcounts; |
801 | 7.02k | float* qwt_errors = tmpbuf.qwt_errors; |
802 | | |
803 | 7.02k | unsigned int start_2plane = bsd.block_mode_count_1plane_selected; |
804 | 7.02k | unsigned int end_2plane = bsd.block_mode_count_1plane_2plane_selected; |
805 | | |
806 | 78.9k | for (unsigned int i = start_2plane; i < end_2plane; i++) |
807 | 71.9k | { |
808 | 71.9k | const block_mode& bm = bsd.block_modes[i]; |
809 | 71.9k | assert(bm.is_dual_plane); |
810 | | |
811 | 71.9k | if (bm.quant_mode > max_weight_quant) |
812 | 22.1k | { |
813 | 22.1k | qwt_errors[i] = 1e38f; |
814 | 22.1k | continue; |
815 | 22.1k | } |
816 | | |
817 | 49.7k | qwt_bitcounts[i] = static_cast<int8_t>(109 - bm.weight_bits); |
818 | | |
819 | 49.7k | if (weight_high_value1[i] > 1.02f * min_wt_cutoff1) |
820 | 5.85k | { |
821 | 5.85k | weight_high_value1[i] = 1.0f; |
822 | 5.85k | } |
823 | | |
824 | 49.7k | if (weight_high_value2[i] > 1.02f * min_wt_cutoff2) |
825 | 562 | { |
826 | 562 | weight_high_value2[i] = 1.0f; |
827 | 562 | } |
828 | | |
829 | 49.7k | unsigned int decimation_mode = bm.decimation_mode; |
830 | 49.7k | const auto& di = bsd.get_decimation_info(decimation_mode); |
831 | | |
832 | 49.7k | ASTCENC_ALIGNAS float dec_weights_uquantf[BLOCK_MAX_WEIGHTS]; |
833 | | |
834 | | // Generate the optimized set of weights for the mode |
835 | 49.7k | compute_quantized_weights_for_decimation( |
836 | 49.7k | di, |
837 | 49.7k | weight_low_value1[i], |
838 | 49.7k | weight_high_value1[i], |
839 | 49.7k | dec_weights_ideal + BLOCK_MAX_WEIGHTS * decimation_mode, |
840 | 49.7k | dec_weights_uquantf, |
841 | 49.7k | dec_weights_uquant + BLOCK_MAX_WEIGHTS * i, |
842 | 49.7k | bm.get_weight_quant_mode()); |
843 | | |
844 | 49.7k | compute_quantized_weights_for_decimation( |
845 | 49.7k | di, |
846 | 49.7k | weight_low_value2[i], |
847 | 49.7k | weight_high_value2[i], |
848 | 49.7k | dec_weights_ideal + BLOCK_MAX_WEIGHTS * decimation_mode + WEIGHTS_PLANE2_OFFSET, |
849 | 49.7k | dec_weights_uquantf + WEIGHTS_PLANE2_OFFSET, |
850 | 49.7k | dec_weights_uquant + BLOCK_MAX_WEIGHTS * i + WEIGHTS_PLANE2_OFFSET, |
851 | 49.7k | bm.get_weight_quant_mode()); |
852 | | |
853 | | // Compute weight quantization errors for the block mode |
854 | 49.7k | qwt_errors[i] = compute_error_of_weight_set_2planes( |
855 | 49.7k | ei1, |
856 | 49.7k | ei2, |
857 | 49.7k | di, |
858 | 49.7k | dec_weights_uquantf, |
859 | 49.7k | dec_weights_uquantf + WEIGHTS_PLANE2_OFFSET); |
860 | 49.7k | } |
861 | | |
862 | | // Decide the optimal combination of color endpoint encodings and weight encodings |
863 | 7.02k | uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS]; |
864 | 7.02k | int block_mode_index[TUNE_MAX_TRIAL_CANDIDATES]; |
865 | | |
866 | 7.02k | quant_method color_quant_level[TUNE_MAX_TRIAL_CANDIDATES]; |
867 | 7.02k | quant_method color_quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES]; |
868 | | |
869 | 7.02k | endpoints epm; |
870 | 7.02k | merge_endpoints(ei1.ep, ei2.ep, plane2_component, epm); |
871 | | |
872 | 7.02k | const auto& pi = bsd.get_partition_info(1, 0); |
873 | 7.02k | unsigned int candidate_count = compute_ideal_endpoint_formats( |
874 | 7.02k | pi, blk, epm, qwt_bitcounts, qwt_errors, |
875 | 7.02k | config.tune_candidate_limit, |
876 | 7.02k | bsd.block_mode_count_1plane_selected, bsd.block_mode_count_1plane_2plane_selected, |
877 | 7.02k | partition_format_specifiers, block_mode_index, |
878 | 7.02k | color_quant_level, color_quant_level_mod, tmpbuf); |
879 | | |
880 | | // Iterate over the N believed-to-be-best modes to find out which one is actually best |
881 | 7.02k | float best_errorval_in_mode = ERROR_CALC_DEFAULT; |
882 | 7.02k | float best_errorval_in_scb = scb.errorval; |
883 | | |
884 | 24.7k | for (unsigned int i = 0; i < candidate_count; i++) |
885 | 17.6k | { |
886 | 17.6k | TRACE_NODE(node0, "candidate"); |
887 | | |
888 | 17.6k | const int bm_packed_index = block_mode_index[i]; |
889 | 17.6k | assert(bm_packed_index >= static_cast<int>(bsd.block_mode_count_1plane_selected) && |
890 | 17.6k | bm_packed_index < static_cast<int>(bsd.block_mode_count_1plane_2plane_selected)); |
891 | 17.6k | const block_mode& qw_bm = bsd.block_modes[bm_packed_index]; |
892 | | |
893 | 17.6k | int decimation_mode = qw_bm.decimation_mode; |
894 | 17.6k | const auto& di = bsd.get_decimation_info(decimation_mode); |
895 | 17.6k | promise(di.weight_count > 0); |
896 | | |
897 | 17.6k | trace_add_data("weight_x", di.weight_x); |
898 | 17.6k | trace_add_data("weight_y", di.weight_y); |
899 | 17.6k | trace_add_data("weight_z", di.weight_z); |
900 | 17.6k | trace_add_data("weight_quant", qw_bm.quant_mode); |
901 | | |
902 | 17.6k | vfloat4 rgbs_color; |
903 | 17.6k | vfloat4 rgbo_color; |
904 | | |
905 | 17.6k | symbolic_compressed_block workscb; |
906 | 17.6k | endpoints workep = epm; |
907 | | |
908 | 17.6k | uint8_t* u8_weight1_src = dec_weights_uquant + BLOCK_MAX_WEIGHTS * bm_packed_index; |
909 | 17.6k | uint8_t* u8_weight2_src = dec_weights_uquant + BLOCK_MAX_WEIGHTS * bm_packed_index + WEIGHTS_PLANE2_OFFSET; |
910 | | |
911 | 308k | for (int j = 0; j < di.weight_count; j++) |
912 | 291k | { |
913 | 291k | workscb.weights[j] = u8_weight1_src[j]; |
914 | 291k | workscb.weights[j + WEIGHTS_PLANE2_OFFSET] = u8_weight2_src[j]; |
915 | 291k | } |
916 | | |
917 | 27.3k | for (unsigned int l = 0; l < config.tune_refinement_limit; l++) |
918 | 25.8k | { |
919 | 25.8k | recompute_ideal_colors_2planes( |
920 | 25.8k | blk, bsd, di, |
921 | 25.8k | workscb.weights, workscb.weights + WEIGHTS_PLANE2_OFFSET, |
922 | 25.8k | workep, rgbs_color, rgbo_color, plane2_component); |
923 | | |
924 | | // Quantize the chosen color |
925 | 25.8k | workscb.color_formats[0] = pack_color_endpoints( |
926 | 25.8k | workep.endpt0[0], |
927 | 25.8k | workep.endpt1[0], |
928 | 25.8k | rgbs_color, rgbo_color, |
929 | 25.8k | partition_format_specifiers[i][0], |
930 | 25.8k | workscb.color_values[0], |
931 | 25.8k | color_quant_level[i]); |
932 | | |
933 | | // Store header fields |
934 | 25.8k | workscb.partition_count = 1; |
935 | 25.8k | workscb.partition_index = 0; |
936 | 25.8k | workscb.quant_mode = color_quant_level[i]; |
937 | 25.8k | workscb.color_formats_matched = 0; |
938 | 25.8k | workscb.block_mode = qw_bm.mode_index; |
939 | 25.8k | workscb.plane2_component = static_cast<int8_t>(plane2_component); |
940 | 25.8k | workscb.block_type = SYM_BTYPE_NONCONST; |
941 | | |
942 | | // Pre-realign test |
943 | 25.8k | if (l == 0) |
944 | 17.6k | { |
945 | 17.6k | float errorval = compute_symbolic_block_difference_2plane(config, bsd, workscb, blk); |
946 | 17.6k | if (errorval == -ERROR_CALC_DEFAULT) |
947 | 5.38k | { |
948 | 5.38k | errorval = -errorval; |
949 | 5.38k | workscb.block_type = SYM_BTYPE_ERROR; |
950 | 5.38k | } |
951 | | |
952 | 17.6k | trace_add_data("error_prerealign", errorval); |
953 | 17.6k | best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode); |
954 | | |
955 | | // Average refinement improvement is 3.5% per iteration (allow 4.5%), but the first |
956 | | // iteration can help more so we give it a extra 8% leeway. Use this knowledge to |
957 | | // drive a heuristic to skip blocks that are unlikely to catch up with the best |
958 | | // block we have already. |
959 | 17.6k | unsigned int iters_remaining = config.tune_refinement_limit - l; |
960 | 17.6k | float threshold = (0.045f * static_cast<float>(iters_remaining)) + 1.08f; |
961 | 17.6k | if (errorval > (threshold * best_errorval_in_scb)) |
962 | 9.35k | { |
963 | 9.35k | break; |
964 | 9.35k | } |
965 | | |
966 | 8.33k | if (errorval < best_errorval_in_scb) |
967 | 1.63k | { |
968 | 1.63k | best_errorval_in_scb = errorval; |
969 | 1.63k | workscb.errorval = errorval; |
970 | 1.63k | scb = workscb; |
971 | | |
972 | 1.63k | if (errorval < tune_errorval_threshold) |
973 | 5 | { |
974 | | // Skip remaining candidates - this is "good enough" |
975 | 5 | i = candidate_count; |
976 | 5 | break; |
977 | 5 | } |
978 | 1.63k | } |
979 | 8.33k | } |
980 | | |
981 | | // Perform a final pass over the weights to try to improve them. |
982 | 16.5k | bool adjustments; |
983 | 16.5k | if (di.weight_count != bsd.texel_count) |
984 | 7.77k | { |
985 | 7.77k | adjustments = realign_weights_decimated( |
986 | 7.77k | config.profile, bsd, blk, workscb); |
987 | 7.77k | } |
988 | 8.76k | else |
989 | 8.76k | { |
990 | 8.76k | adjustments = realign_weights_undecimated( |
991 | 8.76k | config.profile, bsd, blk, workscb); |
992 | 8.76k | } |
993 | | |
994 | | // Post-realign test |
995 | 16.5k | float errorval = compute_symbolic_block_difference_2plane(config, bsd, workscb, blk); |
996 | 16.5k | if (errorval == -ERROR_CALC_DEFAULT) |
997 | 5.59k | { |
998 | 5.59k | errorval = -errorval; |
999 | 5.59k | workscb.block_type = SYM_BTYPE_ERROR; |
1000 | 5.59k | } |
1001 | | |
1002 | 16.5k | trace_add_data("error_postrealign", errorval); |
1003 | 16.5k | best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode); |
1004 | | |
1005 | | // Average refinement improvement is 3.5% per iteration, so skip blocks that are |
1006 | | // unlikely to catch up with the best block we have already. Assume a 4.5% per step to |
1007 | | // give benefit of the doubt ... |
1008 | 16.5k | unsigned int iters_remaining = config.tune_refinement_limit - 1 - l; |
1009 | 16.5k | float threshold = (0.045f * static_cast<float>(iters_remaining)) + 1.0f; |
1010 | 16.5k | if (errorval > (threshold * best_errorval_in_scb)) |
1011 | 1.61k | { |
1012 | 1.61k | break; |
1013 | 1.61k | } |
1014 | | |
1015 | 14.9k | if (errorval < best_errorval_in_scb) |
1016 | 2.38k | { |
1017 | 2.38k | best_errorval_in_scb = errorval; |
1018 | 2.38k | workscb.errorval = errorval; |
1019 | 2.38k | scb = workscb; |
1020 | | |
1021 | 2.38k | if (errorval < tune_errorval_threshold) |
1022 | 4 | { |
1023 | | // Skip remaining candidates - this is "good enough" |
1024 | 4 | i = candidate_count; |
1025 | 4 | break; |
1026 | 4 | } |
1027 | 2.38k | } |
1028 | | |
1029 | 14.9k | if (!adjustments) |
1030 | 5.27k | { |
1031 | 5.27k | break; |
1032 | 5.27k | } |
1033 | 14.9k | } |
1034 | 17.6k | } |
1035 | | |
1036 | 7.02k | return best_errorval_in_mode; |
1037 | 7.02k | } |
1038 | | |
1039 | | /** |
1040 | | * @brief Determine the lowest cross-channel correlation factor. |
1041 | | * |
1042 | | * @param texels_per_block The number of texels in a block. |
1043 | | * @param blk The image block color data to compress. |
1044 | | * |
1045 | | * @return Return the lowest correlation factor. |
1046 | | */ |
1047 | | static float prepare_block_statistics( |
1048 | | int texels_per_block, |
1049 | | const image_block& blk |
1050 | 2.19k | ) { |
1051 | | // Compute covariance matrix, as a collection of 10 scalars that form the upper-triangular row |
1052 | | // of the matrix. The matrix is symmetric, so this is all we need for this use case. |
1053 | 2.19k | float rs = 0.0f; |
1054 | 2.19k | float gs = 0.0f; |
1055 | 2.19k | float bs = 0.0f; |
1056 | 2.19k | float as = 0.0f; |
1057 | 2.19k | float rr_var = 0.0f; |
1058 | 2.19k | float gg_var = 0.0f; |
1059 | 2.19k | float bb_var = 0.0f; |
1060 | 2.19k | float aa_var = 0.0f; |
1061 | 2.19k | float rg_cov = 0.0f; |
1062 | 2.19k | float rb_cov = 0.0f; |
1063 | 2.19k | float ra_cov = 0.0f; |
1064 | 2.19k | float gb_cov = 0.0f; |
1065 | 2.19k | float ga_cov = 0.0f; |
1066 | 2.19k | float ba_cov = 0.0f; |
1067 | | |
1068 | 2.19k | float weight_sum = 0.0f; |
1069 | | |
1070 | 2.19k | promise(texels_per_block > 0); |
1071 | 58.8k | for (int i = 0; i < texels_per_block; i++) |
1072 | 56.6k | { |
1073 | 56.6k | float weight = hadd_s(blk.channel_weight) / 4.0f; |
1074 | 56.6k | assert(weight >= 0.0f); |
1075 | 56.6k | weight_sum += weight; |
1076 | | |
1077 | 56.6k | float r = blk.data_r[i]; |
1078 | 56.6k | float g = blk.data_g[i]; |
1079 | 56.6k | float b = blk.data_b[i]; |
1080 | 56.6k | float a = blk.data_a[i]; |
1081 | | |
1082 | 56.6k | float rw = r * weight; |
1083 | 56.6k | rs += rw; |
1084 | 56.6k | rr_var += r * rw; |
1085 | 56.6k | rg_cov += g * rw; |
1086 | 56.6k | rb_cov += b * rw; |
1087 | 56.6k | ra_cov += a * rw; |
1088 | | |
1089 | 56.6k | float gw = g * weight; |
1090 | 56.6k | gs += gw; |
1091 | 56.6k | gg_var += g * gw; |
1092 | 56.6k | gb_cov += b * gw; |
1093 | 56.6k | ga_cov += a * gw; |
1094 | | |
1095 | 56.6k | float bw = b * weight; |
1096 | 56.6k | bs += bw; |
1097 | 56.6k | bb_var += b * bw; |
1098 | 56.6k | ba_cov += a * bw; |
1099 | | |
1100 | 56.6k | float aw = a * weight; |
1101 | 56.6k | as += aw; |
1102 | 56.6k | aa_var += a * aw; |
1103 | 56.6k | } |
1104 | | |
1105 | 2.19k | float rpt = 1.0f / astc::max(weight_sum, 1e-7f); |
1106 | | |
1107 | 2.19k | rr_var -= rs * (rs * rpt); |
1108 | 2.19k | rg_cov -= gs * (rs * rpt); |
1109 | 2.19k | rb_cov -= bs * (rs * rpt); |
1110 | 2.19k | ra_cov -= as * (rs * rpt); |
1111 | | |
1112 | 2.19k | gg_var -= gs * (gs * rpt); |
1113 | 2.19k | gb_cov -= bs * (gs * rpt); |
1114 | 2.19k | ga_cov -= as * (gs * rpt); |
1115 | | |
1116 | 2.19k | bb_var -= bs * (bs * rpt); |
1117 | 2.19k | ba_cov -= as * (bs * rpt); |
1118 | | |
1119 | 2.19k | aa_var -= as * (as * rpt); |
1120 | | |
1121 | | // These will give a NaN if a channel is constant - these are fixed up in the next step |
1122 | 2.19k | rg_cov *= astc::rsqrt(rr_var * gg_var); |
1123 | 2.19k | rb_cov *= astc::rsqrt(rr_var * bb_var); |
1124 | 2.19k | ra_cov *= astc::rsqrt(rr_var * aa_var); |
1125 | 2.19k | gb_cov *= astc::rsqrt(gg_var * bb_var); |
1126 | 2.19k | ga_cov *= astc::rsqrt(gg_var * aa_var); |
1127 | 2.19k | ba_cov *= astc::rsqrt(bb_var * aa_var); |
1128 | | |
1129 | 2.19k | if (astc::isnan(rg_cov)) rg_cov = 1.0f; |
1130 | 2.19k | if (astc::isnan(rb_cov)) rb_cov = 1.0f; |
1131 | 2.19k | if (astc::isnan(ra_cov)) ra_cov = 1.0f; |
1132 | 2.19k | if (astc::isnan(gb_cov)) gb_cov = 1.0f; |
1133 | 2.19k | if (astc::isnan(ga_cov)) ga_cov = 1.0f; |
1134 | 2.19k | if (astc::isnan(ba_cov)) ba_cov = 1.0f; |
1135 | | |
1136 | 2.19k | float lowest_correlation = astc::min(fabsf(rg_cov), fabsf(rb_cov)); |
1137 | 2.19k | lowest_correlation = astc::min(lowest_correlation, fabsf(ra_cov)); |
1138 | 2.19k | lowest_correlation = astc::min(lowest_correlation, fabsf(gb_cov)); |
1139 | 2.19k | lowest_correlation = astc::min(lowest_correlation, fabsf(ga_cov)); |
1140 | 2.19k | lowest_correlation = astc::min(lowest_correlation, fabsf(ba_cov)); |
1141 | | |
1142 | | // Diagnostic trace points |
1143 | 2.19k | trace_add_data("min_r", blk.data_min.lane<0>()); |
1144 | 2.19k | trace_add_data("max_r", blk.data_max.lane<0>()); |
1145 | 2.19k | trace_add_data("min_g", blk.data_min.lane<1>()); |
1146 | 2.19k | trace_add_data("max_g", blk.data_max.lane<1>()); |
1147 | 2.19k | trace_add_data("min_b", blk.data_min.lane<2>()); |
1148 | 2.19k | trace_add_data("max_b", blk.data_max.lane<2>()); |
1149 | 2.19k | trace_add_data("min_a", blk.data_min.lane<3>()); |
1150 | 2.19k | trace_add_data("max_a", blk.data_max.lane<3>()); |
1151 | 2.19k | trace_add_data("cov_rg", fabsf(rg_cov)); |
1152 | 2.19k | trace_add_data("cov_rb", fabsf(rb_cov)); |
1153 | 2.19k | trace_add_data("cov_ra", fabsf(ra_cov)); |
1154 | 2.19k | trace_add_data("cov_gb", fabsf(gb_cov)); |
1155 | 2.19k | trace_add_data("cov_ga", fabsf(ga_cov)); |
1156 | 2.19k | trace_add_data("cov_ba", fabsf(ba_cov)); |
1157 | | |
1158 | 2.19k | return lowest_correlation; |
1159 | 2.19k | } |
1160 | | |
1161 | | /* See header for documentation. */ |
1162 | | void compress_block( |
1163 | | const astcenc_contexti& ctx, |
1164 | | const image_block& blk, |
1165 | | uint8_t pcb[16], |
1166 | | compression_working_buffers& tmpbuf) |
1167 | 2.23k | { |
1168 | 2.23k | astcenc_profile decode_mode = ctx.config.profile; |
1169 | 2.23k | symbolic_compressed_block scb; |
1170 | 2.23k | const block_size_descriptor& bsd = *ctx.bsd; |
1171 | 2.23k | float lowest_correl; |
1172 | | |
1173 | 2.23k | TRACE_NODE(node0, "block"); |
1174 | 2.23k | trace_add_data("pos_x", blk.pos_x); |
1175 | 2.23k | trace_add_data("pos_y", blk.pos_y); |
1176 | 2.23k | trace_add_data("pos_z", blk.pos_z); |
1177 | | |
1178 | | // Set stricter block targets for luminance data as we have more bits to play with |
1179 | 2.23k | bool block_is_l = blk.is_luminance(); |
1180 | 2.23k | float block_is_l_scale = block_is_l ? 1.0f / 1.5f : 1.0f; |
1181 | | |
1182 | | // Set slightly stricter block targets for lumalpha data as we have more bits to play with |
1183 | 2.23k | bool block_is_la = blk.is_luminancealpha(); |
1184 | 2.23k | float block_is_la_scale = block_is_la ? 1.0f / 1.05f : 1.0f; |
1185 | | |
1186 | 2.23k | bool block_skip_two_plane = false; |
1187 | 2.23k | int max_partitions = ctx.config.tune_partition_count_limit; |
1188 | | |
1189 | 2.23k | unsigned int requested_partition_indices[3] { |
1190 | 2.23k | ctx.config.tune_2partition_index_limit, |
1191 | 2.23k | ctx.config.tune_3partition_index_limit, |
1192 | 2.23k | ctx.config.tune_4partition_index_limit |
1193 | 2.23k | }; |
1194 | | |
1195 | 2.23k | unsigned int requested_partition_trials[3] { |
1196 | 2.23k | ctx.config.tune_2partitioning_candidate_limit, |
1197 | 2.23k | ctx.config.tune_3partitioning_candidate_limit, |
1198 | 2.23k | ctx.config.tune_4partitioning_candidate_limit |
1199 | 2.23k | }; |
1200 | | |
1201 | | #if defined(ASTCENC_DIAGNOSTICS) |
1202 | | // Do this early in diagnostic builds so we can dump uniform metrics |
1203 | | // for every block. Do it later in release builds to avoid redundant work! |
1204 | | float error_weight_sum = hadd_s(blk.channel_weight) * bsd.texel_count; |
1205 | | float error_threshold = ctx.config.tune_db_limit |
1206 | | * error_weight_sum |
1207 | | * block_is_l_scale |
1208 | | * block_is_la_scale; |
1209 | | |
1210 | | lowest_correl = prepare_block_statistics(bsd.texel_count, blk); |
1211 | | trace_add_data("lowest_correl", lowest_correl); |
1212 | | trace_add_data("tune_error_threshold", error_threshold); |
1213 | | #endif |
1214 | | |
1215 | | // Detected a constant-color block |
1216 | 2.23k | if (all(blk.data_min == blk.data_max)) |
1217 | 10 | { |
1218 | 10 | TRACE_NODE(node1, "pass"); |
1219 | 10 | trace_add_data("partition_count", 0); |
1220 | 10 | trace_add_data("plane_count", 1); |
1221 | | |
1222 | 10 | scb.partition_count = 0; |
1223 | | |
1224 | | // Encode as FP16 if using HDR |
1225 | 10 | if ((decode_mode == ASTCENC_PRF_HDR) || |
1226 | 7 | (decode_mode == ASTCENC_PRF_HDR_RGB_LDR_A)) |
1227 | 6 | { |
1228 | 6 | scb.block_type = SYM_BTYPE_CONST_F16; |
1229 | 6 | vint4 color_f16 = float_to_float16(blk.origin_texel); |
1230 | 6 | store(color_f16, scb.constant_color); |
1231 | 6 | } |
1232 | | // Encode as UNORM16 if NOT using HDR |
1233 | 4 | else |
1234 | 4 | { |
1235 | 4 | scb.block_type = SYM_BTYPE_CONST_U16; |
1236 | 4 | vfloat4 color_f32 = clamp(0.0f, 1.0f, blk.origin_texel) * 65535.0f; |
1237 | 4 | vint4 color_u16 = float_to_int_rtn(color_f32); |
1238 | 4 | store(color_u16, scb.constant_color); |
1239 | 4 | } |
1240 | | |
1241 | 10 | trace_add_data("exit", "quality hit"); |
1242 | | |
1243 | 10 | symbolic_to_physical(bsd, scb, pcb); |
1244 | 10 | return; |
1245 | 10 | } |
1246 | | |
1247 | 2.22k | #if !defined(ASTCENC_DIAGNOSTICS) |
1248 | 2.22k | float error_weight_sum = hadd_s(blk.channel_weight) * bsd.texel_count; |
1249 | 2.22k | float error_threshold = ctx.config.tune_db_limit |
1250 | 2.22k | * error_weight_sum |
1251 | 2.22k | * block_is_l_scale |
1252 | 2.22k | * block_is_la_scale; |
1253 | 2.22k | #endif |
1254 | | |
1255 | | // Set SCB and mode errors to a very high error value |
1256 | 2.22k | scb.errorval = ERROR_CALC_DEFAULT; |
1257 | 2.22k | scb.block_type = SYM_BTYPE_ERROR; |
1258 | | |
1259 | 2.22k | float best_errorvals_for_pcount[BLOCK_MAX_PARTITIONS] { |
1260 | 2.22k | ERROR_CALC_DEFAULT, ERROR_CALC_DEFAULT, ERROR_CALC_DEFAULT, ERROR_CALC_DEFAULT |
1261 | 2.22k | }; |
1262 | | |
1263 | 2.22k | float exit_thresholds_for_pcount[BLOCK_MAX_PARTITIONS] { |
1264 | 2.22k | 0.0f, |
1265 | 2.22k | ctx.config.tune_2partition_early_out_limit_factor, |
1266 | 2.22k | ctx.config.tune_3partition_early_out_limit_factor, |
1267 | 2.22k | 0.0f |
1268 | 2.22k | }; |
1269 | | |
1270 | | // Trial using 1 plane of weights and 1 partition. |
1271 | | |
1272 | | // Most of the time we test it twice, first with a mode cutoff of 0 and then with the specified |
1273 | | // mode cutoff. This causes an early-out that speeds up encoding of easy blocks. However, this |
1274 | | // optimization is disabled for 4x4 and 5x4 blocks where it nearly always slows down the |
1275 | | // compression and slightly reduces image quality. |
1276 | | |
1277 | 2.22k | float errorval_mult[2] { |
1278 | 2.22k | 1.0f / ctx.config.tune_mse_overshoot, |
1279 | 2.22k | 1.0f |
1280 | 2.22k | }; |
1281 | | |
1282 | 2.22k | const float errorval_overshoot = 1.0f / ctx.config.tune_mse_overshoot; |
1283 | | |
1284 | | // Only enable MODE0 fast path if enabled |
1285 | | // Never enable for 3D blocks as no "always" block modes are available |
1286 | 2.22k | int start_trial = 1; |
1287 | 2.22k | if ((ctx.config.tune_search_mode0_enable >= TUNE_MIN_SEARCH_MODE0) && (bsd.dim_z == 1)) |
1288 | 362 | { |
1289 | 362 | start_trial = 0; |
1290 | 362 | } |
1291 | | |
1292 | 2.22k | int quant_limit = QUANT_32; |
1293 | 4.78k | for (int i = start_trial; i < 2; i++) |
1294 | 2.58k | { |
1295 | 2.58k | TRACE_NODE(node1, "pass"); |
1296 | 2.58k | trace_add_data("partition_count", 1); |
1297 | 2.58k | trace_add_data("plane_count", 1); |
1298 | 2.58k | trace_add_data("search_mode", i); |
1299 | | |
1300 | 2.58k | float errorval = compress_symbolic_block_for_partition_1plane( |
1301 | 2.58k | ctx.config, bsd, blk, i == 0, |
1302 | 2.58k | error_threshold * errorval_mult[i] * errorval_overshoot, |
1303 | 2.58k | 1, 0, scb, tmpbuf, QUANT_32); |
1304 | | |
1305 | | // Record the quant level so we can use the filter later searches |
1306 | 2.58k | if (scb.block_type != SYM_BTYPE_ERROR) |
1307 | 1.95k | { |
1308 | 1.95k | const auto& bm = bsd.get_block_mode(scb.block_mode); |
1309 | 1.95k | quant_limit = bm.get_weight_quant_mode(); |
1310 | 1.95k | } |
1311 | | |
1312 | 2.58k | best_errorvals_for_pcount[0] = astc::min(best_errorvals_for_pcount[0], errorval); |
1313 | 2.58k | if (errorval < (error_threshold * errorval_mult[i])) |
1314 | 27 | { |
1315 | 27 | trace_add_data("exit", "quality hit"); |
1316 | 27 | goto END_OF_TESTS; |
1317 | 27 | } |
1318 | 2.58k | } |
1319 | | |
1320 | 2.19k | #if !defined(ASTCENC_DIAGNOSTICS) |
1321 | 2.19k | lowest_correl = prepare_block_statistics(bsd.texel_count, blk); |
1322 | 2.19k | #endif |
1323 | | |
1324 | 2.19k | block_skip_two_plane = lowest_correl > ctx.config.tune_2plane_early_out_limit_correlation; |
1325 | | |
1326 | | // Test the four possible 1-partition, 2-planes modes. Do this in reverse, as |
1327 | | // alpha is the most likely to be non-correlated if it is present in the data. |
1328 | 9.44k | for (int i = BLOCK_MAX_COMPONENTS - 1; i >= 0; i--) |
1329 | 7.70k | { |
1330 | 7.70k | TRACE_NODE(node1, "pass"); |
1331 | 7.70k | trace_add_data("partition_count", 1); |
1332 | 7.70k | trace_add_data("plane_count", 2); |
1333 | 7.70k | trace_add_data("plane_component", i); |
1334 | | |
1335 | 7.70k | if (block_skip_two_plane) |
1336 | 228 | { |
1337 | 228 | trace_add_data("skip", "tune_2plane_early_out_limit_correlation"); |
1338 | 228 | continue; |
1339 | 228 | } |
1340 | | |
1341 | 7.47k | if (blk.grayscale && i != 3) |
1342 | 3 | { |
1343 | 3 | trace_add_data("skip", "grayscale block"); |
1344 | 3 | continue; |
1345 | 3 | } |
1346 | | |
1347 | 7.46k | if (blk.is_constant_channel(i)) |
1348 | 448 | { |
1349 | 448 | trace_add_data("skip", "constant component"); |
1350 | 448 | continue; |
1351 | 448 | } |
1352 | | |
1353 | 7.02k | float errorval = compress_symbolic_block_for_partition_2planes( |
1354 | 7.02k | ctx.config, bsd, blk, error_threshold * errorval_overshoot, |
1355 | 7.02k | i, scb, tmpbuf, quant_limit); |
1356 | | |
1357 | | // If attempting two planes is much worse than the best one plane result |
1358 | | // then further two plane searches are unlikely to help so move on ... |
1359 | 7.02k | if (errorval > (best_errorvals_for_pcount[0] * 1.85f)) |
1360 | 440 | { |
1361 | 440 | break; |
1362 | 440 | } |
1363 | | |
1364 | 6.58k | if (errorval < error_threshold) |
1365 | 11 | { |
1366 | 11 | trace_add_data("exit", "quality hit"); |
1367 | 11 | goto END_OF_TESTS; |
1368 | 11 | } |
1369 | 6.58k | } |
1370 | | |
1371 | | // Find best blocks for 2, 3 and 4 partitions |
1372 | 5.41k | for (int partition_count = 2; partition_count <= max_partitions; partition_count++) |
1373 | 5.01k | { |
1374 | 5.01k | unsigned int partition_indices[TUNE_MAX_PARTITIONING_CANDIDATES]; |
1375 | | |
1376 | 5.01k | unsigned int requested_indices = requested_partition_indices[partition_count - 2]; |
1377 | | |
1378 | 5.01k | unsigned int requested_trials = requested_partition_trials[partition_count - 2]; |
1379 | 5.01k | requested_trials = astc::min(requested_trials, requested_indices); |
1380 | | |
1381 | 5.01k | unsigned int actual_trials = find_best_partition_candidates( |
1382 | 5.01k | bsd, blk, partition_count, requested_indices, partition_indices, requested_trials); |
1383 | | |
1384 | 5.01k | float best_error_in_prev = best_errorvals_for_pcount[partition_count - 2]; |
1385 | | |
1386 | 11.9k | for (unsigned int i = 0; i < actual_trials; i++) |
1387 | 8.45k | { |
1388 | 8.45k | TRACE_NODE(node1, "pass"); |
1389 | 8.45k | trace_add_data("partition_count", partition_count); |
1390 | 8.45k | trace_add_data("partition_index", partition_indices[i]); |
1391 | 8.45k | trace_add_data("plane_count", 1); |
1392 | 8.45k | trace_add_data("search_mode", i); |
1393 | | |
1394 | 8.45k | float errorval = compress_symbolic_block_for_partition_1plane( |
1395 | 8.45k | ctx.config, bsd, blk, false, |
1396 | 8.45k | error_threshold * errorval_overshoot, |
1397 | 8.45k | partition_count, partition_indices[i], |
1398 | 8.45k | scb, tmpbuf, quant_limit); |
1399 | | |
1400 | 8.45k | best_errorvals_for_pcount[partition_count - 1] = astc::min(best_errorvals_for_pcount[partition_count - 1], errorval); |
1401 | | |
1402 | | // If using N partitions doesn't improve much over using N-1 partitions then skip trying |
1403 | | // N+1. Error can dramatically improve if the data is correlated or non-correlated and |
1404 | | // aligns with a partitioning that suits that encoding, so for this inner loop check add |
1405 | | // a large error scale because the "other" trial could be a lot better. |
1406 | 8.45k | float best_error = best_errorvals_for_pcount[partition_count - 1]; |
1407 | 8.45k | float best_error_scale = exit_thresholds_for_pcount[partition_count - 1] * 1.85f; |
1408 | 8.45k | if (best_error > (best_error_in_prev * best_error_scale)) |
1409 | 1.54k | { |
1410 | 1.54k | trace_add_data("skip", "tune_partition_early_out_limit_factor"); |
1411 | 1.54k | goto END_OF_TESTS; |
1412 | 1.54k | } |
1413 | | |
1414 | 6.91k | if (errorval < error_threshold) |
1415 | 21 | { |
1416 | 21 | trace_add_data("exit", "quality hit"); |
1417 | 21 | goto END_OF_TESTS; |
1418 | 21 | } |
1419 | 6.91k | } |
1420 | | |
1421 | | // If using N partitions doesn't improve much over using N-1 partitions then skip trying N+1 |
1422 | 3.44k | float best_error = best_errorvals_for_pcount[partition_count - 1]; |
1423 | 3.44k | float best_error_scale = exit_thresholds_for_pcount[partition_count - 1]; |
1424 | 3.44k | if (best_error > (best_error_in_prev * best_error_scale)) |
1425 | 216 | { |
1426 | 216 | trace_add_data("skip", "tune_partition_early_out_limit_factor"); |
1427 | 216 | goto END_OF_TESTS; |
1428 | 216 | } |
1429 | 3.44k | } |
1430 | | |
1431 | 402 | trace_add_data("exit", "quality not hit"); |
1432 | | |
1433 | 2.22k | END_OF_TESTS: |
1434 | | // If we still have an error block then convert to something we can encode |
1435 | | // TODO: Do something more sensible here, such as average color block |
1436 | 2.22k | if (scb.block_type == SYM_BTYPE_ERROR) |
1437 | 124 | { |
1438 | | #if defined(ASTCENC_DIAGNOSTICS) |
1439 | | static bool printed_once = false; |
1440 | | if (!printed_once) |
1441 | | { |
1442 | | printed_once = true; |
1443 | | printf("WARN: At least one block failed to find a valid encoding.\n" |
1444 | | " Try increasing compression quality settings.\n\n"); |
1445 | | } |
1446 | | #endif |
1447 | | |
1448 | 124 | scb.block_type = SYM_BTYPE_CONST_U16; |
1449 | 124 | vfloat4 color_f32 = clamp(0.0f, 1.0f, blk.origin_texel) * 65535.0f; |
1450 | 124 | vint4 color_u16 = float_to_int_rtn(color_f32); |
1451 | 124 | store(color_u16, scb.constant_color); |
1452 | 124 | } |
1453 | | |
1454 | | // Compress to a physical block |
1455 | 2.22k | symbolic_to_physical(bsd, scb, pcb); |
1456 | 2.22k | } |
1457 | | |
1458 | | #endif |