include cmath

I dont know why this keeps happening
This commit is contained in:
Andrew Cassidy 2021-02-05 23:29:09 -08:00
parent 7d830daee3
commit 59db9fdb27

View File

@ -2,15 +2,18 @@
// High-performance scalar BC1-5 encoders. Public Domain or MIT license (you choose - see below), written by Richard Geldreich 2020 <richgel99@gmail.com>.
#include "rgbcx.h"
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include "blocks.h"
#include "color.h"
#include "tables.h"
#include "util.h"
#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
namespace rgbcx {
@ -40,14 +43,10 @@ struct hist4 {
}
inline bool operator==(const hist4 &h) const {
if (m_hist[0] != h.m_hist[0])
return false;
if (m_hist[1] != h.m_hist[1])
return false;
if (m_hist[2] != h.m_hist[2])
return false;
if (m_hist[3] != h.m_hist[3])
return false;
if (m_hist[0] != h.m_hist[0]) return false;
if (m_hist[1] != h.m_hist[1]) return false;
if (m_hist[2] != h.m_hist[2]) return false;
if (m_hist[3] != h.m_hist[3]) return false;
return true;
}
@ -80,12 +79,9 @@ struct hist3 {
}
inline bool operator==(const hist3 &h) const {
if (m_hist[0] != h.m_hist[0])
return false;
if (m_hist[1] != h.m_hist[1])
return false;
if (m_hist[2] != h.m_hist[2])
return false;
if (m_hist[0] != h.m_hist[0]) return false;
if (m_hist[1] != h.m_hist[1]) return false;
if (m_hist[2] != h.m_hist[2]) return false;
return true;
}
@ -234,8 +230,7 @@ static void prepare_bc1_single_color_table_half(bc1_match_entry *pTable, const u
int e = iabs(v - i);
// We only need to factor in 3% error in BC1 ideal mode.
if ((mode == bc1_approx_mode::cBC1Ideal) || (mode == bc1_approx_mode::cBC1IdealRound4))
e += (iabs(hi_e - lo_e) * 3) / 100;
if ((mode == bc1_approx_mode::cBC1Ideal) || (mode == bc1_approx_mode::cBC1IdealRound4)) e += (iabs(hi_e - lo_e) * 3) / 100;
// Favor equal endpoints, for lower error on actual GPU's which approximate the interpolation.
if ((e < lowest_e) || ((e == lowest_e) && (lo == hi))) {
@ -266,8 +261,7 @@ static void prepare_bc1_single_color_table(bc1_match_entry *pTable, const uint8_
int e = iabs(v - i);
if ((mode == bc1_approx_mode::cBC1Ideal) || (mode == bc1_approx_mode::cBC1IdealRound4))
e += (iabs(hi_e - lo_e) * 3) / 100;
if ((mode == bc1_approx_mode::cBC1Ideal) || (mode == bc1_approx_mode::cBC1IdealRound4)) e += (iabs(hi_e - lo_e) * 3) / 100;
// Favor equal endpoints, for lower error on actual GPU's which approximate the interpolation.
if ((e < lowest_e) || ((e == lowest_e) && (lo == hi))) {
@ -294,8 +288,7 @@ static const uint32_t g_weight_vals3[3] = {0x000004, 0x040000, 0x010101};
static inline void compute_selector_factors4(const hist4 &h, float &iz00, float &iz10, float &iz11) {
uint32_t weight_accum = 0;
for (uint32_t sel = 0; sel < 4; sel++)
weight_accum += g_weight_vals4[sel] * h.m_hist[sel];
for (uint32_t sel = 0; sel < 4; sel++) weight_accum += g_weight_vals4[sel] * h.m_hist[sel];
float z00 = (float)((weight_accum >> 16) & 0xFF);
float z10 = (float)((weight_accum >> 8) & 0xFF);
@ -315,8 +308,7 @@ static inline void compute_selector_factors4(const hist4 &h, float &iz00, float
static inline void compute_selector_factors3(const hist3 &h, float &iz00, float &iz10, float &iz11) {
uint32_t weight_accum = 0;
for (uint32_t sel = 0; sel < 3; sel++)
weight_accum += g_weight_vals3[sel] * h.m_hist[sel];
for (uint32_t sel = 0; sel < 3; sel++) weight_accum += g_weight_vals3[sel] * h.m_hist[sel];
float z00 = (float)((weight_accum >> 16) & 0xFF);
float z10 = (float)((weight_accum >> 8) & 0xFF);
@ -340,14 +332,12 @@ void init(bc1_approx_mode mode) {
g_bc1_approx_mode = mode;
uint8_t bc1_expand5[32];
for (int i = 0; i < 32; i++)
bc1_expand5[i] = static_cast<uint8_t>((i << 3) | (i >> 2));
for (int i = 0; i < 32; i++) bc1_expand5[i] = static_cast<uint8_t>((i << 3) | (i >> 2));
prepare_bc1_single_color_table(g_bc1_match5_equals_1, bc1_expand5, 32, mode);
prepare_bc1_single_color_table_half(g_bc1_match5_half, bc1_expand5, 32, mode);
uint8_t bc1_expand6[64];
for (int i = 0; i < 64; i++)
bc1_expand6[i] = static_cast<uint8_t>((i << 2) | (i >> 4));
for (int i = 0; i < 64; i++) bc1_expand6[i] = static_cast<uint8_t>((i << 2) | (i >> 4));
prepare_bc1_single_color_table(g_bc1_match6_equals_1, bc1_expand6, 64, mode);
prepare_bc1_single_color_table_half(g_bc1_match6_half, bc1_expand6, 64, mode);
@ -399,8 +389,7 @@ void encode_bc1_solid_block(void *pDst, uint32_t fr, uint32_t fg, uint32_t fb, b
max16 = (g_bc1_match5_half[fr].m_hi << 11) | (g_bc1_match6_half[fg].m_hi << 5) | g_bc1_match5_half[fb].m_hi;
min16 = (g_bc1_match5_half[fr].m_lo << 11) | (g_bc1_match6_half[fg].m_lo << 5) | g_bc1_match5_half[fb].m_lo;
if (max16 > min16)
std::swap(max16, min16);
if (max16 > min16) std::swap(max16, min16);
}
}
@ -505,8 +494,7 @@ static inline bool compute_least_squares_endpoints4_rgb(const Color32 *pColors,
float z01 = z10;
float det = z00 * z11 - z01 * z10;
if (fabs(det) < 1e-8f)
return false;
if (fabs(det) < 1e-8f) return false;
det = (3.0f / 255.0f) / det;
@ -560,14 +548,12 @@ static inline bool compute_least_squares_endpoints3_rgb(bool use_black, const Co
for (uint32_t i = 0; i < 16; i++) {
const uint8_t r = pColors[i].C[0], g = pColors[i].C[1], b = pColors[i].C[2];
if (use_black) {
if ((r | g | b) < 4)
continue;
if ((r | g | b) < 4) continue;
}
const uint8_t sel = pSelectors[i];
assert(sel <= 3);
if (sel == 3)
continue;
if (sel == 3) continue;
weight_accum += g_weight_vals3[sel];
@ -592,8 +578,7 @@ static inline bool compute_least_squares_endpoints3_rgb(bool use_black, const Co
float z01 = z10;
float det = z00 * z11 - z01 * z10;
if (fabs(det) < 1e-8f)
return false;
if (fabs(det) < 1e-8f) return false;
det = (2.0f / 255.0f) / det;
@ -687,8 +672,7 @@ static inline void bc1_find_sels4_noerr(const Color32 *pSrc_pixels, uint32_t lr,
int ar = block_r[3] - block_r[0], ag = block_g[3] - block_g[0], ab = block_b[3] - block_b[0];
int dots[4];
for (uint32_t i = 0; i < 4; i++)
dots[i] = (int)block_r[i] * ar + (int)block_g[i] * ag + (int)block_b[i] * ab;
for (uint32_t i = 0; i < 4; i++) dots[i] = (int)block_r[i] * ar + (int)block_g[i] * ag + (int)block_b[i] * ab;
int t0 = dots[0] + dots[1], t1 = dots[1] + dots[2], t2 = dots[2] + dots[3];
@ -719,8 +703,7 @@ static inline uint32_t bc1_find_sels4_fasterr(const Color32 *pSrc_pixels, uint32
int ar = block_r[3] - block_r[0], ag = block_g[3] - block_g[0], ab = block_b[3] - block_b[0];
int dots[4];
for (uint32_t i = 0; i < 4; i++)
dots[i] = (int)block_r[i] * ar + (int)block_g[i] * ag + (int)block_b[i] * ab;
for (uint32_t i = 0; i < 4; i++) dots[i] = (int)block_r[i] * ar + (int)block_g[i] * ag + (int)block_b[i] * ab;
int t0 = dots[0] + dots[1], t1 = dots[1] + dots[2], t2 = dots[2] + dots[3];
@ -757,8 +740,7 @@ static inline uint32_t bc1_find_sels4_fasterr(const Color32 *pSrc_pixels, uint32
total_err +=
squarei(pSrc_pixels[i + 3].R - block_r[sel3]) + squarei(pSrc_pixels[i + 3].G - block_g[sel3]) + squarei(pSrc_pixels[i + 3].B - block_b[sel3]);
if (total_err >= cur_err)
break;
if (total_err >= cur_err) break;
}
return total_err;
@ -790,8 +772,7 @@ static inline uint32_t bc1_find_sels4_check2_err(const Color32 *pSrc_pixels, uin
uint32_t best_err = err1;
if (err0 == err1) {
// Prefer non-interpolation
if ((best_sel - 1) == 0)
best_sel = 0;
if ((best_sel - 1) == 0) best_sel = 0;
} else if (err0 < best_err) {
best_sel = sel - 1;
best_err = err0;
@ -799,8 +780,7 @@ static inline uint32_t bc1_find_sels4_check2_err(const Color32 *pSrc_pixels, uin
total_err += best_err;
if (total_err >= cur_err)
break;
if (total_err >= cur_err) break;
sels[i] = (uint8_t)best_sel;
}
@ -832,8 +812,7 @@ static inline uint32_t bc1_find_sels4_fullerr(const Color32 *pSrc_pixels, uint32
total_err += best_err;
if (total_err >= cur_err)
break;
if (total_err >= cur_err) break;
sels[i] = (uint8_t)best_sel;
}
@ -890,8 +869,7 @@ static inline uint32_t bc1_find_sels3_fullerr(bool use_black, const Color32 *pSr
}
total_err += best_err;
if (total_err >= cur_err)
return total_err;
if (total_err >= cur_err) return total_err;
sels[i] = (uint8_t)best_sel;
}
@ -984,8 +962,7 @@ static inline void bc1_encode4(BC1Block *pDst_block, int lr, int lg, int lb, int
uint32_t packed_sels = 0;
static const uint8_t s_sel_trans[4] = {0, 2, 3, 1};
for (uint32_t i = 0; i < 16; i++)
packed_sels |= ((uint32_t)s_sel_trans[sels[i]] << (i * 2));
for (uint32_t i = 0; i < 16; i++) packed_sels |= ((uint32_t)s_sel_trans[sels[i]] << (i * 2));
// todo: make this less silly to prevent packing and unpacking
pDst_block->selectors[0] = (uint8_t)packed_sels ^ invert_mask;
@ -1015,11 +992,9 @@ static inline void bc1_encode3(BC1Block *pDst_block, int lr, int lg, int lb, int
if (invert_flag) {
static const uint8_t s_sel_trans_inv[4] = {1, 0, 2, 3};
for (uint32_t i = 0; i < 16; i++)
packed_sels |= ((uint32_t)s_sel_trans_inv[sels[i]] << (i * 2));
for (uint32_t i = 0; i < 16; i++) packed_sels |= ((uint32_t)s_sel_trans_inv[sels[i]] << (i * 2));
} else {
for (uint32_t i = 0; i < 16; i++)
packed_sels |= ((uint32_t)sels[i] << (i * 2));
for (uint32_t i = 0; i < 16; i++) packed_sels |= ((uint32_t)sels[i] << (i * 2));
}
// todo: make this less silly to prevent packing and unpacking
@ -1043,8 +1018,7 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
int total_pixels = 0;
for (uint32_t i = 0; i < 16; i++) {
const int r = pSrc_pixels[i].R, g = pSrc_pixels[i].G, b = pSrc_pixels[i].B;
if ((r | g | b) < 4)
continue;
if ((r | g | b) < 4) continue;
max_r = std::max(max_r, r);
max_g = std::max(max_g, g);
@ -1059,8 +1033,7 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
total_pixels++;
}
if (!total_pixels)
return false;
if (!total_pixels) return false;
int half_total_pixels = total_pixels >> 1;
int avg_r = (total_r + half_total_pixels) / total_pixels;
@ -1075,8 +1048,7 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
int g = (int)pSrc_pixels[i].G;
int b = (int)pSrc_pixels[i].B;
if ((r | g | b) < 4)
continue;
if ((r | g | b) < 4) continue;
r -= avg_r;
g -= avg_g;
@ -1091,18 +1063,15 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
}
float cov[6];
for (uint32_t i = 0; i < 6; i++)
cov[i] = (float)(icov[i]) * (1.0f / 255.0f);
for (uint32_t i = 0; i < 6; i++) cov[i] = (float)(icov[i]) * (1.0f / 255.0f);
float xr = (float)(max_r - min_r);
float xg = (float)(max_g - min_g);
float xb = (float)(max_b - min_b);
if (icov[2] < 0)
xr = -xr;
if (icov[2] < 0) xr = -xr;
if (icov[4] < 0)
xg = -xg;
if (icov[4] < 0) xg = -xg;
for (uint32_t power_iter = 0; power_iter < 4; power_iter++) {
float r = xr * cov[0] + xg * cov[1] + xb * cov[2];
@ -1126,8 +1095,7 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
for (uint32_t i = 0; i < 16; i++) {
int r = (int)pSrc_pixels[i].R, g = (int)pSrc_pixels[i].G, b = (int)pSrc_pixels[i].B;
if ((r | g | b) < 4)
continue;
if ((r | g | b) < 4) continue;
int dot = r * saxis_r + g * saxis_g + b * saxis_b;
if (dot < low_dot) {
@ -1168,8 +1136,7 @@ static bool try_3color_block_useblack(const Color32 *pSrc_pixels, uint32_t flags
precise_round_565(xl, xh, hr2, hg2, hb2, lr2, lg2, lb2);
}
if ((lr == lr2) && (lg == lg2) && (lb == lb2) && (hr == hr2) && (hg == hg2) && (hb == hb2))
break;
if ((lr == lr2) && (lg == lg2) && (lb == lb2) && (hr == hr2) && (hg == hg2) && (hb == hb2)) break;
uint8_t trial_sels2[16];
uint32_t trial_err2 = bc1_find_sels3_fullerr(true, pSrc_pixels, lr2, lg2, lb2, hr2, hg2, hb2, trial_sels2, trial_err);
@ -1228,8 +1195,7 @@ static bool try_3color_block(const Color32 *pSrc_pixels, uint32_t flags, uint32_
precise_round_565(xl, xh, hr2, hg2, hb2, lr2, lg2, lb2);
}
if ((lr == lr2) && (lg == lg2) && (lb == lb2) && (hr == hr2) && (hg == hg2) && (hb == hb2))
break;
if ((lr == lr2) && (lg == lg2) && (lb == lb2) && (hr == hr2) && (hg == hg2) && (hb == hb2)) break;
uint8_t trial_sels2[16];
uint32_t trial_err2 = bc1_find_sels3_fullerr(false, pSrc_pixels, lr2, lg2, lb2, hr2, hg2, hb2, trial_sels2, trial_err);
@ -1505,11 +1471,9 @@ static inline void encode_bc1_pick_initial(const Color32 *pSrc_pixels, uint32_t
} else if (flags & cEncodeBC1Use2DLS) {
// 2D Least Squares approach from Humus's example, with added inset and optimal rounding.
int big_chan = 0, min_chan_val = min_r, max_chan_val = max_r;
if ((max_g - min_g) > (max_chan_val - min_chan_val))
big_chan = 1, min_chan_val = min_g, max_chan_val = max_g;
if ((max_g - min_g) > (max_chan_val - min_chan_val)) big_chan = 1, min_chan_val = min_g, max_chan_val = max_g;
if ((max_b - min_b) > (max_chan_val - min_chan_val))
big_chan = 2, min_chan_val = min_b, max_chan_val = max_b;
if ((max_b - min_b) > (max_chan_val - min_chan_val)) big_chan = 2, min_chan_val = min_b, max_chan_val = max_b;
int sum_xy_r = 0, sum_xy_g = 0, sum_xy_b = 0;
vec3F l, h;
@ -1672,11 +1636,9 @@ static inline void encode_bc1_pick_initial(const Color32 *pSrc_pixels, uint32_t
icov_yz += g * b;
}
if (icov_xz < 0)
std::swap(l.c[0], h.c[0]);
if (icov_xz < 0) std::swap(l.c[0], h.c[0]);
if (icov_yz < 0)
std::swap(l.c[1], h.c[1]);
if (icov_yz < 0) std::swap(l.c[1], h.c[1]);
precise_round_565(l, h, lr, lg, lb, hr, hg, hb);
} else if (flags & cEncodeBC1BoundingBoxInt) {
@ -1717,11 +1679,9 @@ static inline void encode_bc1_pick_initial(const Color32 *pSrc_pixels, uint32_t
int x1 = max_r;
int y1 = max_g;
if (icov_xz < 0)
std::swap(x0, x1);
if (icov_xz < 0) std::swap(x0, x1);
if (icov_yz < 0)
std::swap(y0, y1);
if (icov_yz < 0) std::swap(y0, y1);
lr = scale8To5(x0);
lg = scale8To6(y0);
@ -1753,15 +1713,12 @@ static inline void encode_bc1_pick_initial(const Color32 *pSrc_pixels, uint32_t
float xg = (float)(max_g - min_g);
float xb = (float)(max_b - min_b);
if (icov[2] < 0)
xr = -xr;
if (icov[2] < 0) xr = -xr;
if (icov[4] < 0)
xg = -xg;
if (icov[4] < 0) xg = -xg;
float cov[6];
for (uint32_t i = 0; i < 6; i++)
cov[i] = (float)(icov[i]) * (1.0f / 255.0f);
for (uint32_t i = 0; i < 6; i++) cov[i] = (float)(icov[i]) * (1.0f / 255.0f);
const uint32_t total_power_iters = (flags & cEncodeBC1Use6PowerIters) ? 6 : 4;
for (uint32_t power_iter = 0; power_iter < total_power_iters; power_iter++) {
@ -1849,8 +1806,7 @@ static inline void encode_bc1_endpoint_search(const Color32 *pSrc_pixels, bool a
for (int i = 0; i < endpoint_search_rounds; i++) {
assert(s_adjacent_voxels[s_adjacent_voxels[i & 15][3]][3] == (i & 15));
if (forbidden_direction == (i & 31))
continue;
if (forbidden_direction == (i & 31)) continue;
const int8_t delta[3] = {s_adjacent_voxels[i & 15][0], s_adjacent_voxels[i & 15][1], s_adjacent_voxels[i & 15][2]};
@ -1888,8 +1844,7 @@ static inline void encode_bc1_endpoint_search(const Color32 *pSrc_pixels, bool a
prev_improvement_index = i;
}
if (i - prev_improvement_index > 32)
break;
if (i - prev_improvement_index > 32) break;
}
}
@ -1905,8 +1860,7 @@ void encode_bc1(void *pDst, const uint8_t *pPixels, uint32_t flags, uint32_t tot
uint32_t j;
for (j = 15; j >= 1; --j)
if ((pSrc_pixels[j].R != fr) || (pSrc_pixels[j].G != fg) || (pSrc_pixels[j].B != fb))
break;
if ((pSrc_pixels[j].R != fr) || (pSrc_pixels[j].G != fg) || (pSrc_pixels[j].B != fb)) break;
if (j == 0) {
encode_bc1_solid_block(pDst, fr, fg, fb, (flags & (cEncodeBC1Use3ColorBlocks | cEncodeBC1Use3ColorBlocksForBlackPixels)) != 0);
@ -1985,8 +1939,7 @@ void encode_bc1(void *pDst, const uint8_t *pPixels, uint32_t flags, uint32_t tot
precise_round_565(xl, xh, trial_hr, trial_hg, trial_hb, trial_lr, trial_lg, trial_lb);
}
if ((lr == trial_lr) && (lg == trial_lg) && (lb == trial_lb) && (hr == trial_hr) && (hg == trial_hg) && (hb == trial_hb))
break;
if ((lr == trial_lr) && (lg == trial_lg) && (lb == trial_lb) && (hr == trial_hr) && (hg == trial_hg) && (hb == trial_hb)) break;
bc1_find_sels4_noerr(pSrc_pixels, trial_lr, trial_lg, trial_lb, trial_hr, trial_hg, trial_hb, sels);
@ -2183,8 +2136,7 @@ void encode_bc1(void *pDst, const uint8_t *pPixels, uint32_t flags, uint32_t tot
} // s
if ((!cur_err) || (cur_err == orig_err))
break;
if ((!cur_err) || (cur_err == orig_err)) break;
} // iter_index
}
@ -2497,8 +2449,7 @@ bool unpack_bc3(const void *pBlock_bits, void *pPixels, bc1_approx_mode mode) {
bool success = true;
if (unpack_bc1((const uint8_t *)pBlock_bits + sizeof(BC4Block), pDst_pixels, true, mode))
success = false;
if (unpack_bc1((const uint8_t *)pBlock_bits + sizeof(BC4Block), pDst_pixels, true, mode)) success = false;
unpack_bc4(pBlock_bits, &pDst_pixels[0].A, sizeof(Color32));