/src/astc-encoder/Source/astcenc_partition_tables.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // ---------------------------------------------------------------------------- |
3 | | // Copyright 2011-2026 Arm Limited |
4 | | // |
5 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
6 | | // use this file except in compliance with the License. You may obtain a copy |
7 | | // of the License at: |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, software |
12 | | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
13 | | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
14 | | // License for the specific language governing permissions and limitations |
15 | | // under the License. |
16 | | // ---------------------------------------------------------------------------- |
17 | | |
18 | | /** |
19 | | * @brief Functions for generating partition tables on demand. |
20 | | */ |
21 | | |
22 | | #include "astcenc_internal.h" |
23 | | |
24 | | /** @brief The number of 64-bit words needed to represent a canonical partition bit pattern. */ |
25 | 6.13G | #define BIT_PATTERN_WORDS (((ASTCENC_BLOCK_MAX_TEXELS * 2) + 63) / 64) |
26 | | |
27 | | /** |
28 | | * @brief Generate a canonical representation of a partition pattern. |
29 | | * |
30 | | * The returned value stores two bits per texel, for up to 6x6x6 texels, where the two bits store |
31 | | * the remapped texel index. Remapping ensures that we only match on the partition pattern, |
32 | | * independent of the partition order generated by the hash. |
33 | | * |
34 | | * @param texel_count The number of texels in the block. |
35 | | * @param partition_of_texel The partition assignments, in hash order. |
36 | | * @param[out] bit_pattern The output bit pattern representation. |
37 | | */ |
38 | | static void generate_canonical_partitioning( |
39 | | unsigned int texel_count, |
40 | | const uint8_t* partition_of_texel, |
41 | | uint64_t bit_pattern[BIT_PATTERN_WORDS] |
42 | 9.13M | ) { |
43 | | // Clear the pattern |
44 | 73.0M | for (unsigned int i = 0; i < BIT_PATTERN_WORDS; i++) |
45 | 63.9M | { |
46 | 63.9M | bit_pattern[i] = 0; |
47 | 63.9M | } |
48 | | |
49 | | // Store a mapping to reorder the raw partitions so that the partitions are ordered such |
50 | | // that the lowest texel index in partition N is smaller than the lowest texel index in |
51 | | // partition N + 1. |
52 | 9.13M | int mapped_index[BLOCK_MAX_PARTITIONS]; |
53 | 9.13M | int map_weight_count = 0; |
54 | | |
55 | 45.6M | for (unsigned int i = 0; i < BLOCK_MAX_PARTITIONS; i++) |
56 | 36.5M | { |
57 | 36.5M | mapped_index[i] = -1; |
58 | 36.5M | } |
59 | | |
60 | 373M | for (unsigned int i = 0; i < texel_count; i++) |
61 | 364M | { |
62 | 364M | int index = partition_of_texel[i]; |
63 | 364M | if (mapped_index[index] < 0) |
64 | 22.3M | { |
65 | 22.3M | mapped_index[index] = map_weight_count++; |
66 | 22.3M | } |
67 | | |
68 | 364M | uint64_t xlat_index = mapped_index[index]; |
69 | 364M | bit_pattern[i >> 5] |= xlat_index << (2 * (i & 0x1F)); |
70 | 364M | } |
71 | 9.13M | } |
72 | | |
73 | | /** |
74 | | * @brief Compare two canonical patterns to see if they are the same. |
75 | | * |
76 | | * @param part1 The first canonical bit pattern to check. |
77 | | * @param part2 The second canonical bit pattern to check. |
78 | | * |
79 | | * @return @c true if the patterns are the same, @c false otherwise. |
80 | | */ |
81 | | static bool compare_canonical_partitionings( |
82 | | const uint64_t part1[BIT_PATTERN_WORDS], |
83 | | const uint64_t part2[BIT_PATTERN_WORDS] |
84 | 3.02G | ) { |
85 | 3.02G | return (part1[0] == part2[0]) |
86 | 16.5M | #if BIT_PATTERN_WORDS > 1 |
87 | 16.5M | && (part1[1] == part2[1]) |
88 | 5.08M | #endif |
89 | 5.08M | #if BIT_PATTERN_WORDS > 2 |
90 | 5.08M | && (part1[2] == part2[2]) |
91 | 3.53M | #endif |
92 | 3.53M | #if BIT_PATTERN_WORDS > 3 |
93 | 3.53M | && (part1[3] == part2[3]) |
94 | 3.11M | #endif |
95 | 3.11M | #if BIT_PATTERN_WORDS > 4 |
96 | 3.11M | && (part1[4] == part2[4]) |
97 | 3.04M | #endif |
98 | 3.04M | #if BIT_PATTERN_WORDS > 5 |
99 | 3.04M | && (part1[5] == part2[5]) |
100 | 3.04M | #endif |
101 | 3.04M | #if BIT_PATTERN_WORDS > 6 |
102 | 3.04M | && (part1[6] == part2[6]) |
103 | 3.02G | #endif |
104 | 3.02G | ; |
105 | 3.02G | } |
106 | | |
107 | | /** |
108 | | * @brief Hash function used for procedural partition assignment. |
109 | | * |
110 | | * @param inp The hash seed. |
111 | | * |
112 | | * @return The hashed value. |
113 | | */ |
114 | | static uint32_t hash52( |
115 | | uint32_t inp |
116 | 512M | ) { |
117 | 512M | inp ^= inp >> 15; |
118 | | |
119 | | // (2^4 + 1) * (2^7 + 1) * (2^17 - 1) |
120 | 512M | inp *= 0xEEDE0891; |
121 | 512M | inp ^= inp >> 5; |
122 | 512M | inp += inp << 16; |
123 | 512M | inp ^= inp >> 7; |
124 | 512M | inp ^= inp >> 3; |
125 | 512M | inp ^= inp << 6; |
126 | 512M | inp ^= inp >> 17; |
127 | 512M | return inp; |
128 | 512M | } |
129 | | |
130 | | /** |
131 | | * @brief Select texel assignment for a single coordinate. |
132 | | * |
133 | | * @param seed The seed - the partition index from the block. |
134 | | * @param x The texel X coordinate in the block. |
135 | | * @param y The texel Y coordinate in the block. |
136 | | * @param z The texel Z coordinate in the block. |
137 | | * @param partition_count The total partition count of this encoding. |
138 | | * @param small_block @c true if the block has fewer than 32 texels. |
139 | | * |
140 | | * @return The assigned partition index for this texel. |
141 | | */ |
142 | | static uint8_t select_partition( |
143 | | int seed, |
144 | | int x, |
145 | | int y, |
146 | | int z, |
147 | | int partition_count, |
148 | | bool small_block |
149 | 512M | ) { |
150 | | // For small blocks bias the coordinates to get better distribution |
151 | 512M | if (small_block) |
152 | 174M | { |
153 | 174M | x <<= 1; |
154 | 174M | y <<= 1; |
155 | 174M | z <<= 1; |
156 | 174M | } |
157 | | |
158 | 512M | seed += (partition_count - 1) * 1024; |
159 | | |
160 | 512M | uint32_t rnum = hash52(seed); |
161 | | |
162 | 512M | uint8_t seed1 = rnum & 0xF; |
163 | 512M | uint8_t seed2 = (rnum >> 4) & 0xF; |
164 | 512M | uint8_t seed3 = (rnum >> 8) & 0xF; |
165 | 512M | uint8_t seed4 = (rnum >> 12) & 0xF; |
166 | 512M | uint8_t seed5 = (rnum >> 16) & 0xF; |
167 | 512M | uint8_t seed6 = (rnum >> 20) & 0xF; |
168 | 512M | uint8_t seed7 = (rnum >> 24) & 0xF; |
169 | 512M | uint8_t seed8 = (rnum >> 28) & 0xF; |
170 | 512M | uint8_t seed9 = (rnum >> 18) & 0xF; |
171 | 512M | uint8_t seed10 = (rnum >> 22) & 0xF; |
172 | 512M | uint8_t seed11 = (rnum >> 26) & 0xF; |
173 | 512M | uint8_t seed12 = ((rnum >> 30) | (rnum << 2)) & 0xF; |
174 | | |
175 | | // Squaring all the seeds in order to bias their distribution towards lower values. |
176 | 512M | seed1 *= seed1; |
177 | 512M | seed2 *= seed2; |
178 | 512M | seed3 *= seed3; |
179 | 512M | seed4 *= seed4; |
180 | 512M | seed5 *= seed5; |
181 | 512M | seed6 *= seed6; |
182 | 512M | seed7 *= seed7; |
183 | 512M | seed8 *= seed8; |
184 | 512M | seed9 *= seed9; |
185 | 512M | seed10 *= seed10; |
186 | 512M | seed11 *= seed11; |
187 | 512M | seed12 *= seed12; |
188 | | |
189 | 512M | int sh1, sh2; |
190 | 512M | if (seed & 1) |
191 | 254M | { |
192 | 254M | sh1 = (seed & 2 ? 4 : 5); |
193 | 254M | sh2 = (partition_count == 3 ? 6 : 5); |
194 | 254M | } |
195 | 257M | else |
196 | 257M | { |
197 | 257M | sh1 = (partition_count == 3 ? 6 : 5); |
198 | 257M | sh2 = (seed & 2 ? 4 : 5); |
199 | 257M | } |
200 | | |
201 | 512M | int sh3 = (seed & 0x10) ? sh1 : sh2; |
202 | | |
203 | 512M | seed1 >>= sh1; |
204 | 512M | seed2 >>= sh2; |
205 | 512M | seed3 >>= sh1; |
206 | 512M | seed4 >>= sh2; |
207 | 512M | seed5 >>= sh1; |
208 | 512M | seed6 >>= sh2; |
209 | 512M | seed7 >>= sh1; |
210 | 512M | seed8 >>= sh2; |
211 | | |
212 | 512M | seed9 >>= sh3; |
213 | 512M | seed10 >>= sh3; |
214 | 512M | seed11 >>= sh3; |
215 | 512M | seed12 >>= sh3; |
216 | | |
217 | 512M | int a = seed1 * x + seed2 * y + seed11 * z + (rnum >> 14); |
218 | 512M | int b = seed3 * x + seed4 * y + seed12 * z + (rnum >> 10); |
219 | 512M | int c = seed5 * x + seed6 * y + seed9 * z + (rnum >> 6); |
220 | 512M | int d = seed7 * x + seed8 * y + seed10 * z + (rnum >> 2); |
221 | | |
222 | | // Apply the saw |
223 | 512M | a &= 0x3F; |
224 | 512M | b &= 0x3F; |
225 | 512M | c &= 0x3F; |
226 | 512M | d &= 0x3F; |
227 | | |
228 | | // Remove some of the components if we are to output < 4 partitions. |
229 | 512M | if (partition_count <= 3) |
230 | 342M | { |
231 | 342M | d = 0; |
232 | 342M | } |
233 | | |
234 | 512M | if (partition_count <= 2) |
235 | 162M | { |
236 | 162M | c = 0; |
237 | 162M | } |
238 | | |
239 | 512M | if (partition_count <= 1) |
240 | 129k | { |
241 | 129k | b = 0; |
242 | 129k | } |
243 | | |
244 | 512M | uint8_t partition; |
245 | 512M | if (a >= b && a >= c && a >= d) |
246 | 203M | { |
247 | 203M | partition = 0; |
248 | 203M | } |
249 | 308M | else if (b >= c && b >= d) |
250 | 155M | { |
251 | 155M | partition = 1; |
252 | 155M | } |
253 | 152M | else if (c >= d) |
254 | 110M | { |
255 | 110M | partition = 2; |
256 | 110M | } |
257 | 42.6M | else |
258 | 42.6M | { |
259 | 42.6M | partition = 3; |
260 | 42.6M | } |
261 | | |
262 | 512M | return partition; |
263 | 512M | } |
264 | | |
265 | | /** |
266 | | * @brief Generate a single partition info structure. |
267 | | * |
268 | | * @param[out] bsd The block size information. |
269 | | * @param partition_count The partition count of this partitioning. |
270 | | * @param partition_index The partition index / seed of this partitioning. |
271 | | * @param partition_remap_index The remapped partition index of this partitioning. |
272 | | * @param[out] pi The partition info structure to populate. |
273 | | * |
274 | | * @return True if this is a useful partition index, False if we can skip it. |
275 | | */ |
276 | | static bool generate_one_partition_info_entry( |
277 | | block_size_descriptor& bsd, |
278 | | unsigned int partition_count, |
279 | | unsigned int partition_index, |
280 | | unsigned int partition_remap_index, |
281 | | partition_info& pi |
282 | 13.5M | ) { |
283 | | #if defined(ASTCENC_DECOMPRESS_ONLY) |
284 | | // Suppress unused parameter warning |
285 | | (void)partition_remap_index; |
286 | | #endif |
287 | | |
288 | 13.5M | int texels_per_block = bsd.texel_count; |
289 | 13.5M | bool small_block = texels_per_block < 32; |
290 | | |
291 | 13.5M | uint8_t *partition_of_texel = pi.partition_of_texel; |
292 | | |
293 | | // Assign texels to partitions |
294 | 13.5M | int texel_idx = 0; |
295 | 13.5M | int counts[BLOCK_MAX_PARTITIONS] { 0 }; |
296 | 27.1M | for (unsigned int z = 0; z < bsd.dim_z; z++) |
297 | 13.6M | { |
298 | 86.6M | for (unsigned int y = 0; y < bsd.dim_y; y++) |
299 | 73.0M | { |
300 | 585M | for (unsigned int x = 0; x < bsd.dim_x; x++) |
301 | 512M | { |
302 | 512M | uint8_t part = select_partition(partition_index, x, y, z, partition_count, small_block); |
303 | 512M | pi.texels_of_partition[part][counts[part]++] = static_cast<uint8_t>(texel_idx++); |
304 | 512M | *partition_of_texel++ = part; |
305 | 512M | } |
306 | 73.0M | } |
307 | 13.6M | } |
308 | | |
309 | | // Fill loop tail so we can overfetch later |
310 | 54.4M | for (unsigned int i = 0; i < partition_count; i++) |
311 | 40.8M | { |
312 | 40.8M | size_t ptex_count = counts[i]; |
313 | 40.8M | size_t ptex_count_simd = round_up_to_simd_multiple_vla(ptex_count); |
314 | 81.1M | for (size_t j = ptex_count; j < ptex_count_simd; j++) |
315 | 40.2M | { |
316 | 40.2M | pi.texels_of_partition[i][j] = pi.texels_of_partition[i][ptex_count - 1]; |
317 | 40.2M | } |
318 | 40.8M | } |
319 | | |
320 | | // Populate the actual procedural partition count |
321 | 13.5M | if (counts[0] == 0) |
322 | 2.62M | { |
323 | 2.62M | pi.partition_count = 0; |
324 | 2.62M | } |
325 | 10.9M | else if (counts[1] == 0) |
326 | 3.09M | { |
327 | 3.09M | pi.partition_count = 1; |
328 | 3.09M | } |
329 | 7.86M | else if (counts[2] == 0) |
330 | 4.53M | { |
331 | 4.53M | pi.partition_count = 2; |
332 | 4.53M | } |
333 | 3.32M | else if (counts[3] == 0) |
334 | 2.06M | { |
335 | 2.06M | pi.partition_count = 3; |
336 | 2.06M | } |
337 | 1.26M | else |
338 | 1.26M | { |
339 | 1.26M | pi.partition_count = 4; |
340 | 1.26M | } |
341 | | |
342 | | // Populate the partition index |
343 | 13.5M | pi.partition_index = static_cast<uint16_t>(partition_index); |
344 | | |
345 | 67.9M | for (unsigned int i = 0; i < BLOCK_MAX_PARTITIONS; i++) |
346 | 54.3M | { |
347 | 54.3M | pi.partition_texel_count[i] = static_cast<uint8_t>(counts[i]); |
348 | 54.3M | } |
349 | | |
350 | | // Valid partitionings have texels in all of the requested partitions |
351 | 13.5M | bool valid = pi.partition_count == partition_count; |
352 | | |
353 | 13.5M | #if !defined(ASTCENC_DECOMPRESS_ONLY) |
354 | | // Populate the coverage bitmaps for 2/3/4 partitions |
355 | 13.5M | uint64_t* bitmaps { nullptr }; |
356 | 13.5M | if (partition_count == 2) |
357 | 4.40M | { |
358 | 4.40M | bitmaps = bsd.coverage_bitmaps_2[partition_remap_index]; |
359 | 4.40M | } |
360 | 9.18M | else if (partition_count == 3) |
361 | 4.70M | { |
362 | 4.70M | bitmaps = bsd.coverage_bitmaps_3[partition_remap_index]; |
363 | 4.70M | } |
364 | 4.48M | else if (partition_count == 4) |
365 | 4.47M | { |
366 | 4.47M | bitmaps = bsd.coverage_bitmaps_4[partition_remap_index]; |
367 | 4.47M | } |
368 | | |
369 | 13.5M | if (bitmaps) |
370 | 13.5M | { |
371 | | // Populate the partition coverage bitmap |
372 | 54.4M | for (unsigned int i = 0; i < partition_count; i++) |
373 | 40.8M | { |
374 | 40.8M | bitmaps[i] = 0ULL; |
375 | 40.8M | } |
376 | | |
377 | 13.5M | unsigned int texels_to_process = astc::min(bsd.texel_count, BLOCK_MAX_KMEANS_TEXELS); |
378 | 426M | for (unsigned int i = 0; i < texels_to_process; i++) |
379 | 413M | { |
380 | 413M | unsigned int idx = bsd.kmeans_texels[i]; |
381 | 413M | bitmaps[pi.partition_of_texel[idx]] |= 1ULL << i; |
382 | 413M | } |
383 | 13.5M | } |
384 | 13.5M | #endif |
385 | | |
386 | 13.5M | return valid; |
387 | 13.5M | } |
388 | | |
389 | | static void build_partition_table_for_one_partition_count( |
390 | | block_size_descriptor& bsd, |
391 | | bool can_omit_partitionings, |
392 | | unsigned int partition_count_cutoff, |
393 | | unsigned int partition_count, |
394 | | partition_info* ptab, |
395 | | uint64_t* canonical_patterns |
396 | 10.2k | ) { |
397 | 10.2k | unsigned int next_index = 0; |
398 | 10.2k | bsd.partitioning_count_selected[partition_count - 1] = 0; |
399 | 10.2k | bsd.partitioning_count_all[partition_count - 1] = 0; |
400 | | |
401 | | // Mark all partitionings as unused; the loops below overwrite the entries |
402 | | // that are actually kept. Partitionings dropped in self-decompress mode |
403 | | // must retain this known-bad value so that decoding an unknown raw |
404 | | // partition index does not result in a bad packed index. |
405 | 10.4M | for (unsigned int i = 0; i < BLOCK_MAX_PARTITIONINGS; i++) |
406 | 10.4M | { |
407 | 10.4M | bsd.partitioning_packed_index[partition_count - 2][i] = BLOCK_BAD_PARTITIONING; |
408 | 10.4M | } |
409 | | |
410 | | // Skip tables larger than config max partition count if we can omit modes |
411 | 10.2k | if (can_omit_partitionings && (partition_count > partition_count_cutoff)) |
412 | 346 | { |
413 | 346 | return; |
414 | 346 | } |
415 | | |
416 | | // Iterate through twice |
417 | | // - Pass 0: Keep selected partitionings |
418 | | // - Pass 1: Keep non-selected partitionings (skip if in omit mode) |
419 | 9.85k | unsigned int max_iter = can_omit_partitionings ? 1 : 2; |
420 | | |
421 | | // Tracker for things we built in the first iteration |
422 | 9.85k | uint8_t build[BLOCK_MAX_PARTITIONINGS] { 0 }; |
423 | 26.1k | for (unsigned int x = 0; x < max_iter; x++) |
424 | 16.3k | { |
425 | 16.7M | for (unsigned int i = 0; i < BLOCK_MAX_PARTITIONINGS; i++) |
426 | 16.7M | { |
427 | | // Don't include things we built in the first pass |
428 | 16.7M | if ((x == 1) && build[i]) |
429 | 3.13M | { |
430 | 3.13M | continue; |
431 | 3.13M | } |
432 | | |
433 | 13.5M | bool keep_useful = generate_one_partition_info_entry(bsd, partition_count, i, next_index, ptab[next_index]); |
434 | 13.5M | if ((x == 0) && !keep_useful) |
435 | 4.44M | { |
436 | 4.44M | continue; |
437 | 4.44M | } |
438 | | |
439 | 9.13M | generate_canonical_partitioning(bsd.texel_count, ptab[next_index].partition_of_texel, canonical_patterns + next_index * BIT_PATTERN_WORDS); |
440 | 9.13M | bool keep_canonical = true; |
441 | 3.03G | for (unsigned int j = 0; j < next_index; j++) |
442 | 3.02G | { |
443 | 3.02G | bool match = compare_canonical_partitionings(canonical_patterns + next_index * BIT_PATTERN_WORDS, canonical_patterns + j * BIT_PATTERN_WORDS); |
444 | 3.02G | if (match) |
445 | 3.04M | { |
446 | 3.04M | keep_canonical = false; |
447 | 3.04M | break; |
448 | 3.04M | } |
449 | 3.02G | } |
450 | | |
451 | 9.13M | if (keep_useful && keep_canonical) |
452 | 4.56M | { |
453 | 4.56M | if (x == 0) |
454 | 4.56M | { |
455 | 4.56M | bsd.partitioning_packed_index[partition_count - 2][i] = static_cast<uint16_t>(next_index); |
456 | 4.56M | bsd.partitioning_count_selected[partition_count - 1]++; |
457 | 4.56M | bsd.partitioning_count_all[partition_count - 1]++; |
458 | 4.56M | build[i] = 1; |
459 | 4.56M | next_index++; |
460 | 4.56M | } |
461 | 4.56M | } |
462 | 4.56M | else |
463 | 4.56M | { |
464 | 4.56M | if (x == 1) |
465 | 3.49M | { |
466 | 3.49M | bsd.partitioning_packed_index[partition_count - 2][i] = static_cast<uint16_t>(next_index); |
467 | 3.49M | bsd.partitioning_count_all[partition_count - 1]++; |
468 | 3.49M | next_index++; |
469 | 3.49M | } |
470 | 4.56M | } |
471 | 9.13M | } |
472 | 16.3k | } |
473 | 9.85k | } |
474 | | |
475 | | /* See header for documentation. */ |
476 | | void init_partition_tables( |
477 | | block_size_descriptor& bsd, |
478 | | bool can_omit_partitionings, |
479 | | unsigned int partition_count_cutoff |
480 | 3.40k | ) { |
481 | 3.40k | partition_info* par_tab2 = bsd.partitionings; |
482 | 3.40k | partition_info* par_tab3 = par_tab2 + BLOCK_MAX_PARTITIONINGS; |
483 | 3.40k | partition_info* par_tab4 = par_tab3 + BLOCK_MAX_PARTITIONINGS; |
484 | 3.40k | partition_info* par_tab1 = par_tab4 + BLOCK_MAX_PARTITIONINGS; |
485 | | |
486 | 3.40k | generate_one_partition_info_entry(bsd, 1, 0, 0, *par_tab1); |
487 | 3.40k | bsd.partitioning_count_selected[0] = 1; |
488 | 3.40k | bsd.partitioning_count_all[0] = 1; |
489 | | |
490 | 3.40k | uint64_t* canonical_patterns = new uint64_t[BLOCK_MAX_PARTITIONINGS * BIT_PATTERN_WORDS]; |
491 | | |
492 | 3.40k | build_partition_table_for_one_partition_count(bsd, can_omit_partitionings, partition_count_cutoff, 2, par_tab2, canonical_patterns); |
493 | 3.40k | build_partition_table_for_one_partition_count(bsd, can_omit_partitionings, partition_count_cutoff, 3, par_tab3, canonical_patterns); |
494 | 3.40k | build_partition_table_for_one_partition_count(bsd, can_omit_partitionings, partition_count_cutoff, 4, par_tab4, canonical_patterns); |
495 | | |
496 | 3.40k | delete[] canonical_patterns; |
497 | 3.40k | } |