Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/astcenc/astcenc.h
9896 views
1
// SPDX-License-Identifier: Apache-2.0
2
// ----------------------------------------------------------------------------
3
// Copyright 2020-2025 Arm Limited
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
// use this file except in compliance with the License. You may obtain a copy
7
// of the License at:
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
// License for the specific language governing permissions and limitations
15
// under the License.
16
// ----------------------------------------------------------------------------
17
18
/**
19
* @brief The core astcenc codec library interface.
20
*
21
* This interface is the entry point to the core astcenc codec. It aims to be easy to use for
22
* non-experts, but also to allow experts to have fine control over the compressor heuristics if
23
* needed. The core codec only handles compression and decompression, transferring all inputs and
24
* outputs via memory buffers. To catch obvious input/output buffer sizing issues, which can cause
25
* security and stability problems, all transfer buffers are explicitly sized.
26
*
27
* While the aim is that we keep this interface mostly stable, it should be viewed as a mutable
28
* interface tied to a specific source version. We are not trying to maintain backwards
29
* compatibility across codec versions.
30
*
31
* The API state management is based around an explicit context object, which is the context for all
32
* allocated memory resources needed to compress and decompress a single image. A context can be
33
* used to sequentially compress multiple images using the same configuration, allowing setup
34
* overheads to be amortized over multiple images, which is particularly important when images are
35
* small.
36
*
37
* Multi-threading can be used two ways.
38
*
39
* * An application wishing to process multiple images in parallel can allocate multiple
40
* contexts and assign each context to a thread.
41
* * An application wishing to process a single image in using multiple threads can configure
42
* contexts for multi-threaded use, and invoke astcenc_compress/decompress() once per thread
43
* for faster processing. The caller is responsible for creating the worker threads, and
44
* synchronizing between images.
45
*
46
* Extended instruction set support
47
* ================================
48
*
49
* This library supports use of extended instruction sets, such as SSE4.1 and AVX2. These are
50
* enabled at compile time when building the library. There is no runtime checking in the core
51
* library that the instruction sets used are actually available. Checking compatibility is the
52
* responsibility of the calling code.
53
*
54
* Threading
55
* =========
56
*
57
* In pseudo-code, the usage for manual user threading looks like this:
58
*
59
* // Configure the compressor run
60
* astcenc_config my_config;
61
* astcenc_config_init(..., &my_config);
62
*
63
* // Power users can tweak <my_config> settings here ...
64
*
65
* // Allocate working state given config and thread_count
66
* astcenc_context* my_context;
67
* astcenc_context_alloc(&my_config, thread_count, &my_context);
68
*
69
* // Compress each image using these config settings
70
* foreach image:
71
* // For each thread in the thread pool
72
* for i in range(0, thread_count):
73
* astcenc_compress_image(my_context, &my_input, my_output, i);
74
*
75
* astcenc_compress_reset(my_context);
76
*
77
* // Clean up
78
* astcenc_context_free(my_context);
79
*
80
* Images
81
* ======
82
*
83
* The codec supports compressing single images, which can be either 2D images or volumetric 3D
84
* images. Calling code is responsible for any handling of aggregate types, such as mipmap chains,
85
* texture arrays, or sliced 3D textures.
86
*
87
* Images are passed in as an astcenc_image structure. Inputs can be either 8-bit unorm, 16-bit
88
* half-float, or 32-bit float, as indicated by the data_type field.
89
*
90
* Images can be any dimension; there is no requirement to be a multiple of the ASTC block size.
91
*
92
* Data is always passed in as 4 color components, and accessed as an array of 2D image slices. Data
93
* within an image slice is always tightly packed without padding. Addressing looks like this:
94
*
95
* data[z_coord][y_coord * x_dim * 4 + x_coord * 4 ] // Red
96
* data[z_coord][y_coord * x_dim * 4 + x_coord * 4 + 1] // Green
97
* data[z_coord][y_coord * x_dim * 4 + x_coord * 4 + 2] // Blue
98
* data[z_coord][y_coord * x_dim * 4 + x_coord * 4 + 3] // Alpha
99
*
100
* Common compressor usage
101
* =======================
102
*
103
* One of the most important things for coding image quality is to align the input data component
104
* count with the ASTC color endpoint mode. This avoids wasting bits encoding components you don't
105
* actually need in the endpoint colors.
106
*
107
* | Input data | Encoding swizzle | Sampling swizzle |
108
* | ------------ | ---------------- | ---------------- |
109
* | 1 component | RRR1 | .[rgb] |
110
* | 2 components | RRRG | .[rgb]a |
111
* | 3 components | RGB1 | .rgb |
112
* | 4 components | RGBA | .rgba |
113
*
114
* The 1 and 2 component modes recommend sampling from "g" to recover the luminance value as this
115
* provide best compatibility with other texture formats where the green component may be stored at
116
* higher precision than the others, such as RGB565. For ASTC any of the RGB components can be used;
117
* the luminance endpoint component will be returned for all three.
118
*
119
* When using the normal map compression mode ASTC will store normals as a two component X+Y map.
120
* Input images must contain unit-length normalized and should be passed in using a two component
121
* swizzle. The astcenc command line tool defaults to an RRRG swizzle, but some developers prefer
122
* to use GGGR for compatability with BC5n which will work just as well. The Z component can be
123
* recovered programmatically in shader code, using knowledge that the vector is unit length and
124
* that Z must be positive for a tangent-space normal map.
125
*
126
* Decompress-only usage
127
* =====================
128
*
129
* For some use cases it is useful to have a cut-down context and/or library which supports
130
* decompression but not compression.
131
*
132
* A context can be made decompress-only using the ASTCENC_FLG_DECOMPRESS_ONLY flag when the context
133
* is allocated. These contexts have lower dynamic memory footprint than a full context.
134
*
135
* The entire library can be made decompress-only by building the files with the define
136
* ASTCENC_DECOMPRESS_ONLY set. In this build the context will be smaller, and the library will
137
* exclude the functionality which is only needed for compression. This reduces the binary size by
138
* ~180KB. For these builds contexts must be created with the ASTCENC_FLG_DECOMPRESS_ONLY flag.
139
*
140
* Note that context structures returned by a library built as decompress-only are incompatible with
141
* a library built with compression included, and visa versa, as they have different sizes and
142
* memory layout.
143
*
144
* Self-decompress-only usage
145
* ==========================
146
*
147
* ASTC is a complex format with a large search space. The parts of this search space that are
148
* searched is determined by heuristics that are, in part, tied to the quality level used when
149
* creating the context.
150
*
151
* A normal context is capable of decompressing any ASTC texture, including those generated by other
152
* compressors with unknown heuristics. This is the most flexible implementation, but forces the
153
* data tables used by the codec to include entries that are not needed during compression. This
154
* can slow down context creation by a significant amount, especially for the faster compression
155
* modes where few data table entries are actually used. To optimize this use case the context can
156
* be created with the ASTCENC_FLG_SELF_DECOMPRESS_ONLY flag. This tells the compressor that it will
157
* only be asked to decompress images that it compressed itself, allowing the data tables to
158
* exclude entries that are not needed by the current compression configuration. This reduces the
159
* size of the context data tables in memory and improves context creation performance. Note that,
160
* as of the 3.6 release, this flag no longer affects compression performance.
161
*
162
* Using this flag while attempting to decompress an valid image which was created by another
163
* compressor, or even another astcenc compressor version or configuration, may result in blocks
164
* returning as solid magenta or NaN value error blocks.
165
*/
166
167
#ifndef ASTCENC_INCLUDED
168
#define ASTCENC_INCLUDED
169
170
#include <cstddef>
171
#include <cstdint>
172
173
#if defined(ASTCENC_DYNAMIC_LIBRARY)
174
#if defined(_MSC_VER)
175
#define ASTCENC_PUBLIC extern "C" __declspec(dllexport)
176
#else
177
#define ASTCENC_PUBLIC extern "C" __attribute__ ((visibility ("default")))
178
#endif
179
#else
180
#define ASTCENC_PUBLIC
181
#endif
182
183
/* ============================================================================
184
Data declarations
185
============================================================================ */
186
187
/**
188
* @brief An opaque structure; see astcenc_internal.h for definition.
189
*/
190
struct astcenc_context;
191
192
/**
193
* @brief A codec API error code.
194
*/
195
enum astcenc_error {
196
/** @brief The call was successful. */
197
ASTCENC_SUCCESS = 0,
198
/** @brief The call failed due to low memory, or undersized I/O buffers. */
199
ASTCENC_ERR_OUT_OF_MEM,
200
/** @brief The call failed due to the build using fast math. */
201
ASTCENC_ERR_BAD_CPU_FLOAT,
202
/** @brief The call failed due to an out-of-spec parameter. */
203
ASTCENC_ERR_BAD_PARAM,
204
/** @brief The call failed due to an out-of-spec block size. */
205
ASTCENC_ERR_BAD_BLOCK_SIZE,
206
/** @brief The call failed due to an out-of-spec color profile. */
207
ASTCENC_ERR_BAD_PROFILE,
208
/** @brief The call failed due to an out-of-spec quality value. */
209
ASTCENC_ERR_BAD_QUALITY,
210
/** @brief The call failed due to an out-of-spec component swizzle. */
211
ASTCENC_ERR_BAD_SWIZZLE,
212
/** @brief The call failed due to an out-of-spec flag set. */
213
ASTCENC_ERR_BAD_FLAGS,
214
/** @brief The call failed due to the context not supporting the operation. */
215
ASTCENC_ERR_BAD_CONTEXT,
216
/** @brief The call failed due to unimplemented functionality. */
217
ASTCENC_ERR_NOT_IMPLEMENTED,
218
/** @brief The call failed due to an out-of-spec decode mode flag set. */
219
ASTCENC_ERR_BAD_DECODE_MODE,
220
#if defined(ASTCENC_DIAGNOSTICS)
221
/** @brief The call failed due to an issue with diagnostic tracing. */
222
ASTCENC_ERR_DTRACE_FAILURE,
223
#endif
224
};
225
226
/**
227
* @brief A codec color profile.
228
*/
229
enum astcenc_profile {
230
/** @brief The LDR sRGB color profile. */
231
ASTCENC_PRF_LDR_SRGB = 0,
232
/** @brief The LDR linear color profile. */
233
ASTCENC_PRF_LDR,
234
/** @brief The HDR RGB with LDR alpha color profile. */
235
ASTCENC_PRF_HDR_RGB_LDR_A,
236
/** @brief The HDR RGBA color profile. */
237
ASTCENC_PRF_HDR
238
};
239
240
/** @brief The fastest, lowest quality, search preset. */
241
static const float ASTCENC_PRE_FASTEST = 0.0f;
242
243
/** @brief The fast search preset. */
244
static const float ASTCENC_PRE_FAST = 10.0f;
245
246
/** @brief The medium quality search preset. */
247
static const float ASTCENC_PRE_MEDIUM = 60.0f;
248
249
/** @brief The thorough quality search preset. */
250
static const float ASTCENC_PRE_THOROUGH = 98.0f;
251
252
/** @brief The thorough quality search preset. */
253
static const float ASTCENC_PRE_VERYTHOROUGH = 99.0f;
254
255
/** @brief The exhaustive, highest quality, search preset. */
256
static const float ASTCENC_PRE_EXHAUSTIVE = 100.0f;
257
258
/**
259
* @brief A codec component swizzle selector.
260
*/
261
enum astcenc_swz
262
{
263
/** @brief Select the red component. */
264
ASTCENC_SWZ_R = 0,
265
/** @brief Select the green component. */
266
ASTCENC_SWZ_G = 1,
267
/** @brief Select the blue component. */
268
ASTCENC_SWZ_B = 2,
269
/** @brief Select the alpha component. */
270
ASTCENC_SWZ_A = 3,
271
/** @brief Use a constant zero component. */
272
ASTCENC_SWZ_0 = 4,
273
/** @brief Use a constant one component. */
274
ASTCENC_SWZ_1 = 5,
275
/** @brief Use a reconstructed normal vector Z component. */
276
ASTCENC_SWZ_Z = 6
277
};
278
279
/**
280
* @brief A texel component swizzle.
281
*/
282
struct astcenc_swizzle
283
{
284
/** @brief The red component selector. */
285
astcenc_swz r;
286
/** @brief The green component selector. */
287
astcenc_swz g;
288
/** @brief The blue component selector. */
289
astcenc_swz b;
290
/** @brief The alpha component selector. */
291
astcenc_swz a;
292
};
293
294
/**
295
* @brief A texel component data format.
296
*/
297
enum astcenc_type
298
{
299
/** @brief Unorm 8-bit data per component. */
300
ASTCENC_TYPE_U8 = 0,
301
/** @brief 16-bit float per component. */
302
ASTCENC_TYPE_F16 = 1,
303
/** @brief 32-bit float per component. */
304
ASTCENC_TYPE_F32 = 2
305
};
306
307
/**
308
* @brief Function pointer type for compression progress reporting callback.
309
*/
310
extern "C" typedef void (*astcenc_progress_callback)(float);
311
312
/**
313
* @brief Enable normal map compression.
314
*
315
* Input data will be treated a two component normal map, storing X and Y, and the codec will
316
* optimize for angular error rather than simple linear PSNR. In this mode the input swizzle should
317
* be e.g. rrrg (the default ordering for ASTC normals on the command line) or gggr (the ordering
318
* used by BC5n).
319
*/
320
static const unsigned int ASTCENC_FLG_MAP_NORMAL = 1 << 0;
321
322
/**
323
* @brief Enable compression heuristics that assume use of decode_unorm8 decode mode.
324
*
325
* The decode_unorm8 decode mode rounds differently to the decode_fp16 decode mode, so enabling this
326
* flag during compression will allow the compressor to use the correct rounding when selecting
327
* encodings. This will improve the compressed image quality if your application is using the
328
* decode_unorm8 decode mode, but will reduce image quality if using decode_fp16.
329
*
330
* Note that LDR_SRGB images will always use decode_unorm8 for the RGB channels, irrespective of
331
* this setting.
332
*/
333
static const unsigned int ASTCENC_FLG_USE_DECODE_UNORM8 = 1 << 1;
334
335
/**
336
* @brief Enable alpha weighting.
337
*
338
* The input alpha value is used for transparency, so errors in the RGB components are weighted by
339
* the transparency level. This allows the codec to more accurately encode the alpha value in areas
340
* where the color value is less significant.
341
*/
342
static const unsigned int ASTCENC_FLG_USE_ALPHA_WEIGHT = 1 << 2;
343
344
/**
345
* @brief Enable perceptual error metrics.
346
*
347
* This mode enables perceptual compression mode, which will optimize for perceptual error rather
348
* than best PSNR. Only some input modes support perceptual error metrics.
349
*/
350
static const unsigned int ASTCENC_FLG_USE_PERCEPTUAL = 1 << 3;
351
352
/**
353
* @brief Create a decompression-only context.
354
*
355
* This mode disables support for compression. This enables context allocation to skip some
356
* transient buffer allocation, resulting in lower memory usage.
357
*/
358
static const unsigned int ASTCENC_FLG_DECOMPRESS_ONLY = 1 << 4;
359
360
/**
361
* @brief Create a self-decompression context.
362
*
363
* This mode configures the compressor so that it is only guaranteed to be able to decompress images
364
* that were actually created using the current context. This is the common case for compression use
365
* cases, and setting this flag enables additional optimizations, but does mean that the context
366
* cannot reliably decompress arbitrary ASTC images.
367
*/
368
static const unsigned int ASTCENC_FLG_SELF_DECOMPRESS_ONLY = 1 << 5;
369
370
/**
371
* @brief Enable RGBM map compression.
372
*
373
* Input data will be treated as HDR data that has been stored in an LDR RGBM-encoded wrapper
374
* format. Data must be preprocessed by the user to be in LDR RGBM format before calling the
375
* compression function, this flag is only used to control the use of RGBM-specific heuristics and
376
* error metrics.
377
*
378
* IMPORTANT: The ASTC format is prone to bad failure modes with unconstrained RGBM data; very small
379
* M values can round to zero due to quantization and result in black or white pixels. It is highly
380
* recommended that the minimum value of M used in the encoding is kept above a lower threshold (try
381
* 16 or 32). Applying this threshold reduces the number of very dark colors that can be
382
* represented, but is still higher precision than 8-bit LDR.
383
*
384
* When this flag is set the value of @c rgbm_m_scale in the context must be set to the RGBM scale
385
* factor used during reconstruction. This defaults to 5 when in RGBM mode.
386
*
387
* It is recommended that the value of @c cw_a_weight is set to twice the value of the multiplier
388
* scale, ensuring that the M value is accurately encoded. This defaults to 10 when in RGBM mode,
389
* matching the default scale factor.
390
*/
391
static const unsigned int ASTCENC_FLG_MAP_RGBM = 1 << 6;
392
393
/**
394
* @brief The bit mask of all valid flags.
395
*/
396
static const unsigned int ASTCENC_ALL_FLAGS =
397
ASTCENC_FLG_MAP_NORMAL |
398
ASTCENC_FLG_MAP_RGBM |
399
ASTCENC_FLG_USE_ALPHA_WEIGHT |
400
ASTCENC_FLG_USE_PERCEPTUAL |
401
ASTCENC_FLG_USE_DECODE_UNORM8 |
402
ASTCENC_FLG_DECOMPRESS_ONLY |
403
ASTCENC_FLG_SELF_DECOMPRESS_ONLY;
404
405
/**
406
* @brief The config structure.
407
*
408
* This structure will initially be populated by a call to astcenc_config_init, but power users may
409
* modify it before calling astcenc_context_alloc. See astcenccli_toplevel_help.cpp for full user
410
* documentation of the power-user settings.
411
*
412
* Note for any settings which are associated with a specific color component, the value in the
413
* config applies to the component that exists after any compression data swizzle is applied.
414
*/
415
struct astcenc_config
416
{
417
/** @brief The color profile. */
418
astcenc_profile profile;
419
420
/** @brief The set of set flags. */
421
unsigned int flags;
422
423
/** @brief The ASTC block size X dimension. */
424
unsigned int block_x;
425
426
/** @brief The ASTC block size Y dimension. */
427
unsigned int block_y;
428
429
/** @brief The ASTC block size Z dimension. */
430
unsigned int block_z;
431
432
/** @brief The red component weight scale for error weighting (-cw). */
433
float cw_r_weight;
434
435
/** @brief The green component weight scale for error weighting (-cw). */
436
float cw_g_weight;
437
438
/** @brief The blue component weight scale for error weighting (-cw). */
439
float cw_b_weight;
440
441
/** @brief The alpha component weight scale for error weighting (-cw). */
442
float cw_a_weight;
443
444
/**
445
* @brief The radius for any alpha-weight scaling (-a).
446
*
447
* It is recommended that this is set to 1 when using FLG_USE_ALPHA_WEIGHT on a texture that
448
* will be sampled using linear texture filtering to minimize color bleed out of transparent
449
* texels that are adjacent to non-transparent texels.
450
*/
451
unsigned int a_scale_radius;
452
453
/** @brief The RGBM scale factor for the shared multiplier (-rgbm). */
454
float rgbm_m_scale;
455
456
/**
457
* @brief The maximum number of partitions searched (-partitioncountlimit).
458
*
459
* Valid values are between 1 and 4.
460
*/
461
unsigned int tune_partition_count_limit;
462
463
/**
464
* @brief The maximum number of partitions searched (-2partitionindexlimit).
465
*
466
* Valid values are between 1 and 1024.
467
*/
468
unsigned int tune_2partition_index_limit;
469
470
/**
471
* @brief The maximum number of partitions searched (-3partitionindexlimit).
472
*
473
* Valid values are between 1 and 1024.
474
*/
475
unsigned int tune_3partition_index_limit;
476
477
/**
478
* @brief The maximum number of partitions searched (-4partitionindexlimit).
479
*
480
* Valid values are between 1 and 1024.
481
*/
482
unsigned int tune_4partition_index_limit;
483
484
/**
485
* @brief The maximum centile for block modes searched (-blockmodelimit).
486
*
487
* Valid values are between 1 and 100.
488
*/
489
unsigned int tune_block_mode_limit;
490
491
/**
492
* @brief The maximum iterative refinements applied (-refinementlimit).
493
*
494
* Valid values are between 1 and N; there is no technical upper limit
495
* but little benefit is expected after N=4.
496
*/
497
unsigned int tune_refinement_limit;
498
499
/**
500
* @brief The number of trial candidates per mode search (-candidatelimit).
501
*
502
* Valid values are between 1 and TUNE_MAX_TRIAL_CANDIDATES.
503
*/
504
unsigned int tune_candidate_limit;
505
506
/**
507
* @brief The number of trial partitionings per search (-2partitioncandidatelimit).
508
*
509
* Valid values are between 1 and TUNE_MAX_PARTITIONING_CANDIDATES.
510
*/
511
unsigned int tune_2partitioning_candidate_limit;
512
513
/**
514
* @brief The number of trial partitionings per search (-3partitioncandidatelimit).
515
*
516
* Valid values are between 1 and TUNE_MAX_PARTITIONING_CANDIDATES.
517
*/
518
unsigned int tune_3partitioning_candidate_limit;
519
520
/**
521
* @brief The number of trial partitionings per search (-4partitioncandidatelimit).
522
*
523
* Valid values are between 1 and TUNE_MAX_PARTITIONING_CANDIDATES.
524
*/
525
unsigned int tune_4partitioning_candidate_limit;
526
527
/**
528
* @brief The dB threshold for stopping block search (-dblimit).
529
*
530
* This option is ineffective for HDR textures.
531
*/
532
float tune_db_limit;
533
534
/**
535
* @brief The amount of MSE overshoot needed to early-out trials.
536
*
537
* The first early-out is for 1 partition, 1 plane trials, where we try a minimal encode using
538
* the high probability block modes. This can short-cut compression for simple blocks.
539
*
540
* The second early-out is for refinement trials, where we can exit refinement once quality is
541
* reached.
542
*/
543
float tune_mse_overshoot;
544
545
/**
546
* @brief The threshold for skipping 3.1/4.1 trials (-2partitionlimitfactor).
547
*
548
* This option is further scaled for normal maps, so it skips less often.
549
*/
550
float tune_2partition_early_out_limit_factor;
551
552
/**
553
* @brief The threshold for skipping 4.1 trials (-3partitionlimitfactor).
554
*
555
* This option is further scaled for normal maps, so it skips less often.
556
*/
557
float tune_3partition_early_out_limit_factor;
558
559
/**
560
* @brief The threshold for skipping two weight planes (-2planelimitcorrelation).
561
*
562
* This option is ineffective for normal maps.
563
*/
564
float tune_2plane_early_out_limit_correlation;
565
566
/**
567
* @brief The config enable for the mode0 fast-path search.
568
*
569
* If this is set to TUNE_MIN_TEXELS_MODE0 or higher then the early-out fast mode0
570
* search is enabled. This option is ineffective for 3D block sizes.
571
*/
572
float tune_search_mode0_enable;
573
574
/**
575
* @brief The progress callback, can be @c nullptr.
576
*
577
* If this is specified the codec will peridocially report progress for
578
* compression as a percentage between 0 and 100. The callback is called from one
579
* of the compressor threads, so doing significant work in the callback will
580
* reduce compression performance.
581
*/
582
astcenc_progress_callback progress_callback;
583
584
#if defined(ASTCENC_DIAGNOSTICS)
585
/**
586
* @brief The path to save the diagnostic trace data to.
587
*
588
* This option is not part of the public API, and requires special builds
589
* of the library.
590
*/
591
const char* trace_file_path;
592
#endif
593
};
594
595
/**
596
* @brief An uncompressed 2D or 3D image.
597
*
598
* 3D image are passed in as an array of 2D slices. Each slice has identical
599
* size and color format.
600
*/
601
struct astcenc_image
602
{
603
/** @brief The X dimension of the image, in texels. */
604
unsigned int dim_x;
605
606
/** @brief The Y dimension of the image, in texels. */
607
unsigned int dim_y;
608
609
/** @brief The Z dimension of the image, in texels. */
610
unsigned int dim_z;
611
612
/** @brief The data type per component. */
613
astcenc_type data_type;
614
615
/** @brief The array of 2D slices, of length @c dim_z. */
616
void** data;
617
};
618
619
/**
620
* @brief A block encoding metadata query result.
621
*
622
* If the block is an error block or a constant color block or an error block all fields other than
623
* the profile, block dimensions, and error/constant indicator will be zero.
624
*/
625
struct astcenc_block_info
626
{
627
/** @brief The block encoding color profile. */
628
astcenc_profile profile;
629
630
/** @brief The number of texels in the X dimension. */
631
unsigned int block_x;
632
633
/** @brief The number of texels in the Y dimension. */
634
unsigned int block_y;
635
636
/** @brief The number of texel in the Z dimension. */
637
unsigned int block_z;
638
639
/** @brief The number of texels in the block. */
640
unsigned int texel_count;
641
642
/** @brief True if this block is an error block. */
643
bool is_error_block;
644
645
/** @brief True if this block is a constant color block. */
646
bool is_constant_block;
647
648
/** @brief True if this block is an HDR block. */
649
bool is_hdr_block;
650
651
/** @brief True if this block uses two weight planes. */
652
bool is_dual_plane_block;
653
654
/** @brief The number of partitions if not constant color. */
655
unsigned int partition_count;
656
657
/** @brief The partition index if 2 - 4 partitions used. */
658
unsigned int partition_index;
659
660
/** @brief The component index of the second plane if dual plane. */
661
unsigned int dual_plane_component;
662
663
/** @brief The color endpoint encoding mode for each partition. */
664
unsigned int color_endpoint_modes[4];
665
666
/** @brief The number of color endpoint quantization levels. */
667
unsigned int color_level_count;
668
669
/** @brief The number of weight quantization levels. */
670
unsigned int weight_level_count;
671
672
/** @brief The number of weights in the X dimension. */
673
unsigned int weight_x;
674
675
/** @brief The number of weights in the Y dimension. */
676
unsigned int weight_y;
677
678
/** @brief The number of weights in the Z dimension. */
679
unsigned int weight_z;
680
681
/** @brief The unpacked color endpoints for each partition. */
682
float color_endpoints[4][2][4];
683
684
/** @brief The per-texel interpolation weights for the block. */
685
float weight_values_plane1[216];
686
687
/** @brief The per-texel interpolation weights for the block. */
688
float weight_values_plane2[216];
689
690
/** @brief The per-texel partition assignments for the block. */
691
uint8_t partition_assignment[216];
692
};
693
694
/**
695
* Populate a codec config based on default settings.
696
*
697
* Power users can edit the returned config struct to fine tune before allocating the context.
698
*
699
* @param profile Color profile.
700
* @param block_x ASTC block size X dimension.
701
* @param block_y ASTC block size Y dimension.
702
* @param block_z ASTC block size Z dimension.
703
* @param quality Search quality preset / effort level. Either an
704
* @c ASTCENC_PRE_* value, or a effort level between 0
705
* and 100. Performance is not linear between 0 and 100.
706
707
* @param flags A valid set of @c ASTCENC_FLG_* flag bits.
708
* @param[out] config Output config struct to populate.
709
*
710
* @return @c ASTCENC_SUCCESS on success, or an error if the inputs are invalid
711
* either individually, or in combination.
712
*/
713
ASTCENC_PUBLIC astcenc_error astcenc_config_init(
714
astcenc_profile profile,
715
unsigned int block_x,
716
unsigned int block_y,
717
unsigned int block_z,
718
float quality,
719
unsigned int flags,
720
astcenc_config* config);
721
722
/**
723
* @brief Allocate a new codec context based on a config.
724
*
725
* This function allocates all of the memory resources and threads needed by the codec. This can be
726
* slow, so it is recommended that contexts are reused to serially compress or decompress multiple
727
* images to amortize setup cost.
728
*
729
* Contexts can be allocated to support only decompression using the @c ASTCENC_FLG_DECOMPRESS_ONLY
730
* flag when creating the configuration. The compression functions will fail if invoked. For a
731
* decompress-only library build the @c ASTCENC_FLG_DECOMPRESS_ONLY flag must be set when creating
732
* any context.
733
*
734
* @param[in] config Codec config.
735
* @param thread_count Thread count to configure for.
736
* @param[out] context Location to store an opaque context pointer.
737
*
738
* @return @c ASTCENC_SUCCESS on success, or an error if context creation failed.
739
*/
740
ASTCENC_PUBLIC astcenc_error astcenc_context_alloc(
741
const astcenc_config* config,
742
unsigned int thread_count,
743
astcenc_context** context);
744
745
/**
746
* @brief Compress an image.
747
*
748
* A single context can only compress or decompress a single image at a time.
749
*
750
* For a context configured for multi-threading, any set of the N threads can call this function.
751
* Work will be dynamically scheduled across the threads available. Each thread must have a unique
752
* @c thread_index.
753
*
754
* @param context Codec context.
755
* @param[in,out] image An input image, in 2D slices.
756
* @param swizzle Compression data swizzle, applied before compression.
757
* @param[out] data_out Pointer to output data array.
758
* @param data_len Length of the output data array.
759
* @param thread_index Thread index [0..N-1] of calling thread.
760
*
761
* @return @c ASTCENC_SUCCESS on success, or an error if compression failed.
762
*/
763
ASTCENC_PUBLIC astcenc_error astcenc_compress_image(
764
astcenc_context* context,
765
astcenc_image* image,
766
const astcenc_swizzle* swizzle,
767
uint8_t* data_out,
768
size_t data_len,
769
unsigned int thread_index);
770
771
/**
772
* @brief Reset the codec state for a new compression.
773
*
774
* The caller is responsible for synchronizing threads in the worker thread pool. This function must
775
* only be called when all threads have exited the @c astcenc_compress_image() function for image N,
776
* but before any thread enters it for image N + 1.
777
*
778
* Calling this is not required (but won't hurt), if the context is created for single threaded use.
779
*
780
* @param context Codec context.
781
*
782
* @return @c ASTCENC_SUCCESS on success, or an error if reset failed.
783
*/
784
ASTCENC_PUBLIC astcenc_error astcenc_compress_reset(
785
astcenc_context* context);
786
787
/**
788
* @brief Cancel any pending compression operation.
789
*
790
* The caller must behave as if the compression completed normally, even though the data will be
791
* undefined. They are still responsible for synchronizing threads in the worker thread pool, and
792
* must call reset before starting another compression.
793
*
794
* @param context Codec context.
795
*
796
* @return @c ASTCENC_SUCCESS on success, or an error if cancellation failed.
797
*/
798
ASTCENC_PUBLIC astcenc_error astcenc_compress_cancel(
799
astcenc_context* context);
800
801
/**
802
* @brief Decompress an image.
803
*
804
* @param context Codec context.
805
* @param[in] data Pointer to compressed data.
806
* @param data_len Length of the compressed data, in bytes.
807
* @param[in,out] image_out Output image.
808
* @param swizzle Decompression data swizzle, applied after decompression.
809
* @param thread_index Thread index [0..N-1] of calling thread.
810
*
811
* @return @c ASTCENC_SUCCESS on success, or an error if decompression failed.
812
*/
813
ASTCENC_PUBLIC astcenc_error astcenc_decompress_image(
814
astcenc_context* context,
815
const uint8_t* data,
816
size_t data_len,
817
astcenc_image* image_out,
818
const astcenc_swizzle* swizzle,
819
unsigned int thread_index);
820
821
/**
822
* @brief Reset the codec state for a new decompression.
823
*
824
* The caller is responsible for synchronizing threads in the worker thread pool. This function must
825
* only be called when all threads have exited the @c astcenc_decompress_image() function for image
826
* N, but before any thread enters it for image N + 1.
827
*
828
* Calling this is not required (but won't hurt), if the context is created for single threaded use.
829
*
830
* @param context Codec context.
831
*
832
* @return @c ASTCENC_SUCCESS on success, or an error if reset failed.
833
*/
834
ASTCENC_PUBLIC astcenc_error astcenc_decompress_reset(
835
astcenc_context* context);
836
837
/**
838
* Free the compressor context.
839
*
840
* @param context The codec context.
841
*/
842
ASTCENC_PUBLIC void astcenc_context_free(
843
astcenc_context* context);
844
845
/**
846
* @brief Provide a high level summary of a block's encoding.
847
*
848
* This feature is primarily useful for codec developers but may be useful for developers building
849
* advanced content packaging pipelines.
850
*
851
* @param context Codec context.
852
* @param data One block of compressed ASTC data.
853
* @param info The output info structure to populate.
854
*
855
* @return @c ASTCENC_SUCCESS if the block was decoded, or an error otherwise. Note that this
856
* function will return success even if the block itself was an error block encoding, as the
857
* decode was correctly handled.
858
*/
859
ASTCENC_PUBLIC astcenc_error astcenc_get_block_info(
860
astcenc_context* context,
861
const uint8_t data[16],
862
astcenc_block_info* info);
863
864
/**
865
* @brief Get a printable string for specific status code.
866
*
867
* @param status The status value.
868
*
869
* @return A human readable nul-terminated string.
870
*/
871
ASTCENC_PUBLIC const char* astcenc_get_error_string(
872
astcenc_error status);
873
874
#endif
875
876