Coverage Report

Created: 2026-10-28 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/astc-encoder/Source/astcenc_entry.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 the library entrypoint.
20
 */
21
22
#include <array>
23
#include <cstring>
24
#include <new>
25
26
#include "astcenc.h"
27
#include "astcenc_diagnostic_trace.h"
28
#include "astcenc_internal_entry.h"
29
#include "astcenc_mathlib.h"
30
31
/**
32
 * @brief Record of the quality tuning parameter values.
33
 *
34
 * See the @c astcenc_config structure for detailed parameter documentation.
35
 *
36
 * Note that the mse_overshoot entries are scaling factors relative to the base MSE to hit db_limit.
37
 * A 20% overshoot is harder to hit for a higher base db_limit, so we may actually use lower ratios
38
 * for the more through search presets because the underlying db_limit is so much higher.
39
 */
40
struct astcenc_preset_config
41
{
42
  float quality;
43
  unsigned int tune_partition_count_limit;
44
  unsigned int tune_2partition_index_limit;
45
  unsigned int tune_3partition_index_limit;
46
  unsigned int tune_4partition_index_limit;
47
  unsigned int tune_block_mode_limit;
48
  unsigned int tune_refinement_limit;
49
  unsigned int tune_candidate_limit;
50
  unsigned int tune_2partitioning_candidate_limit;
51
  unsigned int tune_3partitioning_candidate_limit;
52
  unsigned int tune_4partitioning_candidate_limit;
53
  float tune_db_limit_a_base;
54
  float tune_db_limit_b_base;
55
  float tune_mse_overshoot;
56
  float tune_2partition_early_out_limit_factor;
57
  float tune_3partition_early_out_limit_factor;
58
  float tune_2plane_early_out_limit_correlation;
59
  float tune_search_mode0_enable;
60
};
61
62
/**
63
 * @brief The static presets for high bandwidth encodings (x < 25 texels per block).
64
 */
65
static const std::array<astcenc_preset_config, 6> preset_configs_high {{
66
  {
67
    ASTCENC_PRE_FASTEST,
68
    2,  10,   6,   4,  43, 2, 2, 2, 2, 2,  85.2f,  63.2f,  3.5f, 1.00f, 1.00f, 0.85f, 0.0f
69
  }, {
70
    ASTCENC_PRE_FAST,
71
    3,  18,  10,   8,  55, 3, 3, 2, 2, 2,  85.2f,  63.2f,  3.5f, 1.00f, 1.00f, 0.90f, 0.0f
72
  }, {
73
    ASTCENC_PRE_MEDIUM,
74
    4,  34,  28,  16,  77, 3, 3, 2, 2, 2,  95.0f,  70.0f,  2.5f, 1.10f, 1.05f, 0.95f, 0.0f
75
  }, {
76
    ASTCENC_PRE_THOROUGH,
77
    4,  82,  60,  30,  94, 4, 4, 3, 2, 2, 105.0f,  77.0f, 10.0f, 1.35f, 1.15f, 0.97f, 0.0f
78
  }, {
79
    ASTCENC_PRE_VERYTHOROUGH,
80
    4, 256, 128,  64,  98, 4, 6, 8, 6, 4, 200.0f, 200.0f, 10.0f, 1.60f, 1.40f, 0.98f, 0.0f
81
  }, {
82
    ASTCENC_PRE_EXHAUSTIVE,
83
    4, 512, 512, 512, 100, 4, 8, 8, 8, 8, 200.0f, 200.0f, 10.0f, 2.00f, 2.00f, 0.99f, 0.0f
84
  }
85
}};
86
87
/**
88
 * @brief The static presets for medium bandwidth encodings (25 <= x < 64 texels per block).
89
 */
90
static const std::array<astcenc_preset_config, 6> preset_configs_mid {{
91
  {
92
    ASTCENC_PRE_FASTEST,
93
    2,  10,   6,   4,  43, 2, 2, 2, 2, 2,  85.2f,  63.2f,  3.5f, 1.00f, 1.00f, 0.80f, 1.0f
94
  }, {
95
    ASTCENC_PRE_FAST,
96
    3,  18,  12,  10,  55, 3, 3, 2, 2, 2,  85.2f,  63.2f,  3.5f, 1.00f, 1.00f, 0.85f, 1.0f
97
  }, {
98
    ASTCENC_PRE_MEDIUM,
99
    3,  34,  28,  16,  77, 3, 3, 2, 2, 2,  95.0f,  70.0f,  3.0f, 1.10f, 1.05f, 0.90f, 1.0f
100
  }, {
101
    ASTCENC_PRE_THOROUGH,
102
    4,  82,  60,  30,  94, 4, 4, 3, 2, 2, 105.0f,  77.0f, 10.0f, 1.40f, 1.20f, 0.95f, 0.0f
103
  }, {
104
    ASTCENC_PRE_VERYTHOROUGH,
105
    4, 256, 128,  64,  98, 4, 6, 8, 6, 3, 200.0f, 200.0f, 10.0f, 1.60f, 1.40f, 0.98f, 0.0f
106
  }, {
107
    ASTCENC_PRE_EXHAUSTIVE,
108
    4, 256, 256, 256, 100, 4, 8, 8, 8, 8, 200.0f, 200.0f, 10.0f, 2.00f, 2.00f, 0.99f, 0.0f
109
  }
110
}};
111
112
/**
113
 * @brief The static presets for low bandwidth encodings (64 <= x texels per block).
114
 */
115
static const std::array<astcenc_preset_config, 6> preset_configs_low {{
116
  {
117
    ASTCENC_PRE_FASTEST,
118
    2,  10,   6,   4,  40, 2, 2, 2, 2, 2,  85.0f,  63.0f,  3.5f, 1.00f, 1.00f, 0.80f, 1.0f
119
  }, {
120
    ASTCENC_PRE_FAST,
121
    2,  18,  12,  10,  55, 3, 3, 2, 2, 2,  85.0f,  63.0f,  3.5f, 1.00f, 1.00f, 0.85f, 1.0f
122
  }, {
123
    ASTCENC_PRE_MEDIUM,
124
    3,  34,  28,  16,  77, 3, 3, 2, 2, 2,  95.0f,  70.0f,  3.5f, 1.10f, 1.05f, 0.90f, 1.0f
125
  }, {
126
    ASTCENC_PRE_THOROUGH,
127
    4,  82,  60,  30,  93, 4, 4, 3, 2, 2, 105.0f,  77.0f, 10.0f, 1.30f, 1.20f, 0.97f, 1.0f
128
  }, {
129
    ASTCENC_PRE_VERYTHOROUGH,
130
    4, 256, 128,  64,  98, 4, 6, 8, 5, 2, 200.0f, 200.0f, 10.0f, 1.60f, 1.40f, 0.98f, 1.0f
131
  }, {
132
    ASTCENC_PRE_EXHAUSTIVE,
133
    4, 256, 256, 256, 100, 4, 8, 8, 8, 8, 200.0f, 200.0f, 10.0f, 2.00f, 2.00f, 0.99f, 1.0f
134
  }
135
}};
136
137
/**
138
 * @brief Get the total number of texels in an image.
139
 *
140
 * This function validates that the total size would fit in a size_t and returns
141
 * 0 if it does not.
142
 *
143
 * @param texels_x   Number of texels in the X axis.
144
 * @param texels_y   Number of texels in the Y axis.
145
 * @param texels_z   Number of texels in the Z axis.
146
 *
147
 * @return The number of texels in the image, or zero if total size would not
148
 *         fit into a size_t.
149
 */
150
static size_t get_texels_count(
151
  size_t texels_x,
152
  size_t texels_y,
153
  size_t texels_z
154
3.20k
) {
155
3.20k
  bool overflow { false };
156
157
  // Compute texel count
158
3.20k
  size_t texels_count = astc::mul_safe(texels_x, texels_y, overflow);
159
3.20k
  texels_count = astc::mul_safe(texels_count, texels_z, overflow);
160
161
3.20k
  if (overflow)
162
0
  {
163
0
    return 0;
164
0
  }
165
166
3.20k
  return texels_count;
167
3.20k
}
168
169
/**
170
 * @brief Get the total number of blocks in an image.
171
 *
172
 * This function also validates that the total size of the compressed image,
173
 * in bytes, would fit in a size_t.
174
 *
175
 * @param blocks_x   Number of blocks in the X axis.
176
 * @param blocks_y   Number of blocks in the Y axis.
177
 * @param blocks_z   Number of blocks in the Z axis.
178
 *
179
 * @return The number of blocks in the image, or zero if total size would not
180
 *         fit into a size_t.
181
 */
182
static size_t get_blocks_count(
183
  size_t blocks_x,
184
  size_t blocks_y,
185
  size_t blocks_z
186
5.33k
) {
187
5.33k
  bool overflow { false };
188
189
  // Compute block count
190
5.33k
  size_t blocks_count = astc::mul_safe(blocks_x, blocks_y, overflow);
191
5.33k
  blocks_count = astc::mul_safe(blocks_count, blocks_z, overflow);
192
193
  // Also compute byte count, but we only use overflow and not the result
194
5.33k
  astc::mul_safe(blocks_count, 16, overflow);
195
196
5.33k
  if (overflow)
197
0
  {
198
0
    return 0;
199
0
  }
200
201
5.33k
  return blocks_count;
202
5.33k
}
203
204
/**
205
 * @brief Validate CPU floating point meets assumptions made in the codec.
206
 *
207
 * The codec is written with the assumption that float bit patterns are valid
208
 * IEEE754 values that are stored and reloaded with round-to-nearest rounding.
209
 * This is always the case in an IEEE-754 compliant system, however not every
210
 * system or compilation mode is actually IEEE-754 compliant. This normally
211
 * fails if the code is compiled with fast math enabled, for example.
212
 *
213
 * @return Return @c ASTCENC_SUCCESS if validated, an error on failure.
214
 */
215
static astcenc_error validate_cpu_float()
216
6.95k
{
217
6.95k
  volatile float xprec_testval = 2.51f;
218
6.95k
  float store = xprec_testval + 12582912.0f;
219
6.95k
  float q = store - 12582912.0f;
220
221
6.95k
  if (q != 3.0f)
222
0
  {
223
0
    return ASTCENC_ERR_BAD_CPU_FLOAT;
224
0
  }
225
226
6.95k
  return ASTCENC_SUCCESS;
227
6.95k
}
228
229
/**
230
 * @brief Validate config profile.
231
 *
232
 * @param profile   The profile to check.
233
 *
234
 * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
235
 */
236
static astcenc_error validate_profile(
237
  astcenc_profile profile
238
3.45k
) {
239
  // Values in this enum are from an external user, so not guaranteed to be
240
  // bounded to the enum values
241
3.45k
  switch (static_cast<int>(profile))
242
3.45k
  {
243
1.14k
  case ASTCENC_PRF_LDR_SRGB:
244
1.80k
  case ASTCENC_PRF_LDR:
245
2.56k
  case ASTCENC_PRF_HDR_RGB_LDR_A:
246
3.45k
  case ASTCENC_PRF_HDR:
247
3.45k
    return ASTCENC_SUCCESS;
248
0
  default:
249
0
    return ASTCENC_ERR_BAD_PROFILE;
250
3.45k
  }
251
3.45k
}
252
253
/**
254
 * @brief Validate block size.
255
 *
256
 * @param block_x   The block x dimensions.
257
 * @param block_y   The block y dimensions.
258
 * @param block_z   The block z dimensions.
259
 *
260
 * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
261
 */
262
static astcenc_error validate_block_size(
263
  unsigned int block_x,
264
  unsigned int block_y,
265
  unsigned int block_z
266
6.95k
) {
267
  // Test if this is a legal block size at all
268
6.95k
  bool is_legal = (((block_z <= 1) && is_legal_2d_block_size(block_x, block_y)) ||
269
0
                   ((block_z >= 2) && is_legal_3d_block_size(block_x, block_y, block_z)));
270
6.95k
  if (!is_legal)
271
0
  {
272
0
    return ASTCENC_ERR_BAD_BLOCK_SIZE;
273
0
  }
274
275
  // Test if this build has sufficient capacity for this block size
276
6.95k
  bool have_capacity = (block_x * block_y * block_z) <= BLOCK_MAX_TEXELS;
277
6.95k
  if (!have_capacity)
278
0
  {
279
0
    return ASTCENC_ERR_NOT_IMPLEMENTED;
280
0
  }
281
282
6.95k
  return ASTCENC_SUCCESS;
283
6.95k
}
284
285
/**
286
 * @brief Validate flags.
287
 *
288
 * @param profile   The profile to check.
289
 * @param flags     The flags to check.
290
 *
291
 * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
292
 */
293
static astcenc_error validate_flags(
294
  astcenc_profile profile,
295
  unsigned int flags
296
6.95k
) {
297
  // Flags field must not contain any unknown flag bits
298
6.95k
  unsigned int exMask = ~ASTCENC_ALL_FLAGS;
299
6.95k
  if (popcount(flags & exMask) != 0)
300
0
  {
301
0
    return ASTCENC_ERR_BAD_FLAGS;
302
0
  }
303
304
  // Flags field must only contain at most a single map type
305
6.95k
  exMask = ASTCENC_FLG_MAP_NORMAL
306
6.95k
         | ASTCENC_FLG_MAP_RGBM;
307
6.95k
  if (popcount(flags & exMask) > 1)
308
25
  {
309
25
    return ASTCENC_ERR_BAD_FLAGS;
310
25
  }
311
312
  // Decode_unorm8 must only be used with an LDR profile
313
6.93k
  bool is_unorm8 = flags & ASTCENC_FLG_USE_DECODE_UNORM8;
314
6.93k
  bool is_hdr = (profile == ASTCENC_PRF_HDR) || (profile == ASTCENC_PRF_HDR_RGB_LDR_A);
315
6.93k
  if (is_unorm8 && is_hdr)
316
17
  {
317
17
    return ASTCENC_ERR_BAD_DECODE_MODE;
318
17
  }
319
320
6.91k
  return ASTCENC_SUCCESS;
321
6.93k
}
322
323
#if !defined(ASTCENC_DECOMPRESS_ONLY)
324
325
/**
326
 * @brief Validate single channel compression swizzle.
327
 *
328
 * @param swizzle   The swizzle to check.
329
 *
330
 * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
331
 */
332
static astcenc_error validate_compression_swz(
333
  astcenc_swz swizzle
334
8.54k
) {
335
  // Not all enum values are handled; SWZ_Z is invalid for compression
336
8.54k
  switch (static_cast<int>(swizzle))
337
8.54k
  {
338
2.13k
  case ASTCENC_SWZ_R:
339
4.27k
  case ASTCENC_SWZ_G:
340
6.41k
  case ASTCENC_SWZ_B:
341
8.54k
  case ASTCENC_SWZ_A:
342
8.54k
  case ASTCENC_SWZ_0:
343
8.54k
  case ASTCENC_SWZ_1:
344
8.54k
    return ASTCENC_SUCCESS;
345
0
  default:
346
0
    return ASTCENC_ERR_BAD_SWIZZLE;
347
8.54k
  }
348
8.54k
}
349
350
/**
351
 * @brief Validate overall compression swizzle.
352
 *
353
 * @param swizzle   The swizzle to check.
354
 *
355
 * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
356
 */
357
static astcenc_error validate_compression_swizzle(
358
  const astcenc_swizzle& swizzle
359
2.13k
) {
360
2.13k
  if (validate_compression_swz(swizzle.r) ||
361
2.13k
      validate_compression_swz(swizzle.g) ||
362
2.13k
      validate_compression_swz(swizzle.b) ||
363
2.13k
      validate_compression_swz(swizzle.a))
364
0
  {
365
0
    return ASTCENC_ERR_BAD_SWIZZLE;
366
0
  }
367
368
2.13k
  return ASTCENC_SUCCESS;
369
2.13k
}
370
#endif
371
372
/**
373
 * @brief Validate single channel decompression swizzle.
374
 *
375
 * @param swizzle   The swizzle to check.
376
 *
377
 * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
378
 */
379
static astcenc_error validate_decompression_swz(
380
  astcenc_swz swizzle
381
4.25k
) {
382
  // Values in this enum are from an external user, so not guaranteed to be
383
  // bounded to the enum values
384
4.25k
  switch (static_cast<int>(swizzle))
385
4.25k
  {
386
1.06k
  case ASTCENC_SWZ_R:
387
2.12k
  case ASTCENC_SWZ_G:
388
3.18k
  case ASTCENC_SWZ_B:
389
4.25k
  case ASTCENC_SWZ_A:
390
4.25k
  case ASTCENC_SWZ_0:
391
4.25k
  case ASTCENC_SWZ_1:
392
4.25k
  case ASTCENC_SWZ_Z:
393
4.25k
    return ASTCENC_SUCCESS;
394
0
  default:
395
0
    return ASTCENC_ERR_BAD_SWIZZLE;
396
4.25k
  }
397
4.25k
}
398
399
/**
400
 * @brief Validate overall decompression swizzle.
401
 *
402
 * @param swizzle   The swizzle to check.
403
 *
404
 * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
405
 */
406
static astcenc_error validate_decompression_swizzle(
407
  const astcenc_swizzle& swizzle
408
1.06k
) {
409
1.06k
  if (validate_decompression_swz(swizzle.r) ||
410
1.06k
      validate_decompression_swz(swizzle.g) ||
411
1.06k
      validate_decompression_swz(swizzle.b) ||
412
1.06k
      validate_decompression_swz(swizzle.a))
413
0
  {
414
0
    return ASTCENC_ERR_BAD_SWIZZLE;
415
0
  }
416
417
1.06k
  return ASTCENC_SUCCESS;
418
1.06k
}
419
420
/**
421
 * Validate that an incoming configuration is in-spec.
422
 *
423
 * This function can respond in two ways:
424
 *
425
 *   * Numerical inputs that have valid ranges are clamped to those valid ranges. No error is thrown
426
 *     for out-of-range inputs in this case.
427
 *   * Numerical inputs and logic inputs are are logically invalid and which make no sense
428
 *     algorithmically will return an error.
429
 *
430
 * @param[in,out] config   The input compressor configuration.
431
 *
432
 * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
433
 */
434
static astcenc_error validate_config(
435
  astcenc_config &config
436
3.45k
) {
437
3.45k
  astcenc_error status;
438
439
3.45k
  status = validate_profile(config.profile);
440
3.45k
  if (status != ASTCENC_SUCCESS)
441
0
  {
442
0
    return status;
443
0
  }
444
445
3.45k
  status = validate_flags(config.profile, config.flags);
446
3.45k
  if (status != ASTCENC_SUCCESS)
447
0
  {
448
0
    return status;
449
0
  }
450
451
  // A block_z of zero is accepted to mean 2D. Normalize to 1u so later code paths
452
  // can safely use it as a divisor or loop limit
453
3.45k
  config.block_z = astc::max(config.block_z, 1u);
454
455
3.45k
  status = validate_block_size(config.block_x, config.block_y, config.block_z);
456
3.45k
  if (status != ASTCENC_SUCCESS)
457
0
  {
458
0
    return status;
459
0
  }
460
461
#if defined(ASTCENC_DECOMPRESS_ONLY)
462
  // Decompress-only builds only support decompress-only contexts
463
  if (!(config.flags & ASTCENC_FLG_DECOMPRESS_ONLY))
464
  {
465
    return ASTCENC_ERR_BAD_PARAM;
466
  }
467
#endif
468
469
3.45k
  config.rgbm_m_scale = astc::max(config.rgbm_m_scale, 1.0f);
470
471
  // Clamp alpha-scale kernel radius to put an upper bound on memory use
472
3.45k
  config.a_scale_radius = astc::min(config.a_scale_radius, ALPHASCALE_MAX_RADIUS);
473
474
3.45k
  config.tune_partition_count_limit = astc::clamp(config.tune_partition_count_limit, 1u, 4u);
475
3.45k
  config.tune_2partition_index_limit = astc::clamp(config.tune_2partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS);
476
3.45k
  config.tune_3partition_index_limit = astc::clamp(config.tune_3partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS);
477
3.45k
  config.tune_4partition_index_limit = astc::clamp(config.tune_4partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS);
478
3.45k
  config.tune_block_mode_limit = astc::clamp(config.tune_block_mode_limit, 1u, 100u);
479
3.45k
  config.tune_refinement_limit = astc::max(config.tune_refinement_limit, 1u);
480
3.45k
  config.tune_candidate_limit = astc::clamp(config.tune_candidate_limit, 1u, TUNE_MAX_TRIAL_CANDIDATES);
481
3.45k
  config.tune_2partitioning_candidate_limit = astc::clamp(config.tune_2partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIONING_CANDIDATES);
482
3.45k
  config.tune_3partitioning_candidate_limit = astc::clamp(config.tune_3partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIONING_CANDIDATES);
483
3.45k
  config.tune_4partitioning_candidate_limit = astc::clamp(config.tune_4partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIONING_CANDIDATES);
484
3.45k
  config.tune_db_limit = astc::max(config.tune_db_limit, 0.0f);
485
3.45k
  config.tune_mse_overshoot = astc::max(config.tune_mse_overshoot, 1.0f);
486
3.45k
  config.tune_2partition_early_out_limit_factor = astc::max(config.tune_2partition_early_out_limit_factor, 0.0f);
487
3.45k
  config.tune_3partition_early_out_limit_factor = astc::max(config.tune_3partition_early_out_limit_factor, 0.0f);
488
3.45k
  config.tune_2plane_early_out_limit_correlation = astc::max(config.tune_2plane_early_out_limit_correlation, 0.0f);
489
490
  // Specifying a zero weight color component is not allowed; force to small value
491
3.45k
  float max_weight = astc::max(astc::max(config.cw_r_weight, config.cw_g_weight),
492
3.45k
                               astc::max(config.cw_b_weight, config.cw_a_weight));
493
3.45k
  if (max_weight > 0.0f)
494
3.45k
  {
495
3.45k
    max_weight /= 1000.0f;
496
3.45k
    config.cw_r_weight = astc::max(config.cw_r_weight, max_weight);
497
3.45k
    config.cw_g_weight = astc::max(config.cw_g_weight, max_weight);
498
3.45k
    config.cw_b_weight = astc::max(config.cw_b_weight, max_weight);
499
3.45k
    config.cw_a_weight = astc::max(config.cw_a_weight, max_weight);
500
3.45k
  }
501
  // If all color components error weights are zero then return an error
502
0
  else
503
0
  {
504
0
    return ASTCENC_ERR_BAD_PARAM;
505
0
  }
506
507
3.45k
  return ASTCENC_SUCCESS;
508
3.45k
}
509
510
/* See header for documentation. */
511
astcenc_error astcenc_config_init(
512
  astcenc_profile profile,
513
  unsigned int block_x,
514
  unsigned int block_y,
515
  unsigned int block_z,
516
  float quality,
517
  unsigned int flags,
518
  astcenc_config* configp
519
3.49k
) {
520
3.49k
  astcenc_error status;
521
522
3.49k
  status = validate_cpu_float();
523
3.49k
  if (status != ASTCENC_SUCCESS)
524
0
  {
525
0
    return status;
526
0
  }
527
528
  // Zero init all config fields; although most of will be overwritten
529
3.49k
  astcenc_config& config = *configp;
530
3.49k
  std::memset(&config, 0, sizeof(config));
531
532
  // Process the block size
533
  // For 2D blocks Z==0 is accepted, but convert to 1
534
3.49k
  block_z = astc::max(block_z, 1u);
535
3.49k
  status = validate_block_size(block_x, block_y, block_z);
536
3.49k
  if (status != ASTCENC_SUCCESS)
537
0
  {
538
0
    return status;
539
0
  }
540
541
3.49k
  config.block_x = block_x;
542
3.49k
  config.block_y = block_y;
543
3.49k
  config.block_z = block_z;
544
545
3.49k
  float texels = static_cast<float>(block_x * block_y * block_z);
546
3.49k
  float ltexels = logf(texels) / logf(10.0f);
547
548
  // Process the performance quality level or preset; note that this must be done before we
549
  // process any additional settings, such as color profile and flags, which may replace some of
550
  // these settings with more use case tuned values
551
3.49k
  if (quality < ASTCENC_PRE_FASTEST ||
552
3.49k
      quality > ASTCENC_PRE_EXHAUSTIVE)
553
0
  {
554
0
    return ASTCENC_ERR_BAD_QUALITY;
555
0
  }
556
557
3.49k
  static const std::array<astcenc_preset_config, 6>* preset_configs;
558
3.49k
  size_t texels_int = block_x * block_y * block_z;
559
3.49k
  if (texels_int < 25)
560
1.90k
  {
561
1.90k
    preset_configs = &preset_configs_high;
562
1.90k
  }
563
1.59k
  else if (texels_int < 64)
564
957
  {
565
957
    preset_configs = &preset_configs_mid;
566
957
  }
567
637
  else
568
637
  {
569
637
    preset_configs = &preset_configs_low;
570
637
  }
571
572
  // Determine which preset to use, or which pair to interpolate
573
3.49k
  size_t start;
574
3.49k
  size_t end;
575
10.2k
  for (end = 0; end < preset_configs->size(); end++)
576
10.2k
  {
577
10.2k
    if ((*preset_configs)[end].quality >= quality)
578
3.49k
    {
579
3.49k
      break;
580
3.49k
    }
581
10.2k
  }
582
583
3.49k
  start = end == 0 ? 0 : end - 1;
584
585
  // Start and end node are the same - so just transfer the values.
586
3.49k
  if (start == end)
587
144
  {
588
144
    config.tune_partition_count_limit = (*preset_configs)[start].tune_partition_count_limit;
589
144
    config.tune_2partition_index_limit = (*preset_configs)[start].tune_2partition_index_limit;
590
144
    config.tune_3partition_index_limit = (*preset_configs)[start].tune_3partition_index_limit;
591
144
    config.tune_4partition_index_limit = (*preset_configs)[start].tune_4partition_index_limit;
592
144
    config.tune_block_mode_limit = (*preset_configs)[start].tune_block_mode_limit;
593
144
    config.tune_refinement_limit = (*preset_configs)[start].tune_refinement_limit;
594
144
    config.tune_candidate_limit = (*preset_configs)[start].tune_candidate_limit;
595
144
    config.tune_2partitioning_candidate_limit = (*preset_configs)[start].tune_2partitioning_candidate_limit;
596
144
    config.tune_3partitioning_candidate_limit = (*preset_configs)[start].tune_3partitioning_candidate_limit;
597
144
    config.tune_4partitioning_candidate_limit = (*preset_configs)[start].tune_4partitioning_candidate_limit;
598
144
    config.tune_db_limit = astc::max((*preset_configs)[start].tune_db_limit_a_base - 35 * ltexels,
599
144
                                     (*preset_configs)[start].tune_db_limit_b_base - 19 * ltexels);
600
601
144
    config.tune_mse_overshoot = (*preset_configs)[start].tune_mse_overshoot;
602
603
144
    config.tune_2partition_early_out_limit_factor = (*preset_configs)[start].tune_2partition_early_out_limit_factor;
604
144
    config.tune_3partition_early_out_limit_factor = (*preset_configs)[start].tune_3partition_early_out_limit_factor;
605
144
    config.tune_2plane_early_out_limit_correlation = (*preset_configs)[start].tune_2plane_early_out_limit_correlation;
606
144
    config.tune_search_mode0_enable = (*preset_configs)[start].tune_search_mode0_enable;
607
144
  }
608
  // Start and end node are not the same - so interpolate between them
609
3.35k
  else
610
3.35k
  {
611
3.35k
    auto& node_a = (*preset_configs)[start];
612
3.35k
    auto& node_b = (*preset_configs)[end];
613
614
3.35k
    float wt_range = node_b.quality - node_a.quality;
615
3.35k
    assert(wt_range > 0);
616
617
    // Compute interpolation factors
618
3.35k
    float wt_node_a = (node_b.quality - quality) / wt_range;
619
3.35k
    float wt_node_b = (quality - node_a.quality) / wt_range;
620
621
23.4k
    #define LERP(param) ((node_a.param * wt_node_a) + (node_b.param * wt_node_b))
622
33.5k
    #define LERPI(param) astc::flt2int_rtn(\
623
33.5k
                             (static_cast<float>(node_a.param) * wt_node_a) + \
624
33.5k
                             (static_cast<float>(node_b.param) * wt_node_b))
625
13.4k
    #define LERPUI(param) static_cast<unsigned int>(LERPI(param))
626
627
3.35k
    config.tune_partition_count_limit = LERPI(tune_partition_count_limit);
628
3.35k
    config.tune_2partition_index_limit = LERPI(tune_2partition_index_limit);
629
3.35k
    config.tune_3partition_index_limit = LERPI(tune_3partition_index_limit);
630
3.35k
    config.tune_4partition_index_limit = LERPI(tune_4partition_index_limit);
631
3.35k
    config.tune_block_mode_limit = LERPI(tune_block_mode_limit);
632
3.35k
    config.tune_refinement_limit = LERPI(tune_refinement_limit);
633
3.35k
    config.tune_candidate_limit = LERPUI(tune_candidate_limit);
634
3.35k
    config.tune_2partitioning_candidate_limit = LERPUI(tune_2partitioning_candidate_limit);
635
3.35k
    config.tune_3partitioning_candidate_limit = LERPUI(tune_3partitioning_candidate_limit);
636
3.35k
    config.tune_4partitioning_candidate_limit = LERPUI(tune_4partitioning_candidate_limit);
637
3.35k
    config.tune_db_limit = astc::max(LERP(tune_db_limit_a_base) - 35 * ltexels,
638
3.35k
                                     LERP(tune_db_limit_b_base) - 19 * ltexels);
639
640
3.35k
    config.tune_mse_overshoot = LERP(tune_mse_overshoot);
641
642
3.35k
    config.tune_2partition_early_out_limit_factor = LERP(tune_2partition_early_out_limit_factor);
643
3.35k
    config.tune_3partition_early_out_limit_factor = LERP(tune_3partition_early_out_limit_factor);
644
3.35k
    config.tune_2plane_early_out_limit_correlation = LERP(tune_2plane_early_out_limit_correlation);
645
3.35k
    config.tune_search_mode0_enable = LERP(tune_search_mode0_enable);
646
3.35k
    #undef LERP
647
3.35k
    #undef LERPI
648
3.35k
    #undef LERPUI
649
3.35k
  }
650
651
  // Set heuristics to the defaults for each color profile
652
3.49k
  config.cw_r_weight = 1.0f;
653
3.49k
  config.cw_g_weight = 1.0f;
654
3.49k
  config.cw_b_weight = 1.0f;
655
3.49k
  config.cw_a_weight = 1.0f;
656
657
3.49k
  config.a_scale_radius = 0;
658
659
3.49k
  config.rgbm_m_scale = 0.0f;
660
661
3.49k
  config.profile = profile;
662
663
  // Values in this enum are from an external user, so not guaranteed to be
664
  // bounded to the enum values
665
3.49k
  switch (static_cast<int>(profile))
666
3.49k
  {
667
662
  case ASTCENC_PRF_LDR:
668
1.81k
  case ASTCENC_PRF_LDR_SRGB:
669
1.81k
    break;
670
776
  case ASTCENC_PRF_HDR_RGB_LDR_A:
671
1.68k
  case ASTCENC_PRF_HDR:
672
1.68k
    config.tune_db_limit = 999.0f;
673
1.68k
    config.tune_search_mode0_enable = 0.0f;
674
1.68k
    break;
675
0
  default:
676
0
    return ASTCENC_ERR_BAD_PROFILE;
677
3.49k
  }
678
679
  // Flags field must not contain any unknown flag bits
680
3.49k
  status = validate_flags(profile, flags);
681
3.49k
  if (status != ASTCENC_SUCCESS)
682
42
  {
683
42
    return status;
684
42
  }
685
686
3.45k
  if (flags & ASTCENC_FLG_MAP_NORMAL)
687
920
  {
688
    // Normal map encoding uses L+A blocks, so allow one more partitioning
689
    // than usual. We need fewer bits for endpoints, so are more likely
690
    // to be able to use more partitions
691
920
    config.tune_partition_count_limit = astc::min(config.tune_partition_count_limit + 1u, 4u);
692
693
920
    config.cw_g_weight = 0.0f;
694
920
    config.cw_b_weight = 0.0f;
695
920
    config.tune_2partition_early_out_limit_factor *= 1.5f;
696
920
    config.tune_3partition_early_out_limit_factor *= 1.5f;
697
920
    config.tune_2plane_early_out_limit_correlation = 0.99f;
698
699
    // Normals are prone to blocking artifacts on smooth curves so force
700
    // compressor to try harder
701
920
    config.tune_db_limit *= 1.03f;
702
920
  }
703
2.53k
  else if (flags & ASTCENC_FLG_MAP_RGBM)
704
1.10k
  {
705
1.10k
    config.rgbm_m_scale = 5.0f;
706
1.10k
    config.cw_a_weight = 2.0f * config.rgbm_m_scale;
707
1.10k
  }
708
1.43k
  else // (This is color data)
709
1.43k
  {
710
    // This is a very basic perceptual metric for RGB color data, which weights error
711
    // significance by the perceptual luminance contribution of each color channel. For
712
    // luminance the usual weights to compute luminance from a linear RGB value are as
713
    // follows:
714
    //
715
    //     l = r * 0.3 + g * 0.59 + b * 0.11
716
    //
717
    // ... but we scale these up to keep a better balance between color and alpha. Note
718
    // that if the content is using alpha we'd recommend using the -a option to weight
719
    // the color contribution by the alpha transparency.
720
1.43k
    if (flags & ASTCENC_FLG_USE_PERCEPTUAL)
721
208
    {
722
208
      config.cw_r_weight = 0.30f * 2.25f;
723
208
      config.cw_g_weight = 0.59f * 2.25f;
724
208
      config.cw_b_weight = 0.11f * 2.25f;
725
208
    }
726
1.43k
  }
727
3.45k
  config.flags = flags;
728
729
3.45k
  return ASTCENC_SUCCESS;
730
3.49k
}
731
732
/* See header for documentation. */
733
astcenc_error astcenc_context_alloc(
734
  const astcenc_config* configp,
735
  unsigned int thread_count,
736
  astcenc_context** context,
737
  const astcenc_context* parent_context
738
3.45k
) {
739
3.45k
  astcenc_error status;
740
741
3.45k
  status = validate_cpu_float();
742
3.45k
  if (status != ASTCENC_SUCCESS)
743
0
  {
744
0
    return status;
745
0
  }
746
747
3.45k
  if (thread_count == 0)
748
0
  {
749
0
    return ASTCENC_ERR_BAD_PARAM;
750
0
  }
751
752
#if defined(ASTCENC_DIAGNOSTICS)
753
  // Force single threaded compressor use in diagnostic mode
754
  if (thread_count != 1)
755
  {
756
    return ASTCENC_ERR_BAD_PARAM;
757
  }
758
#endif
759
760
  // Exactly one of config or parent_context must be set
761
3.45k
  bool has_config = configp != nullptr;
762
3.45k
  bool has_parent = parent_context != nullptr;
763
3.45k
  if (!(has_config ^ has_parent))
764
0
  {
765
0
    return ASTCENC_ERR_BAD_PARAM;
766
0
  }
767
768
3.45k
  if (has_parent)
769
0
  {
770
0
    configp = &parent_context->context.config;
771
0
  }
772
773
3.45k
  const astcenc_config& config = *configp;
774
3.45k
  astcenc_context* ctxo = new astcenc_context;
775
3.45k
  astcenc_contexti* ctx = &ctxo->context;
776
3.45k
  ctx->thread_count = thread_count;
777
3.45k
  ctx->config = *configp;
778
3.45k
  ctx->working_buffers = nullptr;
779
780
  // These are allocated per-compress, as they depend on image size
781
3.45k
  ctx->input_alpha_averages = nullptr;
782
783
  // Copy the config first and validate the copy (we may modify it)
784
3.45k
  status = validate_config(ctx->config);
785
3.45k
  if (status != ASTCENC_SUCCESS)
786
0
  {
787
0
    delete ctxo;
788
0
    return status;
789
0
  }
790
791
3.45k
  if (!parent_context)
792
3.45k
  {
793
3.45k
    block_size_descriptor* bsd = aligned_malloc<block_size_descriptor>(sizeof(block_size_descriptor), ASTCENC_VECALIGN);
794
3.45k
    if (!bsd)
795
0
    {
796
0
      delete ctxo;
797
0
      return ASTCENC_ERR_OUT_OF_MEM;
798
0
    }
799
800
3.45k
    bool can_omit_modes = static_cast<bool>(config.flags & ASTCENC_FLG_SELF_DECOMPRESS_ONLY);
801
3.45k
    init_block_size_descriptor(config.block_x, config.block_y, config.block_z,
802
3.45k
                               can_omit_modes,
803
3.45k
                               config.tune_partition_count_limit,
804
3.45k
                               static_cast<float>(config.tune_block_mode_limit) / 100.0f,
805
3.45k
                               *bsd);
806
807
3.45k
    ctx->owns_bsd = true;
808
3.45k
    ctx->bsd = bsd;
809
3.45k
  }
810
0
  else
811
0
  {
812
0
    ctx->owns_bsd = false;
813
0
    ctx->bsd = parent_context->context.bsd;
814
0
  }
815
816
3.45k
#if !defined(ASTCENC_DECOMPRESS_ONLY)
817
  // Do setup only needed by compression
818
3.45k
  if (!(ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY))
819
3.15k
  {
820
    // Turn a dB limit into a per-texel error for faster use later
821
3.15k
    if ((ctx->config.profile == ASTCENC_PRF_LDR) || (ctx->config.profile == ASTCENC_PRF_LDR_SRGB))
822
1.61k
    {
823
1.61k
      ctx->config.tune_db_limit = astc::pow(0.1f, ctx->config.tune_db_limit * 0.1f) * 65535.0f * 65535.0f;
824
1.61k
    }
825
1.54k
    else
826
1.54k
    {
827
1.54k
      ctx->config.tune_db_limit = 0.0f;
828
1.54k
    }
829
830
3.15k
    size_t worksize = sizeof(compression_working_buffers) * thread_count;
831
3.15k
    ctx->working_buffers = aligned_malloc<compression_working_buffers>(worksize, ASTCENC_VECALIGN);
832
3.15k
    static_assert((ASTCENC_VECALIGN == 0) || ((sizeof(compression_working_buffers) % ASTCENC_VECALIGN) == 0),
833
3.15k
                  "compression_working_buffers size must be multiple of vector alignment");
834
3.15k
    if (!ctx->working_buffers)
835
0
    {
836
0
      if (ctx->owns_bsd)
837
0
      {
838
0
        aligned_free<const block_size_descriptor>(ctx->bsd);
839
0
      }
840
0
      delete ctxo;
841
0
      *context = nullptr;
842
0
      return ASTCENC_ERR_OUT_OF_MEM;
843
0
    }
844
3.15k
  }
845
3.45k
#endif
846
847
#if defined(ASTCENC_DIAGNOSTICS)
848
  ctx->trace_log = new TraceLog(ctx->config.trace_file_path);
849
  if (!ctx->trace_log->m_file)
850
  {
851
    return ASTCENC_ERR_DTRACE_FAILURE;
852
  }
853
854
  trace_add_data("block_x", config.block_x);
855
  trace_add_data("block_y", config.block_y);
856
  trace_add_data("block_z", config.block_z);
857
#endif
858
859
3.45k
  *context = ctxo;
860
861
3.45k
#if !defined(ASTCENC_DECOMPRESS_ONLY)
862
3.45k
  prepare_angular_tables();
863
3.45k
#endif
864
865
3.45k
  return ASTCENC_SUCCESS;
866
3.45k
}
867
868
/* See header for documentation. */
869
void astcenc_context_free(
870
  astcenc_context* ctxo
871
3.45k
) {
872
3.45k
  if (ctxo)
873
3.45k
  {
874
3.45k
    astcenc_contexti* ctx = &ctxo->context;
875
3.45k
    aligned_free<compression_working_buffers>(ctx->working_buffers);
876
3.45k
    if (ctx->owns_bsd)
877
3.45k
    {
878
3.45k
      aligned_free<const block_size_descriptor>(ctx->bsd);
879
3.45k
    }
880
#if defined(ASTCENC_DIAGNOSTICS)
881
    delete ctx->trace_log;
882
#endif
883
3.45k
    delete ctxo;
884
3.45k
  }
885
3.45k
}
886
887
#if !defined(ASTCENC_DECOMPRESS_ONLY)
888
889
/**
890
 * @brief Compress an image, after any preflight has completed.
891
 *
892
 * @param[out] ctxo           The compressor context.
893
 * @param      thread_index   The thread index.
894
 * @param      image          The input image.
895
 * @param      swizzle        The input swizzle.
896
 * @param[out] buffer         The output array for the compressed data.
897
 */
898
static void compress_image(
899
  astcenc_context& ctxo,
900
  unsigned int thread_index,
901
  const astcenc_image& image,
902
  const astcenc_swizzle& swizzle,
903
  uint8_t* buffer
904
2.13k
) {
905
2.13k
  astcenc_contexti& ctx = ctxo.context;
906
2.13k
  const block_size_descriptor& bsd = *ctx.bsd;
907
2.13k
  astcenc_profile decode_mode = ctx.config.profile;
908
909
2.13k
  image_block blk {};
910
911
2.13k
  size_t block_x = bsd.dim_x;
912
2.13k
  size_t block_y = bsd.dim_y;
913
2.13k
  size_t block_z = bsd.dim_z;
914
2.13k
  blk.texel_count = static_cast<uint8_t>(block_x * block_y * block_z);
915
916
2.13k
  size_t dim_x = image.dim_x;
917
2.13k
  size_t dim_y = image.dim_y;
918
2.13k
  size_t dim_z = image.dim_z;
919
920
2.13k
  size_t blocks_x = astc::get_block_count_safe(dim_x, block_x);
921
2.13k
  size_t blocks_y = astc::get_block_count_safe(dim_y, block_y);
922
2.13k
  size_t blocks_z = astc::get_block_count_safe(dim_z, block_z);
923
924
2.13k
  size_t block_count = get_blocks_count(blocks_x, blocks_y, blocks_z);
925
  // Should never fail here - tested in caller before calling here
926
2.13k
  assert(block_count > 0);
927
928
929
2.13k
  size_t row_blocks = blocks_x;
930
2.13k
  size_t plane_blocks = blocks_x * blocks_y;
931
932
2.13k
  blk.decode_unorm8 = ctxo.context.config.flags & ASTCENC_FLG_USE_DECODE_UNORM8;
933
934
  // Populate the block channel weights
935
2.13k
  blk.channel_weight = vfloat4(ctx.config.cw_r_weight,
936
2.13k
                               ctx.config.cw_g_weight,
937
2.13k
                               ctx.config.cw_b_weight,
938
2.13k
                               ctx.config.cw_a_weight);
939
940
  // Use preallocated scratch buffer
941
2.13k
  auto& temp_buffers = ctx.working_buffers[thread_index];
942
943
  // Only the first thread actually runs the initializer
944
2.13k
  ctxo.manage_compress.init(block_count, ctx.config.progress_callback);
945
946
  // Determine if we can use an optimized load function
947
2.13k
  bool needs_swz = (swizzle.r != ASTCENC_SWZ_R) || (swizzle.g != ASTCENC_SWZ_G) ||
948
2.13k
                   (swizzle.b != ASTCENC_SWZ_B) || (swizzle.a != ASTCENC_SWZ_A);
949
950
2.13k
  bool needs_hdr = (decode_mode == ASTCENC_PRF_HDR) ||
951
1.61k
                   (decode_mode == ASTCENC_PRF_HDR_RGB_LDR_A);
952
953
2.13k
  bool use_fast_load = !needs_swz && !needs_hdr &&
954
1.05k
                       block_z == 1 && image.data_type == ASTCENC_TYPE_U8;
955
956
2.13k
  auto load_func = load_image_block;
957
2.13k
  if (use_fast_load)
958
1.05k
  {
959
1.05k
    load_func = load_image_block_fast_ldr;
960
1.05k
  }
961
962
  // All threads run this processing loop until there is no work remaining
963
4.27k
  while (true)
964
4.27k
  {
965
4.27k
    size_t count;
966
4.27k
    size_t base = ctxo.manage_compress.get_task_assignment(16, count);
967
4.27k
    if (!count)
968
2.13k
    {
969
2.13k
      break;
970
2.13k
    }
971
972
4.27k
    for (size_t i = base; i < base + count; i++)
973
2.13k
    {
974
      // Decode i into x, y, z block indices
975
2.13k
      size_t z = i / plane_blocks;
976
2.13k
      size_t rem = i - (z * plane_blocks);
977
2.13k
      size_t y = rem / row_blocks;
978
2.13k
      size_t x = rem - (y * row_blocks);
979
980
      // Test if we can apply some basic alpha-scale RDO
981
2.13k
      bool use_full_block = true;
982
2.13k
      if (ctx.config.a_scale_radius != 0 && block_z == 1)
983
0
      {
984
0
        size_t start_x = x * block_x;
985
0
        size_t end_x = astc::min(dim_x, start_x + block_x);
986
987
0
        size_t start_y = y * block_y;
988
0
        size_t end_y = astc::min(dim_y, start_y + block_y);
989
990
        // SATs accumulate error, so don't test exactly zero. Test for
991
        // less than 1 alpha in the expanded block footprint that
992
        // includes the alpha radius.
993
0
        size_t x_footprint = block_x + 2 * (ctx.config.a_scale_radius - 1);
994
0
        size_t y_footprint = block_y + 2 * (ctx.config.a_scale_radius - 1);
995
996
0
        float footprint = static_cast<float>(x_footprint * y_footprint);
997
0
        float threshold = 0.9f / (255.0f * footprint);
998
999
        // Do we have any alpha values?
1000
0
        use_full_block = false;
1001
0
        for (size_t ay = start_y; ay < end_y; ay++)
1002
0
        {
1003
0
          for (size_t ax = start_x; ax < end_x; ax++)
1004
0
          {
1005
0
            float a_avg = ctx.input_alpha_averages[ay * dim_x + ax];
1006
0
            if (a_avg > threshold)
1007
0
            {
1008
0
              use_full_block = true;
1009
0
              ax = end_x;
1010
0
              ay = end_y;
1011
0
            }
1012
0
          }
1013
0
        }
1014
0
      }
1015
1016
      // Fetch the full block for compression
1017
2.13k
      if (use_full_block)
1018
2.13k
      {
1019
2.13k
        load_func(decode_mode, image, blk, bsd, x * block_x, y * block_y, z * block_z, swizzle);
1020
1021
        // Scale RGB error contribution by the maximum alpha in the block
1022
        // This encourages preserving alpha accuracy in regions with high
1023
        // transparency, and can buy up to 0.5 dB PSNR.
1024
2.13k
        if (ctx.config.flags & ASTCENC_FLG_USE_ALPHA_WEIGHT)
1025
918
        {
1026
918
          float alpha_scale = blk.data_max.lane<3>() * (1.0f / 65535.0f);
1027
918
          blk.channel_weight = vfloat4(ctx.config.cw_r_weight * alpha_scale,
1028
918
                                       ctx.config.cw_g_weight * alpha_scale,
1029
918
                                       ctx.config.cw_b_weight * alpha_scale,
1030
918
                                       ctx.config.cw_a_weight);
1031
918
        }
1032
2.13k
      }
1033
      // Apply alpha scale RDO - substitute constant color block
1034
0
      else
1035
0
      {
1036
0
        blk.origin_texel = vfloat4::zero();
1037
0
        blk.data_min = vfloat4::zero();
1038
0
        blk.data_mean = vfloat4::zero();
1039
0
        blk.data_max = vfloat4::zero();
1040
0
        blk.grayscale = true;
1041
0
      }
1042
1043
2.13k
      size_t offset = ((z * blocks_y + y) * blocks_x + x) * 16;
1044
2.13k
      uint8_t *bp = buffer + offset;
1045
2.13k
      compress_block(ctx, blk, bp, temp_buffers);
1046
2.13k
    }
1047
1048
2.13k
    ctxo.manage_compress.complete_task_assignment(count);
1049
2.13k
  }
1050
2.13k
}
1051
1052
/**
1053
 * @brief Compute regional averages in an image.
1054
 *
1055
 * This function can be called by multiple threads, but only after a single
1056
 * thread calls the setup function @c init_compute_averages().
1057
 *
1058
 * Results are written back into @c img->input_alpha_averages.
1059
 *
1060
 * @param[out] ctx   The context.
1061
 * @param      ag    The average and variance arguments created during setup.
1062
 */
1063
static void compute_averages(
1064
  astcenc_context& ctx,
1065
  const avg_args &ag
1066
0
) {
1067
0
  pixel_region_args arg = ag.arg;
1068
0
  arg.work_memory = new vfloat4[ag.work_memory_size];
1069
1070
0
  size_t size_x = ag.img_size_x;
1071
0
  size_t size_y = ag.img_size_y;
1072
0
  size_t size_z = ag.img_size_z;
1073
1074
0
  size_t step_xy = ag.blk_size_xy;
1075
0
  size_t step_z = ag.blk_size_z;
1076
1077
0
  size_t tasks_y = (size_y + step_xy - 1) / step_xy;
1078
1079
  // All threads run this processing loop until there is no work remaining
1080
0
  while (true)
1081
0
  {
1082
0
    size_t count;
1083
0
    size_t base = ctx.manage_avg.get_task_assignment(16, count);
1084
0
    if (!count)
1085
0
    {
1086
0
      break;
1087
0
    }
1088
1089
0
    for (size_t i = base; i < base + count; i++)
1090
0
    {
1091
0
      size_t z_task = i / tasks_y;
1092
0
      size_t y_task = i - (z_task * tasks_y);
1093
1094
0
      size_t z = z_task * step_z;
1095
0
      size_t y = y_task * step_xy;
1096
1097
0
      arg.size_z = astc::min(step_z, size_z - z);
1098
0
      arg.offset_z = z;
1099
1100
0
      arg.size_y = astc::min(step_xy, size_y - y);
1101
0
      arg.offset_y = y;
1102
1103
0
      for (size_t x = 0; x < size_x; x += step_xy)
1104
0
      {
1105
0
        arg.size_x = astc::min(step_xy, size_x - x);
1106
0
        arg.offset_x = x;
1107
0
        compute_pixel_region_variance(ctx.context, arg);
1108
0
      }
1109
0
    }
1110
1111
0
    ctx.manage_avg.complete_task_assignment(count);
1112
0
  }
1113
1114
0
  delete[] arg.work_memory;
1115
0
}
1116
1117
#endif
1118
1119
/* See header for documentation. */
1120
astcenc_error astcenc_compress_image(
1121
  astcenc_context* ctxo,
1122
  astcenc_image* imagep,
1123
  const astcenc_swizzle* swizzle,
1124
  uint8_t* data_out,
1125
  size_t data_len,
1126
  unsigned int thread_index
1127
2.13k
) {
1128
#if defined(ASTCENC_DECOMPRESS_ONLY)
1129
  (void)ctxo;
1130
  (void)imagep;
1131
  (void)swizzle;
1132
  (void)data_out;
1133
  (void)data_len;
1134
  (void)thread_index;
1135
  return ASTCENC_ERR_BAD_CONTEXT;
1136
#else
1137
2.13k
  astcenc_contexti* ctx = &ctxo->context;
1138
2.13k
  astcenc_error status;
1139
2.13k
  astcenc_image& image = *imagep;
1140
1141
2.13k
  if (ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY)
1142
0
  {
1143
0
    return ASTCENC_ERR_BAD_CONTEXT;
1144
0
  }
1145
1146
2.13k
  status = validate_compression_swizzle(*swizzle);
1147
2.13k
  if (status != ASTCENC_SUCCESS)
1148
0
  {
1149
0
    return status;
1150
0
  }
1151
1152
2.13k
  if (thread_index >= ctx->thread_count)
1153
0
  {
1154
0
    return ASTCENC_ERR_BAD_PARAM;
1155
0
  }
1156
1157
2.13k
  size_t dim_x = image.dim_x;
1158
2.13k
  size_t dim_y = image.dim_y;
1159
2.13k
  size_t dim_z = image.dim_z;
1160
1161
2.13k
  size_t texel_count = get_texels_count(dim_x, dim_y, dim_z);
1162
  // Cumulative texel sizes would overflow a size_t
1163
2.13k
  if (texel_count == 0)
1164
0
  {
1165
0
    return ASTCENC_ERR_BAD_PARAM;
1166
0
  }
1167
1168
2.13k
  size_t block_x = ctx->config.block_x;
1169
2.13k
  size_t block_y = ctx->config.block_y;
1170
2.13k
  size_t block_z = ctx->config.block_z;
1171
1172
2.13k
  size_t blocks_x = astc::get_block_count_safe(dim_x, block_x);
1173
2.13k
  size_t blocks_y = astc::get_block_count_safe(dim_y, block_y);
1174
2.13k
  size_t blocks_z = astc::get_block_count_safe(dim_z, block_z);
1175
1176
2.13k
  size_t block_count = get_blocks_count(blocks_x, blocks_y, blocks_z);
1177
  // Cumulative block sizes would overflow a size_t
1178
2.13k
  if (block_count == 0)
1179
0
  {
1180
0
    return ASTCENC_ERR_BAD_PARAM;
1181
0
  }
1182
1183
  // Check we have enough output space, size_needed calc cannot overflow as
1184
  // get_blocks_count() already validated that a byte count would fit
1185
2.13k
  size_t size_needed = block_count * 16;
1186
2.13k
  if (data_len < size_needed)
1187
0
  {
1188
0
    return ASTCENC_ERR_OUT_OF_MEM;
1189
0
  }
1190
1191
  // If context thread count is one then implicitly reset
1192
2.13k
  if (ctx->thread_count == 1)
1193
2.13k
  {
1194
2.13k
    astcenc_compress_reset(ctxo);
1195
2.13k
  }
1196
1197
2.13k
  if (ctx->config.a_scale_radius != 0)
1198
0
  {
1199
    // First thread to enter will do setup, other threads will subsequently
1200
    // enter the critical section but simply skip over the initialization
1201
0
    auto init_avg = [ctx, &image, swizzle, texel_count]() {
1202
      // Perform memory allocations for the destination buffers
1203
0
      ctx->input_alpha_averages = new float[texel_count];
1204
1205
0
      return init_compute_averages(
1206
0
        image, ctx->config.a_scale_radius, *swizzle,
1207
0
        ctx->avg_preprocess_args);
1208
0
    };
1209
1210
    // Only the first thread actually runs the initializer
1211
0
    ctxo->manage_avg.init(init_avg);
1212
1213
    // All threads will enter this function and dynamically grab work
1214
0
    compute_averages(*ctxo, ctx->avg_preprocess_args);
1215
0
  }
1216
1217
  // Wait for compute_averages to complete before compressing
1218
2.13k
  ctxo->manage_avg.wait();
1219
1220
2.13k
  compress_image(*ctxo, thread_index, image, *swizzle, data_out);
1221
1222
  // Wait for compress to complete before freeing memory
1223
2.13k
  ctxo->manage_compress.wait();
1224
1225
2.13k
  auto term_compress = [ctx]() {
1226
2.13k
    delete[] ctx->input_alpha_averages;
1227
2.13k
    ctx->input_alpha_averages = nullptr;
1228
2.13k
  };
1229
1230
  // Only the first thread to arrive actually runs the term
1231
2.13k
  ctxo->manage_compress.term(term_compress);
1232
1233
2.13k
  return ASTCENC_SUCCESS;
1234
2.13k
#endif
1235
2.13k
}
1236
1237
/* See header for documentation. */
1238
astcenc_error astcenc_compress_reset(
1239
  astcenc_context* ctxo
1240
2.13k
) {
1241
#if defined(ASTCENC_DECOMPRESS_ONLY)
1242
  (void)ctxo;
1243
  return ASTCENC_ERR_BAD_CONTEXT;
1244
#else
1245
2.13k
  astcenc_contexti* ctx = &ctxo->context;
1246
2.13k
  if (ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY)
1247
0
  {
1248
0
    return ASTCENC_ERR_BAD_CONTEXT;
1249
0
  }
1250
1251
2.13k
  ctxo->manage_avg.reset();
1252
2.13k
  ctxo->manage_compress.reset();
1253
2.13k
  return ASTCENC_SUCCESS;
1254
2.13k
#endif
1255
2.13k
}
1256
1257
/* See header for documentation. */
1258
astcenc_error astcenc_compress_cancel(
1259
  astcenc_context* ctxo
1260
0
) {
1261
#if defined(ASTCENC_DECOMPRESS_ONLY)
1262
  (void)ctxo;
1263
  return ASTCENC_ERR_BAD_CONTEXT;
1264
#else
1265
0
  astcenc_contexti* ctx = &ctxo->context;
1266
0
  if (ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY)
1267
0
  {
1268
0
    return ASTCENC_ERR_BAD_CONTEXT;
1269
0
  }
1270
1271
  // Cancel compression before cancelling avg. This avoids the race condition
1272
  // where cancelling them in the other order could see a compression worker
1273
  // starting to process even though some of the avg data is undefined.
1274
0
  ctxo->manage_compress.cancel();
1275
0
  ctxo->manage_avg.cancel();
1276
0
  return ASTCENC_SUCCESS;
1277
0
#endif
1278
0
}
1279
1280
/* See header for documentation. */
1281
astcenc_error astcenc_decompress_image(
1282
  astcenc_context* ctxo,
1283
  const uint8_t* data,
1284
  size_t data_len,
1285
  astcenc_image* image_outp,
1286
  const astcenc_swizzle* swizzle,
1287
  unsigned int thread_index
1288
1.06k
) {
1289
1.06k
  astcenc_error status;
1290
1.06k
  astcenc_image& image_out = *image_outp;
1291
1.06k
  astcenc_contexti* ctx = &ctxo->context;
1292
1293
  // Today this doesn't matter (working set on stack) but might in future ...
1294
1.06k
  if (thread_index >= ctx->thread_count)
1295
0
  {
1296
0
    return ASTCENC_ERR_BAD_PARAM;
1297
0
  }
1298
1299
1.06k
  status = validate_decompression_swizzle(*swizzle);
1300
1.06k
  if (status != ASTCENC_SUCCESS)
1301
0
  {
1302
0
    return status;
1303
0
  }
1304
1305
1.06k
  size_t dim_x = image_out.dim_x;
1306
1.06k
  size_t dim_y = image_out.dim_y;
1307
1.06k
  size_t dim_z = image_out.dim_z;
1308
1309
1.06k
  size_t texel_count = get_texels_count(dim_x, dim_y, dim_z);
1310
  // Cumulative texel sizes would overflow a size_t
1311
1.06k
  if (texel_count == 0)
1312
0
  {
1313
0
    return ASTCENC_ERR_BAD_PARAM;
1314
0
  }
1315
1316
1.06k
  size_t block_x = ctx->config.block_x;
1317
1.06k
  size_t block_y = ctx->config.block_y;
1318
1.06k
  size_t block_z = ctx->config.block_z;
1319
1320
1.06k
  size_t blocks_x = astc::get_block_count_safe(dim_x, block_x);
1321
1.06k
  size_t blocks_y = astc::get_block_count_safe(dim_y, block_y);
1322
1.06k
  size_t blocks_z = astc::get_block_count_safe(dim_z, block_z);
1323
1324
1.06k
  size_t block_count = get_blocks_count(blocks_x, blocks_y, blocks_z);
1325
  // Cumulative block sizes would overflow a size_t
1326
1.06k
  if (block_count == 0)
1327
0
  {
1328
0
    return ASTCENC_ERR_BAD_PARAM;
1329
0
  }
1330
1331
  // Check we have enough output space, size_needed calc cannot overflow as
1332
  // get_blocks_count() already validated that a byte count would fit
1333
1.06k
  size_t size_needed = block_count * 16;
1334
1.06k
  if (data_len < size_needed)
1335
0
  {
1336
0
    return ASTCENC_ERR_OUT_OF_MEM;
1337
0
  }
1338
1339
1.06k
  size_t row_blocks = blocks_x;
1340
1.06k
  size_t plane_blocks = blocks_x * blocks_y;
1341
1342
1.06k
  image_block blk {};
1343
1.06k
  blk.texel_count = static_cast<uint8_t>(block_x * block_y * block_z);
1344
1345
  // Decode mode inferred from the output data type
1346
1.06k
  blk.decode_unorm8 = image_out.data_type == ASTCENC_TYPE_U8;
1347
1348
  // If context thread count is one then implicitly reset
1349
1.06k
  if (ctx->thread_count == 1)
1350
1.06k
  {
1351
1.06k
    astcenc_decompress_reset(ctxo);
1352
1.06k
  }
1353
1354
  // Only the first thread actually runs the initializer
1355
1.06k
  ctxo->manage_decompress.init(block_count, nullptr);
1356
1357
  // All threads run this processing loop until there is no work remaining
1358
2.12k
  while (true)
1359
2.12k
  {
1360
2.12k
    size_t count;
1361
2.12k
    size_t base = ctxo->manage_decompress.get_task_assignment(128, count);
1362
2.12k
    if (!count)
1363
1.06k
    {
1364
1.06k
      break;
1365
1.06k
    }
1366
1367
5.16k
    for (size_t i = base; i < base + count; i++)
1368
4.09k
    {
1369
      // Decode i into x, y, z block indices
1370
4.09k
      size_t z = i / plane_blocks;
1371
4.09k
      size_t rem = i - (z * plane_blocks);
1372
4.09k
      size_t y = rem / row_blocks;
1373
4.09k
      size_t x = rem - (y * row_blocks);
1374
1375
4.09k
      size_t offset = (((z * blocks_y + y) * blocks_x) + x) * 16;
1376
4.09k
      const uint8_t* bp = data + offset;
1377
1378
4.09k
      symbolic_compressed_block scb;
1379
1380
4.09k
      physical_to_symbolic(*ctx->bsd, bp, scb);
1381
1382
4.09k
      decompress_symbolic_block(ctx->config.profile, *ctx->bsd,
1383
4.09k
                                x * block_x,
1384
4.09k
                                y * block_y,
1385
4.09k
                                z * block_z,
1386
4.09k
                                scb, blk);
1387
1388
4.09k
      store_image_block(image_out, blk, *ctx->bsd,
1389
4.09k
                        x * block_x, y * block_y, z * block_z,
1390
4.09k
                        *swizzle);
1391
4.09k
    }
1392
1393
1.06k
    ctxo->manage_decompress.complete_task_assignment(count);
1394
1.06k
  }
1395
1396
1.06k
  return ASTCENC_SUCCESS;
1397
1.06k
}
1398
1399
/* See header for documentation. */
1400
astcenc_error astcenc_decompress_reset(
1401
  astcenc_context* ctxo
1402
1.06k
) {
1403
1.06k
  ctxo->manage_decompress.reset();
1404
1.06k
  return ASTCENC_SUCCESS;
1405
1.06k
}
1406
1407
/* See header for documentation. */
1408
astcenc_error astcenc_get_block_info(
1409
  astcenc_context* ctxo,
1410
  const uint8_t data[16],
1411
  astcenc_block_info* info
1412
0
) {
1413
#if defined(ASTCENC_DECOMPRESS_ONLY)
1414
  (void)ctxo;
1415
  (void)data;
1416
  (void)info;
1417
  return ASTCENC_ERR_BAD_CONTEXT;
1418
#else
1419
0
  astcenc_contexti* ctx = &ctxo->context;
1420
1421
  // Decode the compressed data into a symbolic form
1422
0
  symbolic_compressed_block scb;
1423
0
  physical_to_symbolic(*ctx->bsd, data, scb);
1424
1425
  // Fetch the appropriate partition and decimation tables
1426
0
  const block_size_descriptor& bsd = *ctx->bsd;
1427
1428
  // Start from a clean slate
1429
0
  memset(info, 0, sizeof(*info));
1430
1431
  // Basic info we can always populate
1432
0
  info->profile = ctx->config.profile;
1433
1434
0
  info->block_x = ctx->config.block_x;
1435
0
  info->block_y = ctx->config.block_y;
1436
0
  info->block_z = ctx->config.block_z;
1437
0
  info->texel_count = bsd.texel_count;
1438
1439
  // Check for error blocks first
1440
0
  info->is_error_block = scb.block_type == SYM_BTYPE_ERROR;
1441
0
  if (info->is_error_block)
1442
0
  {
1443
0
    return ASTCENC_SUCCESS;
1444
0
  }
1445
1446
  // Check for constant color blocks second
1447
0
  info->is_constant_block = scb.block_type == SYM_BTYPE_CONST_F16 ||
1448
0
                            scb.block_type == SYM_BTYPE_CONST_U16;
1449
0
  if (info->is_constant_block)
1450
0
  {
1451
0
    return ASTCENC_SUCCESS;
1452
0
  }
1453
1454
  // Otherwise handle a full block ; known to be valid after conditions above have been checked
1455
0
  unsigned int partition_count = scb.partition_count;
1456
0
  const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index);
1457
1458
0
  const block_mode& bm = bsd.get_block_mode(scb.block_mode);
1459
0
  const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode);
1460
1461
0
  info->weight_x = di.weight_x;
1462
0
  info->weight_y = di.weight_y;
1463
0
  info->weight_z = di.weight_z;
1464
1465
0
  info->is_dual_plane_block = bm.is_dual_plane != 0;
1466
1467
0
  info->partition_count = scb.partition_count;
1468
0
  info->partition_index = scb.partition_index;
1469
0
  info->dual_plane_component = scb.plane2_component;
1470
1471
0
  info->color_level_count = get_quant_level(scb.get_color_quant_mode());
1472
0
  info->weight_level_count = get_quant_level(bm.get_weight_quant_mode());
1473
1474
  // Unpack color endpoints for each active partition
1475
0
  for (size_t i = 0; i < scb.partition_count; i++)
1476
0
  {
1477
0
    bool rgb_hdr;
1478
0
    bool a_hdr;
1479
0
    vint4 endpnt[2];
1480
1481
0
    unpack_color_endpoints(ctx->config.profile,
1482
0
                           scb.color_formats[i],
1483
0
                           scb.color_values[i],
1484
0
                           rgb_hdr, a_hdr,
1485
0
                           endpnt[0], endpnt[1]);
1486
1487
    // Store the color endpoint mode info
1488
0
    info->color_endpoint_modes[i] = scb.color_formats[i];
1489
0
    info->is_hdr_block = info->is_hdr_block || rgb_hdr || a_hdr;
1490
1491
    // Store the unpacked and decoded color endpoint
1492
0
    vmask4 hdr_mask(rgb_hdr, rgb_hdr, rgb_hdr, a_hdr);
1493
0
    for (size_t j = 0; j < 2; j++)
1494
0
    {
1495
0
      vint4 color_lns = lns_to_sf16(endpnt[j]);
1496
0
      vint4 color_unorm = unorm16_to_sf16(endpnt[j]);
1497
0
      vint4 datai = select(color_unorm, color_lns, hdr_mask);
1498
0
      store(float16_to_float(datai), info->color_endpoints[i][j]);
1499
0
    }
1500
0
  }
1501
1502
  // Unpack weights for each texel
1503
0
  int weight_plane1[BLOCK_MAX_TEXELS];
1504
0
  int weight_plane2[BLOCK_MAX_TEXELS];
1505
1506
0
  unpack_weights(bsd, scb, di, bm.is_dual_plane, weight_plane1, weight_plane2);
1507
0
  for (size_t i = 0; i < bsd.texel_count; i++)
1508
0
  {
1509
0
    info->weight_values_plane1[i] = static_cast<float>(weight_plane1[i]) * (1.0f / WEIGHTS_TEXEL_SUM);
1510
0
    if (info->is_dual_plane_block)
1511
0
    {
1512
0
      info->weight_values_plane2[i] = static_cast<float>(weight_plane2[i]) * (1.0f / WEIGHTS_TEXEL_SUM);
1513
0
    }
1514
0
  }
1515
1516
  // Unpack partition assignments for each texel
1517
0
  for (size_t i = 0; i < bsd.texel_count; i++)
1518
0
  {
1519
0
    info->partition_assignment[i] = pi.partition_of_texel[i];
1520
0
  }
1521
1522
0
  return ASTCENC_SUCCESS;
1523
0
#endif
1524
0
}
1525
1526
/* See header for documentation. */
1527
const char* astcenc_get_error_string(
1528
  astcenc_error status
1529
0
) {
1530
  // Values in this enum are from an external user, so not guaranteed to be
1531
  // bounded to the enum values
1532
0
  switch (static_cast<int>(status))
1533
0
  {
1534
0
  case ASTCENC_SUCCESS:
1535
0
    return "ASTCENC_SUCCESS";
1536
0
  case ASTCENC_ERR_OUT_OF_MEM:
1537
0
    return "ASTCENC_ERR_OUT_OF_MEM";
1538
0
  case ASTCENC_ERR_BAD_CPU_FLOAT:
1539
0
    return "ASTCENC_ERR_BAD_CPU_FLOAT";
1540
0
  case ASTCENC_ERR_BAD_PARAM:
1541
0
    return "ASTCENC_ERR_BAD_PARAM";
1542
0
  case ASTCENC_ERR_BAD_BLOCK_SIZE:
1543
0
    return "ASTCENC_ERR_BAD_BLOCK_SIZE";
1544
0
  case ASTCENC_ERR_BAD_PROFILE:
1545
0
    return "ASTCENC_ERR_BAD_PROFILE";
1546
0
  case ASTCENC_ERR_BAD_QUALITY:
1547
0
    return "ASTCENC_ERR_BAD_QUALITY";
1548
0
  case ASTCENC_ERR_BAD_FLAGS:
1549
0
    return "ASTCENC_ERR_BAD_FLAGS";
1550
0
  case ASTCENC_ERR_BAD_SWIZZLE:
1551
0
    return "ASTCENC_ERR_BAD_SWIZZLE";
1552
0
  case ASTCENC_ERR_BAD_CONTEXT:
1553
0
    return "ASTCENC_ERR_BAD_CONTEXT";
1554
0
  case ASTCENC_ERR_NOT_IMPLEMENTED:
1555
0
    return "ASTCENC_ERR_NOT_IMPLEMENTED";
1556
0
  case ASTCENC_ERR_BAD_DECODE_MODE:
1557
0
    return "ASTCENC_ERR_BAD_DECODE_MODE";
1558
#if defined(ASTCENC_DIAGNOSTICS)
1559
  case ASTCENC_ERR_DTRACE_FAILURE:
1560
    return "ASTCENC_ERR_DTRACE_FAILURE";
1561
#endif
1562
0
  default:
1563
0
    return nullptr;
1564
0
  }
1565
0
}