Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate transform matrices at compile-time #24

Closed
FreezyLemon opened this issue Apr 10, 2024 · 5 comments · Fixed by #27
Closed

Calculate transform matrices at compile-time #24

FreezyLemon opened this issue Apr 10, 2024 · 5 comments · Fixed by #27

Comments

@FreezyLemon
Copy link
Contributor

src/yuv_rgb/color.rs has quite a bit of code that can logically be const-evaluated.

Basically, get_rgb_to_yuv_matrix and get_yuv_to_rgb_matrix (which is the first function plus 1 step) can be simplified into lookups of 3x3 matrices based only on the MatrixCoefficients and ColorPrimaries.

This could either be done by moving the calculation code into a build script (similar to this PR in ssimulacra2), or by using soft floats (e.g. const_soft_float and/or constgebra).

This could also allow removing the nalgebra dependency, since the only nalgebra code that really needs to run at runtime is the pix_vector * transform_matrix multiplication, which can be ported to a trivial helper function (because the dimensions are always the same).

@FreezyLemon
Copy link
Contributor Author

Looks like 1.82 (currently beta) has stabilized some const floating point arithmetic: rust-lang/rust#128596.

Might be worth revisiting after stable release.

@FreezyLemon
Copy link
Contributor Author

I've played around with this a bit and it looks like this won't be a real performance increase, maybe 1-2%. It could still be nice to ditch the nalgebra dependency... I've basically implemented this on my fork. It needs a bit of polishing, but the matrix stuff is ~200 lines of mostly boilerplate. I'll think about whether I want to open a PR with it.

@FreezyLemon
Copy link
Contributor Author

Even when using const-evaluated LUTs, there doesn't seem to be a noteworthy performance increase. I think it's probably best to "just" ditch the nalgebra dependency by adding some Matrix boilerplate...

@FreezyLemon
Copy link
Contributor Author

FreezyLemon commented Nov 18, 2024

@shssoichiro What do you think about publishing a crate like "yuvxyb-matrix" or similar, so we can roll our own matrix math? That shouldn't be too difficult, would allow reuse in ssimulacra2 (we can drop nalgebra there too) and probably allows us to make some functions const later without too much work.

The code is <250 lines and is fairly rudimentary. Some of the easier matrix operations without generics, since we only need 3x3, 3x1, 1x3.

@shssoichiro
Copy link
Collaborator

If it's reusable, then I think making a new crate for it makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants