Branch on lower color values for more accurate speedy color conversion, add simple color conversion benchmarks

This commit is contained in:
Imbris 2020-01-13 23:19:38 -05:00
parent 71bd7f15ee
commit d36b263627
4 changed files with 45 additions and 8 deletions

View File

@ -37,3 +37,7 @@ criterion = "0.3"
[[bench]]
name = "chonk_benchmark"
harness = false
[[bench]]
name = "color_benchmark"
harness = false

View File

@ -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::*;

View File

@ -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);

View File

@ -9,14 +9,26 @@ use vek::{Mat3, Rgb, Rgba, Vec3};
#[inline(always)]
pub fn srgb_to_linear(col: Rgb<f32>) -> Rgb<f32> {
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<f32>) -> Rgb<f32> {
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<f32>) -> Rgba<f32> {