Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
YunHsiao committed Jun 8, 2024
1 parent c860cc3 commit 92a2784
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
48 changes: 44 additions & 4 deletions Source/astcenc_rate_distortion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ static constexpr uint32_t ASTCENC_BYTES_PER_BLOCK = 16;

template<typename T> T sqr(T v) { return v * v; }

extern "C" void progress_emitter(float value);

extern "C" void rdo_progress_emitter(
float value
) {
Expand All @@ -52,7 +50,27 @@ extern "C" void rdo_progress_emitter(
}
previous_value = value;

progress_emitter(value);
const unsigned int bar_size = 25;
unsigned int parts = static_cast<int>(value / 4.0f);

char buffer[bar_size + 3];
buffer[0] = '[';

for (unsigned int i = 0; i < parts; i++)
{
buffer[i + 1] = '=';
}

for (unsigned int i = parts; i < bar_size; i++)
{
buffer[i + 1] = ' ';
}

buffer[bar_size + 1] = ']';
buffer[bar_size + 2] = '\0';

printf(" Progress: %s %03.1f%%\r", buffer, static_cast<double>(value));
fflush(stdout);
}

static uint32_t init_rdo_context(
Expand Down Expand Up @@ -323,6 +341,27 @@ struct local_rdo_context
uint32_t base_offset;
};

static bool is_transparent(int v) { return (v & 0xFF) != 0xFF; }

static bool has_any_transparency(
astcenc_profile decode_mode,
const symbolic_compressed_block& scb
) {
if (scb.block_type != SYM_BTYPE_NONCONST) return is_transparent(scb.constant_color[3]);

vint4 ep0;
vint4 ep1;
bool rgb_lns;
bool a_lns;

for (int i = 0; i < scb.partition_count; i++)
{
unpack_color_endpoints(decode_mode, scb.color_formats[i], scb.color_values[i], rgb_lns, a_lns, ep0, ep1);
if (is_transparent(ep0.lane<3>()) || is_transparent(ep1.lane<3>())) return true;
}
return false;
}

static float compute_block_difference(
void* user_data,
const uint8_t* pcb,
Expand All @@ -339,6 +378,7 @@ static float compute_block_difference(
if (scb.block_type == SYM_BTYPE_ERROR) return -ERROR_CALC_DEFAULT;
bool is_dual_plane = scb.block_type == SYM_BTYPE_NONCONST && ctx.bsd->get_block_mode(scb.block_mode).is_dual_plane;
if (is_dual_plane && scb.partition_count != 1) return -ERROR_CALC_DEFAULT;
if (ctx.config.cw_a_weight < 0.01f && has_any_transparency(ctx.config.profile, scb)) return -ERROR_CALC_DEFAULT;

const astcenc_rdo_context& rdo_ctx = *ctx.rdo_context;
uint32_t block_idx = local_block_idx + local_ctx.base_offset;
Expand Down Expand Up @@ -422,7 +462,7 @@ void rate_distortion_optimize(
ert::reduce_entropy(buffer + base * ASTCENC_BYTES_PER_BLOCK, count,
ASTCENC_BYTES_PER_BLOCK, ASTCENC_BYTES_PER_BLOCK,
ctx.rdo_context->m_ert_params, total_modified,
compute_block_difference, &local_ctx);
&compute_block_difference, &local_ctx);

ctxo.manage_rdo.complete_task_assignment(count);
}
Expand Down
14 changes: 11 additions & 3 deletions Source/astcenccli_toplevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ static int edit_astcenc_config(
argidx += 2;
if (argidx > argc)
{
print_error("ERROR: -rdo-dict-size switch with no argument\n");
print_error("ERROR: -rdo-lookback switch with no argument\n");
return 1;
}

Expand Down Expand Up @@ -1655,7 +1655,8 @@ static void print_diagnostic_image(
static void print_diagnostic_images(
astcenc_context* context,
const astc_compressed_image& image,
const std::string& output_file
const std::string& output_file,
astcenc_operation operation
) {
if (image.dim_z != 1)
{
Expand All @@ -1672,6 +1673,13 @@ static void print_diagnostic_images(

auto diag_image = alloc_image(8, image.dim_x, image.dim_y, image.dim_z);

// ---- ---- ---- ---- Compressed Output ---- ---- ---- ----
if ((operation & ASTCENC_STAGE_ST_COMP) == 0)
{
std::string fname = stem + "_diag.astc";
store_cimage(image, fname.c_str());
}

// ---- ---- ---- ---- Partitioning ---- ---- ---- ----
auto partition_func = [](astcenc_block_info& info, size_t texel_x, size_t texel_y) {
const vint4 colors[] {
Expand Down Expand Up @@ -2421,7 +2429,7 @@ int astcenc_main(
// Store diagnostic images
if (cli_config.diagnostic_images && !is_null)
{
print_diagnostic_images(codec_context, image_comp, output_filename);
print_diagnostic_images(codec_context, image_comp, output_filename, operation);
}

free_image(image_uncomp_in);
Expand Down
10 changes: 5 additions & 5 deletions Source/ert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ namespace ert
bool reduce_entropy(uint8_t* pBlock_bytes, uint32_t num_blocks,
uint32_t total_block_stride_in_bytes, uint32_t block_size_to_optimize_in_bytes,
const reduce_entropy_params& params, uint32_t& total_modified,
diff_block_func_type pDiff_block_func, void* pDiff_block_func_user_data,
diff_block_func_type* pDiff_block_func, void* pDiff_block_func_user_data,
const float* pBlock_mse_scales)
{
assert(total_block_stride_in_bytes && block_size_to_optimize_in_bytes);
Expand Down Expand Up @@ -176,7 +176,7 @@ namespace ert
uint8_t* pOrig_block = &pBlock_bytes[block_index * total_block_stride_in_bytes];

float max_std_dev = 0.0f;
float cur_mse = pDiff_block_func(pDiff_block_func_user_data, pOrig_block, block_index, &max_std_dev);
float cur_mse = (*pDiff_block_func)(pDiff_block_func_user_data, pOrig_block, block_index, &max_std_dev);
if (cur_mse < 0.0f)
return false;

Expand Down Expand Up @@ -284,7 +284,7 @@ namespace ert
memcpy(trial_block, pOrig_block, block_size_to_optimize_in_bytes);
memcpy(trial_block + dst_ofs, pPrev_blk + src_ofs, len);

float trial_mse = pDiff_block_func(pDiff_block_func_user_data, trial_block, block_index, nullptr);
float trial_mse = (*pDiff_block_func)(pDiff_block_func_user_data, trial_block, block_index, nullptr);
if (trial_mse < 0.0f)
continue;

Expand Down Expand Up @@ -367,7 +367,7 @@ namespace ert
memcpy(trial_block, pOrig_block, block_size_to_optimize_in_bytes);
memcpy(trial_block + ofs, pPrev_blk + ofs, len);

float trial_mse = pDiff_block_func(pDiff_block_func_user_data, trial_block, block_index, nullptr);
float trial_mse = (*pDiff_block_func)(pDiff_block_func_user_data, trial_block, block_index, nullptr);
if (trial_mse < 0.0f)
continue;

Expand Down Expand Up @@ -436,7 +436,7 @@ namespace ert
memcpy(trial_block, orig_best_block, block_size_to_optimize_in_bytes);
memcpy(trial_block + ofs, pPrev_blk + ofs, len);

float trial_mse = pDiff_block_func(pDiff_block_func_user_data, trial_block, block_index, nullptr);
float trial_mse = (*pDiff_block_func)(pDiff_block_func_user_data, trial_block, block_index, nullptr);
if (trial_mse < 0.0f)
continue;

Expand Down
2 changes: 1 addition & 1 deletion Source/ert.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace ert
bool reduce_entropy(uint8_t* pBlock_bytes, uint32_t num_blocks,
uint32_t total_block_stride_in_bytes, uint32_t block_size_to_optimize_in_bytes,
const reduce_entropy_params& params, uint32_t& total_modified,
diff_block_func_type pDiff_block_func, void* pDiff_block_func_user_data,
diff_block_func_type* pDiff_block_func, void* pDiff_block_func_user_data,
const float* pBlock_mse_scales = nullptr);

} // namespace ert

0 comments on commit 92a2784

Please sign in to comment.