/src/astc-encoder/Source/astcenc_internal.h
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // ---------------------------------------------------------------------------- |
3 | | // Copyright 2011-2023 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 and data declarations. |
20 | | */ |
21 | | |
22 | | #ifndef ASTCENC_INTERNAL_INCLUDED |
23 | | #define ASTCENC_INTERNAL_INCLUDED |
24 | | |
25 | | #include <algorithm> |
26 | | #include <cstddef> |
27 | | #include <cstdint> |
28 | | #if defined(ASTCENC_DIAGNOSTICS) |
29 | | #include <cstdio> |
30 | | #endif |
31 | | #include <cstdlib> |
32 | | #include <limits> |
33 | | |
34 | | #include "astcenc.h" |
35 | | #include "astcenc_mathlib.h" |
36 | | #include "astcenc_vecmathlib.h" |
37 | | |
38 | | /** |
39 | | * @brief Make a promise to the compiler's optimizer. |
40 | | * |
41 | | * A promise is an expression that the optimizer is can assume is true for to help it generate |
42 | | * faster code. Common use cases for this are to promise that a for loop will iterate more than |
43 | | * once, or that the loop iteration count is a multiple of a vector length, which avoids pre-loop |
44 | | * checks and can avoid loop tails if loops are unrolled by the auto-vectorizer. |
45 | | */ |
46 | | #if defined(NDEBUG) |
47 | | #if !defined(__clang__) && defined(_MSC_VER) |
48 | | #define promise(cond) __assume(cond) |
49 | | #elif defined(__clang__) |
50 | | #if __has_builtin(__builtin_assume) |
51 | | #define promise(cond) __builtin_assume(cond) |
52 | | #elif __has_builtin(__builtin_unreachable) |
53 | | #define promise(cond) if (!(cond)) { __builtin_unreachable(); } |
54 | | #else |
55 | | #define promise(cond) |
56 | | #endif |
57 | | #else // Assume GCC |
58 | | #define promise(cond) if (!(cond)) { __builtin_unreachable(); } |
59 | | #endif |
60 | | #else |
61 | 756 | #define promise(cond) assert(cond) |
62 | | #endif |
63 | | |
64 | | /* ============================================================================ |
65 | | Constants |
66 | | ============================================================================ */ |
67 | | #if !defined(ASTCENC_BLOCK_MAX_TEXELS) |
68 | 8.15M | #define ASTCENC_BLOCK_MAX_TEXELS 216 // A 3D 6x6x6 block |
69 | | #endif |
70 | | |
71 | | /** @brief The maximum number of texels a block can support (6x6x6 block). */ |
72 | | static constexpr unsigned int BLOCK_MAX_TEXELS { ASTCENC_BLOCK_MAX_TEXELS }; |
73 | | |
74 | | /** @brief The maximum number of components a block can support. */ |
75 | | static constexpr unsigned int BLOCK_MAX_COMPONENTS { 4 }; |
76 | | |
77 | | /** @brief The maximum number of partitions a block can support. */ |
78 | | static constexpr unsigned int BLOCK_MAX_PARTITIONS { 4 }; |
79 | | |
80 | | /** @brief The number of partitionings, per partition count, suported by the ASTC format. */ |
81 | | static constexpr unsigned int BLOCK_MAX_PARTITIONINGS { 1024 }; |
82 | | |
83 | | /** @brief The maximum number of texels used during partition selection for texel clustering. */ |
84 | | static constexpr uint8_t BLOCK_MAX_KMEANS_TEXELS { 64 }; |
85 | | |
86 | | /** @brief The maximum number of weights a block can support. */ |
87 | | static constexpr unsigned int BLOCK_MAX_WEIGHTS { 64 }; |
88 | | |
89 | | /** @brief The maximum number of weights a block can support per plane in 2 plane mode. */ |
90 | | static constexpr unsigned int BLOCK_MAX_WEIGHTS_2PLANE { BLOCK_MAX_WEIGHTS / 2 }; |
91 | | |
92 | | /** @brief The minimum number of weight bits a candidate encoding must encode. */ |
93 | | static constexpr unsigned int BLOCK_MIN_WEIGHT_BITS { 24 }; |
94 | | |
95 | | /** @brief The maximum number of weight bits a candidate encoding can encode. */ |
96 | | static constexpr unsigned int BLOCK_MAX_WEIGHT_BITS { 96 }; |
97 | | |
98 | | /** @brief The index indicating a bad (unused) block mode in the remap array. */ |
99 | | static constexpr uint16_t BLOCK_BAD_BLOCK_MODE { 0xFFFFu }; |
100 | | |
101 | | /** @brief The index indicating a bad (unused) partitioning in the remap array. */ |
102 | | static constexpr uint16_t BLOCK_BAD_PARTITIONING { 0xFFFFu }; |
103 | | |
104 | | /** @brief The number of partition index bits supported by the ASTC format . */ |
105 | | static constexpr unsigned int PARTITION_INDEX_BITS { 10 }; |
106 | | |
107 | | /** @brief The offset of the plane 2 weights in shared weight arrays. */ |
108 | | static constexpr unsigned int WEIGHTS_PLANE2_OFFSET { BLOCK_MAX_WEIGHTS_2PLANE }; |
109 | | |
110 | | /** @brief The sum of quantized weights for one texel. */ |
111 | | static constexpr float WEIGHTS_TEXEL_SUM { 16.0f }; |
112 | | |
113 | | /** @brief The number of block modes supported by the ASTC format. */ |
114 | | static constexpr unsigned int WEIGHTS_MAX_BLOCK_MODES { 2048 }; |
115 | | |
116 | | /** @brief The number of weight grid decimation modes supported by the ASTC format. */ |
117 | | static constexpr unsigned int WEIGHTS_MAX_DECIMATION_MODES { 87 }; |
118 | | |
119 | | /** @brief The high default error used to initialize error trackers. */ |
120 | | static constexpr float ERROR_CALC_DEFAULT { 1e30f }; |
121 | | |
122 | | /** |
123 | | * @brief The minimum tuning setting threshold for the one partition fast path. |
124 | | */ |
125 | | static constexpr float TUNE_MIN_SEARCH_MODE0 { 0.85f }; |
126 | | |
127 | | /** |
128 | | * @brief The maximum number of candidate encodings tested for each encoding mode. |
129 | | * |
130 | | * This can be dynamically reduced by the compression quality preset. |
131 | | */ |
132 | | static constexpr unsigned int TUNE_MAX_TRIAL_CANDIDATES { 8 }; |
133 | | |
134 | | /** |
135 | | * @brief The maximum number of candidate partitionings tested for each encoding mode. |
136 | | * |
137 | | * This can be dynamically reduced by the compression quality preset. |
138 | | */ |
139 | | static constexpr unsigned int TUNE_MAX_PARTITIONING_CANDIDATES { 8 }; |
140 | | |
141 | | /** |
142 | | * @brief The maximum quant level using full angular endpoint search method. |
143 | | * |
144 | | * The angular endpoint search is used to find the min/max weight that should |
145 | | * be used for a given quantization level. It is effective but expensive, so |
146 | | * we only use it where it has the most value - low quant levels with wide |
147 | | * spacing. It is used below TUNE_MAX_ANGULAR_QUANT (inclusive). Above this we |
148 | | * assume the min weight is 0.0f, and the max weight is 1.0f. |
149 | | * |
150 | | * Note the angular algorithm is vectorized, and using QUANT_12 exactly fills |
151 | | * one 8-wide vector. Decreasing by one doesn't buy much performance, and |
152 | | * increasing by one is disproportionately expensive. |
153 | | */ |
154 | | static constexpr unsigned int TUNE_MAX_ANGULAR_QUANT { 7 }; /* QUANT_12 */ |
155 | | |
156 | | static_assert((BLOCK_MAX_TEXELS % ASTCENC_SIMD_WIDTH) == 0, |
157 | | "BLOCK_MAX_TEXELS must be multiple of ASTCENC_SIMD_WIDTH"); |
158 | | |
159 | | static_assert(BLOCK_MAX_TEXELS <= 216, |
160 | | "BLOCK_MAX_TEXELS must not be greater than 216"); |
161 | | |
162 | | static_assert((BLOCK_MAX_WEIGHTS % ASTCENC_SIMD_WIDTH) == 0, |
163 | | "BLOCK_MAX_WEIGHTS must be multiple of ASTCENC_SIMD_WIDTH"); |
164 | | |
165 | | static_assert((WEIGHTS_MAX_BLOCK_MODES % ASTCENC_SIMD_WIDTH) == 0, |
166 | | "WEIGHTS_MAX_BLOCK_MODES must be multiple of ASTCENC_SIMD_WIDTH"); |
167 | | |
168 | | |
169 | | /* ============================================================================ |
170 | | Commonly used data structures |
171 | | ============================================================================ */ |
172 | | |
173 | | /** |
174 | | * @brief The ASTC endpoint formats. |
175 | | * |
176 | | * Note, the values here are used directly in the encoding in the format so do not rearrange. |
177 | | */ |
178 | | enum endpoint_formats |
179 | | { |
180 | | FMT_LUMINANCE = 0, |
181 | | FMT_LUMINANCE_DELTA = 1, |
182 | | FMT_HDR_LUMINANCE_LARGE_RANGE = 2, |
183 | | FMT_HDR_LUMINANCE_SMALL_RANGE = 3, |
184 | | FMT_LUMINANCE_ALPHA = 4, |
185 | | FMT_LUMINANCE_ALPHA_DELTA = 5, |
186 | | FMT_RGB_SCALE = 6, |
187 | | FMT_HDR_RGB_SCALE = 7, |
188 | | FMT_RGB = 8, |
189 | | FMT_RGB_DELTA = 9, |
190 | | FMT_RGB_SCALE_ALPHA = 10, |
191 | | FMT_HDR_RGB = 11, |
192 | | FMT_RGBA = 12, |
193 | | FMT_RGBA_DELTA = 13, |
194 | | FMT_HDR_RGB_LDR_ALPHA = 14, |
195 | | FMT_HDR_RGBA = 15 |
196 | | }; |
197 | | |
198 | | /** |
199 | | * @brief The ASTC quantization methods. |
200 | | * |
201 | | * Note, the values here are used directly in the encoding in the format so do not rearrange. |
202 | | */ |
203 | | enum quant_method |
204 | | { |
205 | | QUANT_2 = 0, |
206 | | QUANT_3 = 1, |
207 | | QUANT_4 = 2, |
208 | | QUANT_5 = 3, |
209 | | QUANT_6 = 4, |
210 | | QUANT_8 = 5, |
211 | | QUANT_10 = 6, |
212 | | QUANT_12 = 7, |
213 | | QUANT_16 = 8, |
214 | | QUANT_20 = 9, |
215 | | QUANT_24 = 10, |
216 | | QUANT_32 = 11, |
217 | | QUANT_40 = 12, |
218 | | QUANT_48 = 13, |
219 | | QUANT_64 = 14, |
220 | | QUANT_80 = 15, |
221 | | QUANT_96 = 16, |
222 | | QUANT_128 = 17, |
223 | | QUANT_160 = 18, |
224 | | QUANT_192 = 19, |
225 | | QUANT_256 = 20 |
226 | | }; |
227 | | |
228 | | /** |
229 | | * @brief The number of levels use by an ASTC quantization method. |
230 | | * |
231 | | * @param method The quantization method |
232 | | * |
233 | | * @return The number of levels used by @c method. |
234 | | */ |
235 | | static inline unsigned int get_quant_level(quant_method method) |
236 | 0 | { |
237 | 0 | switch (method) |
238 | 0 | { |
239 | 0 | case QUANT_2: return 2; |
240 | 0 | case QUANT_3: return 3; |
241 | 0 | case QUANT_4: return 4; |
242 | 0 | case QUANT_5: return 5; |
243 | 0 | case QUANT_6: return 6; |
244 | 0 | case QUANT_8: return 8; |
245 | 0 | case QUANT_10: return 10; |
246 | 0 | case QUANT_12: return 12; |
247 | 0 | case QUANT_16: return 16; |
248 | 0 | case QUANT_20: return 20; |
249 | 0 | case QUANT_24: return 24; |
250 | 0 | case QUANT_32: return 32; |
251 | 0 | case QUANT_40: return 40; |
252 | 0 | case QUANT_48: return 48; |
253 | 0 | case QUANT_64: return 64; |
254 | 0 | case QUANT_80: return 80; |
255 | 0 | case QUANT_96: return 96; |
256 | 0 | case QUANT_128: return 128; |
257 | 0 | case QUANT_160: return 160; |
258 | 0 | case QUANT_192: return 192; |
259 | 0 | case QUANT_256: return 256; |
260 | 0 | } |
261 | | |
262 | | // Unreachable - the enum is fully described |
263 | 0 | return 0; |
264 | 0 | } Unexecuted instantiation: fuzz_astc_physical_to_symbolic.cpp:get_quant_level(quant_method) Unexecuted instantiation: astcenc_block_sizes.cpp:get_quant_level(quant_method) Unexecuted instantiation: astcenc_integer_sequence.cpp:get_quant_level(quant_method) Unexecuted instantiation: astcenc_partition_tables.cpp:get_quant_level(quant_method) Unexecuted instantiation: astcenc_percentile_tables.cpp:get_quant_level(quant_method) Unexecuted instantiation: astcenc_symbolic_physical.cpp:get_quant_level(quant_method) Unexecuted instantiation: astcenc_weight_quant_xfer_tables.cpp:get_quant_level(quant_method) Unexecuted instantiation: astcenc_quantization.cpp:get_quant_level(quant_method) |
265 | | |
266 | | /** |
267 | | * @brief Computed metrics about a partition in a block. |
268 | | */ |
269 | | struct partition_metrics |
270 | | { |
271 | | /** @brief The error-weighted average color in the partition. */ |
272 | | vfloat4 avg; |
273 | | |
274 | | /** @brief The dominant error-weighted direction in the partition. */ |
275 | | vfloat4 dir; |
276 | | }; |
277 | | |
278 | | /** |
279 | | * @brief Computed lines for a a three component analysis. |
280 | | */ |
281 | | struct partition_lines3 |
282 | | { |
283 | | /** @brief Line for uncorrelated chroma. */ |
284 | | line3 uncor_line; |
285 | | |
286 | | /** @brief Line for correlated chroma, passing though the origin. */ |
287 | | line3 samec_line; |
288 | | |
289 | | /** @brief Post-processed line for uncorrelated chroma. */ |
290 | | processed_line3 uncor_pline; |
291 | | |
292 | | /** @brief Post-processed line for correlated chroma, passing though the origin. */ |
293 | | processed_line3 samec_pline; |
294 | | |
295 | | /** |
296 | | * @brief The length of the line for uncorrelated chroma. |
297 | | * |
298 | | * This is used for both the uncorrelated and same chroma lines - they are normally very similar |
299 | | * and only used for the relative ranking of partitionings against one another. |
300 | | */ |
301 | | float line_length; |
302 | | }; |
303 | | |
304 | | /** |
305 | | * @brief The partition information for a single partition. |
306 | | * |
307 | | * ASTC has a total of 1024 candidate partitions for each of 2/3/4 partition counts, although this |
308 | | * 1024 includes seeds that generate duplicates of other seeds and seeds that generate completely |
309 | | * empty partitions. These are both valid encodings, but astcenc will skip both during compression |
310 | | * as they are not useful. |
311 | | */ |
312 | | struct partition_info |
313 | | { |
314 | | /** @brief The number of partitions in this partitioning. */ |
315 | | uint16_t partition_count; |
316 | | |
317 | | /** @brief The index (seed) of this partitioning. */ |
318 | | uint16_t partition_index; |
319 | | |
320 | | /** |
321 | | * @brief The number of texels in each partition. |
322 | | * |
323 | | * Note that some seeds result in zero texels assigned to a partition. These are valid, but are |
324 | | * skipped by this compressor as there is no point spending bits encoding an unused endpoints. |
325 | | */ |
326 | | uint8_t partition_texel_count[BLOCK_MAX_PARTITIONS]; |
327 | | |
328 | | /** @brief The partition of each texel in the block. */ |
329 | | uint8_t partition_of_texel[BLOCK_MAX_TEXELS]; |
330 | | |
331 | | /** @brief The list of texels in each partition. */ |
332 | | uint8_t texels_of_partition[BLOCK_MAX_PARTITIONS][BLOCK_MAX_TEXELS]; |
333 | | }; |
334 | | |
335 | | /** |
336 | | * @brief The weight grid information for a single decimation pattern. |
337 | | * |
338 | | * ASTC can store one weight per texel, but is also capable of storing lower resolution weight grids |
339 | | * that are interpolated during decompression to assign a with to a texel. Storing fewer weights |
340 | | * can free up a substantial amount of bits that we can then spend on more useful things, such as |
341 | | * more accurate endpoints and weights, or additional partitions. |
342 | | * |
343 | | * This data structure is used to store information about a single weight grid decimation pattern, |
344 | | * for a single block size. |
345 | | */ |
346 | | struct decimation_info |
347 | | { |
348 | | /** @brief The total number of texels in the block. */ |
349 | | uint8_t texel_count; |
350 | | |
351 | | /** @brief The maximum number of stored weights that contribute to each texel, between 1 and 4. */ |
352 | | uint8_t max_texel_weight_count; |
353 | | |
354 | | /** @brief The total number of weights stored. */ |
355 | | uint8_t weight_count; |
356 | | |
357 | | /** @brief The number of stored weights in the X dimension. */ |
358 | | uint8_t weight_x; |
359 | | |
360 | | /** @brief The number of stored weights in the Y dimension. */ |
361 | | uint8_t weight_y; |
362 | | |
363 | | /** @brief The number of stored weights in the Z dimension. */ |
364 | | uint8_t weight_z; |
365 | | |
366 | | /** |
367 | | * @brief The number of weights that contribute to each texel. |
368 | | * Value is between 1 and 4. |
369 | | */ |
370 | | uint8_t texel_weight_count[BLOCK_MAX_TEXELS]; |
371 | | |
372 | | /** |
373 | | * @brief The weight index of the N weights that are interpolated for each texel. |
374 | | * Stored transposed to improve vectorization. |
375 | | */ |
376 | | uint8_t texel_weights_tr[4][BLOCK_MAX_TEXELS]; |
377 | | |
378 | | /** |
379 | | * @brief The bilinear contribution of the N weights that are interpolated for each texel. |
380 | | * Value is between 0 and 16, stored transposed to improve vectorization. |
381 | | */ |
382 | | uint8_t texel_weight_contribs_int_tr[4][BLOCK_MAX_TEXELS]; |
383 | | |
384 | | /** |
385 | | * @brief The bilinear contribution of the N weights that are interpolated for each texel. |
386 | | * Value is between 0 and 1, stored transposed to improve vectorization. |
387 | | */ |
388 | | alignas(ASTCENC_VECALIGN) float texel_weight_contribs_float_tr[4][BLOCK_MAX_TEXELS]; |
389 | | |
390 | | /** @brief The number of texels that each stored weight contributes to. */ |
391 | | uint8_t weight_texel_count[BLOCK_MAX_WEIGHTS]; |
392 | | |
393 | | /** |
394 | | * @brief The list of texels that use a specific weight index. |
395 | | * Stored transposed to improve vectorization. |
396 | | */ |
397 | | uint8_t weight_texels_tr[BLOCK_MAX_TEXELS][BLOCK_MAX_WEIGHTS]; |
398 | | |
399 | | /** |
400 | | * @brief The bilinear contribution to the N texels that use each weight. |
401 | | * Value is between 0 and 1, stored transposed to improve vectorization. |
402 | | */ |
403 | | alignas(ASTCENC_VECALIGN) float weights_texel_contribs_tr[BLOCK_MAX_TEXELS][BLOCK_MAX_WEIGHTS]; |
404 | | |
405 | | /** |
406 | | * @brief The bilinear contribution to the Nth texel that uses each weight. |
407 | | * Value is between 0 and 1, stored transposed to improve vectorization. |
408 | | */ |
409 | | float texel_contrib_for_weight[BLOCK_MAX_TEXELS][BLOCK_MAX_WEIGHTS]; |
410 | | }; |
411 | | |
412 | | /** |
413 | | * @brief Metadata for single block mode for a specific block size. |
414 | | */ |
415 | | struct block_mode |
416 | | { |
417 | | /** @brief The block mode index in the ASTC encoded form. */ |
418 | | uint16_t mode_index; |
419 | | |
420 | | /** @brief The decimation mode index in the compressor reindexed list. */ |
421 | | uint8_t decimation_mode; |
422 | | |
423 | | /** @brief The weight quantization used by this block mode. */ |
424 | | uint8_t quant_mode; |
425 | | |
426 | | /** @brief The weight quantization used by this block mode. */ |
427 | | uint8_t weight_bits; |
428 | | |
429 | | /** @brief Is a dual weight plane used by this block mode? */ |
430 | | uint8_t is_dual_plane : 1; |
431 | | |
432 | | /** |
433 | | * @brief Get the weight quantization used by this block mode. |
434 | | * |
435 | | * @return The quantization level. |
436 | | */ |
437 | | inline quant_method get_weight_quant_mode() const |
438 | 920 | { |
439 | 920 | return static_cast<quant_method>(this->quant_mode); |
440 | 920 | } |
441 | | }; |
442 | | |
443 | | /** |
444 | | * @brief Metadata for single decimation mode for a specific block size. |
445 | | */ |
446 | | struct decimation_mode |
447 | | { |
448 | | /** @brief The max weight precision for 1 plane, or -1 if not supported. */ |
449 | | int8_t maxprec_1plane; |
450 | | |
451 | | /** @brief The max weight precision for 2 planes, or -1 if not supported. */ |
452 | | int8_t maxprec_2planes; |
453 | | |
454 | | /** |
455 | | * @brief Bitvector indicating weight quant modes used by active 1 plane block modes. |
456 | | * |
457 | | * Bit 0 = QUANT_2, Bit 1 = QUANT_3, etc. |
458 | | */ |
459 | | uint16_t refprec_1plane; |
460 | | |
461 | | /** |
462 | | * @brief Bitvector indicating weight quant methods used by active 2 plane block modes. |
463 | | * |
464 | | * Bit 0 = QUANT_2, Bit 1 = QUANT_3, etc. |
465 | | */ |
466 | | uint16_t refprec_2planes; |
467 | | |
468 | | /** |
469 | | * @brief Set a 1 plane weight quant as active. |
470 | | * |
471 | | * @param weight_quant The quant method to set. |
472 | | */ |
473 | | void set_ref_1plane(quant_method weight_quant) |
474 | 590 | { |
475 | 590 | refprec_1plane |= (1 << weight_quant); |
476 | 590 | } |
477 | | |
478 | | /** |
479 | | * @brief Test if this mode is active below a given 1 plane weight quant (inclusive). |
480 | | * |
481 | | * @param max_weight_quant The max quant method to test. |
482 | | */ |
483 | | bool is_ref_1plane(quant_method max_weight_quant) const |
484 | 0 | { |
485 | 0 | uint16_t mask = static_cast<uint16_t>((1 << (max_weight_quant + 1)) - 1); |
486 | 0 | return (refprec_1plane & mask) != 0; |
487 | 0 | } |
488 | | |
489 | | /** |
490 | | * @brief Set a 2 plane weight quant as active. |
491 | | * |
492 | | * @param weight_quant The quant method to set. |
493 | | */ |
494 | | void set_ref_2plane(quant_method weight_quant) |
495 | 330 | { |
496 | 330 | refprec_2planes |= static_cast<uint16_t>(1 << weight_quant); |
497 | 330 | } |
498 | | |
499 | | /** |
500 | | * @brief Test if this mode is active below a given 2 plane weight quant (inclusive). |
501 | | * |
502 | | * @param max_weight_quant The max quant method to test. |
503 | | */ |
504 | | bool is_ref_2plane(quant_method max_weight_quant) const |
505 | 0 | { |
506 | 0 | uint16_t mask = static_cast<uint16_t>((1 << (max_weight_quant + 1)) - 1); |
507 | 0 | return (refprec_2planes & mask) != 0; |
508 | 0 | } |
509 | | }; |
510 | | |
511 | | /** |
512 | | * @brief Data tables for a single block size. |
513 | | * |
514 | | * The decimation tables store the information to apply weight grid dimension reductions. We only |
515 | | * store the decimation modes that are actually needed by the current context; many of the possible |
516 | | * modes will be unused (too many weights for the current block size or disabled by heuristics). The |
517 | | * actual number of weights stored is @c decimation_mode_count, and the @c decimation_modes and |
518 | | * @c decimation_tables arrays store the active modes contiguously at the start of the array. These |
519 | | * entries are not stored in any particular order. |
520 | | * |
521 | | * The block mode tables store the unpacked block mode settings. Block modes are stored in the |
522 | | * compressed block as an 11 bit field, but for any given block size and set of compressor |
523 | | * heuristics, only a subset of the block modes will be used. The actual number of block modes |
524 | | * stored is indicated in @c block_mode_count, and the @c block_modes array store the active modes |
525 | | * contiguously at the start of the array. These entries are stored in incrementing "packed" value |
526 | | * order, which doesn't mean much once unpacked. To allow decompressors to reference the packed data |
527 | | * efficiently the @c block_mode_packed_index array stores the mapping between physical ID and the |
528 | | * actual remapped array index. |
529 | | */ |
530 | | struct block_size_descriptor |
531 | | { |
532 | | /** @brief The block X dimension, in texels. */ |
533 | | uint8_t xdim; |
534 | | |
535 | | /** @brief The block Y dimension, in texels. */ |
536 | | uint8_t ydim; |
537 | | |
538 | | /** @brief The block Z dimension, in texels. */ |
539 | | uint8_t zdim; |
540 | | |
541 | | /** @brief The block total texel count. */ |
542 | | uint8_t texel_count; |
543 | | |
544 | | /** |
545 | | * @brief The number of stored decimation modes which are "always" modes. |
546 | | * |
547 | | * Always modes are stored at the start of the decimation_modes list. |
548 | | */ |
549 | | unsigned int decimation_mode_count_always; |
550 | | |
551 | | /** @brief The number of stored decimation modes for selected encodings. */ |
552 | | unsigned int decimation_mode_count_selected; |
553 | | |
554 | | /** @brief The number of stored decimation modes for any encoding. */ |
555 | | unsigned int decimation_mode_count_all; |
556 | | |
557 | | /** |
558 | | * @brief The number of stored block modes which are "always" modes. |
559 | | * |
560 | | * Always modes are stored at the start of the block_modes list. |
561 | | */ |
562 | | unsigned int block_mode_count_1plane_always; |
563 | | |
564 | | /** @brief The number of stored block modes for active 1 plane encodings. */ |
565 | | unsigned int block_mode_count_1plane_selected; |
566 | | |
567 | | /** @brief The number of stored block modes for active 1 and 2 plane encodings. */ |
568 | | unsigned int block_mode_count_1plane_2plane_selected; |
569 | | |
570 | | /** @brief The number of stored block modes for any encoding. */ |
571 | | unsigned int block_mode_count_all; |
572 | | |
573 | | /** @brief The number of selected partitionings for 1/2/3/4 partitionings. */ |
574 | | unsigned int partitioning_count_selected[BLOCK_MAX_PARTITIONS]; |
575 | | |
576 | | /** @brief The number of partitionings for 1/2/3/4 partitionings. */ |
577 | | unsigned int partitioning_count_all[BLOCK_MAX_PARTITIONS]; |
578 | | |
579 | | /** @brief The active decimation modes, stored in low indices. */ |
580 | | decimation_mode decimation_modes[WEIGHTS_MAX_DECIMATION_MODES]; |
581 | | |
582 | | /** @brief The active decimation tables, stored in low indices. */ |
583 | | alignas(ASTCENC_VECALIGN) decimation_info decimation_tables[WEIGHTS_MAX_DECIMATION_MODES]; |
584 | | |
585 | | /** @brief The packed block mode array index, or @c BLOCK_BAD_BLOCK_MODE if not active. */ |
586 | | uint16_t block_mode_packed_index[WEIGHTS_MAX_BLOCK_MODES]; |
587 | | |
588 | | /** @brief The active block modes, stored in low indices. */ |
589 | | block_mode block_modes[WEIGHTS_MAX_BLOCK_MODES]; |
590 | | |
591 | | /** @brief The active partition tables, stored in low indices per-count. */ |
592 | | partition_info partitionings[(3 * BLOCK_MAX_PARTITIONINGS) + 1]; |
593 | | |
594 | | /** |
595 | | * @brief The packed partition table array index, or @c BLOCK_BAD_PARTITIONING if not active. |
596 | | * |
597 | | * Indexed by partition_count - 2, containing 2, 3 and 4 partitions. |
598 | | */ |
599 | | uint16_t partitioning_packed_index[3][BLOCK_MAX_PARTITIONINGS]; |
600 | | |
601 | | /** @brief The active texels for k-means partition selection. */ |
602 | | uint8_t kmeans_texels[BLOCK_MAX_KMEANS_TEXELS]; |
603 | | |
604 | | /** |
605 | | * @brief The canonical 2-partition coverage pattern used during block partition search. |
606 | | * |
607 | | * Indexed by remapped index, not physical index. |
608 | | */ |
609 | | uint64_t coverage_bitmaps_2[BLOCK_MAX_PARTITIONINGS][2]; |
610 | | |
611 | | /** |
612 | | * @brief The canonical 3-partition coverage pattern used during block partition search. |
613 | | * |
614 | | * Indexed by remapped index, not physical index. |
615 | | */ |
616 | | uint64_t coverage_bitmaps_3[BLOCK_MAX_PARTITIONINGS][3]; |
617 | | |
618 | | /** |
619 | | * @brief The canonical 4-partition coverage pattern used during block partition search. |
620 | | * |
621 | | * Indexed by remapped index, not physical index. |
622 | | */ |
623 | | uint64_t coverage_bitmaps_4[BLOCK_MAX_PARTITIONINGS][4]; |
624 | | |
625 | | /** |
626 | | * @brief Get the block mode structure for index @c block_mode. |
627 | | * |
628 | | * This function can only return block modes that are enabled by the current compressor config. |
629 | | * Decompression from an arbitrary source should not use this without first checking that the |
630 | | * packed block mode index is not @c BLOCK_BAD_BLOCK_MODE. |
631 | | * |
632 | | * @param block_mode The packed block mode index. |
633 | | * |
634 | | * @return The block mode structure. |
635 | | */ |
636 | | const block_mode& get_block_mode(unsigned int block_mode) const |
637 | 48 | { |
638 | 48 | unsigned int packed_index = this->block_mode_packed_index[block_mode]; |
639 | 48 | assert(packed_index != BLOCK_BAD_BLOCK_MODE && packed_index < this->block_mode_count_all); |
640 | 0 | return this->block_modes[packed_index]; |
641 | 48 | } |
642 | | |
643 | | /** |
644 | | * @brief Get the decimation mode structure for index @c decimation_mode. |
645 | | * |
646 | | * This function can only return decimation modes that are enabled by the current compressor |
647 | | * config. The mode array is stored packed, but this is only ever indexed by the packed index |
648 | | * stored in the @c block_mode and never exists in an unpacked form. |
649 | | * |
650 | | * @param decimation_mode The packed decimation mode index. |
651 | | * |
652 | | * @return The decimation mode structure. |
653 | | */ |
654 | | const decimation_mode& get_decimation_mode(unsigned int decimation_mode) const |
655 | 0 | { |
656 | 0 | return this->decimation_modes[decimation_mode]; |
657 | 0 | } |
658 | | |
659 | | /** |
660 | | * @brief Get the decimation info structure for index @c decimation_mode. |
661 | | * |
662 | | * This function can only return decimation modes that are enabled by the current compressor |
663 | | * config. The mode array is stored packed, but this is only ever indexed by the packed index |
664 | | * stored in the @c block_mode and never exists in an unpacked form. |
665 | | * |
666 | | * @param decimation_mode The packed decimation mode index. |
667 | | * |
668 | | * @return The decimation info structure. |
669 | | */ |
670 | | const decimation_info& get_decimation_info(unsigned int decimation_mode) const |
671 | 48 | { |
672 | 48 | return this->decimation_tables[decimation_mode]; |
673 | 48 | } |
674 | | |
675 | | /** |
676 | | * @brief Get the partition info table for a given partition count. |
677 | | * |
678 | | * @param partition_count The number of partitions we want the table for. |
679 | | * |
680 | | * @return The pointer to the table of 1024 entries (for 2/3/4 parts) or 1 entry (for 1 part). |
681 | | */ |
682 | | const partition_info* get_partition_table(unsigned int partition_count) const |
683 | 0 | { |
684 | 0 | if (partition_count == 1) |
685 | 0 | { |
686 | 0 | partition_count = 5; |
687 | 0 | } |
688 | 0 | unsigned int index = (partition_count - 2) * BLOCK_MAX_PARTITIONINGS; |
689 | 0 | return this->partitionings + index; |
690 | 0 | } |
691 | | |
692 | | /** |
693 | | * @brief Get the partition info structure for a given partition count and seed. |
694 | | * |
695 | | * @param partition_count The number of partitions we want the info for. |
696 | | * @param index The partition seed (between 0 and 1023). |
697 | | * |
698 | | * @return The partition info structure. |
699 | | */ |
700 | | const partition_info& get_partition_info(unsigned int partition_count, unsigned int index) const |
701 | 0 | { |
702 | 0 | unsigned int packed_index = 0; |
703 | 0 | if (partition_count >= 2) |
704 | 0 | { |
705 | 0 | packed_index = this->partitioning_packed_index[partition_count - 2][index]; |
706 | 0 | } |
707 | 0 |
|
708 | 0 | assert(packed_index != BLOCK_BAD_PARTITIONING && packed_index < this->partitioning_count_all[partition_count - 1]); |
709 | 0 | auto& result = get_partition_table(partition_count)[packed_index]; |
710 | 0 | assert(index == result.partition_index); |
711 | 0 | return result; |
712 | 0 | } |
713 | | |
714 | | /** |
715 | | * @brief Get the partition info structure for a given partition count and seed. |
716 | | * |
717 | | * @param partition_count The number of partitions we want the info for. |
718 | | * @param packed_index The raw array offset. |
719 | | * |
720 | | * @return The partition info structure. |
721 | | */ |
722 | | const partition_info& get_raw_partition_info(unsigned int partition_count, unsigned int packed_index) const |
723 | 0 | { |
724 | 0 | assert(packed_index != BLOCK_BAD_PARTITIONING && packed_index < this->partitioning_count_all[partition_count - 1]); |
725 | 0 | auto& result = get_partition_table(partition_count)[packed_index]; |
726 | 0 | return result; |
727 | 0 | } |
728 | | }; |
729 | | |
730 | | /** |
731 | | * @brief The image data for a single block. |
732 | | * |
733 | | * The @c data_[rgba] fields store the image data in an encoded SoA float form designed for easy |
734 | | * vectorization. Input data is converted to float and stored as values between 0 and 65535. LDR |
735 | | * data is stored as direct UNORM data, HDR data is stored as LNS data. |
736 | | * |
737 | | * The @c rgb_lns and @c alpha_lns fields that assigned a per-texel use of HDR are only used during |
738 | | * decompression. The current compressor will always use HDR endpoint formats when in HDR mode. |
739 | | */ |
740 | | struct image_block |
741 | | { |
742 | | /** @brief The input (compress) or output (decompress) data for the red color component. */ |
743 | | alignas(ASTCENC_VECALIGN) float data_r[BLOCK_MAX_TEXELS]; |
744 | | |
745 | | /** @brief The input (compress) or output (decompress) data for the green color component. */ |
746 | | alignas(ASTCENC_VECALIGN) float data_g[BLOCK_MAX_TEXELS]; |
747 | | |
748 | | /** @brief The input (compress) or output (decompress) data for the blue color component. */ |
749 | | alignas(ASTCENC_VECALIGN) float data_b[BLOCK_MAX_TEXELS]; |
750 | | |
751 | | /** @brief The input (compress) or output (decompress) data for the alpha color component. */ |
752 | | alignas(ASTCENC_VECALIGN) float data_a[BLOCK_MAX_TEXELS]; |
753 | | |
754 | | /** @brief The number of texels in the block. */ |
755 | | uint8_t texel_count; |
756 | | |
757 | | /** @brief The original data for texel 0 for constant color block encoding. */ |
758 | | vfloat4 origin_texel; |
759 | | |
760 | | /** @brief The min component value of all texels in the block. */ |
761 | | vfloat4 data_min; |
762 | | |
763 | | /** @brief The mean component value of all texels in the block. */ |
764 | | vfloat4 data_mean; |
765 | | |
766 | | /** @brief The max component value of all texels in the block. */ |
767 | | vfloat4 data_max; |
768 | | |
769 | | /** @brief The relative error significance of the color channels. */ |
770 | | vfloat4 channel_weight; |
771 | | |
772 | | /** @brief Is this grayscale block where R == G == B for all texels? */ |
773 | | bool grayscale; |
774 | | |
775 | | /** @brief Set to 1 if a texel is using HDR RGB endpoints (decompression only). */ |
776 | | uint8_t rgb_lns[BLOCK_MAX_TEXELS]; |
777 | | |
778 | | /** @brief Set to 1 if a texel is using HDR alpha endpoints (decompression only). */ |
779 | | uint8_t alpha_lns[BLOCK_MAX_TEXELS]; |
780 | | |
781 | | /** @brief The X position of this block in the input or output image. */ |
782 | | unsigned int xpos; |
783 | | |
784 | | /** @brief The Y position of this block in the input or output image. */ |
785 | | unsigned int ypos; |
786 | | |
787 | | /** @brief The Z position of this block in the input or output image. */ |
788 | | unsigned int zpos; |
789 | | |
790 | | /** |
791 | | * @brief Get an RGBA texel value from the data. |
792 | | * |
793 | | * @param index The texel index. |
794 | | * |
795 | | * @return The texel in RGBA component ordering. |
796 | | */ |
797 | | inline vfloat4 texel(unsigned int index) const |
798 | 0 | { |
799 | 0 | return vfloat4(data_r[index], |
800 | 0 | data_g[index], |
801 | 0 | data_b[index], |
802 | 0 | data_a[index]); |
803 | 0 | } |
804 | | |
805 | | /** |
806 | | * @brief Get an RGB texel value from the data. |
807 | | * |
808 | | * @param index The texel index. |
809 | | * |
810 | | * @return The texel in RGB0 component ordering. |
811 | | */ |
812 | | inline vfloat4 texel3(unsigned int index) const |
813 | 0 | { |
814 | 0 | return vfloat3(data_r[index], |
815 | 0 | data_g[index], |
816 | 0 | data_b[index]); |
817 | 0 | } |
818 | | |
819 | | /** |
820 | | * @brief Get the default alpha value for endpoints that don't store it. |
821 | | * |
822 | | * The default depends on whether the alpha endpoint is LDR or HDR. |
823 | | * |
824 | | * @return The alpha value in the scaled range used by the compressor. |
825 | | */ |
826 | | inline float get_default_alpha() const |
827 | 0 | { |
828 | 0 | return this->alpha_lns[0] ? static_cast<float>(0x7800) : static_cast<float>(0xFFFF); |
829 | 0 | } |
830 | | |
831 | | /** |
832 | | * @brief Test if a single color channel is constant across the block. |
833 | | * |
834 | | * Constant color channels are easier to compress as interpolating between two identical colors |
835 | | * always returns the same value, irrespective of the weight used. They therefore can be ignored |
836 | | * for the purposes of weight selection and use of a second weight plane. |
837 | | * |
838 | | * @return @c true if the channel is constant across the block, @c false otherwise. |
839 | | */ |
840 | | inline bool is_constant_channel(int channel) const |
841 | 0 | { |
842 | 0 | vmask4 lane_mask = vint4::lane_id() == vint4(channel); |
843 | 0 | vmask4 color_mask = this->data_min == this->data_max; |
844 | 0 | return any(lane_mask & color_mask); |
845 | 0 | } |
846 | | |
847 | | /** |
848 | | * @brief Test if this block is a luminance block with constant 1.0 alpha. |
849 | | * |
850 | | * @return @c true if the block is a luminance block , @c false otherwise. |
851 | | */ |
852 | | inline bool is_luminance() const |
853 | 0 | { |
854 | 0 | float default_alpha = this->get_default_alpha(); |
855 | 0 | bool alpha1 = (this->data_min.lane<3>() == default_alpha) && |
856 | 0 | (this->data_max.lane<3>() == default_alpha); |
857 | 0 | return this->grayscale && alpha1; |
858 | 0 | } |
859 | | |
860 | | /** |
861 | | * @brief Test if this block is a luminance block with variable alpha. |
862 | | * |
863 | | * @return @c true if the block is a luminance + alpha block , @c false otherwise. |
864 | | */ |
865 | | inline bool is_luminancealpha() const |
866 | 0 | { |
867 | 0 | float default_alpha = this->get_default_alpha(); |
868 | 0 | bool alpha1 = (this->data_min.lane<3>() == default_alpha) && |
869 | 0 | (this->data_max.lane<3>() == default_alpha); |
870 | 0 | return this->grayscale && !alpha1; |
871 | 0 | } |
872 | | }; |
873 | | |
874 | | /** |
875 | | * @brief Data structure storing the color endpoints for a block. |
876 | | */ |
877 | | struct endpoints |
878 | | { |
879 | | /** @brief The number of partition endpoints stored. */ |
880 | | unsigned int partition_count; |
881 | | |
882 | | /** @brief The colors for endpoint 0. */ |
883 | | vfloat4 endpt0[BLOCK_MAX_PARTITIONS]; |
884 | | |
885 | | /** @brief The colors for endpoint 1. */ |
886 | | vfloat4 endpt1[BLOCK_MAX_PARTITIONS]; |
887 | | }; |
888 | | |
889 | | /** |
890 | | * @brief Data structure storing the color endpoints and weights. |
891 | | */ |
892 | | struct endpoints_and_weights |
893 | | { |
894 | | /** @brief True if all active values in weight_error_scale are the same. */ |
895 | | bool is_constant_weight_error_scale; |
896 | | |
897 | | /** @brief The color endpoints. */ |
898 | | endpoints ep; |
899 | | |
900 | | /** @brief The ideal weight for each texel; may be undecimated or decimated. */ |
901 | | alignas(ASTCENC_VECALIGN) float weights[BLOCK_MAX_TEXELS]; |
902 | | |
903 | | /** @brief The ideal weight error scaling for each texel; may be undecimated or decimated. */ |
904 | | alignas(ASTCENC_VECALIGN) float weight_error_scale[BLOCK_MAX_TEXELS]; |
905 | | }; |
906 | | |
907 | | /** |
908 | | * @brief Utility storing estimated errors from choosing particular endpoint encodings. |
909 | | */ |
910 | | struct encoding_choice_errors |
911 | | { |
912 | | /** @brief Error of using LDR RGB-scale instead of complete endpoints. */ |
913 | | float rgb_scale_error; |
914 | | |
915 | | /** @brief Error of using HDR RGB-scale instead of complete endpoints. */ |
916 | | float rgb_luma_error; |
917 | | |
918 | | /** @brief Error of using luminance instead of RGB. */ |
919 | | float luminance_error; |
920 | | |
921 | | /** @brief Error of discarding alpha and using a constant 1.0 alpha. */ |
922 | | float alpha_drop_error; |
923 | | |
924 | | /** @brief Can we use delta offset encoding? */ |
925 | | bool can_offset_encode; |
926 | | |
927 | | /** @brief Can we use blue contraction encoding? */ |
928 | | bool can_blue_contract; |
929 | | }; |
930 | | |
931 | | /** |
932 | | * @brief Preallocated working buffers, allocated per thread during context creation. |
933 | | */ |
934 | | struct alignas(ASTCENC_VECALIGN) compression_working_buffers |
935 | | { |
936 | | /** @brief Ideal endpoints and weights for plane 1. */ |
937 | | endpoints_and_weights ei1; |
938 | | |
939 | | /** @brief Ideal endpoints and weights for plane 2. */ |
940 | | endpoints_and_weights ei2; |
941 | | |
942 | | /** |
943 | | * @brief Decimated ideal weight values in the ~0-1 range. |
944 | | * |
945 | | * Note that values can be slightly below zero or higher than one due to |
946 | | * endpoint extents being inside the ideal color representation. |
947 | | * |
948 | | * For two planes, second plane starts at @c WEIGHTS_PLANE2_OFFSET offsets. |
949 | | */ |
950 | | alignas(ASTCENC_VECALIGN) float dec_weights_ideal[WEIGHTS_MAX_DECIMATION_MODES * BLOCK_MAX_WEIGHTS]; |
951 | | |
952 | | /** |
953 | | * @brief Decimated quantized weight values in the unquantized 0-64 range. |
954 | | * |
955 | | * For two planes, second plane starts at @c WEIGHTS_PLANE2_OFFSET offsets. |
956 | | */ |
957 | | uint8_t dec_weights_uquant[WEIGHTS_MAX_BLOCK_MODES * BLOCK_MAX_WEIGHTS]; |
958 | | |
959 | | /** @brief Error of the best encoding combination for each block mode. */ |
960 | | alignas(ASTCENC_VECALIGN) float errors_of_best_combination[WEIGHTS_MAX_BLOCK_MODES]; |
961 | | |
962 | | /** @brief The best color quant for each block mode. */ |
963 | | uint8_t best_quant_levels[WEIGHTS_MAX_BLOCK_MODES]; |
964 | | |
965 | | /** @brief The best color quant for each block mode if modes are the same and we have spare bits. */ |
966 | | uint8_t best_quant_levels_mod[WEIGHTS_MAX_BLOCK_MODES]; |
967 | | |
968 | | /** @brief The best endpoint format for each partition. */ |
969 | | uint8_t best_ep_formats[WEIGHTS_MAX_BLOCK_MODES][BLOCK_MAX_PARTITIONS]; |
970 | | |
971 | | /** @brief The total bit storage needed for quantized weights for each block mode. */ |
972 | | int8_t qwt_bitcounts[WEIGHTS_MAX_BLOCK_MODES]; |
973 | | |
974 | | /** @brief The cumulative error for quantized weights for each block mode. */ |
975 | | float qwt_errors[WEIGHTS_MAX_BLOCK_MODES]; |
976 | | |
977 | | /** @brief The low weight value in plane 1 for each block mode. */ |
978 | | float weight_low_value1[WEIGHTS_MAX_BLOCK_MODES]; |
979 | | |
980 | | /** @brief The high weight value in plane 1 for each block mode. */ |
981 | | float weight_high_value1[WEIGHTS_MAX_BLOCK_MODES]; |
982 | | |
983 | | /** @brief The low weight value in plane 1 for each quant level and decimation mode. */ |
984 | | float weight_low_values1[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1]; |
985 | | |
986 | | /** @brief The high weight value in plane 1 for each quant level and decimation mode. */ |
987 | | float weight_high_values1[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1]; |
988 | | |
989 | | /** @brief The low weight value in plane 2 for each block mode. */ |
990 | | float weight_low_value2[WEIGHTS_MAX_BLOCK_MODES]; |
991 | | |
992 | | /** @brief The high weight value in plane 2 for each block mode. */ |
993 | | float weight_high_value2[WEIGHTS_MAX_BLOCK_MODES]; |
994 | | |
995 | | /** @brief The low weight value in plane 2 for each quant level and decimation mode. */ |
996 | | float weight_low_values2[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1]; |
997 | | |
998 | | /** @brief The high weight value in plane 2 for each quant level and decimation mode. */ |
999 | | float weight_high_values2[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1]; |
1000 | | }; |
1001 | | |
1002 | | struct dt_init_working_buffers |
1003 | | { |
1004 | | uint8_t weight_count_of_texel[BLOCK_MAX_TEXELS]; |
1005 | | uint8_t grid_weights_of_texel[BLOCK_MAX_TEXELS][4]; |
1006 | | uint8_t weights_of_texel[BLOCK_MAX_TEXELS][4]; |
1007 | | |
1008 | | uint8_t texel_count_of_weight[BLOCK_MAX_WEIGHTS]; |
1009 | | uint8_t texels_of_weight[BLOCK_MAX_WEIGHTS][BLOCK_MAX_TEXELS]; |
1010 | | uint8_t texel_weights_of_weight[BLOCK_MAX_WEIGHTS][BLOCK_MAX_TEXELS]; |
1011 | | }; |
1012 | | |
1013 | | /** |
1014 | | * @brief Weight quantization transfer table. |
1015 | | * |
1016 | | * ASTC can store texel weights at many quantization levels, so for performance we store essential |
1017 | | * information about each level as a precomputed data structure. Unquantized weights are integers |
1018 | | * or floats in the range [0, 64]. |
1019 | | * |
1020 | | * This structure provides a table, used to estimate the closest quantized weight for a given |
1021 | | * floating-point weight. For each quantized weight, the corresponding unquantized values. For each |
1022 | | * quantized weight, a previous-value and a next-value. |
1023 | | */ |
1024 | | struct quant_and_transfer_table |
1025 | | { |
1026 | | /** @brief The unscrambled unquantized value. */ |
1027 | | uint8_t quant_to_unquant[32]; |
1028 | | |
1029 | | /** @brief The scrambling order: scrambled_quant = map[unscrambled_quant]. */ |
1030 | | uint8_t scramble_map[32]; |
1031 | | |
1032 | | /** @brief The unscrambling order: unscrambled_unquant = map[scrambled_quant]. */ |
1033 | | uint8_t unscramble_and_unquant_map[32]; |
1034 | | |
1035 | | /** |
1036 | | * @brief A table of previous-and-next weights, indexed by the current unquantized value. |
1037 | | * * bits 7:0 = previous-index, unquantized |
1038 | | * * bits 15:8 = next-index, unquantized |
1039 | | */ |
1040 | | uint16_t prev_next_values[65]; |
1041 | | }; |
1042 | | |
1043 | | /** @brief The precomputed quant and transfer table. */ |
1044 | | extern const quant_and_transfer_table quant_and_xfer_tables[12]; |
1045 | | |
1046 | | /** @brief The block is an error block, and will return error color or NaN. */ |
1047 | | static constexpr uint8_t SYM_BTYPE_ERROR { 0 }; |
1048 | | |
1049 | | /** @brief The block is a constant color block using FP16 colors. */ |
1050 | | static constexpr uint8_t SYM_BTYPE_CONST_F16 { 1 }; |
1051 | | |
1052 | | /** @brief The block is a constant color block using UNORM16 colors. */ |
1053 | | static constexpr uint8_t SYM_BTYPE_CONST_U16 { 2 }; |
1054 | | |
1055 | | /** @brief The block is a normal non-constant color block. */ |
1056 | | static constexpr uint8_t SYM_BTYPE_NONCONST { 3 }; |
1057 | | |
1058 | | /** |
1059 | | * @brief A symbolic representation of a compressed block. |
1060 | | * |
1061 | | * The symbolic representation stores the unpacked content of a single |
1062 | | * physical compressed block, in a form which is much easier to access for |
1063 | | * the rest of the compressor code. |
1064 | | */ |
1065 | | struct symbolic_compressed_block |
1066 | | { |
1067 | | /** @brief The block type, one of the @c SYM_BTYPE_* constants. */ |
1068 | | uint8_t block_type; |
1069 | | |
1070 | | /** @brief The number of partitions; valid for @c NONCONST blocks. */ |
1071 | | uint8_t partition_count; |
1072 | | |
1073 | | /** @brief Non-zero if the color formats matched; valid for @c NONCONST blocks. */ |
1074 | | uint8_t color_formats_matched; |
1075 | | |
1076 | | /** @brief The plane 2 color component, or -1 if single plane; valid for @c NONCONST blocks. */ |
1077 | | int8_t plane2_component; |
1078 | | |
1079 | | /** @brief The block mode; valid for @c NONCONST blocks. */ |
1080 | | uint16_t block_mode; |
1081 | | |
1082 | | /** @brief The partition index; valid for @c NONCONST blocks if 2 or more partitions. */ |
1083 | | uint16_t partition_index; |
1084 | | |
1085 | | /** @brief The endpoint color formats for each partition; valid for @c NONCONST blocks. */ |
1086 | | uint8_t color_formats[BLOCK_MAX_PARTITIONS]; |
1087 | | |
1088 | | /** @brief The endpoint color quant mode; valid for @c NONCONST blocks. */ |
1089 | | quant_method quant_mode; |
1090 | | |
1091 | | /** @brief The error of the current encoding; valid for @c NONCONST blocks. */ |
1092 | | float errorval; |
1093 | | |
1094 | | // We can't have both of these at the same time |
1095 | | union { |
1096 | | /** @brief The constant color; valid for @c CONST blocks. */ |
1097 | | int constant_color[BLOCK_MAX_COMPONENTS]; |
1098 | | |
1099 | | /** @brief The quantized endpoint color pairs; valid for @c NONCONST blocks. */ |
1100 | | uint8_t color_values[BLOCK_MAX_PARTITIONS][8]; |
1101 | | }; |
1102 | | |
1103 | | /** @brief The quantized and decimated weights. |
1104 | | * |
1105 | | * Weights are stored in the 0-64 unpacked range allowing them to be used |
1106 | | * directly in encoding passes without per-use unpacking. Packing happens |
1107 | | * when converting to/from the physical bitstream encoding. |
1108 | | * |
1109 | | * If dual plane, the second plane starts at @c weights[WEIGHTS_PLANE2_OFFSET]. |
1110 | | */ |
1111 | | uint8_t weights[BLOCK_MAX_WEIGHTS]; |
1112 | | |
1113 | | /** |
1114 | | * @brief Get the weight quantization used by this block mode. |
1115 | | * |
1116 | | * @return The quantization level. |
1117 | | */ |
1118 | | inline quant_method get_color_quant_mode() const |
1119 | 0 | { |
1120 | 0 | return this->quant_mode; |
1121 | 0 | } |
1122 | | }; |
1123 | | |
1124 | | /** |
1125 | | * @brief Parameter structure for @c compute_pixel_region_variance(). |
1126 | | * |
1127 | | * This function takes a structure to avoid spilling arguments to the stack on every function |
1128 | | * invocation, as there are a lot of parameters. |
1129 | | */ |
1130 | | struct pixel_region_args |
1131 | | { |
1132 | | /** @brief The image to analyze. */ |
1133 | | const astcenc_image* img; |
1134 | | |
1135 | | /** @brief The component swizzle pattern. */ |
1136 | | astcenc_swizzle swz; |
1137 | | |
1138 | | /** @brief Should the algorithm bother with Z axis processing? */ |
1139 | | bool have_z; |
1140 | | |
1141 | | /** @brief The kernel radius for alpha processing. */ |
1142 | | unsigned int alpha_kernel_radius; |
1143 | | |
1144 | | /** @brief The X dimension of the working data to process. */ |
1145 | | unsigned int size_x; |
1146 | | |
1147 | | /** @brief The Y dimension of the working data to process. */ |
1148 | | unsigned int size_y; |
1149 | | |
1150 | | /** @brief The Z dimension of the working data to process. */ |
1151 | | unsigned int size_z; |
1152 | | |
1153 | | /** @brief The X position of first src and dst data in the data set. */ |
1154 | | unsigned int offset_x; |
1155 | | |
1156 | | /** @brief The Y position of first src and dst data in the data set. */ |
1157 | | unsigned int offset_y; |
1158 | | |
1159 | | /** @brief The Z position of first src and dst data in the data set. */ |
1160 | | unsigned int offset_z; |
1161 | | |
1162 | | /** @brief The working memory buffer. */ |
1163 | | vfloat4 *work_memory; |
1164 | | }; |
1165 | | |
1166 | | /** |
1167 | | * @brief Parameter structure for @c compute_averages_proc(). |
1168 | | */ |
1169 | | struct avg_args |
1170 | | { |
1171 | | /** @brief The arguments for the nested variance computation. */ |
1172 | | pixel_region_args arg; |
1173 | | |
1174 | | /** @brief The image X dimensions. */ |
1175 | | unsigned int img_size_x; |
1176 | | |
1177 | | /** @brief The image Y dimensions. */ |
1178 | | unsigned int img_size_y; |
1179 | | |
1180 | | /** @brief The image Z dimensions. */ |
1181 | | unsigned int img_size_z; |
1182 | | |
1183 | | /** @brief The maximum working block dimensions in X and Y dimensions. */ |
1184 | | unsigned int blk_size_xy; |
1185 | | |
1186 | | /** @brief The maximum working block dimensions in Z dimensions. */ |
1187 | | unsigned int blk_size_z; |
1188 | | |
1189 | | /** @brief The working block memory size. */ |
1190 | | unsigned int work_memory_size; |
1191 | | }; |
1192 | | |
1193 | | #if defined(ASTCENC_DIAGNOSTICS) |
1194 | | /* See astcenc_diagnostic_trace header for details. */ |
1195 | | class TraceLog; |
1196 | | #endif |
1197 | | |
1198 | | /** |
1199 | | * @brief The astcenc compression context. |
1200 | | */ |
1201 | | struct astcenc_contexti |
1202 | | { |
1203 | | /** @brief The configuration this context was created with. */ |
1204 | | astcenc_config config; |
1205 | | |
1206 | | /** @brief The thread count supported by this context. */ |
1207 | | unsigned int thread_count; |
1208 | | |
1209 | | /** @brief The block size descriptor this context was created with. */ |
1210 | | block_size_descriptor* bsd; |
1211 | | |
1212 | | /* |
1213 | | * Fields below here are not needed in a decompress-only build, but some remain as they are |
1214 | | * small and it avoids littering the code with #ifdefs. The most significant contributors to |
1215 | | * large structure size are omitted. |
1216 | | */ |
1217 | | |
1218 | | /** @brief The input image alpha channel averages table, may be @c nullptr if not needed. */ |
1219 | | float* input_alpha_averages; |
1220 | | |
1221 | | /** @brief The scratch working buffers, one per thread (see @c thread_count). */ |
1222 | | compression_working_buffers* working_buffers; |
1223 | | |
1224 | | #if !defined(ASTCENC_DECOMPRESS_ONLY) |
1225 | | /** @brief The pixel region and variance worker arguments. */ |
1226 | | avg_args avg_preprocess_args; |
1227 | | #endif |
1228 | | |
1229 | | #if defined(ASTCENC_DIAGNOSTICS) |
1230 | | /** |
1231 | | * @brief The diagnostic trace logger. |
1232 | | * |
1233 | | * Note that this is a singleton, so can only be used in single threaded mode. It only exists |
1234 | | * here so we have a reference to close the file at the end of the capture. |
1235 | | */ |
1236 | | TraceLog* trace_log; |
1237 | | #endif |
1238 | | }; |
1239 | | |
1240 | | /* ============================================================================ |
1241 | | Functionality for managing block sizes and partition tables. |
1242 | | ============================================================================ */ |
1243 | | |
1244 | | /** |
1245 | | * @brief Populate the block size descriptor for the target block size. |
1246 | | * |
1247 | | * This will also initialize the partition table metadata, which is stored as part of the BSD |
1248 | | * structure. |
1249 | | * |
1250 | | * @param x_texels The number of texels in the block X dimension. |
1251 | | * @param y_texels The number of texels in the block Y dimension. |
1252 | | * @param z_texels The number of texels in the block Z dimension. |
1253 | | * @param can_omit_modes Can we discard modes and partitionings that astcenc won't use? |
1254 | | * @param partition_count_cutoff The partition count cutoff to use, if we can omit partitionings. |
1255 | | * @param mode_cutoff The block mode percentile cutoff [0-1]. |
1256 | | * @param[out] bsd The descriptor to initialize. |
1257 | | */ |
1258 | | void init_block_size_descriptor( |
1259 | | unsigned int x_texels, |
1260 | | unsigned int y_texels, |
1261 | | unsigned int z_texels, |
1262 | | bool can_omit_modes, |
1263 | | unsigned int partition_count_cutoff, |
1264 | | float mode_cutoff, |
1265 | | block_size_descriptor& bsd); |
1266 | | |
1267 | | /** |
1268 | | * @brief Populate the partition tables for the target block size. |
1269 | | * |
1270 | | * Note the @c bsd descriptor must be initialized by calling @c init_block_size_descriptor() before |
1271 | | * calling this function. |
1272 | | * |
1273 | | * @param[out] bsd The block size information structure to populate. |
1274 | | * @param can_omit_partitionings True if we can we drop partitionings that astcenc won't use. |
1275 | | * @param partition_count_cutoff The partition count cutoff to use, if we can omit partitionings. |
1276 | | */ |
1277 | | void init_partition_tables( |
1278 | | block_size_descriptor& bsd, |
1279 | | bool can_omit_partitionings, |
1280 | | unsigned int partition_count_cutoff); |
1281 | | |
1282 | | /** |
1283 | | * @brief Get the percentile table for 2D block modes. |
1284 | | * |
1285 | | * This is an empirically determined prioritization of which block modes to use in the search in |
1286 | | * terms of their centile (lower centiles = more useful). |
1287 | | * |
1288 | | * Returns a dynamically allocated array; caller must free with delete[]. |
1289 | | * |
1290 | | * @param xdim The block x size. |
1291 | | * @param ydim The block y size. |
1292 | | * |
1293 | | * @return The unpacked table. |
1294 | | */ |
1295 | | const float* get_2d_percentile_table( |
1296 | | unsigned int xdim, |
1297 | | unsigned int ydim); |
1298 | | |
1299 | | /** |
1300 | | * @brief Query if a 2D block size is legal. |
1301 | | * |
1302 | | * @return True if legal, false otherwise. |
1303 | | */ |
1304 | | bool is_legal_2d_block_size( |
1305 | | unsigned int xdim, |
1306 | | unsigned int ydim); |
1307 | | |
1308 | | /** |
1309 | | * @brief Query if a 3D block size is legal. |
1310 | | * |
1311 | | * @return True if legal, false otherwise. |
1312 | | */ |
1313 | | bool is_legal_3d_block_size( |
1314 | | unsigned int xdim, |
1315 | | unsigned int ydim, |
1316 | | unsigned int zdim); |
1317 | | |
1318 | | /* ============================================================================ |
1319 | | Functionality for managing BISE quantization and unquantization. |
1320 | | ============================================================================ */ |
1321 | | |
1322 | | /** |
1323 | | * @brief The precomputed table for quantizing color values. |
1324 | | * |
1325 | | * Converts unquant value in 0-255 range into quant value in 0-255 range. |
1326 | | * No BISE scrambling is applied at this stage. |
1327 | | * |
1328 | | * The BISE encoding results in ties where available quant<256> values are |
1329 | | * equidistant the available quant<BISE> values. This table stores two values |
1330 | | * for each input - one for use with a negative residual, and one for use with |
1331 | | * a positive residual. |
1332 | | * |
1333 | | * Indexed by [quant_mode - 4][data_value * 2 + residual]. |
1334 | | */ |
1335 | | extern const uint8_t color_unquant_to_uquant_tables[17][512]; |
1336 | | |
1337 | | /** |
1338 | | * @brief The precomputed table for packing quantized color values. |
1339 | | * |
1340 | | * Converts quant value in 0-255 range into packed quant value in 0-N range, |
1341 | | * with BISE scrambling applied. |
1342 | | * |
1343 | | * Indexed by [quant_mode - 4][data_value]. |
1344 | | */ |
1345 | | extern const uint8_t color_uquant_to_scrambled_pquant_tables[17][256]; |
1346 | | |
1347 | | /** |
1348 | | * @brief The precomputed table for unpacking color values. |
1349 | | * |
1350 | | * Converts quant value in 0-N range into unpacked value in 0-255 range, |
1351 | | * with BISE unscrambling applied. |
1352 | | * |
1353 | | * Indexed by [quant_mode - 4][data_value]. |
1354 | | */ |
1355 | | extern const uint8_t* color_scrambled_pquant_to_uquant_tables[17]; |
1356 | | |
1357 | | /** |
1358 | | * @brief The precomputed quant mode storage table. |
1359 | | * |
1360 | | * Indexing by [integer_count/2][bits] gives us the quantization level for a given integer count and |
1361 | | * number of compressed storage bits. Returns -1 for cases where the requested integer count cannot |
1362 | | * ever fit in the supplied storage size. |
1363 | | */ |
1364 | | extern const int8_t quant_mode_table[10][128]; |
1365 | | |
1366 | | /** |
1367 | | * @brief Encode a packed string using BISE. |
1368 | | * |
1369 | | * Note that BISE can return strings that are not a whole number of bytes in length, and ASTC can |
1370 | | * start storing strings in a block at arbitrary bit offsets in the encoded data. |
1371 | | * |
1372 | | * @param quant_level The BISE alphabet size. |
1373 | | * @param character_count The number of characters in the string. |
1374 | | * @param input_data The unpacked string, one byte per character. |
1375 | | * @param[in,out] output_data The output packed string. |
1376 | | * @param bit_offset The starting offset in the output storage. |
1377 | | */ |
1378 | | void encode_ise( |
1379 | | quant_method quant_level, |
1380 | | unsigned int character_count, |
1381 | | const uint8_t* input_data, |
1382 | | uint8_t* output_data, |
1383 | | unsigned int bit_offset); |
1384 | | |
1385 | | /** |
1386 | | * @brief Decode a packed string using BISE. |
1387 | | * |
1388 | | * Note that BISE input strings are not a whole number of bytes in length, and ASTC can start |
1389 | | * strings at arbitrary bit offsets in the encoded data. |
1390 | | * |
1391 | | * @param quant_level The BISE alphabet size. |
1392 | | * @param character_count The number of characters in the string. |
1393 | | * @param input_data The packed string. |
1394 | | * @param[in,out] output_data The output storage, one byte per character. |
1395 | | * @param bit_offset The starting offset in the output storage. |
1396 | | */ |
1397 | | void decode_ise( |
1398 | | quant_method quant_level, |
1399 | | unsigned int character_count, |
1400 | | const uint8_t* input_data, |
1401 | | uint8_t* output_data, |
1402 | | unsigned int bit_offset); |
1403 | | |
1404 | | /** |
1405 | | * @brief Return the number of bits needed to encode an ISE sequence. |
1406 | | * |
1407 | | * This implementation assumes that the @c quant level is untrusted, given it may come from random |
1408 | | * data being decompressed, so we return an arbitrary unencodable size if that is the case. |
1409 | | * |
1410 | | * @param character_count The number of items in the sequence. |
1411 | | * @param quant_level The desired quantization level. |
1412 | | * |
1413 | | * @return The number of bits needed to encode the BISE string. |
1414 | | */ |
1415 | | unsigned int get_ise_sequence_bitcount( |
1416 | | unsigned int character_count, |
1417 | | quant_method quant_level); |
1418 | | |
1419 | | /* ============================================================================ |
1420 | | Functionality for managing color partitioning. |
1421 | | ============================================================================ */ |
1422 | | |
1423 | | /** |
1424 | | * @brief Compute averages and dominant directions for each partition in a 2 component texture. |
1425 | | * |
1426 | | * @param pi The partition info for the current trial. |
1427 | | * @param blk The image block color data to be compressed. |
1428 | | * @param component1 The first component included in the analysis. |
1429 | | * @param component2 The second component included in the analysis. |
1430 | | * @param[out] pm The output partition metrics. |
1431 | | * - Only pi.partition_count array entries actually get initialized. |
1432 | | * - Direction vectors @c pm.dir are not normalized. |
1433 | | */ |
1434 | | void compute_avgs_and_dirs_2_comp( |
1435 | | const partition_info& pi, |
1436 | | const image_block& blk, |
1437 | | unsigned int component1, |
1438 | | unsigned int component2, |
1439 | | partition_metrics pm[BLOCK_MAX_PARTITIONS]); |
1440 | | |
1441 | | /** |
1442 | | * @brief Compute averages and dominant directions for each partition in a 3 component texture. |
1443 | | * |
1444 | | * @param pi The partition info for the current trial. |
1445 | | * @param blk The image block color data to be compressed. |
1446 | | * @param omitted_component The component excluded from the analysis. |
1447 | | * @param[out] pm The output partition metrics. |
1448 | | * - Only pi.partition_count array entries actually get initialized. |
1449 | | * - Direction vectors @c pm.dir are not normalized. |
1450 | | */ |
1451 | | void compute_avgs_and_dirs_3_comp( |
1452 | | const partition_info& pi, |
1453 | | const image_block& blk, |
1454 | | unsigned int omitted_component, |
1455 | | partition_metrics pm[BLOCK_MAX_PARTITIONS]); |
1456 | | |
1457 | | /** |
1458 | | * @brief Compute averages and dominant directions for each partition in a 3 component texture. |
1459 | | * |
1460 | | * This is a specialization of @c compute_avgs_and_dirs_3_comp where the omitted component is |
1461 | | * always alpha, a common case during partition search. |
1462 | | * |
1463 | | * @param pi The partition info for the current trial. |
1464 | | * @param blk The image block color data to be compressed. |
1465 | | * @param[out] pm The output partition metrics. |
1466 | | * - Only pi.partition_count array entries actually get initialized. |
1467 | | * - Direction vectors @c pm.dir are not normalized. |
1468 | | */ |
1469 | | void compute_avgs_and_dirs_3_comp_rgb( |
1470 | | const partition_info& pi, |
1471 | | const image_block& blk, |
1472 | | partition_metrics pm[BLOCK_MAX_PARTITIONS]); |
1473 | | |
1474 | | /** |
1475 | | * @brief Compute averages and dominant directions for each partition in a 4 component texture. |
1476 | | * |
1477 | | * @param pi The partition info for the current trial. |
1478 | | * @param blk The image block color data to be compressed. |
1479 | | * @param[out] pm The output partition metrics. |
1480 | | * - Only pi.partition_count array entries actually get initialized. |
1481 | | * - Direction vectors @c pm.dir are not normalized. |
1482 | | */ |
1483 | | void compute_avgs_and_dirs_4_comp( |
1484 | | const partition_info& pi, |
1485 | | const image_block& blk, |
1486 | | partition_metrics pm[BLOCK_MAX_PARTITIONS]); |
1487 | | |
1488 | | /** |
1489 | | * @brief Compute the RGB error for uncorrelated and same chroma projections. |
1490 | | * |
1491 | | * The output of compute averages and dirs is post processed to define two lines, both of which go |
1492 | | * through the mean-color-value. One line has a direction defined by the dominant direction; this |
1493 | | * is used to assess the error from using an uncorrelated color representation. The other line goes |
1494 | | * through (0,0,0) and is used to assess the error from using an RGBS color representation. |
1495 | | * |
1496 | | * This function computes the squared error when using these two representations. |
1497 | | * |
1498 | | * @param pi The partition info for the current trial. |
1499 | | * @param blk The image block color data to be compressed. |
1500 | | * @param[in,out] plines Processed line inputs, and line length outputs. |
1501 | | * @param[out] uncor_error The cumulative error for using the uncorrelated line. |
1502 | | * @param[out] samec_error The cumulative error for using the same chroma line. |
1503 | | */ |
1504 | | void compute_error_squared_rgb( |
1505 | | const partition_info& pi, |
1506 | | const image_block& blk, |
1507 | | partition_lines3 plines[BLOCK_MAX_PARTITIONS], |
1508 | | float& uncor_error, |
1509 | | float& samec_error); |
1510 | | |
1511 | | /** |
1512 | | * @brief Compute the RGBA error for uncorrelated and same chroma projections. |
1513 | | * |
1514 | | * The output of compute averages and dirs is post processed to define two lines, both of which go |
1515 | | * through the mean-color-value. One line has a direction defined by the dominant direction; this |
1516 | | * is used to assess the error from using an uncorrelated color representation. The other line goes |
1517 | | * through (0,0,0,1) and is used to assess the error from using an RGBS color representation. |
1518 | | * |
1519 | | * This function computes the squared error when using these two representations. |
1520 | | * |
1521 | | * @param pi The partition info for the current trial. |
1522 | | * @param blk The image block color data to be compressed. |
1523 | | * @param uncor_plines Processed uncorrelated partition lines for each partition. |
1524 | | * @param samec_plines Processed same chroma partition lines for each partition. |
1525 | | * @param[out] line_lengths The length of each components deviation from the line. |
1526 | | * @param[out] uncor_error The cumulative error for using the uncorrelated line. |
1527 | | * @param[out] samec_error The cumulative error for using the same chroma line. |
1528 | | */ |
1529 | | void compute_error_squared_rgba( |
1530 | | const partition_info& pi, |
1531 | | const image_block& blk, |
1532 | | const processed_line4 uncor_plines[BLOCK_MAX_PARTITIONS], |
1533 | | const processed_line4 samec_plines[BLOCK_MAX_PARTITIONS], |
1534 | | float line_lengths[BLOCK_MAX_PARTITIONS], |
1535 | | float& uncor_error, |
1536 | | float& samec_error); |
1537 | | |
1538 | | /** |
1539 | | * @brief Find the best set of partitions to trial for a given block. |
1540 | | * |
1541 | | * On return the @c best_partitions list will contain the two best partition |
1542 | | * candidates; one assuming data has uncorrelated chroma and one assuming the |
1543 | | * data has correlated chroma. The best candidate is returned first in the list. |
1544 | | * |
1545 | | * @param bsd The block size information. |
1546 | | * @param blk The image block color data to compress. |
1547 | | * @param partition_count The number of partitions in the block. |
1548 | | * @param partition_search_limit The number of candidate partition encodings to trial. |
1549 | | * @param[out] best_partitions The best partition candidates. |
1550 | | * @param requested_candidates The number of requested partitionings. May return fewer if |
1551 | | * candidates are not available. |
1552 | | * |
1553 | | * @return The actual number of candidates returned. |
1554 | | */ |
1555 | | unsigned int find_best_partition_candidates( |
1556 | | const block_size_descriptor& bsd, |
1557 | | const image_block& blk, |
1558 | | unsigned int partition_count, |
1559 | | unsigned int partition_search_limit, |
1560 | | unsigned int best_partitions[TUNE_MAX_PARTITIONING_CANDIDATES], |
1561 | | unsigned int requested_candidates); |
1562 | | |
1563 | | /* ============================================================================ |
1564 | | Functionality for managing images and image related data. |
1565 | | ============================================================================ */ |
1566 | | |
1567 | | /** |
1568 | | * @brief Setup computation of regional averages in an image. |
1569 | | * |
1570 | | * This must be done by only a single thread per image, before any thread calls |
1571 | | * @c compute_averages(). |
1572 | | * |
1573 | | * Results are written back into @c img->input_alpha_averages. |
1574 | | * |
1575 | | * @param img The input image data, also holds output data. |
1576 | | * @param alpha_kernel_radius The kernel radius (in pixels) for alpha mods. |
1577 | | * @param swz Input data component swizzle. |
1578 | | * @param[out] ag The average variance arguments to init. |
1579 | | * |
1580 | | * @return The number of tasks in the processing stage. |
1581 | | */ |
1582 | | unsigned int init_compute_averages( |
1583 | | const astcenc_image& img, |
1584 | | unsigned int alpha_kernel_radius, |
1585 | | const astcenc_swizzle& swz, |
1586 | | avg_args& ag); |
1587 | | |
1588 | | /** |
1589 | | * @brief Compute averages for a pixel region. |
1590 | | * |
1591 | | * The routine computes both in a single pass, using a summed-area table to decouple the running |
1592 | | * time from the averaging/variance kernel size. |
1593 | | * |
1594 | | * @param[out] ctx The compressor context storing the output data. |
1595 | | * @param arg The input parameter structure. |
1596 | | */ |
1597 | | void compute_pixel_region_variance( |
1598 | | astcenc_contexti& ctx, |
1599 | | const pixel_region_args& arg); |
1600 | | /** |
1601 | | * @brief Load a single image block from the input image. |
1602 | | * |
1603 | | * @param decode_mode The compression color profile. |
1604 | | * @param img The input image data. |
1605 | | * @param[out] blk The image block to populate. |
1606 | | * @param bsd The block size information. |
1607 | | * @param xpos The block X coordinate in the input image. |
1608 | | * @param ypos The block Y coordinate in the input image. |
1609 | | * @param zpos The block Z coordinate in the input image. |
1610 | | * @param swz The swizzle to apply on load. |
1611 | | */ |
1612 | | void load_image_block( |
1613 | | astcenc_profile decode_mode, |
1614 | | const astcenc_image& img, |
1615 | | image_block& blk, |
1616 | | const block_size_descriptor& bsd, |
1617 | | unsigned int xpos, |
1618 | | unsigned int ypos, |
1619 | | unsigned int zpos, |
1620 | | const astcenc_swizzle& swz); |
1621 | | |
1622 | | /** |
1623 | | * @brief Load a single image block from the input image. |
1624 | | * |
1625 | | * This specialized variant can be used only if the block is 2D LDR U8 data, |
1626 | | * with no swizzle. |
1627 | | * |
1628 | | * @param decode_mode The compression color profile. |
1629 | | * @param img The input image data. |
1630 | | * @param[out] blk The image block to populate. |
1631 | | * @param bsd The block size information. |
1632 | | * @param xpos The block X coordinate in the input image. |
1633 | | * @param ypos The block Y coordinate in the input image. |
1634 | | * @param zpos The block Z coordinate in the input image. |
1635 | | * @param swz The swizzle to apply on load. |
1636 | | */ |
1637 | | void load_image_block_fast_ldr( |
1638 | | astcenc_profile decode_mode, |
1639 | | const astcenc_image& img, |
1640 | | image_block& blk, |
1641 | | const block_size_descriptor& bsd, |
1642 | | unsigned int xpos, |
1643 | | unsigned int ypos, |
1644 | | unsigned int zpos, |
1645 | | const astcenc_swizzle& swz); |
1646 | | |
1647 | | /** |
1648 | | * @brief Store a single image block to the output image. |
1649 | | * |
1650 | | * @param[out] img The output image data. |
1651 | | * @param blk The image block to export. |
1652 | | * @param bsd The block size information. |
1653 | | * @param xpos The block X coordinate in the input image. |
1654 | | * @param ypos The block Y coordinate in the input image. |
1655 | | * @param zpos The block Z coordinate in the input image. |
1656 | | * @param swz The swizzle to apply on store. |
1657 | | */ |
1658 | | void store_image_block( |
1659 | | astcenc_image& img, |
1660 | | const image_block& blk, |
1661 | | const block_size_descriptor& bsd, |
1662 | | unsigned int xpos, |
1663 | | unsigned int ypos, |
1664 | | unsigned int zpos, |
1665 | | const astcenc_swizzle& swz); |
1666 | | |
1667 | | /* ============================================================================ |
1668 | | Functionality for computing endpoint colors and weights for a block. |
1669 | | ============================================================================ */ |
1670 | | |
1671 | | /** |
1672 | | * @brief Compute ideal endpoint colors and weights for 1 plane of weights. |
1673 | | * |
1674 | | * The ideal endpoints define a color line for the partition. For each texel the ideal weight |
1675 | | * defines an exact position on the partition color line. We can then use these to assess the error |
1676 | | * introduced by removing and quantizing the weight grid. |
1677 | | * |
1678 | | * @param blk The image block color data to compress. |
1679 | | * @param pi The partition info for the current trial. |
1680 | | * @param[out] ei The endpoint and weight values. |
1681 | | */ |
1682 | | void compute_ideal_colors_and_weights_1plane( |
1683 | | const image_block& blk, |
1684 | | const partition_info& pi, |
1685 | | endpoints_and_weights& ei); |
1686 | | |
1687 | | /** |
1688 | | * @brief Compute ideal endpoint colors and weights for 2 planes of weights. |
1689 | | * |
1690 | | * The ideal endpoints define a color line for the partition. For each texel the ideal weight |
1691 | | * defines an exact position on the partition color line. We can then use these to assess the error |
1692 | | * introduced by removing and quantizing the weight grid. |
1693 | | * |
1694 | | * @param bsd The block size information. |
1695 | | * @param blk The image block color data to compress. |
1696 | | * @param plane2_component The component assigned to plane 2. |
1697 | | * @param[out] ei1 The endpoint and weight values for plane 1. |
1698 | | * @param[out] ei2 The endpoint and weight values for plane 2. |
1699 | | */ |
1700 | | void compute_ideal_colors_and_weights_2planes( |
1701 | | const block_size_descriptor& bsd, |
1702 | | const image_block& blk, |
1703 | | unsigned int plane2_component, |
1704 | | endpoints_and_weights& ei1, |
1705 | | endpoints_and_weights& ei2); |
1706 | | |
1707 | | /** |
1708 | | * @brief Compute the optimal unquantized weights for a decimation table. |
1709 | | * |
1710 | | * After computing ideal weights for the case for a complete weight grid, we we want to compute the |
1711 | | * ideal weights for the case where weights exist only for some texels. We do this with a |
1712 | | * steepest-descent grid solver which works as follows: |
1713 | | * |
1714 | | * First, for each actual weight, perform a weighted averaging of the texels affected by the weight. |
1715 | | * Then, set step size to <some initial value> and attempt one step towards the original ideal |
1716 | | * weight if it helps to reduce error. |
1717 | | * |
1718 | | * @param ei The non-decimated endpoints and weights. |
1719 | | * @param di The selected weight decimation. |
1720 | | * @param[out] dec_weight_ideal_value The ideal values for the decimated weight set. |
1721 | | */ |
1722 | | void compute_ideal_weights_for_decimation( |
1723 | | const endpoints_and_weights& ei, |
1724 | | const decimation_info& di, |
1725 | | float* dec_weight_ideal_value); |
1726 | | |
1727 | | /** |
1728 | | * @brief Compute the optimal quantized weights for a decimation table. |
1729 | | * |
1730 | | * We test the two closest weight indices in the allowed quantization range and keep the weight that |
1731 | | * is the closest match. |
1732 | | * |
1733 | | * @param di The selected weight decimation. |
1734 | | * @param low_bound The lowest weight allowed. |
1735 | | * @param high_bound The highest weight allowed. |
1736 | | * @param dec_weight_ideal_value The ideal weight set. |
1737 | | * @param[out] dec_weight_quant_uvalue The output quantized weight as a float. |
1738 | | * @param[out] dec_weight_uquant The output quantized weight as encoded int. |
1739 | | * @param quant_level The desired weight quant level. |
1740 | | */ |
1741 | | void compute_quantized_weights_for_decimation( |
1742 | | const decimation_info& di, |
1743 | | float low_bound, |
1744 | | float high_bound, |
1745 | | const float* dec_weight_ideal_value, |
1746 | | float* dec_weight_quant_uvalue, |
1747 | | uint8_t* dec_weight_uquant, |
1748 | | quant_method quant_level); |
1749 | | |
1750 | | /** |
1751 | | * @brief Compute the error of a decimated weight set for 1 plane. |
1752 | | * |
1753 | | * After computing ideal weights for the case with one weight per texel, we want to compute the |
1754 | | * error for decimated weight grids where weights are stored at a lower resolution. This function |
1755 | | * computes the error of the reduced grid, compared to the full grid. |
1756 | | * |
1757 | | * @param eai The ideal weights for the full grid. |
1758 | | * @param di The selected weight decimation. |
1759 | | * @param dec_weight_quant_uvalue The quantized weights for the decimated grid. |
1760 | | * |
1761 | | * @return The accumulated error. |
1762 | | */ |
1763 | | float compute_error_of_weight_set_1plane( |
1764 | | const endpoints_and_weights& eai, |
1765 | | const decimation_info& di, |
1766 | | const float* dec_weight_quant_uvalue); |
1767 | | |
1768 | | /** |
1769 | | * @brief Compute the error of a decimated weight set for 2 planes. |
1770 | | * |
1771 | | * After computing ideal weights for the case with one weight per texel, we want to compute the |
1772 | | * error for decimated weight grids where weights are stored at a lower resolution. This function |
1773 | | * computes the error of the reduced grid, compared to the full grid. |
1774 | | * |
1775 | | * @param eai1 The ideal weights for the full grid and plane 1. |
1776 | | * @param eai2 The ideal weights for the full grid and plane 2. |
1777 | | * @param di The selected weight decimation. |
1778 | | * @param dec_weight_quant_uvalue_plane1 The quantized weights for the decimated grid plane 1. |
1779 | | * @param dec_weight_quant_uvalue_plane2 The quantized weights for the decimated grid plane 2. |
1780 | | * |
1781 | | * @return The accumulated error. |
1782 | | */ |
1783 | | float compute_error_of_weight_set_2planes( |
1784 | | const endpoints_and_weights& eai1, |
1785 | | const endpoints_and_weights& eai2, |
1786 | | const decimation_info& di, |
1787 | | const float* dec_weight_quant_uvalue_plane1, |
1788 | | const float* dec_weight_quant_uvalue_plane2); |
1789 | | |
1790 | | /** |
1791 | | * @brief Pack a single pair of color endpoints as effectively as possible. |
1792 | | * |
1793 | | * The user requests a base color endpoint mode in @c format, but the quantizer may choose a |
1794 | | * delta-based representation. It will report back the format variant it actually used. |
1795 | | * |
1796 | | * @param color0 The input unquantized color0 endpoint for absolute endpoint pairs. |
1797 | | * @param color1 The input unquantized color1 endpoint for absolute endpoint pairs. |
1798 | | * @param rgbs_color The input unquantized RGBS variant endpoint for same chroma endpoints. |
1799 | | * @param rgbo_color The input unquantized RGBS variant endpoint for HDR endpoints. |
1800 | | * @param format The desired base format. |
1801 | | * @param[out] output The output storage for the quantized colors/ |
1802 | | * @param quant_level The quantization level requested. |
1803 | | * |
1804 | | * @return The actual endpoint mode used. |
1805 | | */ |
1806 | | uint8_t pack_color_endpoints( |
1807 | | vfloat4 color0, |
1808 | | vfloat4 color1, |
1809 | | vfloat4 rgbs_color, |
1810 | | vfloat4 rgbo_color, |
1811 | | int format, |
1812 | | uint8_t* output, |
1813 | | quant_method quant_level); |
1814 | | |
1815 | | /** |
1816 | | * @brief Unpack a single pair of encoded endpoints. |
1817 | | * |
1818 | | * Endpoints must be unscrambled and converted into the 0-255 range before calling this functions. |
1819 | | * |
1820 | | * @param decode_mode The decode mode (LDR, HDR). |
1821 | | * @param format The color endpoint mode used. |
1822 | | * @param input The raw array of encoded input integers. The length of this array |
1823 | | * depends on @c format; it can be safely assumed to be large enough. |
1824 | | * @param[out] rgb_hdr Is the endpoint using HDR for the RGB channels? |
1825 | | * @param[out] alpha_hdr Is the endpoint using HDR for the A channel? |
1826 | | * @param[out] output0 The output color for endpoint 0. |
1827 | | * @param[out] output1 The output color for endpoint 1. |
1828 | | */ |
1829 | | void unpack_color_endpoints( |
1830 | | astcenc_profile decode_mode, |
1831 | | int format, |
1832 | | const uint8_t* input, |
1833 | | bool& rgb_hdr, |
1834 | | bool& alpha_hdr, |
1835 | | vint4& output0, |
1836 | | vint4& output1); |
1837 | | |
1838 | | /** |
1839 | | * @brief Unpack an LDR RGBA color that uses delta encoding. |
1840 | | * |
1841 | | * @param input0 The packed endpoint 0 color. |
1842 | | * @param input1 The packed endpoint 1 color deltas. |
1843 | | * @param[out] output0 The unpacked endpoint 0 color. |
1844 | | * @param[out] output1 The unpacked endpoint 1 color. |
1845 | | */ |
1846 | | void rgba_delta_unpack( |
1847 | | vint4 input0, |
1848 | | vint4 input1, |
1849 | | vint4& output0, |
1850 | | vint4& output1); |
1851 | | |
1852 | | /** |
1853 | | * @brief Unpack an LDR RGBA color that uses direct encoding. |
1854 | | * |
1855 | | * @param input0 The packed endpoint 0 color. |
1856 | | * @param input1 The packed endpoint 1 color. |
1857 | | * @param[out] output0 The unpacked endpoint 0 color. |
1858 | | * @param[out] output1 The unpacked endpoint 1 color. |
1859 | | */ |
1860 | | void rgba_unpack( |
1861 | | vint4 input0, |
1862 | | vint4 input1, |
1863 | | vint4& output0, |
1864 | | vint4& output1); |
1865 | | |
1866 | | /** |
1867 | | * @brief Unpack a set of quantized and decimated weights. |
1868 | | * |
1869 | | * TODO: Can we skip this for non-decimated weights now that the @c scb is |
1870 | | * already storing unquantized weights? |
1871 | | * |
1872 | | * @param bsd The block size information. |
1873 | | * @param scb The symbolic compressed encoding. |
1874 | | * @param di The weight grid decimation table. |
1875 | | * @param is_dual_plane @c true if this is a dual plane block, @c false otherwise. |
1876 | | * @param[out] weights_plane1 The output array for storing the plane 1 weights. |
1877 | | * @param[out] weights_plane2 The output array for storing the plane 2 weights. |
1878 | | */ |
1879 | | void unpack_weights( |
1880 | | const block_size_descriptor& bsd, |
1881 | | const symbolic_compressed_block& scb, |
1882 | | const decimation_info& di, |
1883 | | bool is_dual_plane, |
1884 | | int weights_plane1[BLOCK_MAX_TEXELS], |
1885 | | int weights_plane2[BLOCK_MAX_TEXELS]); |
1886 | | |
1887 | | /** |
1888 | | * @brief Identify, for each mode, which set of color endpoint produces the best result. |
1889 | | * |
1890 | | * Returns the best @c tune_candidate_limit best looking modes, along with the ideal color encoding |
1891 | | * combination for each. The modified quantization level can be used when all formats are the same, |
1892 | | * as this frees up two additional bits of storage. |
1893 | | * |
1894 | | * @param pi The partition info for the current trial. |
1895 | | * @param blk The image block color data to compress. |
1896 | | * @param ep The ideal endpoints. |
1897 | | * @param qwt_bitcounts Bit counts for different quantization methods. |
1898 | | * @param qwt_errors Errors for different quantization methods. |
1899 | | * @param tune_candidate_limit The max number of candidates to return, may be less. |
1900 | | * @param start_block_mode The first block mode to inspect. |
1901 | | * @param end_block_mode The last block mode to inspect. |
1902 | | * @param[out] partition_format_specifiers The best formats per partition. |
1903 | | * @param[out] block_mode The best packed block mode indexes. |
1904 | | * @param[out] quant_level The best color quant level. |
1905 | | * @param[out] quant_level_mod The best color quant level if endpoints are the same. |
1906 | | * @param[out] tmpbuf Preallocated scratch buffers for the compressor. |
1907 | | * |
1908 | | * @return The actual number of candidate matches returned. |
1909 | | */ |
1910 | | unsigned int compute_ideal_endpoint_formats( |
1911 | | const partition_info& pi, |
1912 | | const image_block& blk, |
1913 | | const endpoints& ep, |
1914 | | const int8_t* qwt_bitcounts, |
1915 | | const float* qwt_errors, |
1916 | | unsigned int tune_candidate_limit, |
1917 | | unsigned int start_block_mode, |
1918 | | unsigned int end_block_mode, |
1919 | | uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS], |
1920 | | int block_mode[TUNE_MAX_TRIAL_CANDIDATES], |
1921 | | quant_method quant_level[TUNE_MAX_TRIAL_CANDIDATES], |
1922 | | quant_method quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES], |
1923 | | compression_working_buffers& tmpbuf); |
1924 | | |
1925 | | /** |
1926 | | * @brief For a given 1 plane weight set recompute the endpoint colors. |
1927 | | * |
1928 | | * As we quantize and decimate weights the optimal endpoint colors may change slightly, so we must |
1929 | | * recompute the ideal colors for a specific weight set. |
1930 | | * |
1931 | | * @param blk The image block color data to compress. |
1932 | | * @param pi The partition info for the current trial. |
1933 | | * @param di The weight grid decimation table. |
1934 | | * @param dec_weights_uquant The quantized weight set. |
1935 | | * @param[in,out] ep The color endpoints (modifed in place). |
1936 | | * @param[out] rgbs_vectors The RGB+scale vectors for LDR blocks. |
1937 | | * @param[out] rgbo_vectors The RGB+offset vectors for HDR blocks. |
1938 | | */ |
1939 | | void recompute_ideal_colors_1plane( |
1940 | | const image_block& blk, |
1941 | | const partition_info& pi, |
1942 | | const decimation_info& di, |
1943 | | const uint8_t* dec_weights_uquant, |
1944 | | endpoints& ep, |
1945 | | vfloat4 rgbs_vectors[BLOCK_MAX_PARTITIONS], |
1946 | | vfloat4 rgbo_vectors[BLOCK_MAX_PARTITIONS]); |
1947 | | |
1948 | | /** |
1949 | | * @brief For a given 2 plane weight set recompute the endpoint colors. |
1950 | | * |
1951 | | * As we quantize and decimate weights the optimal endpoint colors may change slightly, so we must |
1952 | | * recompute the ideal colors for a specific weight set. |
1953 | | * |
1954 | | * @param blk The image block color data to compress. |
1955 | | * @param bsd The block_size descriptor. |
1956 | | * @param di The weight grid decimation table. |
1957 | | * @param dec_weights_uquant_plane1 The quantized weight set for plane 1. |
1958 | | * @param dec_weights_uquant_plane2 The quantized weight set for plane 2. |
1959 | | * @param[in,out] ep The color endpoints (modifed in place). |
1960 | | * @param[out] rgbs_vector The RGB+scale color for LDR blocks. |
1961 | | * @param[out] rgbo_vector The RGB+offset color for HDR blocks. |
1962 | | * @param plane2_component The component assigned to plane 2. |
1963 | | */ |
1964 | | void recompute_ideal_colors_2planes( |
1965 | | const image_block& blk, |
1966 | | const block_size_descriptor& bsd, |
1967 | | const decimation_info& di, |
1968 | | const uint8_t* dec_weights_uquant_plane1, |
1969 | | const uint8_t* dec_weights_uquant_plane2, |
1970 | | endpoints& ep, |
1971 | | vfloat4& rgbs_vector, |
1972 | | vfloat4& rgbo_vector, |
1973 | | int plane2_component); |
1974 | | |
1975 | | /** |
1976 | | * @brief Expand the angular tables needed for the alternative to PCA that we use. |
1977 | | */ |
1978 | | void prepare_angular_tables(); |
1979 | | |
1980 | | /** |
1981 | | * @brief Compute the angular endpoints for one plane for each block mode. |
1982 | | * |
1983 | | * @param only_always Only consider block modes that are always enabled. |
1984 | | * @param bsd The block size descriptor for the current trial. |
1985 | | * @param dec_weight_ideal_value The ideal decimated unquantized weight values. |
1986 | | * @param max_weight_quant The maximum block mode weight quantization allowed. |
1987 | | * @param[out] tmpbuf Preallocated scratch buffers for the compressor. |
1988 | | */ |
1989 | | void compute_angular_endpoints_1plane( |
1990 | | bool only_always, |
1991 | | const block_size_descriptor& bsd, |
1992 | | const float* dec_weight_ideal_value, |
1993 | | unsigned int max_weight_quant, |
1994 | | compression_working_buffers& tmpbuf); |
1995 | | |
1996 | | /** |
1997 | | * @brief Compute the angular endpoints for two planes for each block mode. |
1998 | | * |
1999 | | * @param bsd The block size descriptor for the current trial. |
2000 | | * @param dec_weight_ideal_value The ideal decimated unquantized weight values. |
2001 | | * @param max_weight_quant The maximum block mode weight quantization allowed. |
2002 | | * @param[out] tmpbuf Preallocated scratch buffers for the compressor. |
2003 | | */ |
2004 | | void compute_angular_endpoints_2planes( |
2005 | | const block_size_descriptor& bsd, |
2006 | | const float* dec_weight_ideal_value, |
2007 | | unsigned int max_weight_quant, |
2008 | | compression_working_buffers& tmpbuf); |
2009 | | |
2010 | | /* ============================================================================ |
2011 | | Functionality for high level compression and decompression access. |
2012 | | ============================================================================ */ |
2013 | | |
2014 | | /** |
2015 | | * @brief Compress an image block into a physical block. |
2016 | | * |
2017 | | * @param ctx The compressor context and configuration. |
2018 | | * @param blk The image block color data to compress. |
2019 | | * @param[out] pcb The physical compressed block output. |
2020 | | * @param[out] tmpbuf Preallocated scratch buffers for the compressor. |
2021 | | */ |
2022 | | void compress_block( |
2023 | | const astcenc_contexti& ctx, |
2024 | | const image_block& blk, |
2025 | | uint8_t pcb[16], |
2026 | | compression_working_buffers& tmpbuf); |
2027 | | |
2028 | | /** |
2029 | | * @brief Decompress a symbolic block in to an image block. |
2030 | | * |
2031 | | * @param decode_mode The decode mode (LDR, HDR, etc). |
2032 | | * @param bsd The block size information. |
2033 | | * @param xpos The X coordinate of the block in the overall image. |
2034 | | * @param ypos The Y coordinate of the block in the overall image. |
2035 | | * @param zpos The Z coordinate of the block in the overall image. |
2036 | | * @param[out] blk The decompressed image block color data. |
2037 | | */ |
2038 | | void decompress_symbolic_block( |
2039 | | astcenc_profile decode_mode, |
2040 | | const block_size_descriptor& bsd, |
2041 | | int xpos, |
2042 | | int ypos, |
2043 | | int zpos, |
2044 | | const symbolic_compressed_block& scb, |
2045 | | image_block& blk); |
2046 | | |
2047 | | /** |
2048 | | * @brief Compute the error between a symbolic block and the original input data. |
2049 | | * |
2050 | | * This function is specialized for 2 plane and 1 partition search. |
2051 | | * |
2052 | | * In RGBM mode this will reject blocks that attempt to encode a zero M value. |
2053 | | * |
2054 | | * @param config The compressor config. |
2055 | | * @param bsd The block size information. |
2056 | | * @param scb The symbolic compressed encoding. |
2057 | | * @param blk The original image block color data. |
2058 | | * |
2059 | | * @return Returns the computed error, or a negative value if the encoding |
2060 | | * should be rejected for any reason. |
2061 | | */ |
2062 | | float compute_symbolic_block_difference_2plane( |
2063 | | const astcenc_config& config, |
2064 | | const block_size_descriptor& bsd, |
2065 | | const symbolic_compressed_block& scb, |
2066 | | const image_block& blk); |
2067 | | |
2068 | | /** |
2069 | | * @brief Compute the error between a symbolic block and the original input data. |
2070 | | * |
2071 | | * This function is specialized for 1 plane and N partition search. |
2072 | | * |
2073 | | * In RGBM mode this will reject blocks that attempt to encode a zero M value. |
2074 | | * |
2075 | | * @param config The compressor config. |
2076 | | * @param bsd The block size information. |
2077 | | * @param scb The symbolic compressed encoding. |
2078 | | * @param blk The original image block color data. |
2079 | | * |
2080 | | * @return Returns the computed error, or a negative value if the encoding |
2081 | | * should be rejected for any reason. |
2082 | | */ |
2083 | | float compute_symbolic_block_difference_1plane( |
2084 | | const astcenc_config& config, |
2085 | | const block_size_descriptor& bsd, |
2086 | | const symbolic_compressed_block& scb, |
2087 | | const image_block& blk); |
2088 | | |
2089 | | /** |
2090 | | * @brief Compute the error between a symbolic block and the original input data. |
2091 | | * |
2092 | | * This function is specialized for 1 plane and 1 partition search. |
2093 | | * |
2094 | | * In RGBM mode this will reject blocks that attempt to encode a zero M value. |
2095 | | * |
2096 | | * @param config The compressor config. |
2097 | | * @param bsd The block size information. |
2098 | | * @param scb The symbolic compressed encoding. |
2099 | | * @param blk The original image block color data. |
2100 | | * |
2101 | | * @return Returns the computed error, or a negative value if the encoding |
2102 | | * should be rejected for any reason. |
2103 | | */ |
2104 | | float compute_symbolic_block_difference_1plane_1partition( |
2105 | | const astcenc_config& config, |
2106 | | const block_size_descriptor& bsd, |
2107 | | const symbolic_compressed_block& scb, |
2108 | | const image_block& blk); |
2109 | | |
2110 | | /** |
2111 | | * @brief Convert a symbolic representation into a binary physical encoding. |
2112 | | * |
2113 | | * It is assumed that the symbolic encoding is valid and encodable, or |
2114 | | * previously flagged as an error block if an error color it to be encoded. |
2115 | | * |
2116 | | * @param bsd The block size information. |
2117 | | * @param scb The symbolic representation. |
2118 | | * @param[out] pcb The physical compressed block output. |
2119 | | */ |
2120 | | void symbolic_to_physical( |
2121 | | const block_size_descriptor& bsd, |
2122 | | const symbolic_compressed_block& scb, |
2123 | | uint8_t pcb[16]); |
2124 | | |
2125 | | /** |
2126 | | * @brief Convert a binary physical encoding into a symbolic representation. |
2127 | | * |
2128 | | * This function can cope with arbitrary input data; output blocks will be |
2129 | | * flagged as an error block if the encoding is invalid. |
2130 | | * |
2131 | | * @param bsd The block size information. |
2132 | | * @param pcb The physical compresesd block input. |
2133 | | * @param[out] scb The output symbolic representation. |
2134 | | */ |
2135 | | void physical_to_symbolic( |
2136 | | const block_size_descriptor& bsd, |
2137 | | const uint8_t pcb[16], |
2138 | | symbolic_compressed_block& scb); |
2139 | | |
2140 | | /* ============================================================================ |
2141 | | Platform-specific functions. |
2142 | | ============================================================================ */ |
2143 | | /** |
2144 | | * @brief Allocate an aligned memory buffer. |
2145 | | * |
2146 | | * Allocated memory must be freed by aligned_free; |
2147 | | * |
2148 | | * @param size The desired buffer size. |
2149 | | * @param align The desired buffer alignment; must be 2^N. |
2150 | | * |
2151 | | * @return The memory buffer pointer or nullptr on allocation failure. |
2152 | | */ |
2153 | | template<typename T> |
2154 | | T* aligned_malloc(size_t size, size_t align) |
2155 | | { |
2156 | | void* ptr; |
2157 | | int error = 0; |
2158 | | |
2159 | | #if defined(_WIN32) |
2160 | | ptr = _aligned_malloc(size, align); |
2161 | | #else |
2162 | | error = posix_memalign(&ptr, align, size); |
2163 | | #endif |
2164 | | |
2165 | | if (error || (!ptr)) |
2166 | | { |
2167 | | return nullptr; |
2168 | | } |
2169 | | |
2170 | | return static_cast<T*>(ptr); |
2171 | | } |
2172 | | |
2173 | | /** |
2174 | | * @brief Free an aligned memory buffer. |
2175 | | * |
2176 | | * @param ptr The buffer to free. |
2177 | | */ |
2178 | | template<typename T> |
2179 | | void aligned_free(T* ptr) |
2180 | | { |
2181 | | #if defined(_WIN32) |
2182 | | _aligned_free(ptr); |
2183 | | #else |
2184 | | free(ptr); |
2185 | | #endif |
2186 | | } |
2187 | | |
2188 | | #endif |