Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/encode/bc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ fn get_bc4_options(options: &EncodeOptions) -> bc4::Bc4Options {
CompressionQuality::Unreasonable => 10,
},
size_variations: options.quality >= CompressionQuality::High,
iter_initial_inter4: options.quality >= CompressionQuality::High,
}
}

Expand Down
110 changes: 104 additions & 6 deletions src/encode/bc4.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::needless_range_loop)]

use glam::{UVec4, Vec4};

use crate::{n8, s8, util::unlikely_branch};
Expand All @@ -15,6 +17,7 @@ pub(crate) struct Bc4Options {
pub fast_iter: bool,
pub max_refine_iter: u8,
pub size_variations: bool,
pub iter_initial_inter4: bool,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -368,12 +371,13 @@ fn compress_inter6_impl(
fn compress_inter4(block: &Block, options: Bc4Options) -> ([u8; 8], f32) {
let (mut min, mut max) = block.min_max_with_threshold(BC4_MIN_VALUE);

(min, max) = refine_endpoints(
min,
max,
refinement_error_metric::<Inter4Palette>(block, options),
options,
);
let compute_error = refinement_error_metric::<Inter4Palette>(block, options);

if options.iter_initial_inter4 {
(min, max) = inter4_reassign_defaults(block, min, max, compute_error);
}

(min, max) = refine_endpoints(min, max, compute_error, options);

let endpoints = EndPoints::new_inter4(min, max, options.snorm);
let palette = Inter4Palette::from_endpoints(&endpoints);
Expand All @@ -386,6 +390,100 @@ fn compress_inter4(block: &Block, options: Bc4Options) -> ([u8; 8], f32) {

(endpoints.with_indexes(indexes), error)
}
/// The starting values for min/max for inter4 only assigns values very close to
/// 0 or 1 to the default values. This is suboptimal, as a palette that assigns
/// more pixels to the default values might better represent the block overall.
///
/// This function tries to reassign min/max to other values in the block to
/// improve the overall error. The given error function is assumed to be MSE.
fn inter4_reassign_defaults(
block: &Block,
mut min: f32,
mut max: f32,
compute_error: impl Fn((f32, f32)) -> f32,
) -> (f32, f32) {
// There is no point in doing this optimization if min/max are sufficiently
// far away from 0/1.
const MAX_DISTANCE: f32 = 0.125;
if min > MAX_DISTANCE && max < 1.0 - MAX_DISTANCE {
return (min, max);
}

// sort values into an array
let mut values: [f32; 16] = [0.0; 16];
for i in 0..16 {
values[i] = block.get_pixel_at(i);
}
values.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());

// note the indexes of the min and max values
let mut min_index = 0;
let mut max_index = 0;
for i in 0..16 {
let v = values[i];
if v <= min {
min_index = i;
}
if v <= max {
max_index = i;
}
}

let mut error = compute_error((min, max));
#[derive(PartialEq, Eq)]
enum LastMove {
None,
Min,
Max,
}
let mut last_failed_move = LastMove::None;

const MAX_ITER: u8 = 4;
for _ in 0..MAX_ITER {
if min_index >= max_index {
break;
}

// distance of min/max to 0/1
let min_dist = min;
let max_dist = 1.0 - max;

// try to move endpoints towards each other, starting with the one closer to 0/1
if min_dist < max_dist {
if last_failed_move == LastMove::Min {
break;
}

// move min up
let new_min = values[min_index + 1];
let new_error = compute_error((new_min, max));
if new_error < error {
min = new_min;
error = new_error;
min_index += 1;
} else {
last_failed_move = LastMove::Min;
}
} else {
if last_failed_move == LastMove::Max {
break;
}

// move max down
let new_max = values[max_index - 1];
let new_error = compute_error((min, new_max));
if new_error < error {
max = new_max;
error = new_error;
max_index -= 1;
} else {
last_failed_move = LastMove::Max;
}
}
}

(min, max)
}

struct EndPoints {
c0: u8,
Expand Down
14 changes: 7 additions & 7 deletions test-data/encode_quality.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,23 +695,23 @@
| | | | | |
| Summary | fast | Gray | 42.29 | 54.32 | 0.12
| | normal | Gray | 43.56 | 56.16 | 0.11
| | high | Gray | 43.99 | 57.18 | 0.08
| | dither | Gray | 43.33 | 58.44 | 0.06
| | high | Gray | 44.04 | 57.20 | 0.08
| | dither | Gray | 43.38 | 58.55 | 0.06
| | | | | |
| base.png | fast | Gray | 37.97 | 49.36 | 0.18
| | normal | Gray | 40.20 | 52.45 | 0.15
| | high | Gray | 40.90 | 53.92 | 0.08
| | dither | Gray | 40.26 | 55.25 | 0.06
| | high | Gray | 41.01 | 53.95 | 0.08
| | dither | Gray | 40.38 | 55.52 | 0.07
| | | | | |
| color-twirl.png | fast | Gray | 56.81 | 68.74 | 0.05
| | normal | Gray | 57.16 | 69.25 | 0.04
| | high | Gray | 57.82 | 69.64 | 0.04
| | dither | Gray | 56.85 | 72.49 | 0.02
| | dither | Gray | 56.85 | 72.53 | 0.02
| | | | | |
| clovers-r.png | fast | Gray | 39.29 | 52.29 | 0.21
| | normal | Gray | 39.65 | 52.58 | 0.21
| | high | Gray | 39.87 | 53.28 | 0.13
| | dither | Gray | 39.22 | 54.43 | 0.13
| | high | Gray | 39.90 | 53.30 | 0.14
| | dither | Gray | 39.23 | 54.44 | 0.13
| | | | | |
| stone-h.png | fast | Gray | 48.10 | 60.66 | 0.14
| | normal | Gray | 48.43 | 61.16 | 0.12
Expand Down
Binary file modified test-data/output-encode/base/BC3_UNORM.dds
Binary file not shown.
Binary file modified test-data/output-encode/base/BC3_UNORM_NORMAL.dds
Binary file not shown.
Binary file modified test-data/output-encode/base/BC3_UNORM_PREMULTIPLIED_ALPHA.dds
Binary file not shown.
Binary file modified test-data/output-encode/base/BC3_UNORM_RXGB.dds
Binary file not shown.
Binary file modified test-data/output-encode/base/BC4_SNORM.dds
Binary file not shown.
Binary file modified test-data/output-encode/base/BC4_UNORM.dds
Binary file not shown.
Binary file modified test-data/output-encode/base/BC5_SNORM.dds
Binary file not shown.
Binary file modified test-data/output-encode/base/BC5_UNORM.dds
Binary file not shown.
16 changes: 8 additions & 8 deletions test-data/output-encode/base/_hashes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,22 @@ BC2_UNORM_PREMULTIPLIED_ALPHA.dds: >
c695203a9836004f4947fc34346a4036ff26de1046447b7b0c13b1e74074f364

BC3_UNORM.dds: >
e9c2f5a1c8b7245499fbaf9fc094c31a127e9a4bd0ba418a02df1873e7e130d4
b028fa00fa7a77e3200ec5c096f71428684ddce4bb6c760e3b9b395d86d17b48

BC3_UNORM_PREMULTIPLIED_ALPHA.dds: >
cca39d093aaeee9fcb810ad147f9e65d66a961d5965d6e46b84e95a5b1c8463d
e71b0f13b6a051a04fa72ff1360f819dc507fec4e72116b72196777f6391f1cb

BC4_UNORM.dds: >
5d4b68c45bfafccaa34210f2a87f3dc8bf288dd7bd57754d73fa0e75b19d9a8d
9bcf87f5c94afaf9c28812db82a2d292556890cc0ff20f78d14b5424af58fc57

BC4_SNORM.dds: >
cec65b805e9927bfaafb4733fc97437605089ec35b88894f75914d79b9906b9f
6cebfb93196a22a76a59fdb08f93ccfd592e0e81b14fbbd7963a958cdbd5c638

BC5_UNORM.dds: >
532e5a1c14c54b8d05fa46db89e3b3c0c905b5fc04b25701f63f2a1f502435d3
96d8e0347efe74bbb9ca5719af79f610e60b5ee7d6c4052d2b220207e5fc6c31

BC5_SNORM.dds: >
19f471d97c8b025feb5b016ea3dd1854034ab58cddb615373666cfa4166df41a
76a3f43bf1ecb5a402f94771533e7cf69b1615bec6d70f5523db9ffddaae8ef7

BC6H_UF16.dds: >
Error: Unsupported format for encoding: BC6H_UF16
Expand Down Expand Up @@ -212,8 +212,8 @@ ASTC_12X12_UNORM.dds: >
Error: Unsupported format for encoding: ASTC_12X12_UNORM

BC3_UNORM_RXGB.dds: >
a24bce1eb9d97e89d0ba817e5f9a427fb7eab0b081286b5dc1dc955ae6b770f7
38dd299e3c1872f5c267d7bde375fc3ae44abc108ecc1af6a4fcead7e1ccaef9

BC3_UNORM_NORMAL.dds: >
824b1de27dcaeef76f5b8e03bf79089ffd2c0d5e732a73669b6f514340d6c49f
aa14807b59220797761f38d531ce796b2153f65ed76906d45955b4dd086b2e27

12 changes: 6 additions & 6 deletions test-data/output-encode/compression/_hashes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ BC4_UNORM normal base.dds: >
f5f96490630c7f50b5a66369ffd5c9e2cad6a6e9d123b0120132f20b92155420

BC4_UNORM high base.dds: >
5d4b68c45bfafccaa34210f2a87f3dc8bf288dd7bd57754d73fa0e75b19d9a8d
9bcf87f5c94afaf9c28812db82a2d292556890cc0ff20f78d14b5424af58fc57

BC4_UNORM dither base.dds: >
feacea29d2087e1a3d078e258a954cb851910aaf312826053165f164bcc1fe47
96d8706c245606a0e413e6417be31526aa49ac5563b7a68a1300ca15b13a7adf

BC4_UNORM fast color-twirl.dds: >
3719ff650d8d5bc26e620b9614f3b14e151ead6574c8c9aecc8f2f1b56bd066d
Expand All @@ -257,10 +257,10 @@ BC4_UNORM normal color-twirl.dds: >
680d78124c58fbd73f9543330b474a6fb551d460957a58af1e5d7fd60f12d687

BC4_UNORM high color-twirl.dds: >
9e40c3b23e5eb1d8f955e542f126a18e20ba0742578e5566fc0113edf190b562
f3a109d0df3e50f7c9e50fb8bbe5af585dce4a420a5328343ad03fa69bc74d11

BC4_UNORM dither color-twirl.dds: >
4e718b87ea8dc0c9a064d47207dab343838dccf9166cd846b21daf216528ed98
c5191074e262ec21cad49ab31e0e0ec682429307106519db2f8cdd22b3800369

BC4_UNORM fast clovers-r.dds: >
a58f0590abdc924ba80ece990f77e39b294257d8b55119f2797e577ab9024ff5
Expand All @@ -269,10 +269,10 @@ BC4_UNORM normal clovers-r.dds: >
8037301bba85a497fff08f6dd54b266ee95352f789da70d1f79ca68b90869efc

BC4_UNORM high clovers-r.dds: >
d65f6577e55fc88be0ff51953cfc8e7829b0223ab583c4e1e7bc2818651723e9
da72a1f104e403135c238d3aeccb70f31f63ca8ca3d1b516fea75875e06f9d1b

BC4_UNORM dither clovers-r.dds: >
bb9bc574c43315f22cbaa20c5d9cd2b389839086af9465bf3ffda8deeffc94c8
7729acdf6408914b78af5de4ec987fe6220f5ca4b2074699bd734b044cf748c2

BC4_UNORM fast stone-h.dds: >
ce87f8b58dece6d5a73db24e03a2da1245c82295b0ad406cf5aca655ce1e77c3
Expand Down
Binary file modified test-data/output-encode/dither/BC3_UNORM base.dds
Binary file not shown.
Binary file modified test-data/output-encode/dither/BC3_UNORM_NORMAL base.dds
Binary file not shown.
Binary file not shown.
Binary file modified test-data/output-encode/dither/BC3_UNORM_RXGB base.dds
Binary file not shown.
Binary file modified test-data/output-encode/dither/BC4_UNORM base.dds
Binary file not shown.
16 changes: 8 additions & 8 deletions test-data/output-encode/dither/_hashes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,22 @@ BC2_UNORM_PREMULTIPLIED_ALPHA twirl.dds: >
ad0331ca95c32ff14ca0a7194cb3d49b5e7daa4abbf0f4d48e5c9194f44dc46e

BC3_UNORM base.dds: >
926670f2336e5ae1f07e97984e9ec415de3f731397708a771bea4fd579d9b1dd
9dd3e775e42e88bf950d17c1cb786b70a72c2bf1c476c6c5ae914cf27add712e

BC3_UNORM twirl.dds: >
30e419420c93d2a60e42700568072fd027b715a3375acfec51221a147878def3

BC3_UNORM_PREMULTIPLIED_ALPHA base.dds: >
0170d4a53f5c25b81ba6f27d255ee5c65737ab133eeadc3c0c657c9b4270dfca
fe50551bff72ed76b5064c5c0e8ce2243f685565657af870d5663cc80c72c56e

BC3_UNORM_PREMULTIPLIED_ALPHA twirl.dds: >
e830c1830ef47fab07d45a27068e66ab14f822aa0272e65a9ef5924b47e622dc

BC4_UNORM base.dds: >
feacea29d2087e1a3d078e258a954cb851910aaf312826053165f164bcc1fe47
96d8706c245606a0e413e6417be31526aa49ac5563b7a68a1300ca15b13a7adf

BC4_UNORM twirl.dds: >
4e718b87ea8dc0c9a064d47207dab343838dccf9166cd846b21daf216528ed98
c5191074e262ec21cad49ab31e0e0ec682429307106519db2f8cdd22b3800369

BC7_UNORM base.dds: >
67fedcfadb70aaa1a200a5cbf083e034eea48ececdeb328bb0694adb8e4ceb80
Expand All @@ -230,14 +230,14 @@ BC7_UNORM twirl.dds: >
2eeba556fe6609a64d2aecb01858cbd4bf0bb3b4216d0359b7e2c15e6c0363c3

BC3_UNORM_RXGB base.dds: >
d1be34f0a347063b13b5231bc845d3fd4f7de01ab86a0bf2c44f5ea80f5ede97
0136e2f35b3c3daf85bfec9e3b57ee83711d680f4727e0889f990e82492e201c

BC3_UNORM_RXGB twirl.dds: >
5a7668f4152401933db621c604a528f53dd6b13f72d816b09122601bafcd5d36
7794b03163f6a792a64e65f34b3a70539c1715db8c1bdbca4fce42debdd74f51

BC3_UNORM_NORMAL base.dds: >
07795f2763b21ca5f0ac6bf3bfc6f3cc581b2e66c402783c94f29125e4e69308
cdcd1fa3f40891cb4c685d5b4cedabf9f6b85612c68395e3d0a17ab678e49523

BC3_UNORM_NORMAL twirl.dds: >
e8ecff20460f4752a936d73a7e850311caaad1ddc6a8c5ec40cc6f7e692dfaa4
5878f837907845799e425b41a3bc7f3bf3c75fa32c8eb0edd4c81835bb75f39b

Loading