mirror of
https://github.com/BinomialLLC/basis_universal.git
synced 2026-06-08 08:33:53 +00:00
adding new files
This commit is contained in:
5667
encoder/basisu_astc_ldr_common.cpp
Normal file
5667
encoder/basisu_astc_ldr_common.cpp
Normal file
File diff suppressed because it is too large
Load Diff
445
encoder/basisu_astc_ldr_common.h
Normal file
445
encoder/basisu_astc_ldr_common.h
Normal file
@@ -0,0 +1,445 @@
|
||||
// File: basisu_astc_ldr_common.h
|
||||
#pragma once
|
||||
#include "basisu_enc.h"
|
||||
#include "basisu_gpu_texture.h"
|
||||
#include <array>
|
||||
|
||||
namespace basisu
|
||||
{
|
||||
|
||||
namespace astc_ldr
|
||||
{
|
||||
const uint32_t ASTC_LDR_MAX_BLOCK_WIDTH = astc_helpers::MAX_BLOCK_DIM; // 12
|
||||
const uint32_t ASTC_LDR_MAX_BLOCK_HEIGHT = astc_helpers::MAX_BLOCK_DIM; // 12
|
||||
const uint32_t ASTC_LDR_MAX_BLOCK_PIXELS = astc_helpers::MAX_BLOCK_PIXELS; // 144
|
||||
const uint32_t ASTC_LDR_MAX_RAW_WEIGHTS = astc_helpers::MAX_WEIGHT_INTERPOLANT_VALUE + 1; // 65
|
||||
|
||||
const uint32_t WEIGHT_REFINER_MAX_PASSES = 17;
|
||||
|
||||
inline basist::color_rgba convert_to_basist_color_rgba(const color_rgba& c)
|
||||
{
|
||||
return basist::color_rgba(c.r, c.g, c.b, c.a);
|
||||
}
|
||||
|
||||
struct cem_encode_params
|
||||
{
|
||||
uint32_t m_comp_weights[4];
|
||||
bool m_decode_mode_srgb; // todo: store astc_helpers::cDecodeModeSRGB8 : astc_helpers::cDecodeModeLDR8 instead, also the alpha mode for srgb because the decoders are broken
|
||||
|
||||
const uint8_t* m_pForced_weight_vals0;
|
||||
const uint8_t* m_pForced_weight_vals1;
|
||||
|
||||
uint32_t m_max_ls_passes, m_total_weight_refine_passes;
|
||||
bool m_worst_weight_nudging_flag;
|
||||
bool m_endpoint_refinement_flag;
|
||||
|
||||
cem_encode_params()
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
m_comp_weights[0] = 1;
|
||||
m_comp_weights[1] = 1;
|
||||
m_comp_weights[2] = 1;
|
||||
m_comp_weights[3] = 1;
|
||||
|
||||
m_decode_mode_srgb = true;
|
||||
|
||||
m_pForced_weight_vals0 = nullptr;
|
||||
m_pForced_weight_vals1 = nullptr;
|
||||
|
||||
m_max_ls_passes = 3;
|
||||
m_total_weight_refine_passes = 0;
|
||||
m_worst_weight_nudging_flag = false;
|
||||
m_endpoint_refinement_flag = false;
|
||||
}
|
||||
|
||||
float get_total_comp_weights() const
|
||||
{
|
||||
return (float)(m_comp_weights[0] + m_comp_weights[1] + m_comp_weights[2] + m_comp_weights[3]);
|
||||
}
|
||||
};
|
||||
|
||||
struct pixel_stats_t
|
||||
{
|
||||
uint32_t m_num_pixels;
|
||||
|
||||
color_rgba m_pixels[ASTC_LDR_MAX_BLOCK_PIXELS];
|
||||
vec4F m_pixels_f[ASTC_LDR_MAX_BLOCK_PIXELS];
|
||||
|
||||
color_rgba m_min, m_max;
|
||||
|
||||
vec4F m_min_f, m_max_f;
|
||||
vec4F m_mean_f;
|
||||
|
||||
// Always 3D, ignoring alpha
|
||||
vec3F m_mean_rel_axis3;
|
||||
vec3F m_zero_rel_axis3;
|
||||
|
||||
// Always 4D
|
||||
vec4F m_mean_rel_axis4;
|
||||
|
||||
bool m_has_alpha;
|
||||
|
||||
stats<float> m_rgba_stats[4];
|
||||
|
||||
void clear()
|
||||
{
|
||||
clear_obj(*this);
|
||||
}
|
||||
|
||||
void init(uint32_t num_pixels, const color_rgba* pPixels);
|
||||
|
||||
}; // struct struct pixel_stats
|
||||
|
||||
void global_init();
|
||||
|
||||
void bit_transfer_signed_enc(int& a, int& b);
|
||||
void bit_transfer_signed_dec(int& a, int& b); // transfers MSB from a to b, a is then [-32,31]
|
||||
color_rgba blue_contract_enc(color_rgba orig, bool& did_clamp, int encoded_b);
|
||||
int quant_preserve2(uint32_t ise_range, uint32_t v);
|
||||
|
||||
uint32_t get_colors(const color_rgba& l, const color_rgba& h, uint32_t weight_ise_index, color_rgba* pColors, bool decode_mode_srgb);
|
||||
uint32_t get_colors_raw_weights(const color_rgba& l, const color_rgba& h, color_rgba* pColors, bool decode_mode_srgb);
|
||||
void decode_endpoints_ise20(uint32_t cem_index, const uint8_t* pEndpoint_vals, color_rgba& l, color_rgba& h); // assume BISE 20
|
||||
void decode_endpoints(uint32_t cem_index, const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index, color_rgba& l, color_rgba& h, float* pScale = nullptr);
|
||||
uint32_t get_colors(uint32_t cem_index, const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index, uint32_t weight_ise_index, color_rgba* pColors, bool decode_mode_srgb);
|
||||
uint32_t get_colors_raw_weights(uint32_t cem_index, const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index, color_rgba* pColors, bool decode_mode_srgb);
|
||||
|
||||
//int apply_delta_to_bise_endpoint_val(uint32_t endpoint_ise_range, int ise_val, int delta);
|
||||
int apply_delta_to_bise_weight_val(uint32_t weight_ise_range, int ise_val, int delta);
|
||||
|
||||
uint64_t eval_solution(
|
||||
const pixel_stats_t& pixel_stats,
|
||||
uint32_t total_weights, const color_rgba* pWeight_colors,
|
||||
uint8_t* pWeight_vals, uint32_t weight_ise_index,
|
||||
const cem_encode_params& params);
|
||||
|
||||
uint64_t eval_solution(
|
||||
const pixel_stats_t& pixel_stats,
|
||||
uint32_t cem_index,
|
||||
const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index,
|
||||
uint8_t* pWeight_vals, uint32_t weight_ise_index,
|
||||
const cem_encode_params& params);
|
||||
|
||||
uint64_t eval_solution_dp(
|
||||
uint32_t ccs_index,
|
||||
const pixel_stats_t& pixel_stats,
|
||||
uint32_t total_weights, const color_rgba* pWeight_colors,
|
||||
uint8_t* pWeight_vals0, uint8_t* pWeight_vals1, uint32_t weight_ise_index,
|
||||
const cem_encode_params& params);
|
||||
|
||||
uint64_t eval_solution_dp(
|
||||
const pixel_stats_t& pixel_stats,
|
||||
uint32_t cem_index, uint32_t ccs_index,
|
||||
const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index,
|
||||
uint8_t* pWeight_vals0, uint8_t* pWeight_vals1, uint32_t weight_ise_index,
|
||||
const cem_encode_params& params);
|
||||
|
||||
//bool cem8_or_12_used_blue_contraction(uint32_t cem_index, const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index);
|
||||
//bool cem9_or_13_used_blue_contraction(uint32_t cem_index, const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index);
|
||||
//bool used_blue_contraction(uint32_t cem_index, const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index);
|
||||
|
||||
uint64_t cem_encode_pixels(
|
||||
uint32_t cem_index, int ccs_index,
|
||||
const pixel_stats_t& pixel_stats, const cem_encode_params& enc_params,
|
||||
uint32_t endpoint_ise_range, uint32_t weight_ise_range,
|
||||
uint8_t* pEndpoint_vals, uint8_t* pWeight_vals0, uint8_t* pWeight_vals1, uint64_t cur_blk_error,
|
||||
bool use_blue_contraction, bool* pBase_ofs_clamped_flag);
|
||||
|
||||
// TODO: Rename, confusing vs. std::vector or basisu::vector or vec4F etc.
|
||||
struct partition_pattern_vec
|
||||
{
|
||||
uint32_t m_width, m_height;
|
||||
uint8_t m_parts[ASTC_LDR_MAX_BLOCK_PIXELS];
|
||||
|
||||
partition_pattern_vec();
|
||||
|
||||
partition_pattern_vec(const partition_pattern_vec& other);
|
||||
|
||||
partition_pattern_vec(uint32_t width, uint32_t height, const uint8_t* pParts = nullptr);
|
||||
|
||||
void init(uint32_t width, uint32_t height, const uint8_t* pParts = nullptr);
|
||||
|
||||
void init_part_hist();
|
||||
|
||||
void clear();
|
||||
|
||||
partition_pattern_vec& operator= (const partition_pattern_vec& rhs);
|
||||
|
||||
uint32_t get_width() const { return m_width; }
|
||||
uint32_t get_height() const { return m_height; }
|
||||
uint32_t get_total() const { return m_width * m_height; }
|
||||
|
||||
uint8_t operator[] (uint32_t i) const { assert(i < get_total()); return m_parts[i]; }
|
||||
uint8_t& operator[] (uint32_t i) { assert(i < get_total()); return m_parts[i]; }
|
||||
|
||||
uint8_t operator() (uint32_t x, uint32_t y) const { assert((x < m_width) && (y < m_height)); return m_parts[x + y * m_width]; }
|
||||
uint8_t& operator() (uint32_t x, uint32_t y) { assert((x < m_width) && (y < m_height)); return m_parts[x + y * m_width]; }
|
||||
|
||||
int get_squared_distance(const partition_pattern_vec& other) const;
|
||||
|
||||
float get_distance(const partition_pattern_vec& other) const
|
||||
{
|
||||
return sqrtf((float)get_squared_distance(other));
|
||||
}
|
||||
|
||||
enum { cMaxPermute2Index = 1 };
|
||||
partition_pattern_vec get_permuted2(uint32_t permute_index) const;
|
||||
|
||||
enum { cMaxPermute3Index = 5 };
|
||||
partition_pattern_vec get_permuted3(uint32_t permute_index) const;
|
||||
|
||||
partition_pattern_vec get_canonicalized() const;
|
||||
|
||||
bool operator== (const partition_pattern_vec& rhs) const
|
||||
{
|
||||
if ((m_width != rhs.m_width) || (m_height != rhs.m_height))
|
||||
return false;
|
||||
|
||||
return memcmp(m_parts, rhs.m_parts, get_total()) == 0;
|
||||
}
|
||||
|
||||
operator size_t() const
|
||||
{
|
||||
return basist::hash_hsieh(m_parts, get_total());
|
||||
}
|
||||
};
|
||||
|
||||
struct vp_tree_node
|
||||
{
|
||||
partition_pattern_vec m_vantage_point;
|
||||
uint32_t m_point_index;
|
||||
float m_dist;
|
||||
|
||||
int m_inner_node, m_outer_node;
|
||||
};
|
||||
|
||||
const uint32_t NUM_PART3_MAPPINGS = 6;
|
||||
extern uint8_t g_part3_mapping[NUM_PART3_MAPPINGS][3];
|
||||
|
||||
class vp_tree
|
||||
{
|
||||
public:
|
||||
vp_tree()
|
||||
{
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
m_nodes.clear();
|
||||
}
|
||||
|
||||
// This requires no redundant patterns, i.e. all must be unique.
|
||||
bool init(uint32_t n, const partition_pattern_vec* pUnique_pats);
|
||||
|
||||
struct result
|
||||
{
|
||||
uint32_t m_pat_index;
|
||||
uint32_t m_mapping_index;
|
||||
float m_dist;
|
||||
|
||||
bool operator< (const result& rhs) const { return m_dist < rhs.m_dist; }
|
||||
bool operator> (const result& rhs) const { return m_dist > rhs.m_dist; }
|
||||
};
|
||||
|
||||
class result_queue
|
||||
{
|
||||
enum { MaxSupportedSize = 512 + 1 };
|
||||
|
||||
public:
|
||||
result_queue() :
|
||||
m_cur_size(0)
|
||||
{
|
||||
}
|
||||
|
||||
size_t get_size() const
|
||||
{
|
||||
return m_cur_size;
|
||||
}
|
||||
|
||||
bool empty() const
|
||||
{
|
||||
return !m_cur_size;
|
||||
}
|
||||
|
||||
typedef std::array<result, MaxSupportedSize + 1> result_array_type;
|
||||
|
||||
const result_array_type& get_elements() const { return m_elements; }
|
||||
result_array_type& get_elements() { return m_elements; }
|
||||
|
||||
void clear()
|
||||
{
|
||||
m_cur_size = 0;
|
||||
}
|
||||
|
||||
void reserve(uint32_t n)
|
||||
{
|
||||
BASISU_NOTE_UNUSED(n);
|
||||
}
|
||||
|
||||
const result& top() const
|
||||
{
|
||||
assert(m_cur_size);
|
||||
return m_elements[1];
|
||||
}
|
||||
|
||||
bool insert(const result& val, uint32_t max_size)
|
||||
{
|
||||
assert(max_size < MaxSupportedSize);
|
||||
|
||||
if (m_cur_size >= MaxSupportedSize)
|
||||
return false;
|
||||
|
||||
m_elements[++m_cur_size] = val;
|
||||
up_heap(m_cur_size);
|
||||
|
||||
if (m_cur_size > max_size)
|
||||
pop();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool pop()
|
||||
{
|
||||
if (m_cur_size == 0)
|
||||
return false;
|
||||
|
||||
m_elements[1] = m_elements[m_cur_size--];
|
||||
down_heap(1);
|
||||
return true;
|
||||
}
|
||||
|
||||
float get_highest_dist() const
|
||||
{
|
||||
if (!m_cur_size)
|
||||
return 0.0f;
|
||||
|
||||
return top().m_dist;
|
||||
}
|
||||
|
||||
private:
|
||||
result_array_type m_elements;
|
||||
size_t m_cur_size;
|
||||
|
||||
void up_heap(size_t index)
|
||||
{
|
||||
while ((index > 1) && (m_elements[index] > m_elements[index >> 1]))
|
||||
{
|
||||
std::swap(m_elements[index], m_elements[index >> 1]);
|
||||
index >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
void down_heap(size_t index)
|
||||
{
|
||||
for (; ; )
|
||||
{
|
||||
size_t largest = index, left_child = 2 * index, right_child = 2 * index + 1;
|
||||
|
||||
if ((left_child <= m_cur_size) && (m_elements[left_child] > m_elements[largest]))
|
||||
largest = left_child;
|
||||
|
||||
if ((right_child <= m_cur_size) && (m_elements[right_child] > m_elements[largest]))
|
||||
largest = right_child;
|
||||
|
||||
if (largest == index)
|
||||
break;
|
||||
|
||||
std::swap(m_elements[index], m_elements[largest]);
|
||||
index = largest;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void find_nearest(uint32_t num_subsets, const partition_pattern_vec& desired_pat, result_queue& results, uint32_t max_results) const;
|
||||
|
||||
private:
|
||||
basisu::vector<vp_tree_node> m_nodes;
|
||||
|
||||
void find_nearest_at_node(int node_index, uint32_t num_desired_pats, const partition_pattern_vec* pDesired_pats, result_queue& results, uint32_t max_results) const;
|
||||
|
||||
void find_nearest_at_node_non_recursive(int init_node_index, uint32_t num_desired_pats, const partition_pattern_vec* pDesired_pats, result_queue& results, uint32_t max_results) const;
|
||||
|
||||
// returns the index of the new node, or -1 on error
|
||||
int create_node(uint32_t n, const partition_pattern_vec* pUnique_pats, const uint_vec& pat_indices);
|
||||
|
||||
// returns the pattern index of the vantage point (-1 on error), and the optimal split distance
|
||||
std::pair<int, float> find_best_vantage_point(uint32_t num_unique_pats, const partition_pattern_vec* pUnique_pats, const uint_vec& pat_indices);
|
||||
};
|
||||
|
||||
typedef basisu::hash_map<partition_pattern_vec, std::pair<uint32_t, uint32_t > > partition_hash_map;
|
||||
|
||||
struct partition_pattern_hist
|
||||
{
|
||||
uint8_t m_hist[4];
|
||||
|
||||
partition_pattern_hist() { clear(); }
|
||||
|
||||
void clear() { clear_obj(m_hist); }
|
||||
};
|
||||
|
||||
struct partitions_data
|
||||
{
|
||||
uint32_t m_width, m_height, m_num_partitions;
|
||||
partition_pattern_vec m_partition_pats[astc_helpers::NUM_PARTITION_PATTERNS]; // indexed by unique index, NOT the 10-bit ASTC seed/pattern index
|
||||
|
||||
partition_pattern_hist m_partition_pat_histograms[astc_helpers::NUM_PARTITION_PATTERNS]; // indexed by unique index, histograms of each pattern
|
||||
|
||||
// ASTC seed to unique index and vice versa
|
||||
int16_t m_part_seed_to_unique_index[astc_helpers::NUM_PARTITION_PATTERNS];
|
||||
int16_t m_unique_index_to_part_seed[astc_helpers::NUM_PARTITION_PATTERNS];
|
||||
|
||||
// Total number of unique patterns
|
||||
uint32_t m_total_unique_patterns;
|
||||
|
||||
// VP tree used to rapidly find nearby/similar patterns.
|
||||
vp_tree m_part_vp_tree;
|
||||
|
||||
void init(uint32_t num_partitions, uint32_t block_width, uint32_t block_height, bool init_vp_tree = true);
|
||||
};
|
||||
|
||||
float surrogate_quant_endpoint_val(float e, uint32_t num_endpoint_levels, uint32_t flags);
|
||||
vec4F surrogate_quant_endpoint(const vec4F& e, uint32_t num_endpoint_levels, uint32_t flags);
|
||||
|
||||
float surrogate_evaluate_rgba_sp(const pixel_stats_t& ps, const vec4F& l, const vec4F& h, float* pWeights0, uint32_t num_weight_levels, const cem_encode_params& enc_params, uint32_t flags);
|
||||
float surrogate_evaluate_rgba_dp(uint32_t ccs_index, const pixel_stats_t& ps, const vec4F& l, const vec4F& h, float* pWeights0, float* pWeights1, uint32_t num_weight_levels, const cem_encode_params& enc_params, uint32_t flags);
|
||||
|
||||
enum
|
||||
{
|
||||
cFlagDisableQuant = 1,
|
||||
cFlagNoError = 2
|
||||
}
|
||||
;
|
||||
float cem_surrogate_encode_pixels(
|
||||
uint32_t cem_index, int ccs_index,
|
||||
const pixel_stats_t& pixel_stats, const cem_encode_params& enc_params,
|
||||
uint32_t endpoint_ise_range, uint32_t weight_ise_range,
|
||||
vec4F& low_endpoint, vec4F& high_endpoint, float& s, float* pWeights0, float* pWeights1, uint32_t flags = 0);
|
||||
|
||||
#if 0
|
||||
bool requantize_ise_endpoints(uint32_t cem,
|
||||
uint32_t src_ise_endpoint_range, const uint8_t* pSrc_endpoints,
|
||||
uint32_t dst_ise_endpoint_range, uint8_t* pDst_endpoints);
|
||||
|
||||
uint32_t get_base_cem_without_alpha(uint32_t cem);
|
||||
|
||||
bool pack_base_offset(
|
||||
uint32_t cem_index, uint32_t dst_ise_endpoint_range, uint8_t* pPacked_endpoints,
|
||||
const color_rgba& l, const color_rgba& h,
|
||||
bool use_blue_contraction, bool auto_disable_blue_contraction_if_clamped,
|
||||
bool& blue_contraction_clamped_flag, bool& base_ofs_clamped_flag, bool& endpoints_swapped);
|
||||
|
||||
bool convert_endpoints_across_cems(
|
||||
uint32_t prev_cem, uint32_t prev_endpoint_ise_range, const uint8_t* pPrev_endpoints,
|
||||
uint32_t dst_cem, uint32_t dst_endpoint_ise_range, uint8_t* pDst_endpoints,
|
||||
bool always_repack,
|
||||
bool use_blue_contraction, bool auto_disable_blue_contraction_if_clamped,
|
||||
bool& blue_contraction_clamped_flag, bool& base_ofs_clamped_flag);
|
||||
#endif
|
||||
|
||||
} // namespace astc_ldr
|
||||
|
||||
} // namespace basisu
|
||||
11065
encoder/basisu_astc_ldr_encode.cpp
Normal file
11065
encoder/basisu_astc_ldr_encode.cpp
Normal file
File diff suppressed because it is too large
Load Diff
125
encoder/basisu_astc_ldr_encode.h
Normal file
125
encoder/basisu_astc_ldr_encode.h
Normal file
@@ -0,0 +1,125 @@
|
||||
// File: basisu_astc_ldr_encode.cpp
|
||||
#pragma once
|
||||
#include "basisu_enc.h"
|
||||
#include "../transcoder/basisu_astc_helpers.h"
|
||||
|
||||
namespace basisu {
|
||||
namespace astc_ldr {
|
||||
|
||||
void encoder_init();
|
||||
|
||||
const int EFFORT_LEVEL_MIN = 0, EFFORT_LEVEL_MAX = 10, EFFORT_LEVEL_DEF = 3;
|
||||
const int DCT_QUALITY_MIN = 1, DCT_QUALITY_MAX = 100;
|
||||
|
||||
struct astc_ldr_encode_config
|
||||
{
|
||||
astc_ldr_encode_config()
|
||||
{
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
*this = astc_ldr_encode_config();
|
||||
}
|
||||
|
||||
// ASTC LDR block dimensions. Must be a valid ASTC block dimension. Any supported from 4x4-12x12, including unequal dimensions.
|
||||
uint32_t m_astc_block_width = 6;
|
||||
uint32_t m_astc_block_height = 6;
|
||||
|
||||
// If true, the encoder assumes all ASTC blocks will be decompressed using sRGB vs. LDR8 mode. This corresponds to astcenc's -cs vs. cl color profiles.
|
||||
// This should match how the texture is later decoded by the GPU for maximum quality. This bit is stored into the output file.
|
||||
bool m_astc_decode_mode_srgb = true;
|
||||
|
||||
// If true, trade off some compression (3-10%) for faster decompression.
|
||||
// If false, favor highest compression, but slower decompression.
|
||||
//bool m_use_faster_format = true;
|
||||
|
||||
basist::astc_ldr_t::xuastc_ldr_syntax m_compressed_syntax = basist::astc_ldr_t::xuastc_ldr_syntax::cFullArith;
|
||||
|
||||
// Encoder CPU effort vs. quality. [0,10], higher=better.
|
||||
// 0=extremely fast but very brittle (no subsets)
|
||||
// 1=first 2 subset effort level
|
||||
// 10=extremely high CPU requirements.
|
||||
uint32_t m_effort_level = 3;
|
||||
|
||||
// Weight grid DCT quality [1,100] - higher=better quality (JPEG-style).
|
||||
float m_dct_quality = 85;
|
||||
|
||||
// true=use weight grid DCT, false=always use DPCM
|
||||
bool m_use_dct = false;
|
||||
|
||||
// true=use lossy supercompression, false=supercompression stage is always lossless.
|
||||
bool m_lossy_supercompression = false;
|
||||
|
||||
// Channel weights used to compute RGBA colorspace L2 errors. Must be >= 1.
|
||||
uint32_t m_comp_weights[4] = { 1, 1, 1, 1 };
|
||||
|
||||
// Lossy supercompression stage parameters for RGB vs. RGBA image inputs.
|
||||
// (Bounded RDO - explictly not Lagrangian.)
|
||||
float m_replacement_min_psnr = 35.0f; // if the block's base PSNR is less than this, it cannot be changed
|
||||
float m_psnr_trial_diff_thresh = 1.5f; // reject candidates if their PSNR is lower than m_replacement_min_psnr-m_psnr_trial_diff_thresh
|
||||
float m_psnr_trial_diff_thresh_edge = 1.0f; // edge variant
|
||||
|
||||
// Lossy supercompression settings - alpha texture variants
|
||||
float m_replacement_min_psnr_alpha = 38.0f;
|
||||
float m_psnr_trial_diff_thresh_alpha = .75f;
|
||||
float m_psnr_trial_diff_thresh_edge_alpha = .5f;
|
||||
|
||||
// If true, try encoding blurred blocks, in addition to unblurred, for superpass 1 and 2.
|
||||
// Higher quality, but massively slower and not yet tuned/refined.
|
||||
bool m_block_blurring_p1 = false, m_block_blurring_p2 = false;
|
||||
|
||||
// If true, no matter what effort level subset usage will be disabled.
|
||||
bool m_force_disable_subsets = false;
|
||||
|
||||
// If true, no matter what effort level RGB dual plane usage will be disabled.
|
||||
bool m_force_disable_rgb_dual_plane = false;
|
||||
|
||||
bool m_debug_images = false;
|
||||
bool m_debug_output = false;
|
||||
|
||||
std::string m_debug_file_prefix;
|
||||
|
||||
void debug_print() const
|
||||
{
|
||||
fmt_debug_printf("ASTC block dimensions: {}x{}\n", m_astc_block_width, m_astc_block_height);
|
||||
fmt_debug_printf("ASTC decode profile mode sRGB: {}\n", m_astc_decode_mode_srgb);
|
||||
fmt_debug_printf("Syntax: {}\n", (uint32_t)m_compressed_syntax);
|
||||
fmt_debug_printf("Effort level: {}\n", m_effort_level);
|
||||
fmt_debug_printf("Use DCT: {}\n", m_use_dct);
|
||||
fmt_debug_printf("DCT quality level (1-100): {}\n", m_dct_quality);
|
||||
fmt_debug_printf("Comp weights: {} {} {} {}\n", m_comp_weights[0], m_comp_weights[1], m_comp_weights[2], m_comp_weights[3]);
|
||||
fmt_debug_printf("Block blurring: {} {}\n", m_block_blurring_p1, m_block_blurring_p2);
|
||||
fmt_debug_printf("Force disable subsets: {}\n", m_force_disable_subsets);
|
||||
fmt_debug_printf("Force disable RGB dual plane: {}\n", m_force_disable_rgb_dual_plane);
|
||||
|
||||
fmt_debug_printf("\nLossy supercompression: {}\n", m_lossy_supercompression);
|
||||
fmt_debug_printf("m_replacement_min_psnr: {}\n", m_replacement_min_psnr);
|
||||
fmt_debug_printf("m_psnr_trial_diff_thresh: {}\n", m_psnr_trial_diff_thresh);
|
||||
fmt_debug_printf("m_psnr_trial_diff_thresh_edge: {}\n", m_psnr_trial_diff_thresh_edge);
|
||||
fmt_debug_printf("m_replacement_min_psnr_alpha: {}\n", m_replacement_min_psnr_alpha);
|
||||
fmt_debug_printf("m_psnr_trial_diff_thresh_alpha: {}\n", m_psnr_trial_diff_thresh_alpha);
|
||||
fmt_debug_printf("m_psnr_trial_diff_thresh_edge_alpha: {}\n", m_psnr_trial_diff_thresh_edge_alpha);
|
||||
|
||||
fmt_debug_printf("m_debug_images: {}\n", m_debug_images);
|
||||
}
|
||||
};
|
||||
|
||||
bool compress_image(
|
||||
const image& orig_img, uint8_vec &comp_data, vector2D<astc_helpers::log_astc_block>& coded_blocks,
|
||||
const astc_ldr_encode_config& global_cfg,
|
||||
job_pool& job_pool);
|
||||
|
||||
bool decompress_image(
|
||||
const uint8_t* pComp_data, size_t comp_data_size,
|
||||
vector2D<astc_helpers::log_astc_block>& coded_blocks, // the actual supercompressed ASTC LDR blocks emitted by the compressor
|
||||
uint32_t& astc_block_width, uint32_t& astc_block_height,
|
||||
uint32_t &actual_width, uint32_t &actual_height, bool &has_alpha, bool& uses_srgb_astc_decode_mode,
|
||||
bool debug_output);
|
||||
|
||||
void deblock_filter(uint32_t filter_block_width, uint32_t filter_block_height, const image& src_img, image& dst_img, bool stronger_filtering = false, int SKIP_THRESH = 24);
|
||||
|
||||
} // namespace astc_ldr
|
||||
} // namespace basisu
|
||||
|
||||
|
||||
319
encoder/basisu_wasm_api.cpp
Normal file
319
encoder/basisu_wasm_api.cpp
Normal file
@@ -0,0 +1,319 @@
|
||||
// File: basisu_wasm_api.cpp - Simplified compression API for WASM WASI modules and Python native support.
|
||||
// Also useable by plain C callers.
|
||||
#include "basisu_comp.h"
|
||||
#include "basisu_wasm_api.h"
|
||||
|
||||
using namespace basisu;
|
||||
|
||||
static inline uint64_t wasm_offset(void* p)
|
||||
{
|
||||
return (uint64_t)(uintptr_t)p;
|
||||
}
|
||||
|
||||
static inline uint8_t* wasm_ptr(uint64_t offset)
|
||||
{
|
||||
return (uint8_t*)(uintptr_t)offset;
|
||||
}
|
||||
|
||||
BU_WASM_EXPORT("bu_get_version")
|
||||
uint32_t bu_get_version()
|
||||
{
|
||||
printf("Hello from basisu_wasm_api.cpp version %u\n", BASISU_LIB_VERSION);
|
||||
|
||||
return BASISU_LIB_VERSION;
|
||||
}
|
||||
|
||||
BU_WASM_EXPORT("bu_enable_debug_printf")
|
||||
void bu_enable_debug_printf(uint32_t flag)
|
||||
{
|
||||
enable_debug_printf(flag != 0);
|
||||
}
|
||||
|
||||
BU_WASM_EXPORT("bu_init")
|
||||
void bu_init()
|
||||
{
|
||||
basisu_encoder_init(false, false);
|
||||
}
|
||||
|
||||
// Memory alloc/free — stubs
|
||||
BU_WASM_EXPORT("bu_alloc")
|
||||
uint64_t bu_alloc(uint64_t size)
|
||||
{
|
||||
void* p = malloc((size_t)size);
|
||||
return wasm_offset(p);
|
||||
}
|
||||
|
||||
BU_WASM_EXPORT("bu_free")
|
||||
void bu_free(uint64_t ofs)
|
||||
{
|
||||
free(wasm_ptr(ofs));
|
||||
}
|
||||
|
||||
const uint32_t COMP_PARAMS_MAGIC = 0x43504D50; // "CPMP"
|
||||
|
||||
struct comp_params
|
||||
{
|
||||
uint32_t m_magic = COMP_PARAMS_MAGIC;
|
||||
|
||||
comp_params()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
assert(m_magic == COMP_PARAMS_MAGIC);
|
||||
|
||||
m_comp_data.clear();
|
||||
m_images.clear();
|
||||
m_imagesf.clear();
|
||||
|
||||
m_stats.clear();
|
||||
}
|
||||
|
||||
uint8_vec m_comp_data;
|
||||
|
||||
basisu::vector<image> m_images;
|
||||
basisu::vector<imagef> m_imagesf;
|
||||
|
||||
image_stats m_stats;
|
||||
};
|
||||
|
||||
BU_WASM_EXPORT("bu_new_comp_params")
|
||||
uint64_t bu_new_comp_params()
|
||||
{
|
||||
comp_params* p = new comp_params;
|
||||
return wasm_offset(p);
|
||||
}
|
||||
|
||||
BU_WASM_EXPORT("bu_delete_comp_params")
|
||||
wasm_bool_t bu_delete_comp_params(uint64_t params_ofs)
|
||||
{
|
||||
comp_params* p = (comp_params*)wasm_ptr(params_ofs);
|
||||
if (!p)
|
||||
return false;
|
||||
|
||||
assert(p->m_magic == COMP_PARAMS_MAGIC);
|
||||
if (p->m_magic != COMP_PARAMS_MAGIC)
|
||||
return false;
|
||||
|
||||
delete p;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
BU_WASM_EXPORT("bu_comp_params_get_comp_data_size")
|
||||
uint64_t bu_comp_params_get_comp_data_size(uint64_t params_ofs)
|
||||
{
|
||||
comp_params* pParams = (comp_params*)wasm_ptr(params_ofs);
|
||||
if (!pParams)
|
||||
return 0;
|
||||
|
||||
assert(pParams->m_magic == COMP_PARAMS_MAGIC);
|
||||
if (pParams->m_magic != COMP_PARAMS_MAGIC)
|
||||
return 0;
|
||||
|
||||
return pParams->m_comp_data.size();
|
||||
}
|
||||
|
||||
BU_WASM_EXPORT("bu_comp_params_get_comp_data_ofs")
|
||||
uint64_t bu_comp_params_get_comp_data_ofs(uint64_t params_ofs)
|
||||
{
|
||||
comp_params* pParams = (comp_params*)wasm_ptr(params_ofs);
|
||||
if (!pParams)
|
||||
return 0;
|
||||
|
||||
assert(pParams->m_magic == COMP_PARAMS_MAGIC);
|
||||
if (pParams->m_magic != COMP_PARAMS_MAGIC)
|
||||
return 0;
|
||||
|
||||
return wasm_offset(pParams->m_comp_data.get_ptr());
|
||||
}
|
||||
|
||||
BU_WASM_EXPORT("bu_comp_params_clear")
|
||||
wasm_bool_t bu_comp_params_clear(uint64_t params_ofs)
|
||||
{
|
||||
comp_params* pParams = (comp_params*)wasm_ptr(params_ofs);
|
||||
if (!pParams)
|
||||
return false;
|
||||
|
||||
assert(pParams->m_magic == COMP_PARAMS_MAGIC);
|
||||
if (pParams->m_magic != COMP_PARAMS_MAGIC)
|
||||
return false;
|
||||
|
||||
pParams->clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caller wants to give us a LDR/SDR 32bpp RGBA mipmap level (4 bytes per pixel)
|
||||
BU_WASM_EXPORT("bu_comp_params_set_image_rgba32")
|
||||
wasm_bool_t bu_comp_params_set_image_rgba32(
|
||||
uint64_t params_ofs,
|
||||
uint32_t image_index,
|
||||
uint64_t img_data_ofs,
|
||||
uint32_t width, uint32_t height,
|
||||
uint32_t pitch_in_bytes)
|
||||
{
|
||||
if ((!width) || (!height) || (!pitch_in_bytes))
|
||||
return false;
|
||||
|
||||
comp_params* pParams = (comp_params*)wasm_ptr(params_ofs);
|
||||
if (!pParams)
|
||||
return false;
|
||||
|
||||
assert(pParams->m_magic == COMP_PARAMS_MAGIC);
|
||||
if (pParams->m_magic != COMP_PARAMS_MAGIC)
|
||||
return false;
|
||||
|
||||
const uint8_t* pImage = wasm_ptr(img_data_ofs);
|
||||
if (!pImage)
|
||||
return false;
|
||||
|
||||
const uint32_t bytes_per_pixel = sizeof(color_rgba);
|
||||
|
||||
if (pitch_in_bytes < width * bytes_per_pixel)
|
||||
return false;
|
||||
|
||||
if (image_index >= pParams->m_images.size())
|
||||
{
|
||||
if (!pParams->m_images.try_resize(image_index + 1))
|
||||
return false;
|
||||
}
|
||||
|
||||
basisu::image& dst_img = pParams->m_images[image_index];
|
||||
|
||||
dst_img.resize(width, height);
|
||||
|
||||
if (pitch_in_bytes == width * bytes_per_pixel)
|
||||
{
|
||||
memcpy(dst_img.get_ptr(), pImage, pitch_in_bytes * height);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (uint32_t y = 0; y < height; y++)
|
||||
{
|
||||
const uint8_t* pSrc_row = pImage + y * pitch_in_bytes;
|
||||
|
||||
uint8_t* pDst_row = (uint8_t *)&dst_img(0, y);
|
||||
|
||||
memcpy(pDst_row, pSrc_row, width * bytes_per_pixel);
|
||||
} // y
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caller wants to give us a float RGBA mipmap level (4*4=16 bytes per pixel)
|
||||
BU_WASM_EXPORT("bu_comp_params_set_image_float_rgba")
|
||||
wasm_bool_t bu_comp_params_set_image_float_rgba(
|
||||
uint64_t params_ofs,
|
||||
uint32_t image_index,
|
||||
uint64_t img_data_ofs,
|
||||
uint32_t width, uint32_t height,
|
||||
uint32_t pitch_in_bytes)
|
||||
{
|
||||
if ((!width) || (!height) || (!pitch_in_bytes))
|
||||
return false;
|
||||
|
||||
comp_params* pParams = (comp_params*)wasm_ptr(params_ofs);
|
||||
if (!pParams)
|
||||
return false;
|
||||
|
||||
assert(pParams->m_magic == COMP_PARAMS_MAGIC);
|
||||
if (pParams->m_magic != COMP_PARAMS_MAGIC)
|
||||
return false;
|
||||
|
||||
const uint8_t* pImage = wasm_ptr(img_data_ofs);
|
||||
if (!pImage)
|
||||
return false;
|
||||
|
||||
const uint32_t bytes_per_pixel = sizeof(float) * 4;
|
||||
|
||||
if (pitch_in_bytes < width * bytes_per_pixel)
|
||||
return false;
|
||||
|
||||
if (image_index >= pParams->m_images.size())
|
||||
{
|
||||
if (!pParams->m_imagesf.try_resize(image_index + 1))
|
||||
return false;
|
||||
}
|
||||
|
||||
basisu::imagef& dst_img = pParams->m_imagesf[image_index];
|
||||
|
||||
dst_img.resize(width, height);
|
||||
|
||||
if (pitch_in_bytes == width * bytes_per_pixel)
|
||||
{
|
||||
memcpy((void *)dst_img.get_ptr(), (const void *)pImage, pitch_in_bytes * height);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (uint32_t y = 0; y < height; y++)
|
||||
{
|
||||
const uint8_t* pSrc_row = pImage + y * pitch_in_bytes;
|
||||
|
||||
uint8_t* pDst_row = (uint8_t*)&dst_img(0, y);
|
||||
|
||||
memcpy(pDst_row, pSrc_row, width * bytes_per_pixel);
|
||||
} // y
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
BU_WASM_EXPORT("bu_compress_texture")
|
||||
wasm_bool_t bu_compress_texture(
|
||||
uint64_t params_ofs,
|
||||
uint32_t desired_basis_tex_format, // basis_tex_format
|
||||
int quality_level, int effort_level,
|
||||
uint64_t flags_and_quality, float low_level_uastc_rdo_or_dct_quality)
|
||||
{
|
||||
//enable_debug_printf((flags_and_quality & cFlagDebug) != 0);
|
||||
|
||||
comp_params* pParams = (comp_params*)wasm_ptr(params_ofs);
|
||||
if (!pParams)
|
||||
return false;
|
||||
|
||||
assert(pParams->m_magic == COMP_PARAMS_MAGIC);
|
||||
if (pParams->m_magic != COMP_PARAMS_MAGIC)
|
||||
return false;
|
||||
|
||||
pParams->m_comp_data.clear();
|
||||
|
||||
if (desired_basis_tex_format >= (uint32_t)basist::basis_tex_format::cTotalFormats)
|
||||
return false;
|
||||
|
||||
if (!pParams->m_images.size() && !pParams->m_imagesf.size())
|
||||
return false;
|
||||
if (pParams->m_images.size() && pParams->m_imagesf.size())
|
||||
return false;
|
||||
|
||||
size_t comp_size = 0;
|
||||
|
||||
void* pComp_data = basis_compress_internal(
|
||||
(basist::basis_tex_format)desired_basis_tex_format,
|
||||
pParams->m_images.size() ? &pParams->m_images : nullptr,
|
||||
pParams->m_imagesf.size() ? &pParams->m_imagesf : nullptr,
|
||||
(uint32_t)flags_and_quality,
|
||||
low_level_uastc_rdo_or_dct_quality,
|
||||
&comp_size,
|
||||
&pParams->m_stats,
|
||||
quality_level,
|
||||
effort_level);
|
||||
|
||||
if (!pComp_data)
|
||||
return false;
|
||||
|
||||
if (!pParams->m_comp_data.try_resize(comp_size))
|
||||
{
|
||||
basis_free_data(pComp_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(pParams->m_comp_data.get_ptr(), pComp_data, comp_size);
|
||||
|
||||
basis_free_data(pComp_data);
|
||||
|
||||
return true;
|
||||
}
|
||||
58
encoder/basisu_wasm_api.h
Normal file
58
encoder/basisu_wasm_api.h
Normal file
@@ -0,0 +1,58 @@
|
||||
// File: basisu_wasm_api.h
|
||||
#pragma once
|
||||
#include "basisu_wasm_api_common.h"
|
||||
|
||||
BU_WASM_EXPORT("bu_get_version")
|
||||
uint32_t bu_get_version();
|
||||
|
||||
BU_WASM_EXPORT("bu_enable_debug_printf")
|
||||
void bu_enable_debug_printf(uint32_t flag);
|
||||
|
||||
BU_WASM_EXPORT("bu_init")
|
||||
void bu_init();
|
||||
|
||||
BU_WASM_EXPORT("bu_alloc")
|
||||
uint64_t bu_alloc(uint64_t size);
|
||||
|
||||
BU_WASM_EXPORT("bu_free")
|
||||
void bu_free(uint64_t ofs);
|
||||
|
||||
BU_WASM_EXPORT("bu_new_comp_params")
|
||||
uint64_t bu_new_comp_params();
|
||||
|
||||
BU_WASM_EXPORT("bu_delete_comp_params")
|
||||
wasm_bool_t bu_delete_comp_params(uint64_t params_ofs);
|
||||
|
||||
BU_WASM_EXPORT("bu_comp_params_get_comp_data_size")
|
||||
uint64_t bu_comp_params_get_comp_data_size(uint64_t params_ofs);
|
||||
|
||||
BU_WASM_EXPORT("bu_comp_params_get_comp_data_ofs")
|
||||
uint64_t bu_comp_params_get_comp_data_ofs(uint64_t params_ofs);
|
||||
|
||||
BU_WASM_EXPORT("bu_comp_params_clear")
|
||||
wasm_bool_t bu_comp_params_clear(uint64_t params_ofs);
|
||||
|
||||
BU_WASM_EXPORT("bu_comp_params_set_image_rgba32")
|
||||
wasm_bool_t bu_comp_params_set_image_rgba32(
|
||||
uint64_t params_ofs,
|
||||
uint32_t image_index,
|
||||
uint64_t img_data_ofs,
|
||||
uint32_t width, uint32_t height,
|
||||
uint32_t pitch_in_bytes);
|
||||
|
||||
BU_WASM_EXPORT("bu_comp_params_set_image_float_rgba")
|
||||
wasm_bool_t bu_comp_params_set_image_float_rgba(
|
||||
uint64_t params_ofs,
|
||||
uint32_t image_index,
|
||||
uint64_t img_data_ofs,
|
||||
uint32_t width, uint32_t height,
|
||||
uint32_t pitch_in_bytes);
|
||||
|
||||
BU_WASM_EXPORT("bu_compress_texture")
|
||||
wasm_bool_t bu_compress_texture(
|
||||
uint64_t params_ofs,
|
||||
uint32_t desired_basis_tex_format,
|
||||
int quality_level, int effort_level,
|
||||
uint64_t flags_and_quality,
|
||||
float low_level_uastc_rdo_or_dct_quality);
|
||||
|
||||
156
encoder/basisu_wasm_api_common.h
Normal file
156
encoder/basisu_wasm_api_common.h
Normal file
@@ -0,0 +1,156 @@
|
||||
// File: basisu_wasm_api_common.h
|
||||
#pragma once
|
||||
#include "stdint.h"
|
||||
|
||||
#if defined(__wasm__)
|
||||
#if defined(__cplusplus)
|
||||
#define BU_WASM_EXPORT(name) __attribute__((export_name(name))) extern "C"
|
||||
#else
|
||||
#define BU_WASM_EXPORT(name) __attribute__((export_name(name)))
|
||||
#endif
|
||||
#elif defined(__cplusplus)
|
||||
#define BU_WASM_EXPORT(name) extern "C"
|
||||
#else
|
||||
#define BU_WASM_EXPORT(name)
|
||||
#endif
|
||||
|
||||
// wasm_bool_t is an alias for uint32_t
|
||||
typedef uint32_t wasm_bool_t;
|
||||
|
||||
// Compression constants
|
||||
|
||||
#define BU_QUALITY_MIN 0
|
||||
#define BU_QUALITY_MAX 100
|
||||
|
||||
#define BU_EFFORT_MIN 0
|
||||
#define BU_EFFORT_MAX 10
|
||||
#define BU_EFFORT_SUPER_FAST = 0
|
||||
#define BU_EFFORT_FAST = 2
|
||||
#define BU_EFFORT_NORMAL = 5
|
||||
#define BU_EFFORT_DEFAULT = 2
|
||||
#define BU_EFFORT_SLOW = 8
|
||||
#define BU_EFFORT_VERY_SLOW = 10
|
||||
|
||||
#define BU_COMP_FLAGS_NONE (0)
|
||||
#define BU_COMP_FLAGS_USE_OPENCL (1 << 8 )
|
||||
#define BU_COMP_FLAGS_THREADED (1 << 9 )
|
||||
#define BU_COMP_FLAGS_DEBUG_OUTPUT (1 << 10)
|
||||
#define BU_COMP_FLAGS_KTX2_OUTPUT (1 << 11)
|
||||
#define BU_COMP_FLAGS_KTX2_UASTC_ZSTD (1 << 12)
|
||||
#define BU_COMP_FLAGS_SRGB (1 << 13)
|
||||
#define BU_COMP_FLAGS_GEN_MIPS_CLAMP (1 << 14)
|
||||
#define BU_COMP_FLAGS_GEN_MIPS_WRAP (1 << 15)
|
||||
#define BU_COMP_FLAGS_Y_FLIP (1 << 16)
|
||||
#define BU_COMP_FLAGS_PRINT_STATS (1 << 18)
|
||||
#define BU_COMP_FLAGS_PRINT_STATUS (1 << 19)
|
||||
#define BU_COMP_FLAGS_DEBUG_IMAGES (1 << 20)
|
||||
#define BU_COMP_FLAGS_REC2020 (1 << 21)
|
||||
#define BU_COMP_FLAGS_VALIDATE_OUTPUT (1 << 22)
|
||||
|
||||
#define BU_COMP_FLAGS_XUASTC_LDR_FULL_ARITH (0)
|
||||
#define BU_COMP_FLAGS_XUASTC_LDR_HYBRID (1 << 23)
|
||||
#define BU_COMP_FLAGS_XUASTC_LDR_FULL_ZSTD (2 << 23)
|
||||
#define BU_COMP_FLAGS_XUASTC_LDR_SYNTAX_SHIFT (23)
|
||||
#define BU_COMP_FLAGS_XUASTC_LDR_SYNTAX_MASK (3)
|
||||
|
||||
#define BU_COMP_FLAGS_TEXTURE_TYPE_2D (0 << 25)
|
||||
#define BU_COMP_FLAGS_TEXTURE_TYPE_2D_ARRAY (1 << 25)
|
||||
#define BU_COMP_FLAGS_TEXTURE_TYPE_CUBEMAP_ARRAY (2 << 25)
|
||||
#define BU_COMP_FLAGS_TEXTURE_TYPE_VIDEO_FRAMES (3 << 25)
|
||||
#define BU_COMP_FLAGS_TEXTURE_TYPE_SHIFT (25)
|
||||
#define BU_COMP_FLAGS_TEXTURE_TYPE_MASK (3)
|
||||
|
||||
#define BU_COMP_FLAGS_VERBOSE (BU_COMP_FLAGS_DEBUG_OUTPUT | BU_COMP_FLAGS_PRINT_STATS | BU_COMP_FLAGS_PRINT_STATUS)
|
||||
|
||||
// basist::basis_tex_format: the supported .ktx2 (and .basis) file format types
|
||||
#define BTF_ETC1S 0
|
||||
#define BTF_UASTC_LDR_4X4 1
|
||||
#define BTF_UASTC_HDR_4X4 2
|
||||
#define BTF_ASTC_HDR_6X6 3
|
||||
#define BTF_UASTC_HDR_6X6 4
|
||||
#define BTF_XUASTC_LDR_4X4 5
|
||||
#define BTF_XUASTC_LDR_5X4 6
|
||||
#define BTF_XUASTC_LDR_5X5 7
|
||||
#define BTF_XUASTC_LDR_6X5 8
|
||||
#define BTF_XUASTC_LDR_6X6 9
|
||||
#define BTF_XUASTC_LDR_8X5 10
|
||||
#define BTF_XUASTC_LDR_8X6 11
|
||||
#define BTF_XUASTC_LDR_10X5 12
|
||||
#define BTF_XUASTC_LDR_10X6 13
|
||||
#define BTF_XUASTC_LDR_8X8 14
|
||||
#define BTF_XUASTC_LDR_10X8 15
|
||||
#define BTF_XUASTC_LDR_10X10 16
|
||||
#define BTF_XUASTC_LDR_12X10 17
|
||||
#define BTF_XUASTC_LDR_12X12 18
|
||||
#define BTF_ASTC_LDR_4X4 19
|
||||
#define BTF_ASTC_LDR_5X4 20
|
||||
#define BTF_ASTC_LDR_5X5 21
|
||||
#define BTF_ASTC_LDR_6X5 22
|
||||
#define BTF_ASTC_LDR_6X6 23
|
||||
#define BTF_ASTC_LDR_8X5 24
|
||||
#define BTF_ASTC_LDR_8X6 25
|
||||
#define BTF_ASTC_LDR_10X5 26
|
||||
#define BTF_ASTC_LDR_10X6 27
|
||||
#define BTF_ASTC_LDR_8X8 28
|
||||
#define BTF_ASTC_LDR_10X8 29
|
||||
#define BTF_ASTC_LDR_10X10 30
|
||||
#define BTF_ASTC_LDR_12X10 31
|
||||
#define BTF_ASTC_LDR_12X12 32
|
||||
#define BTF_TOTAL_FORMATS 33
|
||||
|
||||
// Transcoding constants
|
||||
|
||||
// basist::transcoder_texture_format: the supported transcode GPU texture formats
|
||||
#define TF_ETC1_RGB 0
|
||||
#define TF_ETC2_RGBA 1
|
||||
#define TF_BC1_RGB 2
|
||||
#define TF_BC3_RGBA 3
|
||||
#define TF_BC4_R 4
|
||||
#define TF_BC5_RG 5
|
||||
#define TF_BC7_RGBA 6
|
||||
#define TF_PVRTC1_4_RGB 8
|
||||
#define TF_PVRTC1_4_RGBA 9
|
||||
#define TF_ASTC_LDR_4X4_RGBA 10
|
||||
#define TF_ATC_RGB 11
|
||||
#define TF_ATC_RGBA 12
|
||||
#define TF_FXT1_RGB 17
|
||||
#define TF_PVRTC2_4_RGB 18
|
||||
#define TF_PVRTC2_4_RGBA 19
|
||||
#define TF_ETC2_EAC_R11 20
|
||||
#define TF_ETC2_EAC_RG11 21
|
||||
#define TF_BC6H 22
|
||||
#define TF_ASTC_HDR_4X4_RGBA 23
|
||||
#define TF_RGBA32 13
|
||||
#define TF_RGB565 14
|
||||
#define TF_BGR565 15
|
||||
#define TF_RGBA4444 16
|
||||
#define TF_RGB_HALF 24
|
||||
#define TF_RGBA_HALF 25
|
||||
#define TF_RGB_9E5 26
|
||||
#define TF_ASTC_HDR_6X6_RGBA 27
|
||||
#define TF_ASTC_LDR_5X4_RGBA 28
|
||||
#define TF_ASTC_LDR_5X5_RGBA 29
|
||||
#define TF_ASTC_LDR_6X5_RGBA 30
|
||||
#define TF_ASTC_LDR_6X6_RGBA 31
|
||||
#define TF_ASTC_LDR_8X5_RGBA 32
|
||||
#define TF_ASTC_LDR_8X6_RGBA 33
|
||||
#define TF_ASTC_LDR_10X5_RGBA 34
|
||||
#define TF_ASTC_LDR_10X6_RGBA 35
|
||||
#define TF_ASTC_LDR_8X8_RGBA 36
|
||||
#define TF_ASTC_LDR_10X8_RGBA 37
|
||||
#define TF_ASTC_LDR_10X10_RGBA 38
|
||||
#define TF_ASTC_LDR_12X10_RGBA 39
|
||||
#define TF_ASTC_LDR_12X12_RGBA 40
|
||||
#define TF_TOTAL_TEXTURE_FORMATS 41
|
||||
|
||||
// basist::basisu_decode_flags: Transcode decode flags (bt_ktx2_transcode_image_level decode_flags parameter, logically OR'd)
|
||||
#define DECODE_FLAGS_PVRTC_DECODE_TO_NEXT_POW2 2
|
||||
#define DECODE_FLAGS_TRANSCODE_ALPHA_DATA_TO_OPAQUE_FORMATS 4
|
||||
#define DECODE_FLAGS_BC1_FORBID_THREE_COLOR_BLOCKS 8
|
||||
#define DECODE_FLAGS_OUTPUT_HAS_ALPHA_INDICES 16
|
||||
#define DECODE_FLAGS_HIGH_QUALITY 32
|
||||
#define DECODE_FLAGS_NO_ETC1S_CHROMA_FILTERING 64
|
||||
#define DECODE_FLAGS_NO_DEBLOCK_FILTERING 128
|
||||
#define DECODE_FLAGS_STRONGER_DEBLOCK_FILTERING 256
|
||||
#define DECODE_FLAGS_FORCE_DEBLOCK_FILTERING 512
|
||||
#define DECODE_FLAGS_XUASTC_LDR_DISABLE_FAST_BC7_TRANSCODING 1024
|
||||
1071
encoder/basisu_wasm_transcoder_api.cpp
Normal file
1071
encoder/basisu_wasm_transcoder_api.cpp
Normal file
File diff suppressed because it is too large
Load Diff
216
encoder/basisu_wasm_transcoder_api.h
Normal file
216
encoder/basisu_wasm_transcoder_api.h
Normal file
@@ -0,0 +1,216 @@
|
||||
// File: basisu_wasm_transcoder_api.h - Transcoding API support for WASM WASI modules and Python native support.
|
||||
#pragma once
|
||||
#include "basisu_wasm_api_common.h"
|
||||
|
||||
// High-level functions
|
||||
|
||||
BU_WASM_EXPORT("bt_get_version")
|
||||
uint32_t bt_get_version();
|
||||
|
||||
BU_WASM_EXPORT("bt_enable_debug_printf")
|
||||
void bt_enable_debug_printf(uint32_t flag);
|
||||
|
||||
BU_WASM_EXPORT("bt_init")
|
||||
void bt_init();
|
||||
|
||||
BU_WASM_EXPORT("bt_alloc")
|
||||
uint64_t bt_alloc(uint64_t size);
|
||||
|
||||
BU_WASM_EXPORT("bt_free")
|
||||
void bt_free(uint64_t ofs);
|
||||
|
||||
// basis_tex_format helpers
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_tex_format_is_xuastc_ldr")
|
||||
wasm_bool_t bt_basis_tex_format_is_xuastc_ldr(uint32_t basis_tex_fmt_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_tex_format_is_astc_ldr")
|
||||
wasm_bool_t bt_basis_tex_format_is_astc_ldr(uint32_t basis_tex_fmt_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_tex_format_get_block_width")
|
||||
uint32_t bt_basis_tex_format_get_block_width(uint32_t basis_tex_fmt_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_tex_format_get_block_height")
|
||||
uint32_t bt_basis_tex_format_get_block_height(uint32_t basis_tex_fmt_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_tex_format_is_hdr")
|
||||
wasm_bool_t bt_basis_tex_format_is_hdr(uint32_t basis_tex_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_tex_format_is_ldr")
|
||||
wasm_bool_t bt_basis_tex_format_is_ldr(uint32_t basis_tex_format_u32);
|
||||
|
||||
// transcoder_texture_format helpers
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_get_bytes_per_block_or_pixel")
|
||||
uint32_t bt_basis_get_bytes_per_block_or_pixel(uint32_t transcoder_texture_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_transcoder_format_has_alpha")
|
||||
wasm_bool_t bt_basis_transcoder_format_has_alpha(uint32_t transcoder_texture_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_transcoder_format_is_hdr")
|
||||
wasm_bool_t bt_basis_transcoder_format_is_hdr(uint32_t transcoder_texture_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_transcoder_format_is_ldr")
|
||||
wasm_bool_t bt_basis_transcoder_format_is_ldr(uint32_t transcoder_texture_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_transcoder_texture_format_is_astc")
|
||||
wasm_bool_t bt_basis_transcoder_texture_format_is_astc(uint32_t transcoder_texture_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_transcoder_format_is_uncompressed")
|
||||
wasm_bool_t bt_basis_transcoder_format_is_uncompressed(uint32_t transcoder_texture_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_get_uncompressed_bytes_per_pixel")
|
||||
uint32_t bt_basis_get_uncompressed_bytes_per_pixel(uint32_t transcoder_texture_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_get_block_width")
|
||||
uint32_t bt_basis_get_block_width(uint32_t transcoder_texture_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_get_block_height")
|
||||
uint32_t bt_basis_get_block_height(uint32_t transcoder_texture_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_get_transcoder_texture_format_from_basis_tex_format")
|
||||
uint32_t bt_basis_get_transcoder_texture_format_from_basis_tex_format(uint32_t basis_tex_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_is_format_supported")
|
||||
wasm_bool_t bt_basis_is_format_supported(uint32_t transcoder_texture_format_u32, uint32_t basis_tex_format_u32);
|
||||
|
||||
BU_WASM_EXPORT("bt_basis_compute_transcoded_image_size_in_bytes")
|
||||
uint32_t bt_basis_compute_transcoded_image_size_in_bytes(uint32_t transcoder_texture_format_u32, uint32_t orig_width, uint32_t orig_height);
|
||||
|
||||
// Transcoding
|
||||
BU_WASM_EXPORT("bt_ktx2_open")
|
||||
uint64_t bt_ktx2_open(uint64_t data_mem_ofs, uint32_t data_len);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_close")
|
||||
void bt_ktx2_close(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_width")
|
||||
uint32_t bt_ktx2_get_width(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_height")
|
||||
uint32_t bt_ktx2_get_height(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_levels")
|
||||
uint32_t bt_ktx2_get_levels(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_faces")
|
||||
uint32_t bt_ktx2_get_faces(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_layers")
|
||||
uint32_t bt_ktx2_get_layers(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_basis_tex_format")
|
||||
uint32_t bt_ktx2_get_basis_tex_format(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_is_etc1s")
|
||||
wasm_bool_t bt_ktx2_is_etc1s(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_is_uastc_ldr_4x4")
|
||||
wasm_bool_t bt_ktx2_is_uastc_ldr_4x4(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_is_hdr")
|
||||
wasm_bool_t bt_ktx2_is_hdr(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_is_hdr_4x4")
|
||||
wasm_bool_t bt_ktx2_is_hdr_4x4(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_is_hdr_6x6")
|
||||
wasm_bool_t bt_ktx2_is_hdr_6x6(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_is_ldr")
|
||||
wasm_bool_t bt_ktx2_is_ldr(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_is_astc_ldr")
|
||||
wasm_bool_t bt_ktx2_is_astc_ldr(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_is_xuastc_ldr")
|
||||
wasm_bool_t bt_ktx2_is_xuastc_ldr(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_block_width")
|
||||
uint32_t bt_ktx2_get_block_width(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_block_height")
|
||||
uint32_t bt_ktx2_get_block_height(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_has_alpha")
|
||||
wasm_bool_t bt_ktx2_has_alpha(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_dfd_color_model")
|
||||
uint32_t bt_ktx2_get_dfd_color_model(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_dfd_color_primaries")
|
||||
uint32_t bt_ktx2_get_dfd_color_primaries(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_dfd_transfer_func")
|
||||
uint32_t bt_ktx2_get_dfd_transfer_func(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_is_srgb")
|
||||
wasm_bool_t bt_ktx2_is_srgb(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_dfd_flags")
|
||||
uint32_t bt_ktx2_get_dfd_flags(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_dfd_total_samples")
|
||||
uint32_t bt_ktx2_get_dfd_total_samples(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_dfd_channel_id0")
|
||||
uint32_t bt_ktx2_get_dfd_channel_id0(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_dfd_channel_id1")
|
||||
uint32_t bt_ktx2_get_dfd_channel_id1(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_is_video")
|
||||
wasm_bool_t bt_ktx2_is_video(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_ldr_hdr_upconversion_nit_multiplier")
|
||||
float bt_ktx2_get_ldr_hdr_upconversion_nit_multiplier(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_level_orig_width")
|
||||
uint32_t bt_ktx2_get_level_orig_width(uint64_t handle, uint32_t level_index, uint32_t layer_index, uint32_t face_index);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_level_orig_height")
|
||||
uint32_t bt_ktx2_get_level_orig_height(uint64_t handle, uint32_t level_index, uint32_t layer_index, uint32_t face_index);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_level_actual_width")
|
||||
uint32_t bt_ktx2_get_level_actual_width(uint64_t handle, uint32_t level_index, uint32_t layer_index, uint32_t face_index);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_level_actual_height")
|
||||
uint32_t bt_ktx2_get_level_actual_height(uint64_t handle, uint32_t level_index, uint32_t layer_index, uint32_t face_index);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_level_num_blocks_x")
|
||||
uint32_t bt_ktx2_get_level_num_blocks_x(uint64_t handle, uint32_t level_index, uint32_t layer_index, uint32_t face_index);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_level_num_blocks_y")
|
||||
uint32_t bt_ktx2_get_level_num_blocks_y(uint64_t handle, uint32_t level_index, uint32_t layer_index, uint32_t face_index);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_level_total_blocks")
|
||||
uint32_t bt_ktx2_get_level_total_blocks(uint64_t handle, uint32_t level_index, uint32_t layer_index, uint32_t face_index);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_level_alpha_flag")
|
||||
wasm_bool_t bt_ktx2_get_level_alpha_flag(uint64_t handle, uint32_t level_index, uint32_t layer_index, uint32_t face_index);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_get_level_iframe_flag")
|
||||
wasm_bool_t bt_ktx2_get_level_iframe_flag(uint64_t handle, uint32_t level_index, uint32_t layer_index, uint32_t face_index);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_start_transcoding")
|
||||
wasm_bool_t bt_ktx2_start_transcoding(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_create_transcode_state")
|
||||
uint64_t bt_ktx2_create_transcode_state();
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_destroy_transcode_state")
|
||||
void bt_ktx2_destroy_transcode_state(uint64_t handle);
|
||||
|
||||
BU_WASM_EXPORT("bt_ktx2_transcode_image_level")
|
||||
wasm_bool_t bt_ktx2_transcode_image_level(
|
||||
uint64_t ktx2_handle, // handle to KTX2 file, see bt_ktx2_open()
|
||||
uint32_t level_index, uint32_t layer_index, uint32_t face_index, // KTX2 level/layer/face to transcode
|
||||
uint64_t output_block_mem_ofs, // allocate using bt_alloc()
|
||||
uint32_t output_blocks_buf_size_in_blocks_or_pixels,
|
||||
uint32_t transcoder_texture_format_u32, // target format, TF_ETC1_RGB etc.
|
||||
uint32_t decode_flags, // DECODE_FLAGS_
|
||||
uint32_t output_row_pitch_in_blocks_or_pixels, // can be 0
|
||||
uint32_t output_rows_in_pixels, // can be 0
|
||||
int channel0, int channel1, // both default to -1
|
||||
uint64_t state_handle); // thread local state: can be 0, or bt_ktx2_create_transcode_state()
|
||||
|
||||
Reference in New Issue
Block a user