blob: 8c6eb3795cd8d41b4627060fc2eec0fae31205df [file] [edit]
// SPDX-License-Identifier: Apache-2.0
// ----------------------------------------------------------------------------
// Copyright 2025-2026 Arm Limited
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
// ----------------------------------------------------------------------------
/**
* @brief Unit tests for the vectorized SIMD functionality.
*/
#include <limits>
#include <memory>
#include <vector>
#include "gtest/gtest.h"
#include "../astcenc_internal.h"
namespace astcenc
{
/** @brief Weighted scoring returns safe partial errors without changing surviving candidates. */
TEST(compress, weighted_error_cutoff)
{
astcenc_config config {};
config.profile = ASTCENC_PRF_LDR;
for (unsigned int block_size : { 4u, 6u })
{
auto bsd = std::make_unique<block_size_descriptor>();
bool is_initialized = init_block_size_descriptor(block_size, block_size, 1, true, 1, 1.0f, *bsd);
ASSERT_TRUE(is_initialized);
const block_mode& bm = bsd->block_modes[0];
const decimation_info& di = bsd->get_decimation_info(bm.decimation_mode);
symbolic_compressed_block scb {};
scb.block_type = SYM_BTYPE_NONCONST;
scb.block_mode = bm.mode_index;
scb.partition_count = 1;
scb.plane2_component = -1;
for (bool is_rgb : { false, true })
{
image_block blk {};
blk.texel_count = block_size * block_size;
blk.channel_weight = vfloat4(1.0f);
float alpha = 256.0f;
int endpoint_alpha = 0;
if (is_rgb)
{
alpha = 65535.0f;
endpoint_alpha = 65535;
}
blk.data_min = vfloat4(256.0f, 256.0f, 256.0f, alpha);
blk.data_max = blk.data_min;
float weights[BLOCK_MAX_TEXELS] {};
for (unsigned int i = 0; i < blk.texel_count; i++)
{
blk.data_r[i] = 256.0f;
blk.data_g[i] = 256.0f;
blk.data_b[i] = 256.0f;
blk.data_a[i] = alpha;
weights[i] = 1.0f;
}
vint4 endpoint(0, 0, 0, endpoint_alpha);
float full_error = compute_symbolic_block_difference_1plane_1partition_weighted(
config, *bsd, di, scb, blk, endpoint, endpoint, weights, ERROR_CALC_DEFAULT);
EXPECT_GT(full_error, 0.0f);
float error_limit = full_error * 0.25f;
float partial_error = compute_symbolic_block_difference_1plane_1partition_weighted(
config, *bsd, di, scb, blk, endpoint, endpoint, weights, error_limit);
EXPECT_GE(partial_error, error_limit);
if (blk.texel_count > 16)
{
EXPECT_LT(partial_error, full_error);
}
else
{
EXPECT_EQ(partial_error, full_error);
}
float boundary_error = compute_symbolic_block_difference_1plane_1partition_weighted(
config, *bsd, di, scb, blk, endpoint, endpoint, weights, partial_error);
EXPECT_EQ(boundary_error, partial_error);
float surviving_error = compute_symbolic_block_difference_1plane_1partition_weighted(
config, *bsd, di, scb, blk, endpoint, endpoint, weights, full_error * 2.0f);
EXPECT_EQ(surviving_error, full_error);
}
}
}
/** @brief State for cancellation from an LZ-RDO progress callback. */
struct lz_rdo_cancel_state
{
/** @brief The context to cancel. */
astcenc_context* context;
/** @brief The progress percentage at which to cancel. */
float cancel_at;
/** @brief The last reported progress percentage. */
float last_progress;
/** @brief Whether cancellation has been requested. */
bool is_cancelled;
/** @brief The result of the cancellation request. */
astcenc_error cancel_status;
};
/** @brief The active single-threaded cancellation test state. */
static lz_rdo_cancel_state lz_rdo_cancel;
/**
* @brief Cancel compression when progress reaches the requested stage.
*
* @param progress The compression completion percentage.
*/
static void cancel_lz_rdo_progress(float progress)
{
lz_rdo_cancel.last_progress = progress;
if (!lz_rdo_cancel.is_cancelled && (progress >= lz_rdo_cancel.cancel_at))
{
lz_rdo_cancel.is_cancelled = true;
lz_rdo_cancel.cancel_status = astcenc_compress_cancel(lz_rdo_cancel.context);
}
}
/** @brief Cancelling either encode stage stops pending LZ-RDO work and permits context reuse. */
TEST(compress, lz_rdo_cancellation)
{
// Enough blocks to report progress before the initial encode completes
constexpr size_t dim = 512;
std::vector<uint8_t> pixels(dim * dim * 4, 255);
std::vector<uint8_t> data((dim / 4) * (dim / 4) * 16);
void* slices[] { pixels.data() };
astcenc_image image {
.dim_x = dim,
.dim_y = dim,
.dim_z = 1,
.data_type = ASTCENC_TYPE_U8,
.data = slices,
.row_stride = 0
};
const astcenc_swizzle swizzle {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
// The initial encode reports up to 33%; later callbacks are from LZ-RDO
for (float cancel_at : { 0.0f, 34.0f })
{
astcenc_config config;
astcenc_error status = astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1,
ASTCENC_PRE_FASTEST, ASTCENC_FLG_USE_LZ_RDO,
&config);
ASSERT_EQ(status, ASTCENC_SUCCESS);
config.lz_rdo_lambda = 20.0f;
config.progress_callback = cancel_lz_rdo_progress;
astcenc_context* context;
status = astcenc_context_alloc(&config, 1, &context, nullptr);
ASSERT_EQ(status, ASTCENC_SUCCESS);
std::unique_ptr<astcenc_context, decltype(&astcenc_context_free)> context_guard(
context, astcenc_context_free);
lz_rdo_cancel = {
.context = context,
.cancel_at = cancel_at,
.last_progress = 0.0f,
.is_cancelled = false,
.cancel_status = ASTCENC_SUCCESS
};
status = astcenc_compress_image(context, &image, &swizzle, data.data(), data.size(), 0);
EXPECT_EQ(status, ASTCENC_SUCCESS);
EXPECT_TRUE(lz_rdo_cancel.is_cancelled);
EXPECT_EQ(lz_rdo_cancel.cancel_status, ASTCENC_SUCCESS);
EXPECT_GE(lz_rdo_cancel.last_progress, cancel_at);
EXPECT_LT(lz_rdo_cancel.last_progress, 100.0f);
// Reset must clear cancellation in every stage before the next image
status = astcenc_compress_reset(context);
EXPECT_EQ(status, ASTCENC_SUCCESS);
lz_rdo_cancel.cancel_at = 101.0f;
lz_rdo_cancel.last_progress = 0.0f;
status = astcenc_compress_image(context, &image, &swizzle, data.data(), data.size(), 0);
EXPECT_EQ(status, ASTCENC_SUCCESS);
EXPECT_EQ(lz_rdo_cancel.last_progress, 100.0f);
}
}
/** @brief Failed searches fall back to the block mean with profile-correct color encoding. */
TEST(compress, mean_color_fallback)
{
auto bsd = std::make_unique<block_size_descriptor>();
auto working_buffers = std::make_unique<compression_working_buffers>();
bool is_initialized = init_block_size_descriptor(4, 4, 1, true, 1, 1.0f, *bsd);
ASSERT_TRUE(is_initialized);
prepare_angular_tables();
// Make every single-plane mode exceed the endpoint bit budget to force search failure
for (unsigned int i = 0; i < bsd->block_mode_count_1plane_selected; i++)
{
bsd->block_modes[i].weight_bits = 127;
}
const astcenc_profile profiles[] {
ASTCENC_PRF_LDR, ASTCENC_PRF_LDR_SRGB, ASTCENC_PRF_HDR_RGB_LDR_A, ASTCENC_PRF_HDR
};
const astcenc_swizzle swizzle {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
for (astcenc_profile profile : profiles)
{
astcenc_contexti ctx {};
astcenc_error status = astcenc_config_init(profile, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0,
&ctx.config);
ASSERT_EQ(status, ASTCENC_SUCCESS);
ctx.config.tune_partition_count_limit = 1;
ctx.config.tune_candidate_limit = 1;
ctx.bsd = bsd.get();
float low = 0.25f;
float high = 0.75f;
float expected = 0.5f;
float expected_alpha = 0.5f;
int expected_type = SYM_BTYPE_CONST_U16;
if (profile == ASTCENC_PRF_LDR_SRGB)
{
expected = 128.0f / 255.0f;
expected_alpha = expected;
}
if ((profile == ASTCENC_PRF_HDR) || (profile == ASTCENC_PRF_HDR_RGB_LDR_A))
{
low = 16.0f;
high = 64.0f;
expected = 32.0f;
expected_type = SYM_BTYPE_CONST_F16;
}
float pixels[16 * 4];
for (unsigned int i = 0; i < 16; i++)
{
float color = low;
if (i >= 8)
{
color = high;
}
pixels[4 * i] = color;
pixels[4 * i + 1] = color;
pixels[4 * i + 2] = color;
pixels[4 * i + 3] = 0.5f;
}
void* slices[] { pixels };
astcenc_image image {
.dim_x = 4,
.dim_y = 4,
.dim_z = 1,
.data_type = ASTCENC_TYPE_F32,
.data = slices,
.row_stride = 4 * 4 * sizeof(float)
};
image_block blk {};
blk.texel_count = 16;
blk.channel_weight = vfloat4(1.0f);
load_image_block(profile, image, blk, *bsd, 0, 0, 0, swizzle);
// Grayscale with constant alpha skips the dual-plane search
uint8_t physical[16];
compress_block(ctx, blk, physical, nullptr, *working_buffers);
symbolic_compressed_block symbolic;
physical_to_symbolic(*bsd, physical, symbolic);
EXPECT_EQ(symbolic.block_type, expected_type);
image_block decoded {};
bool is_successful = decompress_symbolic_block(profile, *bsd, 0, 0, 0, symbolic, decoded);
ASSERT_TRUE(is_successful);
EXPECT_NEAR(decoded.data_r[0], expected, expected * 0.001f);
EXPECT_NEAR(decoded.data_g[0], expected, expected * 0.001f);
EXPECT_NEAR(decoded.data_b[0], expected, expected * 0.001f);
EXPECT_NEAR(decoded.data_a[0], expected_alpha, 0.001f);
}
}
/** @brief Preserve HDR mode bits when endpoint quantization rounds down across a bit boundary. */
TEST(compress, hdr_quantization_mode_bits)
{
const vfloat4 colors[] {
vfloat4(31806.0f, 27290.0f, 27570.0f, 65535.0f),
vfloat4(30545.0f, 26180.0f, 28316.0f, 65535.0f)
};
const float offsets[] { 1209.0f, 1246.0f };
const int formats[] { FMT_HDR_RGB_SCALE, FMT_HDR_RGB };
const float error_limits[] { 5000000.0f, 300000.0f };
for (size_t i = 0; i < 2; i++)
{
vfloat4 ep0 = colors[i];
vfloat4 ep1 = ep0 + vfloat4(offsets[i], offsets[i], offsets[i], 0.0f);
vfloat4 rgbo = ep0;
rgbo.set_lane<3>(offsets[i]);
uint8_t values[8];
int format = pack_color_endpoints(ep0, ep1, vfloat4::zero(), rgbo,
formats[i], values, QUANT_16);
EXPECT_EQ(format, formats[i]);
bool is_rgb_hdr;
bool is_alpha_hdr;
vint4 decoded0;
vint4 decoded1;
unpack_color_endpoints(ASTCENC_PRF_HDR_RGB_LDR_A, format, values,
&is_rgb_hdr, &is_alpha_hdr, decoded0, decoded1);
EXPECT_TRUE(is_rgb_hdr);
EXPECT_FALSE(is_alpha_hdr);
vfloat4 diff0 = ep0 - int_to_float(decoded0);
vfloat4 diff1 = ep1 - int_to_float(decoded1);
float error = hadd_rgb_s(diff0 * diff0 + diff1 * diff1);
EXPECT_LT(error, error_limits[i]);
}
}
/** @brief Input overflow tests for xy * z. */
TEST(compress, overflow_in_z)
{
astcenc_error status;
astcenc_config config;
astcenc_context* context;
static const astcenc_swizzle swizzle {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0, &config);
status = astcenc_context_alloc(&config, 1, &context, nullptr);
EXPECT_EQ(status, ASTCENC_SUCCESS);
// Arrays are too short, but should never be touched
uint8_t data[1];
uint8_t input[1];
uint8_t* slices = input;
// x * y always fits in 64-bit size_t, but xy * z will overflow
astcenc_image image {
.dim_x = 0xFFFFFFFFu,
.dim_y = 0xFFFFFFFFu,
.dim_z = 0xFFFFFFFFu,
.data_type = ASTCENC_TYPE_U8,
.data = reinterpret_cast<void**>(&slices),
.row_stride = 0
};
status = astcenc_compress_image(context, &image, &swizzle, data, -1, 0);
EXPECT_EQ(status, ASTCENC_ERR_BAD_PARAM);
astcenc_context_free(context);
}
/** @brief Input validation for config.block_z = 0. */
TEST(compress, zero_block_z)
{
astcenc_error status;
astcenc_config config;
astcenc_context* context;
static const astcenc_swizzle swizzle {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0, &config);
config.block_z = 0;
status = astcenc_context_alloc(&config, 1, &context, nullptr);
EXPECT_EQ(status, ASTCENC_SUCCESS);
uint8_t data[16];
uint8_t input[4 * 4 * 4];
uint8_t* slices = input;
astcenc_image image {
.dim_x = 4,
.dim_y = 4,
.dim_z = 1,
.data_type = ASTCENC_TYPE_U8,
.data = reinterpret_cast<void**>(&slices),
.row_stride = 0
};
// Compression must succeed despite zero config.block_z value
status = astcenc_compress_image(context, &image, &swizzle, data, sizeof(data), 0);
EXPECT_EQ(status, ASTCENC_SUCCESS);
astcenc_context_free(context);
}
/** @brief Compressed byte size overflow with a valid uncompressed byte size. */
TEST(compress, overflow_in_16)
{
astcenc_error status;
astcenc_config config;
astcenc_context* context;
static const astcenc_swizzle swizzle {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0, &config);
status = astcenc_context_alloc(&config, 1, &context, nullptr);
EXPECT_EQ(status, ASTCENC_SUCCESS);
// Arrays are too short, but should never be touched
uint8_t data[1];
uint8_t input[1];
uint8_t* slices = input;
// Rounding up to a full block makes the compressed byte size overflow
astcenc_image image {
.dim_x = std::numeric_limits<size_t>::max() / 4,
.dim_y = 1,
.dim_z = 1,
.data_type = ASTCENC_TYPE_U8,
.data = reinterpret_cast<void**>(&slices),
.row_stride = 0
};
status = astcenc_compress_image(context, &image, &swizzle, data, -1, 0);
EXPECT_EQ(status, ASTCENC_ERR_BAD_PARAM);
astcenc_context_free(context);
}
/** @brief Input test for an out-of-range alpha-scale radius. */
TEST(compress, ascale_radius_clamped)
{
astcenc_error status;
astcenc_config config;
astcenc_context* context;
static const astcenc_swizzle swizzle {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0, &config);
// Set to largest possible value to check input validation
config.a_scale_radius = 0xFFFFFFFFu;
status = astcenc_context_alloc(&config, 1, &context, nullptr);
EXPECT_EQ(status, ASTCENC_SUCCESS);
uint8_t data[16];
uint8_t input[4 * 4 * 4];
uint8_t* slices = input;
astcenc_image image {
.dim_x = 4,
.dim_y = 4,
.dim_z = 1,
.data_type = ASTCENC_TYPE_U8,
.data = reinterpret_cast<void**>(&slices),
.row_stride = 0
};
// Compression should succeed without bad_alloc or ASAN failure
status = astcenc_compress_image(context, &image, &swizzle, data, sizeof(data), 0);
EXPECT_EQ(status, ASTCENC_SUCCESS);
astcenc_context_free(context);
}
/** @brief Image storage overflow must be rejected before accessing the data. */
TEST(compress, image_size_overflow)
{
astcenc_error status;
astcenc_config config;
astcenc_context* context;
// Use 3D blocks so compressed storage does not mask RGBA byte overflow
astcenc_config_init(ASTCENC_PRF_LDR, 6, 6, 6, ASTCENC_PRE_MEDIUM, 0, &config);
status = astcenc_context_alloc(&config, 1, &context, nullptr);
ASSERT_EQ(status, ASTCENC_SUCCESS);
const astcenc_swizzle swizzle {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
for (size_t component_size : { 1u, 2u, 4u })
{
astcenc_type type = component_size == 1 ? ASTCENC_TYPE_U8 :
component_size == 2 ? ASTCENC_TYPE_F16 : ASTCENC_TYPE_F32;
for (size_t axis = 0; axis < 3; axis++)
{
size_t dims[3] { 1, 1, 1 };
dims[axis] = std::numeric_limits<size_t>::max() / (4 * component_size) + 1;
astcenc_image image {
.dim_x = dims[0],
.dim_y = dims[1],
.dim_z = dims[2],
.data_type = type,
.data = nullptr,
.row_stride = 0
};
status = astcenc_compress_image(context, &image, &swizzle, nullptr, 0, 0);
EXPECT_EQ(status, ASTCENC_ERR_BAD_PARAM);
// Include a multi-row or multi-slice product overflow
dims[axis] = std::numeric_limits<size_t>::max() / (8 * component_size) + 1;
dims[(axis + 1) % 3] = 2;
image = {
.dim_x = dims[0],
.dim_y = dims[1],
.dim_z = dims[2],
.data_type = type,
.data = nullptr,
.row_stride = 0
};
status = astcenc_compress_image(context, &image, &swizzle, nullptr, 0, 0);
EXPECT_EQ(status, ASTCENC_ERR_BAD_PARAM);
}
}
// The slice pointer array can overflow even when U8 texel storage fits
astcenc_image image {
.dim_x = 1,
.dim_y = 1,
.dim_z = std::numeric_limits<size_t>::max() / sizeof(void*) + 1,
.data_type = ASTCENC_TYPE_U8,
.data = nullptr,
.row_stride = 0
};
status = astcenc_compress_image(context, &image, &swizzle, nullptr, 0, 0);
EXPECT_EQ(status, ASTCENC_ERR_BAD_PARAM);
astcenc_context_free(context);
}
/** @brief Dimensions wider than unsigned int must reach the buffer capacity check. */
TEST(compress, large_dimensions)
{
if (sizeof(size_t) <= sizeof(unsigned int))
{
GTEST_SKIP();
}
astcenc_error status;
astcenc_config config;
astcenc_context* context;
// Use 3D blocks so every axis reaches the buffer capacity check
astcenc_config_init(ASTCENC_PRF_LDR, 3, 3, 3, ASTCENC_PRE_MEDIUM, 0, &config);
status = astcenc_context_alloc(&config, 1, &context, nullptr);
ASSERT_EQ(status, ASTCENC_SUCCESS);
const astcenc_swizzle swizzle {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
for (size_t axis = 0; axis < 3; axis++)
{
size_t dims[3] { 1, 1, 1 };
dims[axis] = static_cast<size_t>(std::numeric_limits<unsigned int>::max()) + 1;
astcenc_image image {
.dim_x = dims[0],
.dim_y = dims[1],
.dim_z = dims[2],
.data_type = ASTCENC_TYPE_U8,
.data = nullptr,
.row_stride = 0
};
status = astcenc_compress_image(context, &image, &swizzle, nullptr, 0, 0);
EXPECT_EQ(status, ASTCENC_ERR_OUT_OF_MEM);
}
astcenc_image image {
.dim_x = 1,
.dim_y = 1,
.dim_z = 1,
.data_type = ASTCENC_TYPE_U8,
.data = nullptr,
.row_stride = 0
};
status = astcenc_compress_image(
context, &image, &swizzle, nullptr, 0, std::numeric_limits<size_t>::max());
EXPECT_EQ(status, ASTCENC_ERR_BAD_PARAM);
astcenc_context_free(context);
}
/** @brief Per-thread scratch storage must not wrap when allocating a context. */
TEST(compress, thread_count_overflow)
{
astcenc_error status;
astcenc_config config;
astcenc_context* context = nullptr;
astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0, &config);
status = astcenc_context_alloc(&config, std::numeric_limits<size_t>::max(), &context, nullptr);
EXPECT_EQ(status, ASTCENC_ERR_BAD_PARAM);
EXPECT_EQ(context, nullptr);
}
/** @brief Input data buffer overrun tests. */
TEST(compress, data_buffer_exceeded)
{
astcenc_error status;
astcenc_config config;
astcenc_context* context;
static const astcenc_swizzle swizzle {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
astcenc_config_init(ASTCENC_PRF_LDR, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0, &config);
status = astcenc_context_alloc(&config, 1, &context, nullptr);
EXPECT_EQ(status, ASTCENC_SUCCESS);
// Arrays are too short, but should never be touched
uint8_t data[1];
uint8_t input[1];
uint8_t* slices = input;
astcenc_image image {
.dim_x = 0x4u,
.dim_y = 0x4u,
.dim_z = 0x1u,
.data_type = ASTCENC_TYPE_U8,
.data = reinterpret_cast<void**>(&slices),
.row_stride = 0
};
// Data size is 1 byte too short so this should error
status = astcenc_compress_image(context, &image, &swizzle, data, 15, 0);
EXPECT_EQ(status, ASTCENC_ERR_OUT_OF_MEM);
astcenc_context_free(context);
}
/**
* @brief Helper to run a compression on a single 4x4 block.
*
* @param profile The color profile to test.
* @param input The 4x4 block of interleaved RGBA float values.
*/
static void run_positive_test(
astcenc_profile profile,
float* input
) {
astcenc_error status;
astcenc_config config;
astcenc_context* context;
static const astcenc_swizzle swizzle {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
astcenc_config_init(profile, 4, 4, 1, ASTCENC_PRE_MEDIUM, 0, &config);
status = astcenc_context_alloc(&config, 1, &context, nullptr);
EXPECT_EQ(status, ASTCENC_SUCCESS);
// Single 16 byte block output data
uint8_t data[16];
float* slices = input;
astcenc_image image {
.dim_x = 4u,
.dim_y = 4u,
.dim_z = 1u,
.data_type = ASTCENC_TYPE_F32,
.data = reinterpret_cast<void**>(&slices),
.row_stride = 0
};
// Should not error or trip any UBSAN errors
status = astcenc_compress_image(context, &image, &swizzle, data, 16, 0);
EXPECT_EQ(status, ASTCENC_SUCCESS);
astcenc_context_free(context);
}
/** @brief Input data contains negative infinities. */
TEST(compress, data_neg_inf_ldr)
{
// Cycle through a single bad value in each color channel
for(int offset = 0; offset < 4; offset++)
{
// 4x4 block input data
std::vector<float> input(4 * 4 * 4, 0.5f);
input[0 + offset] = -std::numeric_limits<float>::infinity();
// Make data more complex to hit more compressor paths
input[4 + offset] = 0.75f;
input[8 + offset] = 0.35f;
input[12 + offset] = 0.0f;
run_positive_test(ASTCENC_PRF_LDR, input.data());
}
}
/** @brief Input data contains negative infinities. */
TEST(compress, data_neg_inf_hdr)
{
// Cycle through a single bad value in each color channel
for(int offset = 0; offset < 4; offset++)
{
// 4x4 block input data
std::vector<float> input(4 * 4 * 4, 0.5f);
input[0 + offset] = -std::numeric_limits<float>::infinity();
run_positive_test(ASTCENC_PRF_HDR, input.data());
}
}
/** @brief Input data contains negative infinities. */
TEST(compress, data_neg_inf_hdr_ldra)
{
// Cycle through a single bad value in each color channel
for(int offset = 0; offset < 4; offset++)
{
// 4x4 block input data
std::vector<float> input(4 * 4 * 4, 0.5f);
input[0 + offset] = -std::numeric_limits<float>::infinity();
run_positive_test(ASTCENC_PRF_HDR_RGB_LDR_A, input.data());
}
}
/** @brief Input data contains positive infinities. */
TEST(compress, data_pos_inf_ldr)
{
// Cycle through a single bad value in each color channel
for(int offset = 0; offset < 4; offset++)
{
// 4x4 block input data
std::vector<float> input(4 * 4 * 4, 0.5f);
input[0 + offset] = std::numeric_limits<float>::infinity();
run_positive_test(ASTCENC_PRF_LDR, input.data());
}
}
/** @brief Input data contains positive infinities. */
TEST(compress, data_pos_inf_hdr)
{
// Cycle through a single bad value in each color channel
for(int offset = 0; offset < 4; offset++)
{
// 4x4 block input data
std::vector<float> input(4 * 4 * 4, 0.5f);
input[0 + offset] = std::numeric_limits<float>::infinity();
run_positive_test(ASTCENC_PRF_HDR, input.data());
}
}
/** @brief Input data contains positive infinities. */
TEST(compress, data_pos_inf_hdr_ldra)
{
// Cycle through a single bad value in each color channel
for(int offset = 0; offset < 4; offset++)
{
// 4x4 block input data
std::vector<float> input(4 * 4 * 4, 0.5f);
input[0 + offset] = std::numeric_limits<float>::infinity();
run_positive_test(ASTCENC_PRF_HDR_RGB_LDR_A, input.data());
}
}
/** @brief Input data contains NaN. */
TEST(compress, data_nan_ldr)
{
// Cycle through a single bad value in each color channel
for(int offset = 0; offset < 4; offset++)
{
// 4x4 block input data
std::vector<float> input(4 * 4 * 4, 0.5f);
input[0 + offset] = std::numeric_limits<float>::quiet_NaN();
run_positive_test(ASTCENC_PRF_LDR, input.data());
}
}
/** @brief Input data contains NaN. */
TEST(compress, data_nan_hdr)
{
// Cycle through a single bad value in each color channel
for(int offset = 0; offset < 4; offset++)
{
// 4x4 block input data
std::vector<float> input(4 * 4 * 4, 0.5f);
input[0 + offset] = std::numeric_limits<float>::quiet_NaN();
run_positive_test(ASTCENC_PRF_HDR, input.data());
}
}
/** @brief Input data contains NaN. */
TEST(compress, data_nan_hdr_ldra)
{
// Cycle through a single bad value in each color channel
for(int offset = 0; offset < 4; offset++)
{
// 4x4 block input data
std::vector<float> input(4 * 4 * 4, 0.5f);
input[0 + offset] = std::numeric_limits<float>::quiet_NaN();
run_positive_test(ASTCENC_PRF_HDR_RGB_LDR_A, input.data());
}
}
/** @brief ISE decoding preserves all QUANT levels, partial groups, and bit offsets. */
TEST(ise, quant_levels)
{
for (unsigned int q = 0; q < 21; q++)
{
quant_method quant_level = static_cast<quant_method>(q);
unsigned int levels = get_quant_level(quant_level);
for (unsigned int count = 1; count <= 64; count++)
{
uint8_t source[64];
for (unsigned int i = 0; i < count; i++)
{
source[i] = static_cast<uint8_t>((i * 37 + q * 13 + count) % levels);
}
for (unsigned int offset = 0; offset < 8; offset++)
{
uint8_t packed[66] {};
uint8_t decoded[66];
std::memset(decoded, 0xA5, sizeof(decoded));
encode_ise(quant_level, count, source, packed, offset);
decode_ise(quant_level, count, packed, decoded + 1, offset);
for (unsigned int i = 0; i < count; i++)
{
EXPECT_EQ(decoded[i + 1], source[i]);
}
EXPECT_EQ(decoded[0], 0xA5);
EXPECT_EQ(decoded[count + 1], 0xA5);
}
}
}
}
}