From 7a1b43c6f1b970164c07ee1cc8efb8719ea0f8b4 Mon Sep 17 00:00:00 2001 From: Imbris Date: Mon, 13 Jan 2020 23:19:38 -0500 Subject: [PATCH] Branch on lower color values for more accurate speedy color conversion, add simple color conversion benchmarks --- common/Cargo.toml | 4 ++++ common/benches/chonk_benchmark.rs | 5 ++--- common/benches/color_benchmark.rs | 22 ++++++++++++++++++++++ common/src/util/mod.rs | 22 +++++++++++++++++----- 4 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 common/benches/color_benchmark.rs diff --git a/common/Cargo.toml b/common/Cargo.toml index adcdd3f834..6559126368 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -37,3 +37,7 @@ criterion = "0.3" [[bench]] name = "chonk_benchmark" harness = false + +[[bench]] +name = "color_benchmark" +harness = false diff --git a/common/benches/chonk_benchmark.rs b/common/benches/chonk_benchmark.rs index ee803378cb..d325f02888 100644 --- a/common/benches/chonk_benchmark.rs +++ b/common/benches/chonk_benchmark.rs @@ -1,7 +1,6 @@ -#[macro_use] -extern crate criterion; - use criterion::black_box; +use criterion::criterion_group; +use criterion::criterion_main; use criterion::Criterion; use vek::*; diff --git a/common/benches/color_benchmark.rs b/common/benches/color_benchmark.rs new file mode 100644 index 0000000000..5031cb4c06 --- /dev/null +++ b/common/benches/color_benchmark.rs @@ -0,0 +1,22 @@ +use criterion::black_box; +use criterion::criterion_group; +use criterion::criterion_main; +use criterion::Criterion; + +use vek::*; +use veloren_common::util::{linear_to_srgb, srgb_to_linear}; + +fn criterion_benchmark(c: &mut Criterion) { + c.bench_function("color: srgb to linear (0.5, 0.1, 0.5)", |b| { + b.iter(|| { + black_box(srgb_to_linear(black_box(Rgb::new(0.5, 0.1, 0.5)))); + }) + }); + c.bench_function("color: linear to srgb (0.5, 0.1, 0.5)", |b| { + b.iter(|| { + black_box(linear_to_srgb(black_box(Rgb::new(0.5, 0.1, 0.5)))); + }) + }); +} +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/common/src/util/mod.rs b/common/src/util/mod.rs index 37f726b938..5f49bf49ef 100644 --- a/common/src/util/mod.rs +++ b/common/src/util/mod.rs @@ -9,14 +9,26 @@ use vek::{Mat3, Rgb, Rgba, Vec3}; #[inline(always)] pub fn srgb_to_linear(col: Rgb) -> Rgb { - 0.012522878 * col + 0.682171111 * col * col + 0.305306011 * col * col * col + col.map(|c| { + if c <= 0.104 { + c * 0.08677088 + } else { + 0.012522878 * c + 0.682171111 * c * c + 0.305306011 * c * c * c + } + }) } #[inline(always)] pub fn linear_to_srgb(col: Rgb) -> Rgb { - let s1 = col.sqrt(); - let s2 = s1.sqrt(); - let s3 = s2.sqrt(); - 0.585122381 * s1 + 0.783140355 * s2 - 0.368262736 * s3 + col.map(|c| { + if c <= 0.0060 { + c * 11.500726 + } else { + let s1 = c.sqrt(); + let s2 = s1.sqrt(); + let s3 = s2.sqrt(); + 0.585122381 * s1 + 0.783140355 * s2 - 0.368262736 * s3 + } + }) } #[inline(always)] pub fn srgba_to_linear(col: Rgba) -> Rgba {